Пример #1
0
def rollback_state(project_list, remote_repos=None, interactive=True, dry_run=False, term_width=0):
    """
    Input is a list of package_name-versions that are to be activated
    and some of the options that can be passed on the command-line.
    """
    # Iterate through the list of package_name-versions and for each project,
    # ensure that the appropriate version for that project is activated.
    local = get_site_packages()
    for project in project_list:
        (project_name, project_version) = parse_project_str(project)
        try:
            pkgs = local.projects[project_name].packages
        except KeyError:
            # If we can't find a project key in the local.projects, that
            # means that it was removed from the system (not just
            # deactivated), so we try to re-install it.
            req_str = "%s==%s" % (project_name, project_version)
            requirement = Requirement.parse(req_str)
            if project_name.endswith(".dev") and project_version.startswith("r"):
                # If we found a package that was installed by
                # a 'setup.py develop', try to activate it by finding it's
                # location ignoring the specific revision number, since we
                # can't control if someone has done an 'svn up' on their
                # checkouts.
                name = project_name.split("-")[0]
                pkgs = local.projects[name].packages
            else:
                install_requirement(
                    [requirement],
                    remote_repos=remote_repos,
                    interactive=interactive,
                    dry_run=dry_run,
                    term_width=term_width,
                )
        for pkg in pkgs:
            if pkg.version == project_version:
                pkg.activate(verbose=False)
                break
            if project_name.endswith(".dev") and project_version.startswith("r"):
                if ".dev-r" in pkg.version:
                    pkg.activate(verbose=False)
                    break

    # We also need to deactivate any packages that the user has installed since
    # the rollback point.
    active_local_packages = []
    for project in local.projects:
        pkg = local.projects[project].active_package
        if pkg:
            active_local_packages.append(pkg.project.name)
    for package in active_local_packages:
        new_package = True
        for project in project_list:
            if package in project:
                new_package = False
                break
        if new_package:
            pkg = local.projects[package].active_package
            pkg.deactivate(dry_run=False)
Пример #2
0
def save_state(verbose=False):
    """
    Adds an entry of the current working configuration of packages to the
    ensetuptools.cache file.
    """
    # Determine the current local repository and from that build a list
    # of the current active packages.
    site_packages = sysconfig.get_python_lib()
    local = get_site_packages()
    active_local_packages = []
    for project in local.projects:
        pkg = local.projects[project].active_package
        if pkg:
            active_local_packages.append(pkg)

    # Retrieve a list of project_name-version for the currently active packages.
    # Sort them by name so that we can display them easier.
    project_list = []
    for pkg in active_local_packages:
        project_list.append("%s-%s" % (pkg.project.name, pkg.version))
    project_list.sort()

    # Retrieve the most recently saved state and compare it to the current
    # state of the system.  If there have been no changes, then don't
    # bother saving a entry to the ensetuptools.cache.
    stored_states = retrieve_states()
    if stored_states:
        recent_state = stored_states[0]
        recent_project_list = recent_state[1]
        if project_list == recent_project_list:
            if verbose:
                print (
                    "There is no difference between the current state and "
                    "the most recent saved state in the ensetuptools.cache, so "
                    "the current state does not need to be saved."
                )
            return

    # Save the current state to an entry in the ensetuptools.cache file.
    # Each entry begins with a timestamp, followed by a colon, and then
    # a comma separated list of the project_name-versions for the
    # currently active packages.
    ensetuptools_cache = os.path.join(site_packages, "ensetuptools.cache")
    timestamp = strftime("%Y%m%d%H%M%S")
    pkg_list = ",".join(project_list)
    try:
        if verbose:
            print "Saving current state..."
        f = open(ensetuptools_cache, "a")
        f.write(timestamp + ":")
        f.write(pkg_list)
        f.write("\n")
        f.close()
        if verbose:
            print "Successfully saved the current state."
    except:
        print "Error trying to write to the ensetuptools.cache."