def install_package(self, packages): install = InstallCommand() pkg_list = packages.split(" ") for pkg in pkg_list: # virtualenv active ? if hasattr(sys, 'real_prefix'): options, args = install.parse_args([pkg, '--upgrade']) else: options, args = install.parse_args( [pkg, '--upgrade', '--user']) install.run(options, args)
def install_package(self, packages, params=None): install = InstallCommand() pkg_list = packages.split(" ") for pkg in pkg_list: # virtualenv active ? if hasattr(sys, 'real_prefix'): options, args = install.parse_args([pkg, '--upgrade']) elif params: options, args = install.parse_args([pkg, '--upgrade', '--target', params]) else: options, args = install.parse_args([pkg, '--upgrade', '--user']) install.run(options, args)
def install(reqs, index_urls=None, upgrade=False, pre_releases=False, no_cache=False, reinstall=False): _ensure_patch_pip_get_entry_points() cmd = InstallCommand() args = [] if pre_releases: args.append("--pre") if not virtualenv_no_global(): args.append("--user") if upgrade: args.append("--upgrade") if no_cache: args.append("--no-cache-dir") if reinstall: args.append("--force-reinstall") if index_urls: args.extend(["--index-url", index_urls[0]]) for url in index_urls[1:]: args.extend(["--extra-index-url", url]) args.extend(reqs) options, cmd_args = cmd.parse_args(args) try: cmd.run(options, cmd_args) except InstallationError as e: raise InstallError(str(e))
def test_installed_files_tracking(self): """ Verify that tracking of installed files works correctly. This tests the :py:func:`~pip_accel.bdist.BinaryDistributionManager.update_installed_files()` method. When pip installs a Python package it also creates a file called ``installed-files.txt`` that contains the pathnames of the files that were installed. This file enables pip to uninstall Python packages later on. Because pip-accel implements its own package installation it also creates the ``installed-files.txt`` file, in order to enable the user to uninstall a package with pip even if the package was installed using pip-accel. """ # Prevent unsuspecting users from accidentally running the find_files() # tests below on their complete `/usr' or `/usr/local' tree :-). if not hasattr(sys, 'real_prefix'): logger.warning("Skipping installed files tracking test (not running in a recognized virtual environment).") return # Install the iPython 1.0 source distribution using pip. command = InstallCommand() opts, args = command.parse_args([ '--ignore-installed', '--no-use-wheel', 'ipython==1.0' ]) command.run(opts, args) # Make sure the iPython program works after installation using pip. try_program('ipython3' if sys.version_info[0] == 3 else 'ipython') # Find the iPython related files installed by pip. files_installed_using_pip = set(find_files(sys.prefix, 'ipython')) assert len(files_installed_using_pip) > 0, \ "It looks like pip didn't install iPython where we expected it to do so?!" logger.debug("Found %i files installed using pip: %s", len(files_installed_using_pip), files_installed_using_pip) # Remove the iPython installation. uninstall('ipython') # Install the iPython 1.0 source distribution using pip-accel. accelerator = self.initialize_pip_accel() num_installed = accelerator.install_from_arguments([ '--ignore-installed', '--no-use-wheel', 'ipython==1.0' ]) assert num_installed == 1, "Expected pip-accel to install exactly one package!" # Make sure the iPython program works after installation using pip-accel. try_program('ipython3' if sys.version_info[0] == 3 else 'ipython') # Find the iPython related files installed by pip-accel. files_installed_using_pip_accel = set(find_files(sys.prefix, 'ipython')) assert len(files_installed_using_pip_accel) > 0, \ "It looks like pip-accel didn't install iPython where we expected it to do so?!" logger.debug("Found %i files installed using pip-accel: %s", len(files_installed_using_pip_accel), files_installed_using_pip_accel) # Test that pip and pip-accel installed exactly the same files. assert files_installed_using_pip == files_installed_using_pip_accel, \ "It looks like pip and pip-accel installed different files for iPython!" # Test that pip knows how to uninstall iPython installed by pip-accel # due to the installed-files.txt file generated by pip-accel. uninstall('ipython') # Make sure all files related to iPython were uninstalled by pip. assert len(list(find_files(sys.prefix, 'ipython'))) == 0, \ "It looks like pip didn't properly uninstall iPython after installation using pip-accel!"
def install_requirements(self, requirements, **kw): """ Manually install a requirement set from binary and/or wheel distributions. :param requirements: A list of :class:`pip_accel.req.Requirement` objects. :param kw: Any keyword arguments are passed on to :func:`~pip_accel.bdist.BinaryDistributionManager.install_binary_dist()`. :returns: The number of packages that were just installed (an integer). """ install_timer = Timer() install_types = [] if any(not req.is_wheel for req in requirements): install_types.append('binary') if any(req.is_wheel for req in requirements): install_types.append('wheel') logger.info("Installing from %s distributions ..", concatenate(install_types)) # Track installed files by default (unless the caller specifically opted out). kw.setdefault('track_installed_files', True) num_installed = 0 for requirement in requirements: # If we're upgrading over an older version, first remove the # old version to make sure we don't leave files from old # versions around. if is_installed(requirement.name): uninstall(requirement.name) # When installing setuptools we need to uninstall distribute, # otherwise distribute will shadow setuptools and all sorts of # strange issues can occur (e.g. upgrading to the latest # setuptools to gain wheel support and then having everything # blow up because distribute doesn't know about wheels). if requirement.name == 'setuptools' and is_installed('distribute'): uninstall('distribute') if requirement.is_editable: logger.debug("Installing %s in editable form using pip.", requirement) command = InstallCommand() opts, args = command.parse_args( ['--no-deps', '--editable', requirement.source_directory]) command.run(opts, args) elif requirement.is_wheel: logger.info("Installing %s wheel distribution using pip ..", requirement) wheel_version = pip_wheel_module.wheel_version( requirement.source_directory) pip_wheel_module.check_compatibility(wheel_version, requirement.name) requirement.pip_requirement.move_wheel_files( requirement.source_directory) else: binary_distribution = self.bdists.get_binary_dist(requirement) self.bdists.install_binary_dist(binary_distribution, **kw) num_installed += 1 logger.info("Finished installing %s in %s.", pluralize(num_installed, "requirement"), install_timer) return num_installed
def install(reqs, index_urls=None, upgrade=False): cmd = InstallCommand() args = ["--user"] if upgrade: args.append("--upgrade") if index_urls: args.append("--index-url", index_urls[0]) for url in index_urls[1:]: args.append("--extra-index-url", url) args.extend(reqs) cmd.run(*cmd.parse_args(args))
def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir): """--require-hashes in a requirements file should make its way to the RequirementSet. """ req_set = self.basic_reqset(require_hashes=False) session = PipSession() finder = PackageFinder([data.find_links], [], session=session) command = InstallCommand() with requirements_file("--require-hashes", tmpdir) as reqs_file: options, args = command.parse_args(["-r", reqs_file]) command.populate_requirement_set(req_set, args, options, finder, session, command.name, wheel_cache=None) assert req_set.require_hashes
def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir): """--require-hashes in a requirements file should make its way to the RequirementSet. """ req_set = self.basic_reqset(require_hashes=False) session = PipSession() finder = PackageFinder([data.find_links], [], session=session) command = InstallCommand() with requirements_file('--require-hashes', tmpdir) as reqs_file: options, args = command.parse_args(['-r', reqs_file]) command.populate_requirement_set( req_set, args, options, finder, session, command.name, wheel_cache=None) assert req_set.require_hashes
def install_requirements(self, requirements, **kw): """ Manually install a requirement set from binary and/or wheel distributions. :param requirements: A list of :class:`pip_accel.req.Requirement` objects. :param kw: Any keyword arguments are passed on to :func:`~pip_accel.bdist.BinaryDistributionManager.install_binary_dist()`. :returns: The number of packages that were just installed (an integer). """ install_timer = Timer() install_types = [] if any(not req.is_wheel for req in requirements): install_types.append('binary') if any(req.is_wheel for req in requirements): install_types.append('wheel') logger.info("Installing from %s distributions ..", concatenate(install_types)) # Track installed files by default (unless the caller specifically opted out). kw.setdefault('track_installed_files', True) num_installed = 0 for requirement in requirements: # If we're upgrading over an older version, first remove the # old version to make sure we don't leave files from old # versions around. if is_installed(requirement.name): uninstall(requirement.name) # When installing setuptools we need to uninstall distribute, # otherwise distribute will shadow setuptools and all sorts of # strange issues can occur (e.g. upgrading to the latest # setuptools to gain wheel support and then having everything # blow up because distribute doesn't know about wheels). if requirement.name == 'setuptools' and is_installed('distribute'): uninstall('distribute') if requirement.is_editable: logger.debug("Installing %s in editable form using pip.", requirement) command = InstallCommand() opts, args = command.parse_args(['--no-deps', '--editable', requirement.source_directory]) command.run(opts, args) elif requirement.is_wheel: logger.info("Installing %s wheel distribution using pip ..", requirement) wheel_version = pip_wheel_module.wheel_version(requirement.source_directory) pip_wheel_module.check_compatibility(wheel_version, requirement.name) requirement.pip_requirement.move_wheel_files(requirement.source_directory) else: binary_distribution = self.bdists.get_binary_dist(requirement) self.bdists.install_binary_dist(binary_distribution, **kw) num_installed += 1 logger.info("Finished installing %s in %s.", pluralize(num_installed, "requirement"), install_timer) return num_installed
def upgrade_packages(constraints, user=False): from pip.commands.list import ListCommand from pip.commands.install import InstallCommand from pip.exceptions import InstallationError packages = [] pip_install_cmd = InstallCommand() pip_list_cmd = ListCommand() while True: # list packages that need upgrading try: options = pip_list_cmd.parse_args([])[0] options.use_user_site = user options.cache_dir = None options.outdated = True packages = [p.project_name for p, y, _ in pip_list_cmd.find_packages_latest_versions(options) if getattr(p, 'version', 0) != getattr(y, 'public', 0)] except: packages = [] # Never upgrade yourself. It doesn't end well. (Any sources already loaded are the old version, anything # loaded after this is the new version, it's a mess. It also borks development installs.) if "sickrage" in packages: packages.remove("sickrage") options = pip_install_cmd.parse_args([])[0] options.use_user_site = user options.constraints = [constraints] options.cache_dir = None options.upgrade = True options.quiet = 1 for i, pkg_name in enumerate(packages, start=1): try: print(r"[%3.2f%%]::Upgrading %s package" % (i * 100 / len(packages), pkg_name.lower())) pip_install_cmd.run(options, [pkg_name]) except InstallationError: try: options.ignore_dependencies = True pip_install_cmd.run(options, [pkg_name]) except:continue except IndexError: continue else: break
def upgrade_packages(constraints, user=False): from pip.commands.list import ListCommand from pip.commands.install import InstallCommand from pip.exceptions import InstallationError packages = [] pip_install_cmd = InstallCommand() pip_list_cmd = ListCommand() while True: # list packages that need upgrading try: options = pip_list_cmd.parse_args([])[0] options.use_user_site = user options.cache_dir = None options.outdated = True packages = [p.project_name for p, y, _ in pip_list_cmd.find_packages_latest_versions(options) if getattr(p, 'version', 0) != getattr(y, 'public', 0)] except: packages = [] options = pip_install_cmd.parse_args([])[0] options.use_user_site = user options.constraints = [constraints] options.cache_dir = None options.upgrade = True options.quiet = 1 for i, pkg_name in enumerate(packages, start=1): try: print(r"[%3.2f%%]::Upgrading %s package" % (i * 100 / len(packages), pkg_name.lower())) pip_install_cmd.run(options, [pkg_name]) except InstallationError: try: options.ignore_dependencies = True pip_install_cmd.run(options, [pkg_name]) except:continue except IndexError: continue else: break
def install_packages(file): import pip from pip.commands.install import InstallCommand from pip.exceptions import InstallationError constraints = os.path.abspath(os.path.join(os.path.dirname(__file__), 'constraints.txt')) pip_install_cmd = InstallCommand() # list installed packages try: installed = [x.project_name.lower() for x in pip.get_installed_distributions(local_only=True, user_only=root_check())] except: installed = [] # read requirements file with io.open(os.path.abspath(os.path.join(os.path.dirname(__file__), file))) as f: packages = [x.strip() for x in f.readlines() if x.strip().lower() not in installed] # install requirements packages options = pip_install_cmd.parse_args([])[0] options.use_user_site = root_check() options.constraints = [constraints] options.quiet = 1 for i, pkg_name in enumerate(packages, start=1): try: print(r"[%3.2f%%]::Installing %s package" % (i * 100 / len(packages), pkg_name)) pip_install_cmd.run(options, [pkg_name]) except InstallationError: try: options.ignore_dependencies = True pip_install_cmd.run(options, [pkg_name]) except: continue except IndexError: continue if len(packages) > 0: return True
if installed_packages: pip = UninstallCommand() options, args = pip.parse_args(installed_packages) options.yes = True try: pip.run(options, args) except OSError as e: if e.errno != 13: raise e print("You lack permissions to uninstall this package. Perhaps run with sudo? Exiting.") exit(13) date = parse_iso8601(sys.argv[1]) packages = {p: select_version(date, p) for p in sys.argv[2:]} args = ['=='.join(a) for a in packages.items()] cmd = InstallCommand() options, args = cmd.parse_args(args) options.ignore_installed = True options.force_reinstall = True try: print(cmd.run(options, args)) except OSError as e: if e.errno != 13: raise e print("You lack permissions to uninstall this package. Perhaps run with sudo? Exiting.") exit(13)
if installed_packages: pip = UninstallCommand() options, args = pip.parse_args(installed_packages) options.yes = True try: pip.run(options, args) except OSError as e: if e.errno != 13: raise e print("You lack permissions to uninstall this package. Perhaps run with sudo? Exiting.") exit(13) date = parse_iso8601(sys.argv[1]) packages = {p: select_version(date, p) for p in sys.argv[2:]} args = ["==".join(a) for a in packages.items()] cmd = InstallCommand() options, args = cmd.parse_args(args) options.ignore_installed = True options.force_reinstall = True try: print(cmd.run(options, args)) except OSError as e: if e.errno != 13: raise e print("You lack permissions to uninstall this package. Perhaps run with sudo? Exiting.") exit(13)
def get_pip_requirement_set(self, arguments, use_remote_index, use_wheels=False): """ Get the unpacked requirement(s) specified by the caller by running pip. :param arguments: The command line arguments to ``pip install ...`` (a list of strings). :param use_remote_index: A boolean indicating whether pip is allowed to connect to the main package index (http://pypi.python.org by default). :param use_wheels: Whether pip and pip-accel are allowed to use wheels_ (:data:`False` by default for backwards compatibility with callers that use pip-accel as a Python API). :returns: A :class:`pip.req.RequirementSet` object created by pip. :raises: Any exceptions raised by pip. """ # Compose the pip command line arguments. This is where a lot of the # core logic of pip-accel is hidden and it uses some esoteric features # of pip so this method is heavily commented. command_line = [] # Use `--download' to instruct pip to download requirement(s) into # pip-accel's local source distribution index directory. This has the # following documented side effects (see `pip install --help'): # 1. It disables the installation of requirements (without using the # `--no-install' option which is deprecated and slated for removal # in pip 7.x). # 2. It ignores requirements that are already installed (because # pip-accel doesn't actually need to re-install requirements that # are already installed we will have work around this later, but # that seems fairly simple to do). command_line.append('--download=%s' % self.config.source_index) # Use `--find-links' to point pip at pip-accel's local source # distribution index directory. This ensures that source distribution # archives are never downloaded more than once (regardless of the HTTP # cache that was introduced in pip 6.x). command_line.append('--find-links=%s' % create_file_url(self.config.source_index)) # Use `--no-binary=:all:' to ignore wheel distributions by default in # order to preserve backwards compatibility with callers that expect a # requirement set consisting only of source distributions that can be # converted to `dumb binary distributions'. if not use_wheels and self.arguments_allow_wheels(arguments): command_line.append('--no-binary=:all:') # Use `--no-index' to force pip to only consider source distribution # archives contained in pip-accel's local source distribution index # directory. This enables pip-accel to ask pip "Can the local source # distribution index satisfy all requirements in the given requirement # set?" which enables pip-accel to keep pip off the internet unless # absolutely necessary :-). if not use_remote_index: command_line.append('--no-index') # Use `--no-clean' to instruct pip to unpack the source distribution # archives and *not* clean up the unpacked source distributions # afterwards. This enables pip-accel to replace pip's installation # logic with cached binary distribution archives. command_line.append('--no-clean') # Use `--build-directory' to instruct pip to unpack the source # distribution archives to a temporary directory managed by pip-accel. # We will clean up the build directory when we're done using the # unpacked source distributions. command_line.append('--build-directory=%s' % self.build_directory) # Append the user's `pip install ...' arguments to the command line # that we just assembled. command_line.extend(arguments) logger.info("Executing command: pip install %s", ' '.join(command_line)) # Clear the build directory to prevent PreviousBuildDirError exceptions. self.clear_build_directory() # During the pip 6.x upgrade pip-accel switched to using `pip install # --download' which can produce an interactive prompt as described in # issue 51 [1]. The documented way [2] to get rid of this interactive # prompt is pip's --exists-action option, but due to what is most # likely a bug in pip this doesn't actually work. The environment # variable $PIP_EXISTS_ACTION does work however, so if the user didn't # set it we will set a reasonable default for them. # [1] https://github.com/paylogic/pip-accel/issues/51 # [2] https://pip.pypa.io/en/latest/reference/pip.html#exists-action-option os.environ.setdefault('PIP_EXISTS_ACTION', 'w') # Initialize and run the `pip install' command. command = InstallCommand() opts, args = command.parse_args(command_line) if not opts.ignore_installed: # If the user didn't supply the -I, --ignore-installed option we # will forcefully disable the option. Refer to the documentation of # the AttributeOverrides class for further details. opts = AttributeOverrides(opts, ignore_installed=False) requirement_set = command.run(opts, args) # Make sure the output of pip and pip-accel are not intermingled. sys.stdout.flush() if requirement_set is None: raise NothingToDoError(""" pip didn't generate a requirement set, most likely you specified an empty requirements file? """) else: return self.transform_pip_requirement_set(requirement_set)
def get_pip_requirement_set(self, arguments, use_remote_index, use_wheels=False): """ Get the unpacked requirement(s) specified by the caller by running pip. :param arguments: The command line arguments to ``pip install ..`` (a list of strings). :param use_remote_index: A boolean indicating whether pip is allowed to connect to the main package index (http://pypi.python.org by default). :param use_wheels: Whether pip and pip-accel are allowed to use wheels_ (``False`` by default for backwards compatibility with callers that use pip-accel as a Python API). :returns: A :py:class:`pip.req.RequirementSet` object created by pip. :raises: Any exceptions raised by pip. """ # Compose the pip command line arguments. This is where a lot of the # core logic of pip-accel is hidden and it uses some esoteric features # of pip so this method is heavily commented. command_line = [] # Use `--download' to instruct pip to download requirement(s) into # pip-accel's local source distribution index directory. This has the # following documented side effects (see `pip install --help'): # 1. It disables the installation of requirements (without using the # `--no-install' option which is deprecated and slated for removal # in pip 7.x). # 2. It ignores requirements that are already installed (because # pip-accel doesn't actually need to re-install requirements that # are already installed we will have work around this later, but # that seems fairly simple to do). command_line.append('--download=%s' % self.config.source_index) # Use `--find-links' to point pip at pip-accel's local source # distribution index directory. This ensures that source distribution # archives are never downloaded more than once (regardless of the HTTP # cache that was introduced in pip 6.x). command_line.append('--find-links=file://%s' % self.config.source_index) # Use `--no-use-wheel' to ignore wheel distributions by default in # order to preserve backwards compatibility with callers that expect a # requirement set consisting only of source distributions that can be # converted to `dumb binary distributions'. if not use_wheels and '--no-use-wheel' not in arguments: command_line.append('--no-use-wheel') # Use `--no-index' to force pip to only consider source distribution # archives contained in pip-accel's local source distribution index # directory. This enables pip-accel to ask pip "Can the local source # distribution index satisfy all requirements in the given requirement # set?" which enables pip-accel to keep pip off the internet unless # absolutely necessary :-). if not use_remote_index: command_line.append('--no-index') # Use `--no-clean' to instruct pip to unpack the source distribution # archives and *not* clean up the unpacked source distributions # afterwards. This enables pip-accel to replace pip's installation # logic with cached binary distribution archives. command_line.append('--no-clean') # Use `--build-directory' to instruct pip to unpack the source # distribution archives to a temporary directory managed by pip-accel. # We will clean up the build directory when we're done using the # unpacked source distributions. command_line.append('--build-directory=%s' % self.build_directory) # Append the user's `pip install ...' arguments to the command line # that we just assembled. command_line.extend(arguments) logger.info("Executing command: pip install %s", ' '.join(command_line)) # Clear the build directory to prevent PreviousBuildDirError exceptions. self.clear_build_directory() # During the pip 6.x upgrade pip-accel switched to using `pip install # --download' which can produce an interactive prompt as described in # issue 51 [1]. The documented way [2] to get rid of this interactive # prompt is pip's --exists-action option, but due to what is most # likely a bug in pip this doesn't actually work. The environment # variable $PIP_EXISTS_ACTION does work however, so if the user didn't # set it we will set a reasonable default for them. # [1] https://github.com/paylogic/pip-accel/issues/51 # [2] https://pip.pypa.io/en/latest/reference/pip.html#exists-action-option os.environ.setdefault('PIP_EXISTS_ACTION', 'w') # Initialize and run the `pip install' command. command = InstallCommand() opts, args = command.parse_args(command_line) if not opts.ignore_installed: # If the user didn't supply the -I, --ignore-installed option we # will forcefully disable the option. Refer to the documentation of # the AttributeOverrides class for further details. opts = AttributeOverrides(opts, ignore_installed=False) requirement_set = command.run(opts, args) # Make sure the output of pip and pip-accel are not intermingled. sys.stdout.flush() if requirement_set is None: raise NothingToDoError(""" pip didn't generate a requirement set, most likely you specified an empty requirements file? """) else: return self.transform_pip_requirement_set(requirement_set)
try: import spotipy.util except ImportError: to_install.append('spotipy') if sys.platform == 'win32': try: import psutil except ImportError: to_install.append('psutil') if to_install: from pip.commands.install import InstallCommand cmd = InstallCommand() opts, args = cmd.parse_args(to_install) cmd.run(opts, args) if sys.platform == 'win32': import psutil import aiohttp import spotipy class DiscordRPC: def __init__(self): if sys.platform == 'linux' or sys.platform == 'darwin': env_vars = ['XDG_RUNTIME_DIR', 'TMPDIR', 'TMP', 'TEMP'] path = next((os.environ.get(path, None) for path in env_vars if path in os.environ), '/tmp') self.ipc_path = f'{path}/discord-ipc-0'