Пример #1
0
def find_drivers():
    distros = pkg_resources.AvailableDistributions()

    drivers = []

    for key in distros:

        resources = distros[key]
        resource = resources[0]
        egg = resource.egg_name()
        folder = egg.split('-', 1)[0].lower()
        path = os.path.join(resource.location, folder)
        fname = os.path.join(path, '__init__.py')

        if os.path.exists(fname):

            with open(fname, 'r', encoding='utf-8') as f:

                for line in f:
                    if '__site_url__' in line:

                        site_url = line.split('=')[-1].strip()[1:-1]
                        package = '{}=={}'.format(key, resource.version)

                        drivers.append((site_url, package, path))
                        break

    return drivers
Пример #2
0
    def scan(self, groups=None, group_re=None):
        """
        walks through all distributions available and registers entry points or only those in `groups`
        """
        import pkg_resources as pr
        pr_env = pr.AvailableDistributions()
        pr_env.scan()
        if groups:
            for group in groups:
                self._backend.rm_group(group)
        else:
            self._backend.clear()

        for dists in pr_env._distmap.values():  # pylint: disable=protected-access
            dist = dists[0]
            emap = dist.get_entry_map()
            if groups:
                dmap = {k: v for k, v in six.iteritems(emap) if k in groups}
            elif group_re:
                dmap = {
                    k: v
                    for k, v in six.iteritems(emap) if group_re.match(k)
                }
            else:
                dmap = None
            dname = dist.project_name
            self._backend.write_dist(dname, entry_point_map=dmap)
Пример #3
0
 def scan_all_group_names(self):
     """Use `pkg_resources` to get a set of all available (not only cached) groups."""
     import pkg_resources as pr
     pr_env = pr.AvailableDistributions()
     pr_env.scan()
     all_groups = set()
     for dist_name in pr_env:
         _, dmap = self._backend.scan_dist(dist_name)
         all_groups.update(dmap.keys())
     return all_groups
Пример #4
0
def _get_package_version(package_name):
    import pkg_resources
    try:
        return pkg_resources.get_distribution(package_name).version
    except Exception:
        # Azure CLI exception loads azureml-* package in a special way which makes get_distribution not working
        try:
            all_packages = pkg_resources.AvailableDistributions()  # scan sys.path
            for name in all_packages:
                if name == package_name:
                    return all_packages[name][0].version
        except Exception:
            # In case this approach is not working neither
            return None
Пример #5
0
    def scan(self, groups=None, group_re=None, nocommit=False, nodelete=False):
        """Walk through all distributions available and registers entry points or only those in `groups`."""
        import pkg_resources as pr
        pr_env = pr.AvailableDistributions()
        pr_env.scan()
        if not groups and group_re:
            groups = []

        if group_re:
            if isinstance(group_re, six.string_types):
                group_re = re.compile(group_re)
            all_groups = self.scan_all_group_names()
            groups.extend({group for group in all_groups if group_re.match(group)})

        if not nocommit and not nodelete:
            if groups:
                for group in groups:
                    self._backend.rm_group(group)
            else:
                self._backend.clear()

        full_map = {}

        if nodelete:
            full_map = self._backend.epmap.copy()

        # ~ for dists in pr_env._distmap.values():  # pylint: disable=protected-access
        for dist in pr_env:
            dname, emap = self._backend.scan_dist(dist)
            dmap = full_map.get(dname, {})
            if groups:
                new_dmap = {k: v for k, v in six.iteritems(emap) if k in groups}
                dmap.update(new_dmap)
            else:
                dmap.update(emap)

            # extract entry points that are reserved for other purposes unless excepted
            dmap = clean_map(dmap, exceptions=groups)

            if not nocommit:
                self._backend.write_dist_map(dname, entry_point_map=dmap)
            full_map[dname] = [dmap]

        return full_map
Пример #6
0
    def __init__(self, *args, **kwargs):
        """Creates a new instance of the
        :class:`~rejected.consumer.SmartConsumer` class. To perform
        initialization tasks, extend
        :meth:`~rejected.consumer.SmartConsumer.initialize` or ensure you
        :meth:`super` this method first.

        """
        super(SmartConsumer, self).__init__(*args, **kwargs)
        installed = pkg_resources.AvailableDistributions()
        for key, pkg in {
                'application/msgpack': 'u-msgpack-python',
                'text/html': ('beautifulsoup4', 'lxml'),
                'text/xml': ('beautifulsoup4', 'lxml')
        }.items():
            if isinstance(pkg, tuple):
                self._SERIALIZATION_MAP[key]['enabled'] = \
                    all([p in installed for p in pkg])
            else:
                self._SERIALIZATION_MAP[key]['enabled'] = pkg in installed
            self.logger.debug(
                '%s is %s in serialization map', key, 'enabled'
                if self._SERIALIZATION_MAP[key]['enabled'] else 'disabled')
