def test_exec_command(self, info, get_active_kubos_version): kubos_version = get_installed_version('kubos-cli') get_active_kubos_version.return_value = kubos_version version.execCommand(None, None) get_active_kubos_version.assert_called() calls = [ call('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')), call('Kubos Source version : %s' % kubos_version) ] info.assert_has_calls(calls)
def setup_py(self): assert self.source_dir, "No source dir for %s" % self try: import setuptools # noqa except ImportError: if get_installed_version('setuptools') is None: add_msg = "Please install setuptools." else: add_msg = traceback.format_exc() # Setuptools is not available raise InstallationError( "Could not import setuptools which is required to " "install from a source distribution.\n%s" % add_msg ) setup_file = 'setup.py' if self.editable_options and 'subdirectory' in self.editable_options: setup_py = os.path.join(self.source_dir, self.editable_options['subdirectory'], setup_file) else: setup_py = os.path.join(self.source_dir, setup_file) # Python2 __file__ should not be unicode if six.PY2 and isinstance(setup_py, six.text_type): setup_py = setup_py.encode(sys.getfilesystemencoding()) return setup_py
def setup_py(self): assert self.source_dir, "No source dir for %s" % self try: import setuptools # noqa except ImportError: if get_installed_version('setuptools') is None: add_msg = "Please install setuptools." else: add_msg = traceback.format_exc() # Setuptools is not available raise InstallationError( "Could not import setuptools which is required to " "install from a source distribution.\n%s" % add_msg) setup_file = 'setup.py' if self.editable_options and 'subdirectory' in self.editable_options: setup_py = os.path.join(self.source_dir, self.editable_options['subdirectory'], setup_file) else: setup_py = os.path.join(self.source_dir, setup_file) # Python2 __file__ should not be unicode if six.PY2 and isinstance(setup_py, six.text_type): setup_py = setup_py.encode(sys.getfilesystemencoding()) return setup_py
def setup_py(self): assert self.source_dir, "No source dir for %s" % self cmd = [sys.executable, '-c', 'import setuptools'] output = call_subprocess( cmd, show_stdout=False, command_desc='python -c "import setuptools"', on_returncode='ignore', ) if output: if get_installed_version('setuptools') is None: add_msg = "Please install setuptools." else: add_msg = output # Setuptools is not available raise InstallationError( "Could not import setuptools which is required to " "install from a source distribution.\n%s" % add_msg ) setup_py = os.path.join(self.setup_py_dir, 'setup.py') # Python2 __file__ should not be unicode if six.PY2 and isinstance(setup_py, six.text_type): setup_py = setup_py.encode(sys.getfilesystemencoding()) return setup_py
def setup_py(self): assert self.source_dir, "No source dir for %s" % self cmd = [sys.executable, '-c', 'import setuptools'] output = call_subprocess( cmd, show_stdout=False, command_desc='python -c "import setuptools"', on_returncode='ignore', ) if output: if get_installed_version('setuptools') is None: add_msg = "Please install setuptools." else: add_msg = output # Setuptools is not available raise InstallationError( "Could not import setuptools which is required to " "install from a source distribution.\n%s" % add_msg) setup_py = os.path.join(self.setup_py_dir, 'setup.py') # Python2 __file__ should not be unicode if six.PY2 and isinstance(setup_py, six.text_type): setup_py = setup_py.encode(sys.getfilesystemencoding()) return setup_py
def is_installed(req): try: req_name, req_ver = str(req).split('==') except ValueError: print "ERROR:: Requirement specifier not supported: {req}. Installer supports only specifiers with " \ "'=='".format(req=str(req)) return True installed_version = get_installed_version(req_name) return installed_version == req_ver
def get_filename(dist): from addict import Dict from pip.utils import get_installed_version reddove_repos = [os.path.join('https://www.red-dove.com/pypi/projects', index) for index in list('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')] reddove_dict = Dict(dict(zip(list('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'), reddove_repos))) version = get_installed_version(dist) char = dist[0].upper() template = "package-{}.json".format(version) url = os.path.join(reddove_dict.get(char), dist, template) return url
def pip_version_check(package_finder): """Check for an update for pip. Limit the frequency of checks to once per week. State is stored either in the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix of the pip script path. """ installed_version = get_installed_version("pip") if installed_version is None: return pip_version = packaging_version.parse(installed_version) remote_version = None try: state = load_selfcheck_statefile() current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state if "last_check" in state.state and "remote_version" in state.state: last_check = datetime.datetime.strptime(state.state["last_check"], SELFCHECK_DATE_FMT) if total_seconds(current_time - last_check) < 7 * 24 * 60 * 60: remote_version = state.state["remote_version"] # Refresh the version if we need to or just see if we need to warn if remote_version is None: try: remote_version = package_finder.find_requirement( InstallRequirement.from_line('pip'), upgrade=True).version except BestVersionAlreadyInstalled: remote_version = pip_version remote_version = remote_version.base_version # save that we've performed a check state.save(remote_version, current_time) if not remote_version == installed_version: # Advise "python -m pip" on Windows to avoid issues # with overwriting pip.exe. if WINDOWS: pip_cmd = "python -m pip" else: pip_cmd = "pip" logger.warning( "You are using pip version %s, however version %s is " "available.\nYou should consider upgrading via the " "'%s install --upgrade pip' command.", pip_version, remote_version, pip_cmd) except Exception: logger.debug( "There was an error checking the latest version of pip", exc_info=True, )
def run(self): if getattr(self.distribution, 'salt_installing_pywin32_windows', None) is None: print('This command is not meant to be called on it\'s own') exit(1) import platform # Detect if already installed, any version will work from pip.utils import get_installed_version if get_installed_version('pywin32') is not None: print('PyWin32 already installed') return if get_installed_version('pypiwin32') is not None: print('PyWin32 already installed') return # Install PyWin32 from Salt repo from pip.utils import call_subprocess from pip.utils.logging import indent_log platform_bits, _ = platform.architecture() call_arguments = ['pip', 'install', 'wheel'] if platform_bits == '64bit': if IS_PY3: call_arguments.append( 'https://repo.saltstack.com/windows/dependencies/64/pywin32-221-cp35-cp35m-win_amd64.whl' ) else: call_arguments.append( 'https://repo.saltstack.com/windows/dependencies/64/pywin32-221-cp27-cp27m-win_amd64.whl' ) else: if IS_PY3: call_arguments.append( 'https://repo.saltstack.com/windows/dependencies/32/pywin32-221-cp35-cp35m-win32.whl' ) else: call_arguments.append( 'https://repo.saltstack.com/windows/dependencies/32/pywin32-221-cp27-cp27m-win32.whl' ) with indent_log(): call_subprocess(call_arguments)
def is_index_version_greater_than_current(): MAJOR_INDEX = 0 installed, index = (StrictVersion(get_installed_version(PACKAGE_NAME)), StrictVersion(max_version_from_index())) is_major_release = False is_greater_version = index > installed if is_greater_version and get_index_of_biggest_component_between_two_versions( index.version, installed.version) == MAJOR_INDEX: is_major_release = True return is_greater_version, is_major_release
def get_filename(self, dist): version = get_installed_version(dist) char = dist[0].upper() try: template = "package-{}.json".format(version) url = os.path.join(self.reddove_dict.get(char), dist, template) document = self.grab.request(url=url) json_file = document.json except JSONDecodeError: dirurl = os.path.join(self.reddove_dict.get(char), dist) directory = self.grab.request(url=dirurl) template = directory.select('//table//tr/td/a').attr_list('href')[-2] url = os.path.join(self.reddove_dict.get(char), dist, template) document = self.grab.request(url=url) json_file = document.json return json_file
def cmd_pip_check_version(self, *argv, **kwargs): list_command = ListCommand() options, args = list_command.parse_args([]) session = list_command._build_session(options, retries=0, timeout=min(5, options.timeout)) installed_version = get_installed_version("pip") if installed_version is None: return pypi_version = None try: state = load_selfcheck_statefile() current_time = datetime.datetime.utcnow() if "last_check" in state.state and "pypi_version" in state.state: last_check = datetime.datetime.strptime( state.state["last_check"], SELFCHECK_DATE_FMT ) if total_seconds(current_time - last_check) < 7 * 24 * 60 * 60: pypi_version = state.state["pypi_version"] # Refresh the version if we need to or just see if we need to warn if pypi_version is None: resp = session.get( PyPI.pip_json_url, headers={"Accept": "application/json"}, ) resp.raise_for_status() pypi_version = [ v for v in sorted( list(resp.json()["releases"]), key=parse_version, ) if not parse_version(v).is_prerelease ][-1] state.save(pypi_version, current_time) except Exception: pass return 'pip', installed_version, str(pypi_version)
def test_exec_command_with_no_version_and_no_directory( self, info, get_active_kubos_version): kubos_version = '' get_active_kubos_version.return_value = kubos_version version.execCommand(None, None) calls = [ call('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')), call('Kubos Source version : %s' % kubos_version), call( 'There are not any local versions of the kubos source currently.' ), call( 'Please run `kubos update` to pull the kubos source before running `kubos version` again' ) ] info.assert_has_calls(calls)
def test_exec_command_with_no_version_and_directory( self, info, get_active_kubos_version, get_repo, get_tag_list, print_tag_list): kubos_version = '' get_active_kubos_version.return_value = kubos_version version.execCommand(None, None) get_repo.assert_called() get_tag_list.assert_called() print_tag_list.assert_called() calls = [ call('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')), call('Kubos Source version : %s' % kubos_version), call('There\'s not an active Kubos source version..'), call('The available versions are:'), call('Please run kubos use <version> (with one of the above versions)' + \ 'to checkout a version of the source before working with a project.') ] info.assert_has_calls(calls)
def execCommand(args, following_args): kubos_version = git_utils.get_active_kubos_version() logging.info('Kubos-CLI version : %s' % 'v' + get_installed_version('kubos-cli')) logging.info('Kubos Source version : %s' % kubos_version) if not kubos_version: if os.path.isdir(KUBOS_SRC_DIR): repo = git_utils.get_repo(KUBOS_SRC_DIR) version_list = git_utils.get_tag_list(repo) logging.info('There\'s not an active Kubos source version..') logging.info('The available versions are:') git_utils.print_tag_list(version_list, filter=True) logging.info('Please run kubos use <version> (with one of the above versions)' + \ 'to checkout a version of the source before working with a project.') else: logging.info( 'There are not any local versions of the kubos source currently.' ) logging.info( 'Please run `kubos update` to pull the kubos source before running `kubos version` again' )
def pip_version_check(session): """Check for an update for pip. Limit the frequency of checks to once per week. State is stored either in the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix of the pip script path. """ installed_version = get_installed_version("pip") if installed_version is None: return pip_version = packaging_version.parse(installed_version) pypi_version = None try: state = load_selfcheck_statefile() current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state if "last_check" in state.state and "pypi_version" in state.state: last_check = datetime.datetime.strptime( state.state["last_check"], SELFCHECK_DATE_FMT ) if total_seconds(current_time - last_check) < 7 * 24 * 60 * 60: pypi_version = state.state["pypi_version"] # Refresh the version if we need to or just see if we need to warn if pypi_version is None: resp = session.get( PyPI.pip_json_url, headers={"Accept": "application/json"}, ) resp.raise_for_status() pypi_version = [ v for v in sorted( list(resp.json()["releases"]), key=packaging_version.parse, ) if not packaging_version.parse(v).is_prerelease ][-1] # save that we've performed a check state.save(pypi_version, current_time) remote_version = packaging_version.parse(pypi_version) # Determine if our pypi_version is older if (pip_version < remote_version and pip_version.base_version != remote_version.base_version and pip_installed_by_pip()): # Advise "python -m pip" on Windows to avoid issues # with overwriting pip.exe. if WINDOWS: pip_cmd = "python -m pip" else: pip_cmd = "pip" logger.warning( "You are using pip version %s, however version %s is " "available.\nYou should consider upgrading via the " "'%s install --upgrade pip' command.", pip_version, pypi_version, pip_cmd ) except Exception: logger.debug( "There was an error checking the latest version of pip", exc_info=True, )
def get_kubos_sdk_version(): return get_installed_version('kubos-sdk')
def load_sdk_version(): return get_installed_version('kubos-sdk')
def pip_version_check(session): """Check for an update for pip. Limit the frequency of checks to once per week. State is stored either in the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix of the pip script path. """ installed_version = get_installed_version("pip") if installed_version is None: return pip_version = packaging_version.parse(installed_version) pypi_version = None try: state = load_selfcheck_statefile() current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state if "last_check" in state.state and "pypi_version" in state.state: last_check = datetime.datetime.strptime(state.state["last_check"], SELFCHECK_DATE_FMT) if total_seconds(current_time - last_check) < 7 * 24 * 60 * 60: pypi_version = state.state["pypi_version"] # Refresh the version if we need to or just see if we need to warn if pypi_version is None: resp = session.get( PyPI.pip_json_url, headers={"Accept": "application/json"}, ) resp.raise_for_status() pypi_version = [ v for v in sorted( list(resp.json()["releases"]), key=packaging_version.parse, ) if not packaging_version.parse(v).is_prerelease ][-1] # save that we've performed a check state.save(pypi_version, current_time) remote_version = packaging_version.parse(pypi_version) # Determine if our pypi_version is older if (pip_version < remote_version and pip_version.base_version != remote_version.base_version): # Advise "python -m pip" on Windows to avoid issues # with overwriting pip.exe. if WINDOWS: pip_cmd = "python -m pip" else: pip_cmd = "pip" logger.warning( "You are using pip version %s, however version %s is " "available.\nYou should consider upgrading via the " "'%s install --upgrade pip' command.", pip_version, pypi_version, pip_cmd) except Exception: logger.debug( "There was an error checking the latest version of pip", exc_info=True, )
def pip_version_check(session, options): """Check for an update for pip. Limit the frequency of checks to once per week. State is stored either in the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix of the pip script path. """ installed_version = get_installed_version("pip") if not installed_version: return pip_version = packaging_version.parse(installed_version) pypi_version = None try: state = load_selfcheck_statefile() current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state if "last_check" in state.state and "pypi_version" in state.state: last_check = datetime.datetime.strptime( state.state["last_check"], SELFCHECK_DATE_FMT ) if (current_time - last_check).total_seconds() < 7 * 24 * 60 * 60: pypi_version = state.state["pypi_version"] # Refresh the version if we need to or just see if we need to warn if pypi_version is None: # Lets use PackageFinder to see what the latest pip version is finder = PackageFinder( find_links=options.find_links, index_urls=[options.index_url] + options.extra_index_urls, allow_all_prereleases=False, # Explicitly set to False trusted_hosts=options.trusted_hosts, process_dependency_links=options.process_dependency_links, session=session, ) all_candidates = finder.find_all_candidates("pip") if not all_candidates: return pypi_version = str( max(all_candidates, key=lambda c: c.version).version ) # save that we've performed a check state.save(pypi_version, current_time) remote_version = packaging_version.parse(pypi_version) # Determine if our pypi_version is older if (pip_version < remote_version and pip_version.base_version != remote_version.base_version): # Advise "python -m pip" on Windows to avoid issues # with overwriting pip.exe. if WINDOWS: pip_cmd = "python -m pip" else: pip_cmd = "pip" logger.warning( "You are using pip version %s, however version %s is " "available.\nYou should consider upgrading via the " "'%s install --upgrade pip' command.", pip_version, pypi_version, pip_cmd ) except Exception: logger.debug( "There was an error checking the latest version of pip", exc_info=True, )
def update(args=None): from pip.utils import (get_installed_version, dist_is_editable, dist_location) import platform import subprocess from subprocess import CalledProcessError dist = get_dist('topicexplorer') if dist_is_editable(dist): # pragma: no cover print( "You have an editable install, so updates will be pulled from git." ) print("Your install directory is: {}\n".format(dist.location)) for attempt in range(2): try: import git from git.exc import InvalidGitRepositoryError break except ImportError: install = input( "GitPython is required, but is not installed. Install? [Y/n] " ) if install == '' or install.lower()[0] == 'y': subprocess.check_call('pip install gitpython', shell=True) # TODO: Add error handling for failed gitpython install # Refresh local python path to reflect gitpython import site reload(site) # attempt import once again import git reload(git) from git.exc import InvalidGitRepositoryError else: print("GitPython is required to work with an editable install,") print("but it was not successfully installed.\n") return try: repo = git.Repo(dist.location) except InvalidGitRepositoryError: print( "pip has detected an editable install, but the install directory" ) print("is not a valid git repository.\n") return if repo.is_dirty(): print("There are uncommitted changes in your local repository.") print("Please commit before running `topicexplorer update`.\n") return if repo.active_branch != repo.heads.master: print("You are on the '{}' branch.".format(repo.active_branch), end=' ') install = input("Switch to the 'master' branch? [Y/n] ") if install == '' or install.lower()[0] == 'y': print("Switched to 'master' branch.") repo.heads.master.checkout() else: print( "You must switch to the 'master' branch to use `topicexplorer update`." ) return if not repo.bare: # check for upstream updates branch = repo.active_branch repo.remotes.origin.fetch(branch) commits_behind = list( repo.iter_commits( '{BRANCH}..origin/{BRANCH}'.format(BRANCH=branch.name))) commits_ahead = list( repo.iter_commits( 'origin/{BRANCH}..{BRANCH}'.format(BRANCH=branch.name))) if commits_behind: print("Your branch is {} commits behind GitHub.".format( len(commits_behind))) if platform.system() == 'Windows': # pragma: no cover import sys if sys.argv[0] != __file__: print( "Use the `python -m topicexplorer.update` command to update." ) return # TODO: remove process_exists('vsm.exe') on 1.0rc1 if process_exists('topicexplorer.exe') or process_exists( 'vsm.exe'): print("vsm is currently running,", end=' ') print("please close all Topic Explorers to update.") return print("Pulling changes.") repo.remotes.origin.pull() # reinstall, just in case dependencies or version have updated try: subprocess.check_call('python setup.py develop', cwd=dist.location, shell=True) except: print("ERROR: Update did not comlete installation.\n") else: print("Your local branch was updated.\n") elif commits_ahead: print("Your branch is {} commits ahead of GitHub.".format( len(commits_ahead))) push = input("Do you want to push? [Y/n] ") if push == '' or push.lower()[0] == 'y': repo.remotes.origin.push() else: print( "Your local branch is synced with GitHub. No updates available.\n" ) else: # TODO: Check if pre-release, if so, then continue beta updates. # If not, then wait for stable release. Allow for override flag. from pip._vendor.packaging.version import parse as parse_version installed_version = parse_version( get_installed_version('topicexplorer')) pypi_version = parse_version(pypi_versions('topicexplorer')[-1]) update_available = pypi_version > installed_version if update_available: if platform.system() == 'Windows': # pragma: no cover import sys if sys.argv[0] != __file__: print( "Update available. Use the `python -m topicexplorer.update`", end=' ') print("command to update.") return # TODO: remove process_exists('vsm.exe') on 1.0rc1 if process_exists('topicexplorer.exe') or process_exists( 'vsm.exe'): print( "topicexplorer is currently running, please close all Topic Explorers to update." ) return try: subprocess.check_call( 'pip install topicexplorer=={} --no-cache-dir'.format( pypi_version), shell=True) except CalledProcessError: print("ERROR: Update did not comlete installation.\n") else: print("Updated from {} to {}.\n".format( installed_version, pypi_version)) else: print("You have the most recent release. No updates available.\n")
def installed_version(self): return get_installed_version(self.name)
def run(self, options, args): cmdoptions.resolve_wheel_no_use_binary(options) cmdoptions.check_install_build_global(options) if options.as_egg: warnings.warn( "--egg has been deprecated and will be removed in the future. " "This flag is mutually exclusive with large parts of pip, and " "actually using it invalidates pip's ability to manage the " "installation process.", RemovedInPip10Warning, ) if options.allow_external: warnings.warn( "--allow-external has been deprecated and will be removed in " "the future. Due to changes in the repository protocol, it no " "longer has any effect.", RemovedInPip10Warning, ) if options.allow_all_external: warnings.warn( "--allow-all-external has been deprecated and will be removed " "in the future. Due to changes in the repository protocol, it " "no longer has any effect.", RemovedInPip10Warning, ) if options.allow_unverified: warnings.warn( "--allow-unverified has been deprecated and will be removed " "in the future. Due to changes in the repository protocol, it " "no longer has any effect.", RemovedInPip10Warning, ) if options.download_dir: warnings.warn( "pip install --download has been deprecated and will be " "removed in the future. Pip now has a download command that " "should be used instead.", RemovedInPip10Warning, ) options.ignore_installed = True if options.build_dir: options.build_dir = os.path.abspath(options.build_dir) options.src_dir = os.path.abspath(options.src_dir) install_options = options.install_options or [] if options.use_user_site: if options.prefix_path: raise CommandError( "Can not combine '--user' and '--prefix' as they imply " "different installation locations" ) if virtualenv_no_global(): raise InstallationError( "Can not perform a '--user' install. User site-packages " "are not visible in this virtualenv." ) install_options.append('--user') install_options.append('--prefix=') temp_target_dir = None if options.target_dir: options.ignore_installed = True temp_target_dir = tempfile.mkdtemp() options.target_dir = os.path.abspath(options.target_dir) if (os.path.exists(options.target_dir) and not os.path.isdir(options.target_dir)): raise CommandError( "Target path exists but is not a directory, will not " "continue." ) install_options.append('--home=' + temp_target_dir) global_options = options.global_options or [] with self._build_session(options) as session: finder = self._build_package_finder(options, session) build_delete = (not (options.no_clean or options.build_dir)) wheel_cache = WheelCache(options.cache_dir, options.format_control) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with BuildDirectory(options.build_dir, delete=build_delete) as build_dir: requirement_set = RequirementSet( build_dir=build_dir, src_dir=options.src_dir, download_dir=options.download_dir, upgrade=options.upgrade, upgrade_strategy=options.upgrade_strategy, as_egg=options.as_egg, ignore_installed=options.ignore_installed, ignore_dependencies=options.ignore_dependencies, ignore_requires_python=options.ignore_requires_python, force_reinstall=options.force_reinstall, use_user_site=options.use_user_site, target_dir=temp_target_dir, session=session, pycompile=options.compile, isolated=options.isolated_mode, wheel_cache=wheel_cache, require_hashes=options.require_hashes, toto_verify=options.toto_verify, toto_default=options.toto_default ) self.populate_requirement_set( requirement_set, args, options, finder, session, self.name, wheel_cache ) if not requirement_set.has_requirements: return try: ####print "options.download_dir: %s | wheel:%s | options.cache_dir:%s" % (options.download_dir, wheel, options.cache_dir) if (options.download_dir or not wheel or not options.cache_dir): # on -d don't do complex things like building # wheels, and don't try to build wheels when wheel is # not installed. requirement_set.prepare_files(finder) else: # build wheels before install. wb = WheelBuilder( requirement_set, finder, build_options=[], global_options=[], ) # Ignore the result: a failed wheel will be # installed from the sdist/vcs whatever. wb.build(autobuilding=True) if not options.download_dir: #print options.cache_dir #print "install.py's verify" #layout_keys = {'alice.pub'} #in_toto_verify("root.layout", layout_keys) requirement_set.install( install_options, global_options, root=options.root_path, prefix=options.prefix_path, ) possible_lib_locations = get_lib_location_guesses( user=options.use_user_site, home=temp_target_dir, root=options.root_path, prefix=options.prefix_path, isolated=options.isolated_mode, ) reqs = sorted( requirement_set.successfully_installed, key=operator.attrgetter('name')) items = [] for req in reqs: item = req.name try: installed_version = get_installed_version( req.name, possible_lib_locations ) if installed_version: item += '-' + installed_version except Exception: pass items.append(item) installed = ' '.join(items) if installed: logger.info('Successfully installed %s', installed) else: downloaded = ' '.join([ req.name for req in requirement_set.successfully_downloaded ]) if downloaded: logger.info( 'Successfully downloaded %s', downloaded ) except PreviousBuildDirError: options.no_clean = True raise finally: # Clean up if not options.no_clean: requirement_set.cleanup_files() if options.target_dir: ensure_dir(options.target_dir) # Checking both purelib and platlib directories for installed # packages to be moved to target directory lib_dir_list = [] purelib_dir = distutils_scheme('', home=temp_target_dir)['purelib'] platlib_dir = distutils_scheme('', home=temp_target_dir)['platlib'] if os.path.exists(purelib_dir): lib_dir_list.append(purelib_dir) if os.path.exists(platlib_dir) and platlib_dir != purelib_dir: lib_dir_list.append(platlib_dir) for lib_dir in lib_dir_list: for item in os.listdir(lib_dir): target_item_dir = os.path.join(options.target_dir, item) if os.path.exists(target_item_dir): if not options.upgrade: logger.warning( 'Target directory %s already exists. Specify ' '--upgrade to force replacement.', target_item_dir ) continue if os.path.islink(target_item_dir): logger.warning( 'Target directory %s already exists and is ' 'a link. Pip will not automatically replace ' 'links, please remove if replacement is ' 'desired.', target_item_dir ) continue if os.path.isdir(target_item_dir): shutil.rmtree(target_item_dir) else: os.remove(target_item_dir) shutil.move( os.path.join(lib_dir, item), target_item_dir ) shutil.rmtree(temp_target_dir) return requirement_set
def get_filename(dist): version =get_installed_version(dist) char = dist[0].upper() template = "package-{}.json".format(version) url = os.path.join(reddove_dict[char], dist, template) return url
def container_tag(): # Today our container version is linked to the version of the pip module kubos-sdk. return get_installed_version('kubos-sdk')
def run(self, options, args): cmdoptions.check_install_build_global(options) upgrade_strategy = "to-satisfy-only" if options.upgrade: upgrade_strategy = options.upgrade_strategy if options.build_dir: options.build_dir = os.path.abspath(options.build_dir) options.src_dir = os.path.abspath(options.src_dir) install_options = options.install_options or [] if options.use_user_site: if options.prefix_path: raise CommandError( "Can not combine '--user' and '--prefix' as they imply " "different installation locations") if virtualenv_no_global(): raise InstallationError( "Can not perform a '--user' install. User site-packages " "are not visible in this virtualenv.") install_options.append('--user') install_options.append('--prefix=') target_temp_dir = TempDirectory(kind="target") if options.target_dir: options.ignore_installed = True options.target_dir = os.path.abspath(options.target_dir) if (os.path.exists(options.target_dir) and not os.path.isdir(options.target_dir)): raise CommandError( "Target path exists but is not a directory, will not " "continue.") # Create a target directory for using with the target option target_temp_dir.create() install_options.append('--home=' + target_temp_dir.path) global_options = options.global_options or [] with self._build_session(options) as session: finder = self._build_package_finder(options, session) build_delete = (not (options.no_clean or options.build_dir)) wheel_cache = WheelCache(options.cache_dir, options.format_control) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with TempDirectory(options.build_dir, delete=build_delete, kind="install") as directory: requirement_set = RequirementSet( build_dir=directory.path, src_dir=options.src_dir, target_dir=target_temp_dir.path, pycompile=options.compile, wheel_cache=wheel_cache, require_hashes=options.require_hashes, use_user_site=options.use_user_site, progress_bar=options.progress_bar, ) self.populate_requirement_set(requirement_set, args, options, finder, session, self.name, wheel_cache) try: resolver = Resolver( preparer=RequirementPreparer(), finder=finder, session=session, use_user_site=options.use_user_site, upgrade_strategy=upgrade_strategy, force_reinstall=options.force_reinstall, ignore_dependencies=options.ignore_dependencies, ignore_requires_python=options.ignore_requires_python, ignore_installed=options.ignore_installed, isolated=options.isolated_mode, ) resolver.resolve(requirement_set) # on -d don't do complex things like building # wheels, and don't try to build wheels when wheel is # not installed. if wheel and options.cache_dir: # build wheels before install. wb = WheelBuilder( requirement_set, finder, build_options=[], global_options=[], ) # Ignore the result: a failed wheel will be # installed from the sdist/vcs whatever. wb.build(session=session, autobuilding=True) requirement_set.install( install_options, global_options, root=options.root_path, prefix=options.prefix_path, ) possible_lib_locations = get_lib_location_guesses( user=options.use_user_site, home=target_temp_dir.path, root=options.root_path, prefix=options.prefix_path, isolated=options.isolated_mode, ) reqs = sorted(requirement_set.successfully_installed, key=operator.attrgetter('name')) items = [] for req in reqs: item = req.name try: installed_version = get_installed_version( req.name, possible_lib_locations) if installed_version: item += '-' + installed_version except Exception: pass items.append(item) installed = ' '.join(items) if installed: logger.info('Successfully installed %s', installed) except EnvironmentError as e: message_parts = [] user_option_part = "Consider using the `--user` option" permissions_part = "Check the permissions" if e.errno == errno.EPERM: if not options.use_user_site: message_parts.extend([ user_option_part, " or ", permissions_part.lower(), ]) else: message_parts.append(permissions_part) message_parts.append("\n") logger.error("".join(message_parts), exc_info=(options.verbose > 1)) return ERROR except PreviousBuildDirError: options.no_clean = True raise finally: # Clean up if not options.no_clean: requirement_set.cleanup_files() if options.target_dir: self._handle_target_dir(options.target_dir, target_temp_dir, options.upgrade) return requirement_set
def run(self, options, args): cmdoptions.resolve_wheel_no_use_binary(options) cmdoptions.check_install_build_global(options) if options.as_egg: warnings.warn( "--egg has been deprecated and will be removed in the future. " "This flag is mutually exclusive with large parts of pip, and " "actually using it invalidates pip's ability to manage the " "installation process.", RemovedInPip10Warning, ) if options.allow_external: warnings.warn( "--allow-external has been deprecated and will be removed in " "the future. Due to changes in the repository protocol, it no " "longer has any effect.", RemovedInPip10Warning, ) if options.allow_all_external: warnings.warn( "--allow-all-external has been deprecated and will be removed " "in the future. Due to changes in the repository protocol, it " "no longer has any effect.", RemovedInPip10Warning, ) if options.allow_unverified: warnings.warn( "--allow-unverified has been deprecated and will be removed " "in the future. Due to changes in the repository protocol, it " "no longer has any effect.", RemovedInPip10Warning, ) if options.download_dir: warnings.warn( "pip install --download has been deprecated and will be " "removed in the future. Pip now has a download command that " "should be used instead.", RemovedInPip10Warning, ) options.ignore_installed = True if options.build_dir: options.build_dir = os.path.abspath(options.build_dir) options.src_dir = os.path.abspath(options.src_dir) install_options = options.install_options or [] if options.use_user_site: if options.prefix_path: raise CommandError( "Can not combine '--user' and '--prefix' as they imply " "different installation locations") if virtualenv_no_global(): raise InstallationError( "Can not perform a '--user' install. User site-packages " "are not visible in this virtualenv.") install_options.append('--user') install_options.append('--prefix=') temp_target_dir = None if options.target_dir: options.ignore_installed = True temp_target_dir = tempfile.mkdtemp() options.target_dir = os.path.abspath(options.target_dir) if (os.path.exists(options.target_dir) and not os.path.isdir(options.target_dir)): raise CommandError( "Target path exists but is not a directory, will not " "continue.") install_options.append('--home=' + temp_target_dir) global_options = options.global_options or [] with self._build_session(options) as session: finder = self._build_package_finder(options, session) build_delete = (not (options.no_clean or options.build_dir)) wheel_cache = WheelCache(options.cache_dir, options.format_control) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with BuildDirectory(options.build_dir, delete=build_delete) as build_dir: requirement_set = RequirementSet( build_dir=build_dir, src_dir=options.src_dir, download_dir=options.download_dir, upgrade=options.upgrade, upgrade_strategy=options.upgrade_strategy, as_egg=options.as_egg, ignore_installed=options.ignore_installed, ignore_dependencies=options.ignore_dependencies, ignore_requires_python=options.ignore_requires_python, force_reinstall=options.force_reinstall, use_user_site=options.use_user_site, target_dir=temp_target_dir, session=session, pycompile=options.compile, isolated=options.isolated_mode, wheel_cache=wheel_cache, require_hashes=options.require_hashes, ) self.populate_requirement_set(requirement_set, args, options, finder, session, self.name, wheel_cache) if not requirement_set.has_requirements: return try: if (options.download_dir or not wheel or not options.cache_dir): # on -d don't do complex things like building # wheels, and don't try to build wheels when wheel is # not installed. requirement_set.prepare_files(finder) else: # build wheels before install. wb = WheelBuilder( requirement_set, finder, build_options=[], global_options=[], ) # Ignore the result: a failed wheel will be # installed from the sdist/vcs whatever. wb.build(autobuilding=True) if not options.download_dir: requirement_set.install( install_options, global_options, root=options.root_path, prefix=options.prefix_path, strip_file_prefix=options.strip_file_prefix, ) possible_lib_locations = get_lib_location_guesses( user=options.use_user_site, home=temp_target_dir, root=options.root_path, prefix=options.prefix_path, isolated=options.isolated_mode, ) reqs = sorted(requirement_set.successfully_installed, key=operator.attrgetter('name')) items = [] for req in reqs: item = req.name try: installed_version = get_installed_version( req.name, possible_lib_locations) if installed_version: item += '-' + installed_version except Exception: pass items.append(item) installed = ' '.join(items) if installed: logger.info('Successfully installed %s', installed) else: downloaded = ' '.join([ req.name for req in requirement_set.successfully_downloaded ]) if downloaded: logger.info('Successfully downloaded %s', downloaded) except PreviousBuildDirError: options.no_clean = True raise finally: # Clean up if not options.no_clean: requirement_set.cleanup_files() if options.target_dir: ensure_dir(options.target_dir) # Checking both purelib and platlib directories for installed # packages to be moved to target directory lib_dir_list = [] purelib_dir = distutils_scheme('', home=temp_target_dir)['purelib'] platlib_dir = distutils_scheme('', home=temp_target_dir)['platlib'] if os.path.exists(purelib_dir): lib_dir_list.append(purelib_dir) if os.path.exists(platlib_dir) and platlib_dir != purelib_dir: lib_dir_list.append(platlib_dir) for lib_dir in lib_dir_list: for item in os.listdir(lib_dir): target_item_dir = os.path.join(options.target_dir, item) if os.path.exists(target_item_dir): if not options.upgrade: logger.warning( 'Target directory %s already exists. Specify ' '--upgrade to force replacement.', target_item_dir) continue if os.path.islink(target_item_dir): logger.warning( 'Target directory %s already exists and is ' 'a link. Pip will not automatically replace ' 'links, please remove if replacement is ' 'desired.', target_item_dir) continue if os.path.isdir(target_item_dir): shutil.rmtree(target_item_dir) else: os.remove(target_item_dir) shutil.move(os.path.join(lib_dir, item), target_item_dir) shutil.rmtree(temp_target_dir) return requirement_set
def update(args=None): from pip.utils import (get_installed_version, dist_is_editable, dist_location) import platform import subprocess from subprocess import CalledProcessError dist = get_dist('topicexplorer') if dist_is_editable(dist): # pragma: no cover print("You have an editable install, so updates will be pulled from git.") print("Your install directory is: {}\n".format(dist.location)) for attempt in range(2): try: import git from git.exc import InvalidGitRepositoryError break except ImportError: install = input( "GitPython is required, but is not installed. Install? [Y/n] ") if install == '' or install.lower()[0] == 'y': subprocess.check_call('pip install gitpython', shell=True) # TODO: Add error handling for failed gitpython install # Refresh local python path to reflect gitpython import site reload(site) # attempt import once again import git reload(git) from git.exc import InvalidGitRepositoryError else: print("GitPython is required to work with an editable install,") print("but it was not successfully installed.\n") return try: repo = git.Repo(dist.location) except InvalidGitRepositoryError: print("pip has detected an editable install, but the install directory") print("is not a valid git repository.\n") return if repo.is_dirty(): print("There are uncommitted changes in your local repository.") print("Please commit before running `topicexplorer update`.\n") return if repo.active_branch != repo.heads.master: print("You are on the '{}' branch.".format(repo.active_branch), end=' ') install = input("Switch to the 'master' branch? [Y/n] ") if install == '' or install.lower()[0] == 'y': print("Switched to 'master' branch.") repo.heads.master.checkout() else: print("You must switch to the 'master' branch to use `topicexplorer update`.") return if not repo.bare: # check for upstream updates branch = repo.active_branch repo.remotes.origin.fetch(branch) commits_behind = list(repo.iter_commits( '{BRANCH}..origin/{BRANCH}'.format(BRANCH=branch.name))) commits_ahead = list(repo.iter_commits( 'origin/{BRANCH}..{BRANCH}'.format(BRANCH=branch.name))) if commits_behind: print("Your branch is {} commits behind GitHub.".format(len(commits_behind))) if platform.system() == 'Windows': # pragma: no cover import sys if sys.argv[0] != __file__: print("Use the `python -m topicexplorer.update` command to update.") return # TODO: remove process_exists('vsm.exe') on 1.0rc1 if process_exists('topicexplorer.exe') or process_exists('vsm.exe'): print("vsm is currently running,", end=' ') print("please close all Topic Explorers to update.") return print("Pulling changes.") repo.remotes.origin.pull() # reinstall, just in case dependencies or version have updated try: subprocess.check_call('python setup.py develop', cwd=dist.location, shell=True) except: print("ERROR: Update did not comlete installation.\n") else: print("Your local branch was updated.\n") elif commits_ahead: print("Your branch is {} commits ahead of GitHub.".format(len(commits_ahead))) push = input("Do you want to push? [Y/n] ") if push == '' or push.lower()[0] == 'y': repo.remotes.origin.push() else: print("Your local branch is synced with GitHub. No updates available.\n") else: # TODO: Check if pre-release, if so, then continue beta updates. # If not, then wait for stable release. Allow for override flag. from pip._vendor.packaging.version import parse as parse_version installed_version = parse_version(get_installed_version('topicexplorer')) pypi_version = parse_version(pypi_versions('topicexplorer')[-1]) update_available = pypi_version > installed_version if update_available: if platform.system() == 'Windows': # pragma: no cover import sys if sys.argv[0] != __file__: print("Update available. Use the `python -m topicexplorer.update`", end=' ') print("command to update.") return # TODO: remove process_exists('vsm.exe') on 1.0rc1 if process_exists('topicexplorer.exe') or process_exists('vsm.exe'): print("topicexplorer is currently running, please close all Topic Explorers to update.") return try: subprocess.check_call( 'pip install topicexplorer=={} --no-cache-dir'.format(pypi_version), shell=True) except CalledProcessError: print("ERROR: Update did not comlete installation.\n") else: print("Updated from {} to {}.\n".format(installed_version, pypi_version)) else: print("You have the most recent release. No updates available.\n")
def run(self, options, args): cmdoptions.check_install_build_global(options) if options.build_dir: options.build_dir = os.path.abspath(options.build_dir) options.src_dir = os.path.abspath(options.src_dir) install_options = options.install_options or [] if options.use_user_site: if options.prefix_path: raise CommandError( "Can not combine '--user' and '--prefix' as they imply " "different installation locations" ) if virtualenv_no_global(): raise InstallationError( "Can not perform a '--user' install. User site-packages " "are not visible in this virtualenv." ) install_options.append('--user') install_options.append('--prefix=') target_temp_dir = TempDirectory(kind="target") if options.target_dir: options.ignore_installed = True options.target_dir = os.path.abspath(options.target_dir) if (os.path.exists(options.target_dir) and not os.path.isdir(options.target_dir)): raise CommandError( "Target path exists but is not a directory, will not " "continue." ) # Create a target directory for using with the target option target_temp_dir.create() install_options.append('--home=' + target_temp_dir.path) global_options = options.global_options or [] with self._build_session(options) as session: finder = self._build_package_finder(options, session) build_delete = (not (options.no_clean or options.build_dir)) wheel_cache = WheelCache(options.cache_dir, options.format_control) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with TempDirectory( options.build_dir, delete=build_delete, kind="install" ) as directory: requirement_set = RequirementSet( build_dir=directory.path, src_dir=options.src_dir, upgrade=options.upgrade, upgrade_strategy=options.upgrade_strategy, ignore_installed=options.ignore_installed, ignore_dependencies=options.ignore_dependencies, ignore_requires_python=options.ignore_requires_python, force_reinstall=options.force_reinstall, use_user_site=options.use_user_site, target_dir=target_temp_dir.path, session=session, pycompile=options.compile, isolated=options.isolated_mode, wheel_cache=wheel_cache, require_hashes=options.require_hashes, progress_bar=options.progress_bar, ) self.populate_requirement_set( requirement_set, args, options, finder, session, self.name, wheel_cache ) try: if (not wheel or not options.cache_dir): # on -d don't do complex things like building # wheels, and don't try to build wheels when wheel is # not installed. requirement_set.prepare_files(finder) else: # build wheels before install. wb = WheelBuilder( requirement_set, finder, build_options=[], global_options=[], ) # Ignore the result: a failed wheel will be # installed from the sdist/vcs whatever. wb.build(autobuilding=True) requirement_set.install( install_options, global_options, root=options.root_path, prefix=options.prefix_path, ) possible_lib_locations = get_lib_location_guesses( user=options.use_user_site, home=target_temp_dir.path, root=options.root_path, prefix=options.prefix_path, isolated=options.isolated_mode, ) reqs = sorted( requirement_set.successfully_installed, key=operator.attrgetter('name')) items = [] for req in reqs: item = req.name try: installed_version = get_installed_version( req.name, possible_lib_locations ) if installed_version: item += '-' + installed_version except Exception: pass items.append(item) installed = ' '.join(items) if installed: logger.info('Successfully installed %s', installed) except EnvironmentError as e: message_parts = [] user_option_part = "Consider using the `--user` option" permissions_part = "Check the permissions" if e.errno == errno.EPERM: if not options.use_user_site: message_parts.extend([ user_option_part, " or ", permissions_part.lower(), ]) else: message_parts.append(permissions_part) message_parts.append("\n") logger.error( "".join(message_parts), exc_info=(options.verbose > 1) ) return ERROR except PreviousBuildDirError: options.no_clean = True raise finally: # Clean up if not options.no_clean: requirement_set.cleanup_files() if options.target_dir: self._handle_target_dir( options.target_dir, target_temp_dir, options.upgrade ) return requirement_set
def pip_version_check(session, options): """Check for an update for pip. Limit the frequency of checks to once per week. State is stored either in the active virtualenv or in the user's USER_CACHE_DIR keyed off the prefix of the pip script path. """ installed_version = get_installed_version("pip") if not installed_version: return pip_version = packaging_version.parse(installed_version) pypi_version = None try: state = load_selfcheck_statefile() current_time = datetime.datetime.utcnow() # Determine if we need to refresh the state if "last_check" in state.state and "pypi_version" in state.state: last_check = datetime.datetime.strptime(state.state["last_check"], SELFCHECK_DATE_FMT) if (current_time - last_check).total_seconds() < 7 * 24 * 60 * 60: pypi_version = state.state["pypi_version"] # Refresh the version if we need to or just see if we need to warn if pypi_version is None: # Lets use PackageFinder to see what the latest pip version is finder = PackageFinder( find_links=options.find_links, index_urls=[options.index_url] + options.extra_index_urls, allow_all_prereleases=False, # Explicitly set to False trusted_hosts=options.trusted_hosts, process_dependency_links=options.process_dependency_links, session=session, ) all_candidates = finder.find_all_candidates("pip") if not all_candidates: return pypi_version = str( max(all_candidates, key=lambda c: c.version).version) # save that we've performed a check state.save(pypi_version, current_time) remote_version = packaging_version.parse(pypi_version) # Determine if our pypi_version is older if (pip_version < remote_version and pip_version.base_version != remote_version.base_version): # Advise "python -m pip" on Windows to avoid issues # with overwriting pip.exe. if WINDOWS: pip_cmd = "python -m pip" else: pip_cmd = "pip" logger.warning( "You are using pip version %s, however version %s is " "available.\nYou should consider upgrading via the " "'%s install --upgrade pip' command.", pip_version, pypi_version, pip_cmd) except Exception: logger.debug( "There was an error checking the latest version of pip", exc_info=True, )
def get_dist_file(self, dist, version=None): from pip.utils import get_installed_version if not version: version = get_installed_version(dist)
def user_agent(): """ Return a string representing the user agent. """ data = { "installer": {"name": "pip", "version": pip.__version__}, "python": platform.python_version(), "implementation": { "name": platform.python_implementation(), }, } if data["implementation"]["name"] == 'CPython': data["implementation"]["version"] = platform.python_version() elif data["implementation"]["name"] == 'PyPy': if sys.pypy_version_info.releaselevel == 'final': pypy_version_info = sys.pypy_version_info[:3] else: pypy_version_info = sys.pypy_version_info data["implementation"]["version"] = ".".join( [str(x) for x in pypy_version_info] ) elif data["implementation"]["name"] == 'Jython': # Complete Guess data["implementation"]["version"] = platform.python_version() elif data["implementation"]["name"] == 'IronPython': # Complete Guess data["implementation"]["version"] = platform.python_version() if sys.platform.startswith("linux"): from pip._vendor import distro distro_infos = dict(filter( lambda x: x[1], zip(["name", "version", "id"], distro.linux_distribution()), )) libc = dict(filter( lambda x: x[1], zip(["lib", "version"], libc_ver()), )) if libc: distro_infos["libc"] = libc if distro_infos: data["distro"] = distro_infos if sys.platform.startswith("darwin") and platform.mac_ver()[0]: data["distro"] = {"name": "macOS", "version": platform.mac_ver()[0]} if platform.system(): data.setdefault("system", {})["name"] = platform.system() if platform.release(): data.setdefault("system", {})["release"] = platform.release() if platform.machine(): data["cpu"] = platform.machine() if HAS_TLS: data["openssl_version"] = ssl.OPENSSL_VERSION setuptools_version = get_installed_version("setuptools") if setuptools_version is not None: data["setuptools_version"] = setuptools_version return "{data[installer][name]}/{data[installer][version]} {json}".format( data=data, json=json.dumps(data, separators=(",", ":"), sort_keys=True), )
def user_agent(): """ Return a string representing the user agent. """ data = { "installer": { "name": "pip", "version": pip.__version__ }, "python": platform.python_version(), "implementation": { "name": platform.python_implementation(), }, } if data["implementation"]["name"] == 'CPython': data["implementation"]["version"] = platform.python_version() elif data["implementation"]["name"] == 'PyPy': if sys.pypy_version_info.releaselevel == 'final': pypy_version_info = sys.pypy_version_info[:3] else: pypy_version_info = sys.pypy_version_info data["implementation"]["version"] = ".".join( [str(x) for x in pypy_version_info]) elif data["implementation"]["name"] == 'Jython': # Complete Guess data["implementation"]["version"] = platform.python_version() elif data["implementation"]["name"] == 'IronPython': # Complete Guess data["implementation"]["version"] = platform.python_version() if sys.platform.startswith("linux"): from pip._vendor import distro distro_infos = dict( filter( lambda x: x[1], zip(["name", "version", "id"], distro.linux_distribution()), )) libc = dict( filter( lambda x: x[1], zip(["lib", "version"], libc_ver()), )) if libc: distro_infos["libc"] = libc if distro_infos: data["distro"] = distro_infos if sys.platform.startswith("darwin") and platform.mac_ver()[0]: data["distro"] = {"name": "macOS", "version": platform.mac_ver()[0]} if platform.system(): data.setdefault("system", {})["name"] = platform.system() if platform.release(): data.setdefault("system", {})["release"] = platform.release() if platform.machine(): data["cpu"] = platform.machine() if HAS_TLS: data["openssl_version"] = ssl.OPENSSL_VERSION setuptools_version = get_installed_version("setuptools") if setuptools_version is not None: data["setuptools_version"] = setuptools_version return "{data[installer][name]}/{data[installer][version]} {json}".format( data=data, json=json.dumps(data, separators=(",", ":"), sort_keys=True), )
def run(self, options, args): cmdoptions.check_install_build_global(options) if options.build_dir: options.build_dir = os.path.abspath(options.build_dir) options.src_dir = os.path.abspath(options.src_dir) install_options = options.install_options or [] if options.use_user_site: if options.prefix_path: raise CommandError( "Can not combine '--user' and '--prefix' as they imply " "different installation locations") if virtualenv_no_global(): raise InstallationError( "Can not perform a '--user' install. User site-packages " "are not visible in this virtualenv.") install_options.append('--user') install_options.append('--prefix=') temp_target_dir = None if options.target_dir: options.ignore_installed = True temp_target_dir = tempfile.mkdtemp() options.target_dir = os.path.abspath(options.target_dir) if (os.path.exists(options.target_dir) and not os.path.isdir(options.target_dir)): raise CommandError( "Target path exists but is not a directory, will not " "continue.") install_options.append('--home=' + temp_target_dir) global_options = options.global_options or [] with self._build_session(options) as session: finder = self._build_package_finder(options, session) build_delete = (not (options.no_clean or options.build_dir)) wheel_cache = WheelCache(options.cache_dir, options.format_control) if options.cache_dir and not check_path_owner(options.cache_dir): logger.warning( "The directory '%s' or its parent directory is not owned " "by the current user and caching wheels has been " "disabled. check the permissions and owner of that " "directory. If executing pip with sudo, you may want " "sudo's -H flag.", options.cache_dir, ) options.cache_dir = None with BuildDirectory(options.build_dir, delete=build_delete) as build_dir: requirement_set = RequirementSet( build_dir=build_dir, src_dir=options.src_dir, upgrade=options.upgrade, upgrade_strategy=options.upgrade_strategy, ignore_installed=options.ignore_installed, ignore_dependencies=options.ignore_dependencies, ignore_requires_python=options.ignore_requires_python, force_reinstall=options.force_reinstall, use_user_site=options.use_user_site, target_dir=temp_target_dir, session=session, pycompile=options.compile, isolated=options.isolated_mode, wheel_cache=wheel_cache, require_hashes=options.require_hashes, progress_bar=options.progress_bar, ) self.populate_requirement_set(requirement_set, args, options, finder, session, self.name, wheel_cache) try: if (not wheel or not options.cache_dir): # on -d don't do complex things like building # wheels, and don't try to build wheels when wheel is # not installed. requirement_set.prepare_files(finder) else: # build wheels before install. wb = WheelBuilder( requirement_set, finder, build_options=[], global_options=[], ) # Ignore the result: a failed wheel will be # installed from the sdist/vcs whatever. wb.build(autobuilding=True) requirement_set.install( install_options, global_options, root=options.root_path, prefix=options.prefix_path, ) possible_lib_locations = get_lib_location_guesses( user=options.use_user_site, home=temp_target_dir, root=options.root_path, prefix=options.prefix_path, isolated=options.isolated_mode, ) reqs = sorted(requirement_set.successfully_installed, key=operator.attrgetter('name')) items = [] for req in reqs: item = req.name try: installed_version = get_installed_version( req.name, possible_lib_locations) if installed_version: item += '-' + installed_version except Exception: pass items.append(item) installed = ' '.join(items) if installed: logger.info('Successfully installed %s', installed) except EnvironmentError as e: message_parts = [] user_option_part = "Consider using the `--user` option" permissions_part = "Check the permissions" if e.errno == errno.EPERM: if not options.use_user_site: message_parts.extend([ user_option_part, " or ", permissions_part.lower(), ]) else: message_parts.append(permissions_part) message_parts.append("\n") logger.error("".join(message_parts), exc_info=(options.verbose > 1)) return ERROR except PreviousBuildDirError: options.no_clean = True raise finally: # Clean up if not options.no_clean: requirement_set.cleanup_files() if options.target_dir: ensure_dir(options.target_dir) # Checking both purelib and platlib directories for installed # packages to be moved to target directory lib_dir_list = [] purelib_dir = distutils_scheme('', home=temp_target_dir)['purelib'] platlib_dir = distutils_scheme('', home=temp_target_dir)['platlib'] data_dir = distutils_scheme('', home=temp_target_dir)['data'] if os.path.exists(purelib_dir): lib_dir_list.append(purelib_dir) if os.path.exists(platlib_dir) and platlib_dir != purelib_dir: lib_dir_list.append(platlib_dir) if os.path.exists(data_dir): lib_dir_list.append(data_dir) for lib_dir in lib_dir_list: for item in os.listdir(lib_dir): if lib_dir == data_dir: ddir = os.path.join(data_dir, item) if any(s.startswith(ddir) for s in lib_dir_list[:-1]): continue target_item_dir = os.path.join(options.target_dir, item) if os.path.exists(target_item_dir): if not options.upgrade: logger.warning( 'Target directory %s already exists. Specify ' '--upgrade to force replacement.', target_item_dir) continue if os.path.islink(target_item_dir): logger.warning( 'Target directory %s already exists and is ' 'a link. Pip will not automatically replace ' 'links, please remove if replacement is ' 'desired.', target_item_dir) continue if os.path.isdir(target_item_dir): shutil.rmtree(target_item_dir) else: os.remove(target_item_dir) shutil.move(os.path.join(lib_dir, item), target_item_dir) shutil.rmtree(temp_target_dir) return requirement_set