Example #1
0
def get_installed_packages():
    """ get a list of all packages currently installed in the active environment """
    packages = []

    pool = Pool(4)

    # for dist in track(
    #     list(Distribution.discover()), description="[cyan]Grabbing dependency info"
    # ):
    #     packages.append(Package.from_dist(dist))

    dists = list(Distribution.discover())
    dists_num = len(dists)

    log.info("[bold]Found a total of {} distributions".format(dists_num),
             extra={"markup": True})

    for package_enum in enumerate(pool.imap(Package.from_dist, dists),
                                  start=1):
        package = package_enum[1]
        log.info("{0}/{1}: processed [bold cyan]{2} {3}[/bold cyan]".format(
            package_enum[0], dists_num, package.name, package.version),
                 extra={"markup": True})
        packages.append(package)

    return packages
Example #2
0
 def _find_entry_point(self, app: str) -> Optional[EntryPoint]:
     if not self.python_path.exists():
         return None
     dists = Distribution.discover(
         name=self.main_package_name,
         path=[str(get_site_packages(self.python_path))])
     for dist in dists:
         for ep in dist.entry_points:
             if ep.group == "pipx.run" and ep.name == app:
                 return ep
     return None
Example #3
0
    def create_production_scripts(self, tool, venv_session):
        """Create Rez production used binary scripts

        The binary script will be executed with Python interpreter flag -E,
        which will ignore all PYTHON* env vars, e.g. PYTHONPATH and PYTHONHOME.

        """
        _log.info("Generating production scripts..")

        site_packages = venv_session.creator.purelib
        bin_path = venv_session.creator.bin_dir

        if tool.edit:
            egg_link = site_packages / ("%s.egg-link" % tool.name)
            if not egg_link.is_file():
                _log.error("Tool %r installed in edit mode, but unable "
                           "to find egg-link for generating production "
                           "scripts from source. File not exists: %s" %
                           (tool.name, egg_link))
                return

            with open(str(egg_link), "r") as f:
                package_location = f.readline().strip()
            path = [str(package_location)]
        else:
            path = [str(site_packages)]

        dists = Distribution.discover(name=tool.name, path=path)
        specifications = {
            ep.name: "{ep.name} = {ep.value}".format(ep=ep)
            for dist in dists for ep in dist.entry_points
            if ep.group == "console_scripts"
        }

        # delete bin files written into virtualenv
        # this also avoided naming conflict between script 'rez' and dir 'rez'
        for script_name in specifications.keys():
            script_path = bin_path / script_name
            if script_path.is_file():
                os.remove(str(script_path))

        venv_name = tool.name if tool.isolation else "rez"
        prod_bin_path = self._revision.production_bin_dir(venv_name)
        makedirs(prod_bin_path)

        maker = ScriptMaker(source_dir=None, target_dir=str(prod_bin_path))
        maker.executable = str(venv_session.creator.exe)

        # Align with wheel
        #
        # Ensure we don't generate any variants for scripts because this is
        # almost never what somebody wants.
        # See https://bitbucket.org/pypa/distlib/issue/35/
        maker.variants = {""}
        # Ensure old scripts are overwritten.
        # See https://github.com/pypa/pip/issues/1800
        maker.clobber = True
        # This is required because otherwise distlib creates scripts that are
        # not executable.
        # See https://bitbucket.org/pypa/distlib/issue/32/
        maker.set_mode = True

        if self._rez_in_edit:
            # Allow pre-caching rez_bin_path on script entry if environ var
            # `REZUP_EDIT_IN_PRODUCTION` is set with non-empty value.
            # See https://github.com/davidlatwe/rezup/pull/56
            maker.script_template = r'''# -*- coding: utf-8 -*-
import re
import os
import sys
from %(module)s import %(import_name)s
if os.getenv("REZUP_EDIT_IN_PRODUCTION"):
    from rez.system import system
    setattr(system, 'rez_bin_path', r'{rez_bin_path}')
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(%(func)s())
'''.format(rez_bin_path=str(prod_bin_path))

        scripts = maker.make_multiple(
            specifications=specifications.values(),
            options=dict(interpreter_args=list(tool.flags)))

        return scripts
Example #4
0
 def test_find_distributions_specified_path(self):
     dists = Distribution.discover(path=[str(self.site_dir)])
     assert any(dist.metadata['Name'] == 'distinfo-pkg' for dist in dists)