Пример #7
0
def load_components(env):
    loaded_components = []

    # Load components from the environment plugins directory
    plugins_dir = os.path.join(env.path, 'plugins')
    if pkg_resources is not None:  # But only if setuptools is installed!
        distributions = pkg_resources.AvailableDistributions()
        distributions.scan([plugins_dir])
        for name in distributions:
            egg = distributions[name][0]
            if egg.metadata.has_metadata(TRAC_META):
                env.log.debug('Loading component egg %s from %s', egg.name,
                              egg.path)
                egg.install_on()  # Put the egg on sys.path
                for module in egg.metadata.get_metadata_lines(TRAC_META):
                    if module not in loaded_components:
                        try:
                            __import__(module)
                            loaded_components.append(module)
                        except ImportError, e:
                            env.log.error('Component module %s not found',
                                          module,
                                          exc_info=True)
Пример #8
0
        for line in distribution.get_metadata_lines("RECORD"):
            path, hash, size = line.split(",")
            if path.endswith(".pth"):
                with open(path) as pth:
                    return pth.readline().strip()
        # fall back to the importable location (wheel install)
        return distribution.location
    else:
        raise TypeError(
            f"Unsupported distribution type {type(distribution)} {distribution}"
        )


ampel_folders = {
    _get_module_path(pkg_resources.get_distribution(dist_name))  # type: ignore
    for dist_name in pkg_resources.AvailableDistributions()  # type: ignore
    if dist_name.startswith("pyampel") or dist_name.startswith("ampel-")
}


def load_ipython_extension(ipython):
    pass


def clsimport(modname, local_ns=None):

    clsname = modname.split(".")[-1]

    if clsname in globals():
        return
Пример #9
0
def install(package, dst_dir, params, run=None, profile=None):
    """Installs the services.
    """
    _LOGGER.info('install: %s - %s, profile: %s', package, dst_dir, profile)

    packages = [package]

    module = plugin_manager.load('treadmill.bootstrap', package)
    extension_module = None
    if profile:
        _LOGGER.info('Installing profile: %s', profile)
        extension_name = '{}.{}'.format(package, profile)
        packages.append(extension_name)

        try:
            extension_module = plugin_manager.load('treadmill.bootstrap',
                                                   extension_name)
        except KeyError:
            _LOGGER.info('Extension not defined: %s, profile: %s',
                         package, profile)

    subproc.load_packages(packages, lazy=False)

    # Store resolved aliases
    aliases_path = os.path.join(dst_dir, '.aliases.json')
    aliases = subproc.get_aliases()
    with io.open(aliases_path, 'w') as f_aliases:
        f_aliases.write(json.dumps(aliases))

    defaults = {}
    defaults.update(getattr(module, 'DEFAULTS', {}))

    if extension_module:
        defaults.update(getattr(extension_module, 'DEFAULTS', {}))

    # TODO: this is ugly, error prone and should go away.
    #       aliases should be in default scope, everything else in _args.
    defaults['_alias'] = aliases
    defaults.update(aliases)
    defaults.update(params)

    defaults['aliases_path'] = aliases_path
    os.environ['TREADMILL_ALIASES_PATH'] = defaults['aliases_path']

    interpolated = bootstrap.interpolate(defaults, defaults)

    fs.mkdir_safe(dst_dir)
    with io.open(os.path.join(dst_dir, '.install'), 'w') as rec:

        _install(module, PLATFORM, dst_dir, interpolated, rec=rec)

        if extension_module:
            _install(
                extension_module,
                '.'.join([profile, PLATFORM]), dst_dir, interpolated,
                rec=rec
            )

    # Extract logging configuration.
    logconf_dir = os.path.join(dst_dir, 'logging')
    fs.mkdir_safe(logconf_dir)
    tm_logging.write_configs(logconf_dir)

    # Write entry-point cache
    distributions = pkg_resources.AvailableDistributions()

    plugin_manager.dump_cache(
        os.path.join(dst_dir, 'plugins.json'), distributions
    )

    if run:
        _run(run)
