Example #1
0
    def deactivate(self, save=True, dependencies=False, dry_run=True):
        """
        Deactivate the package, removing it from the Python import path.

        Parameters
        ----------
        save : boolean
            Should changes to the repository be saved now?
        dependencies : boolean
            Should we ensure a consistent state by deactivating all packages
            which depend on this one?

        """
        if self.active:
            #if dependencies:
            #    for package in self.dependent_packages:
            #        package.deactivate(save=False, dependencies=True,
            #                           dry_run=dry_run)
            for package in sorted(self.full_dependent_packages,
                    key=lambda x: x.name):
                warning("Package %s depends upon %s and may no longer work "
                    "correctly" % (package.name, self.name))
            run_scripts(self.distribution, "pre-deactivate", dry_run=dry_run)
            info("Deactivating package %s..." % (self.name))
            if not dry_run:
                self.project.repository.active.remove(self.distribution)
            else:
                print "Deactivate package %s..." % (self.name)
            if save:
                if not dry_run:
                    self.repository.active.save()
                else:
                    print "Save .pth file."
        else:
            warning("Package %s is already inactive." % (self.name))
Example #2
0
    def activate(self, save=True, dependencies=True, dry_run=False,
                 verbose=True):
        """
        Activate the package, adding it to the Python import path.

        Parameters
        ----------
        save : boolean
            Should changes to the repository be saved now?
        dependencies : boolean
            Should we ensure a consistent state after activation?
        verbose : boolean
            Should all of the output be shown or just if something was
            changed(i.e. activated when it was deactivated)?
        """
        if not self.active:
            if self.project.active:
                self.project.deactivate(save=False, dependencies=False,
                    dry_run=dry_run)
            info("Activating package %s..." % (self.name))
            if not dry_run:
                self.project.repository.active.add(self.distribution)
            else:
                print "Activate package %s" % self.name
            if save:
                if not dry_run:
                    self.repository.active.save()
                else:
                    print "Save .pth file."
            run_scripts(self.distribution, "post-activate", dry_run=dry_run)
        else:
            if verbose:
                warning("Package %s is already active." % (self.name))
Example #3
0
    def remove(self, dry_run=False):
        """
        Uninstall the package, removing all files and attempting to
        restore to the pre-installed state.

        """
        # FIXME: we would like to replace most of this with a call out to
        # our patched version of setuptools.  For the time being I'm leaving
        # this in here, since we don't have the interface set yet.

        if self.active:
            # deactivate self and anything which depends on this package
            self.deactivate(dry_run=dry_run)
        run_scripts(self.distribution, "pre-uninstall", dry_run=dry_run)

        files_file = os.path.join(self.location, "EGG-INFO",
            "installed_files.log")
        file_list = []

        # If the file exists then the package was installed as a directory
        if os.path.exists(files_file):
            # extract the list of files
            fp = open(files_file)
            try:
                file_list = [filename.strip() for filename in
                    fp.read().split('\n')]
            finally:
                fp.close()

        # Otherwise this package may have been installed as a zipped egg
        elif os.path.isfile(self.location):
            import zipimport
            egg_zip = zipimport.zipimporter(self.location)
            try:
                file_list = [filename.strip() for filename in
                    egg_zip.get_data('EGG-INFO/installed_files.log'
                    ).split('\n')]
                file_list.remove('')
            except IOError:
                info("installed_files.log not found in %s" % self.location)

        # Otherwise the package was installed as a directory but is missing
        # the installed files log
        else:
            info("installed_files.log not found for %s" % self.name)

        # Now try to remove every element in the list
        for filename in file_list:
            if os.path.exists(filename):
                if os.path.isdir(filename):
                    try:
                        os.rmdir(filename)
                    except Exception, exc:
                        rmtree_error(os.rmdir, filename, exc)
                elif os.path.isfile(filename) or os.path.islink(filename):
                    try:
                        os.remove(filename)
                    except Exception, exc:
                        rmtree_error(os.remove, filename, exc)
Example #4
0
    def install(self, target_repo, source=False, develop=False,
                dry_run=False, *args):
        """
        Install a package by fetching it into a temporary directory and then
        calling easy_install with the appropriate args.

        """
        args = list(args)
        files_file = os.path.join(self.tmpdir, "files")
        #if "--location" not in args:
        #    args += ["--install-dir=%s" % target_repo.location]

        # XXX this option should become the default when we patch setuptools
        # wo we can remove these two lines
        if "--record" not in args:
            args += ["--record=%s" % files_file]
        if "--no-deps" not in args:
            args += ["--no-deps"]

        if not dry_run:
            location = self.local_distribution.location
        else:
            print "Download egg file to temporary directory"

        # XXX more sophistication here?
        info("Installing %s" % str(self.distribution))
        if not dry_run:
            subprocess.call(["easy_install"] + args + [location])
        else:
            print "Execute", " ".join(["easy_install"] + args + ["<location>"])

        # want to copy the files file
        # XXX Don't worry about this when we start to use patched setuptools
        if not dry_run:
            f = open(files_file)
            try:
                files = f.read().split('\n')
            finally:
                f.close()
            for file in files:
                match = EGG_INFO_RE.match(file)
                if match:
                    dir = match.group(0)
                    break
            else:
                # can't find the EGG-INFO dir
                return
            base, egg_info = os.path.split(dir)
            new_files = os.path.join(base, ".files")
            copyfile(files_file, new_files)
        else:
            print "Copy the list of installed files to the install location."

        if not dry_run:
            dist = Distribution.from_filename(base)
            run_scripts(dist, "post-install", dry_run=dry_run)
            # XXX would really like to have a separate activation step, but
            # setuptools automatically activates in install
            run_scripts(dist, "post-activate", dry_run=dry_run)
        else:
            print "Run post-install scripts."
            print "Run post-activate scripts."