Commits

Alexandre Conrad committed 92b4de4

Remove build directories after commands. Fixes #57

Comments (0)

Files changed (4)

   download and installation into two steps. Thanks Simon Cross.
 * Fix uninstalling from requirements file containing -f, -i, or
   --extra-index-url.
+* Leftover build directories are now removed. Thanks Alexandre Conrad.
 
 0.6.3
 -----

pip/commands/install.py

             for req in parse_requirements(filename, finder=finder, options=options):
                 requirement_set.add_requirement(req)
         if not options.no_download:
-            requirement_set.install_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
+            requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
         else:
             requirement_set.locate_files()
         if not options.no_install and not self.bundle:
                                    requirement_set.successfully_downloaded])
             if downloaded:
                 logger.notify('Successfully downloaded %s' % downloaded)
+        requirement_set.cleanup_files(bundle=self.bundle)
         return requirement_set
 
 InstallCommand()
         self.ignore_dependencies = ignore_dependencies
         self.successfully_downloaded = []
         self.successfully_installed = []
+        self.reqs_to_cleanup = []
 
     def __str__(self):
         reqs = [req for req in self.requirements.values()
                                        'an equivalent install with --no-install?)'
                                        % (req_to_install, req_to_install.source_dir))
 
-    def install_files(self, finder, force_root_egg_info=False, bundle=False):
+    def prepare_files(self, finder, force_root_egg_info=False, bundle=False):
+        """Prepare process. Create temp directories, download and/or unpack files."""
         unnamed = list(self.unnamed_requirements)
         reqs = self.requirements.values()
         while reqs or unnamed:
                 else:
                     logger.notify('Downloading/unpacking %s' % req_to_install)
             logger.indent += 2
-            is_bundle = False
             try:
+                is_bundle = False
                 if req_to_install.editable:
                     if req_to_install.source_dir is None:
                         location = req_to_install.build_location(self.src_dir)
                     if req_to_install.name not in self.requirements:
                         self.requirements[req_to_install.name] = req_to_install
                 else:
-                    req_to_install.remove_temporary_source()
+                    self.reqs_to_cleanup.append(req_to_install)
                 if install:
                     self.successfully_downloaded.append(req_to_install)
                     if bundle and (req_to_install.url and req_to_install.url.startswith('file:///')):
             finally:
                 logger.indent -= 2
 
+    def cleanup_files(self, bundle=False):
+        """Clean up files, remove builds."""
+        logger.notify('Cleaning up...')
+        logger.indent += 2
+        for req in self.reqs_to_cleanup:
+            req.remove_temporary_source()
+        try:
+            # create_bundle() is responsible for removing build_dir and
+            # src_dir after compression. create_bundle() is ran afterwards.
+            if not bundle:
+                for directory in self.build_dir,:
+                    if not os.path.exists(directory):
+                        continue
+                    logger.info('Removing %s...' % directory)
+                    os.rmdir(directory)
+        finally:
+            logger.indent -= 2
+
     def copy_to_builddir(self, req_to_install):
         target_dir = req_to_install.editable and self.src_dir or self.build_dir
         logger.info("Copying %s to %s" %(req_to_install.name, target_dir))

tests/test_basic.py

-from os.path import abspath, join, dirname, curdir, pardir
+from os.path import abspath, exists, join, dirname, curdir, pardir
 from test_pip import here, reset_env, run_pip, pyversion, lib_py, mkdir
 
 def test_correct_pip_version():
     result = run_pip('install', pardir, cwd=run_from, expect_error=False)
     assert (lib_py + 'site-packages/fspkg') in result.files_created, str(result.stdout)
     assert (lib_py + 'site-packages/FSPkg-0.1dev-py%s.egg-info' % pyversion) in result.files_created, str(result)
+
+def test_cleanup():
+    """
+    Test clean up of build directory after an install.
+
+    """
+    reset_env()
+    # FIXME: We may want to test more scenarios
+    result = run_pip('install', 'INITools==dev', expect_error=False)
+    build = join(here, "test-scratch", "build")
+    assert not exists(build), "build dir still exists: %s" % build