Пример #10
0
    def main(self):
        try:
            dist = pkg_resources.get_distribution('steelscript')
        except pkg_resources.DistributionNotFound:
            print "Package not found: 'steelscript'"
            print "Check the installation"
            sys.exit(1)

        e = pkg_resources.AvailableDistributions()

        steelscript_pkgs = [x for x in e if x.startswith('steel')]
        egg_info_pkgs = []
        egg_link_pkgs = []
        corrupted_pkgs = []

        for p in steelscript_pkgs:
            pkg = pkg_resources.get_distribution(p)
            if pkg.location.endswith('site-packages'):
                egg_info_pkgs.append(p)
            else:
                egg_link_pkgs.append(p)

        if egg_info_pkgs and egg_link_pkgs:
            corrupted_pkgs = egg_link_pkgs

        print ""
        print "Installed SteelScript Packages"
        print "Core packages:"
        core_pkgs = [
            x for x in e if x.startswith('steel') and 'appfwk' not in x
        ]
        core_pkgs.sort()
        for p in core_pkgs:
            pkg = pkg_resources.get_distribution(p)
            if p in corrupted_pkgs:
                print '  %-40s  corrupted' % (pkg.project_name)
                continue
            print '  %-40s  %s' % (pkg.project_name, pkg.version)

        print ""
        print "Application Framework packages:"

        appfwk_pkgs = [x for x in e if x.startswith('steel') and 'appfwk' in x]
        if appfwk_pkgs:
            appfwk_pkgs.sort()
            for p in appfwk_pkgs:
                pkg = pkg_resources.get_distribution(p)
                if p in corrupted_pkgs:
                    print '  %-40s  corrupted' % (pkg.project_name)
                    continue
                print '  %-40s  %s' % (pkg.project_name, pkg.version)
        else:
            print "  None"

        print ""
        print "REST tools and libraries:"

        installed_rest = set(['reschema', 'sleepwalker']).intersection(set(e))
        rest_pkgs = [pkg_resources.get_distribution(p) for p in installed_rest]

        if rest_pkgs:
            for pkg in rest_pkgs:
                print '  %-40s  %s' % (pkg.project_name, pkg.version)
        else:
            print "  None"

        print ""
        print "Paths to source:"
        paths = [os.path.dirname(p) for p in steelscript.__path__]

        for pkg in rest_pkgs:
            loc = pkg.location
            if loc not in paths:
                paths.append(loc)

        paths.sort()
        for p in paths:
            print "  %s" % p

        if corrupted_pkgs:
            print ""
            print "WARNING: Corrupted installation detected"
            print "Instructions to fix corrupted packages:"
            print "1. pip uninstall <corrupted_package>"
            print "2. pip install <corrupted_package>"
            print "   or do the following:"
            print "      cd <source_directory_of_corrupted_package>"
            print "      pip install ."

        if self.options.verbose:
            print ""
            print "Python information:"
            print '  Version      :', platform.python_version()
            print '  Version tuple:', platform.python_version_tuple()
            print '  Compiler     :', platform.python_compiler()
            print '  Build        :', platform.python_build()
            print '  Architecture :', platform.architecture()

            print ""
            print "Platform information:"
            print '  platform :', platform.platform()
            print '  system   :', platform.system()
            print '  node     :', platform.node()
            print '  release  :', platform.release()
            print '  version  :', platform.version()
            print '  machine  :', platform.machine()
            print '  processor:', platform.processor()

            print ""
            print "Python path:"
            print sys.path
        else:
            print ""
            print "(add -v or --verbose for further information)"
Пример #11
0
    def main(self):
        try:
            dist = pkg_resources.get_distribution('steelscript')
        except pkg_resources.DistributionNotFound:
            print "Package not found: 'steelscript'"
            print "Check the installation"
            sys.exit(1)

        e = pkg_resources.AvailableDistributions()

        print ""
        print "Installed SteelScript Packages"
        print "Core packages:"
        core_pkgs = [
            x for x in e if x.startswith('steel') and 'appfwk' not in x
        ]
        core_pkgs.sort()
        for p in core_pkgs:
            pkg = pkg_resources.get_distribution(p)
            print '  %-40s  %s' % (pkg.project_name, pkg.version)

        print ""
        print "Application Framework packages:"

        appfwk_pkgs = [x for x in e if x.startswith('steel') and 'appfwk' in x]
        if appfwk_pkgs:
            appfwk_pkgs.sort()
            for p in appfwk_pkgs:
                pkg = pkg_resources.get_distribution(p)
                print '  %-40s  %s' % (pkg.project_name, pkg.version)
        else:
            print "None."

        print ""
        print "Paths to source:"
        paths = [os.path.dirname(p) for p in steelscript.__path__]
        paths.sort()
        for p in paths:
            print "  %s" % p

        if self.options.verbose:
            print ""
            print "Python information:"
            print '  Version      :', platform.python_version()
            print '  Version tuple:', platform.python_version_tuple()
            print '  Compiler     :', platform.python_compiler()
            print '  Build        :', platform.python_build()
            print '  Architecture :', platform.architecture()

            print ""
            print "Platform information:"
            print '  platform :', platform.platform()
            print '  system   :', platform.system()
            print '  node     :', platform.node()
            print '  release  :', platform.release()
            print '  version  :', platform.version()
            print '  machine  :', platform.machine()
            print '  processor:', platform.processor()

            print ""
            print "Python path:"
            print sys.path
        else:
            print ""
            print "(add -v or --verbose for further information)"
Пример #12
0
    def scan(self, groups=None, group_re=None, commit=True, delete=True):  # pylint: disable=too-many-branches
        """Walk through all distributions available and register entry points.

        Note: This imports pkg_resources.

        :param groups: a list of group names to register entry points for.
            If None, registers all entry points found.
        :param group_re: a regular expression for group names.
            Groups matched by the regular expression are appended to `groups`
        :type group_re: str or a compiled expression from re.compile
        :param commit: If False, performs just a dry run
        :param delete: If False, append to existing entry point map
        """
        import pkg_resources as pr
        pr_env = pr.AvailableDistributions()
        pr_env.scan()

        groups = groups or []

        if group_re:
            all_groups = self.scan_all_group_names()
            if isinstance(group_re, six.string_types):
                group_re = re.compile(group_re)
            groups.extend(
                {group
                 for group in all_groups if group_re.match(group)})

        if delete:
            full_map = {}

            if commit:
                if groups:
                    for group in groups:
                        self._backend.rm_group(group)
                else:
                    self.clear()

        else:
            full_map = self._backend.epmap.copy()

        # ~ for dists in pr_env._distmap.values():  # pylint: disable=protected-access
        for dist in pr_env:
            # note: in pip 19, the *installing* distribution is part of pr_env but
            # pkg_resources.get_distribution() fails on it
            try:
                dname, emap = self._backend.scan_dist(dist)
            except pr.DistributionNotFound:
                continue

            dmap = full_map.get(dname, {})
            if groups:
                new_dmap = {
                    k: v
                    for k, v in six.iteritems(emap) if k in groups
                }
                dmap.update(new_dmap)
            else:
                dmap.update(emap)

            # extract entry points that are reserved for other purposes unless excepted
            dmap = clean_map(dmap, exceptions=groups)

            if commit:
                self._backend.write_dist_map(dname, entry_point_map=dmap)
            full_map[dname] = [dmap]

        return full_map
Пример #13
0
                    # Support for pre-entry-point plugins
                    if egg.has_metadata('trac_plugin.txt'):
                        env.log.debug('Loading plugin %s from %s', name,
                                      egg.location)
                        egg.activate()
                        for module in egg.get_metadata_lines(
                                'trac_plugin.txt'):
                            if load_module(module):
                                modules.append(module)

                if modules:
                    enable_modules(egg.location, modules)

        else:
            # setuptools < 0.6
            distributions = pkg_resources.AvailableDistributions([plugins_dir] \
                                                                 + sys.path)
            for name in distributions:
                egg = distributions[name][0]
                modules = []
                if egg.metadata.has_metadata(TRAC_META):
                    egg.install_on()
                    for module in egg.metadata.get_metadata_lines(TRAC_META):
                        if load_module(module):
                            modules.append(module)

                if modules:
                    enable_modules(egg.path, modules)

    elif os.path.exists(plugins_dir) and os.listdir(plugins_dir):
        env.log.warning('setuptools is required for plugin deployment')