Esempio n. 1
0
    def scan_for_plugins(self):
        """Scans for available plugins"""
        base_dir = deluge.common.resource_filename('deluge', 'plugins')
        user_dir = os.path.join(deluge.configmanager.get_config_dir(),
                                'plugins')
        base_subdir = [
            os.path.join(base_dir, f) for f in os.listdir(base_dir)
            if os.path.isdir(os.path.join(base_dir, f))
        ]
        plugin_dirs = [base_dir, user_dir] + base_subdir

        for dirname in plugin_dirs:
            pkg_resources.working_set.add_entry(dirname)
        self.pkg_env = pkg_resources.Environment(plugin_dirs, None)

        self.available_plugins = []
        for name in self.pkg_env:
            log.debug(
                'Found plugin: %s %s at %s',
                self.pkg_env[name][0].project_name,
                self.pkg_env[name][0].version,
                self.pkg_env[name][0].location,
            )
            self.available_plugins.append(self.pkg_env[name][0].project_name)
Esempio n. 2
0
    def _load_eggs(env, search_path, auto_enable=None):
        # Note that the following doesn't seem to support unicode search_path
        distributions, errors = working_set.find_plugins(
            pkg_resources.Environment(search_path))
        for dist in distributions:
            if dist not in working_set:
                env.log.debug('Adding plugin %s from %s', dist, dist.location)
                working_set.add(dist)

        def _log_error(item, e):
            ue = exception_to_unicode(e)
            if isinstance(e, DistributionNotFound):
                env.log.debug('Skipping "%s": ("%s" not found)', item, ue)
            elif isinstance(e, VersionConflict):
                env.log.error('Skipping "%s": (version conflict "%s")', item,
                              ue)
            elif isinstance(e, UnknownExtra):
                env.log.error('Skipping "%s": (unknown extra "%s")', item, ue)
            else:
                env.log.error('Skipping "%s": %s', item,
                              exception_to_unicode(e, traceback=True))

        for dist, e in errors.iteritems():
            _log_error(dist, e)

        for entry in sorted(working_set.iter_entry_points(entry_point_name),
                            key=lambda entry: entry.name):
            env.log.debug('Loading %s from %s', entry.name,
                          entry.dist.location)
            try:
                entry.load(require=True)
            except Exception, e:
                _log_error(entry, e)
            else:
                if os.path.dirname(entry.dist.location) == auto_enable:
                    _enable_plugin(env, entry.module_name)
Esempio n. 3
0
def scanPlugins():
    """Scan the system for installed plugins.

    This function scans installed packages for the current Python environment
    and looks for ones that specify PsychoPy entry points in their metadata.
    Afterwards, you can call :func:`listPlugins()` to list them and
    `loadPlugin()` to load them into the current session. This function is
    called automatically when PsychoPy starts, so you do not need to call this
    unless packages have been added since the session began.

    """
    global _installed_plugins_
    _installed_plugins_ = {}  # clear installed plugins

    # find all packages with entry points defined
    pluginEnv = pkg_resources.Environment()  # supported by the platform
    dists, _ = pkg_resources.working_set.find_plugins(pluginEnv)

    for dist in dists:
        entryMap = dist.get_entry_map()
        if any([i.startswith('psychopy') for i in entryMap.keys()]):
            logging.debug('Found plugin `{}` at location `{}`.'.format(
                dist.project_name, dist.location))
            _installed_plugins_[dist.project_name] = entryMap
Esempio n. 4
0
def install_plugin(plugin_name):
    print('installing requirement %s' % plugin_name)
    if isinstance(plugin_name, str):
        requirement = pkg_resources.Requirement(plugin_name)
    pargs = [sys.executable, '-m', 'pip', '--disable-pip-version-check', 
             'install', str(plugin_name), '--target', './plugins']
    env = os.environ.copy()
    pip_stdout = open('pip.log', 'wb')
    p = subprocess.Popen(pargs, stdout=pip_stdout, stderr=subprocess.PIPE,
                         env=env,
                         encoding='UTF8')
    try:
        ret = p.wait(timeout=60)
        if pip_stdout and pip_stdout.readable():
            output = pip_stdout.read()
        else:
            output = ''
        err_output = p.stderr.read()
        new_env = pkg_resources.Environment('plugins')
        new_env.scan(['plugins'])
        dists = new_env[str(plugin_name)]
        if ret != 0:
            sys.stderr.write('Pip install error\n')
            sys.stderr.write(err_output)
        if dists:
            print('%s installed' % dists)
            pkg_resources.working_set.add(dists[0])
            sys.stdout.flush()
            return dists[0]
        else:
            print('dist not found')
            return None

    except subprocess.TimeoutExpired:
        sys.stderr.write('pip install process timeout.')
        return None
Esempio n. 5
0
def main() -> None:

    # first, attempt to launch using nionswift-tool
    if pkg_resources.Environment()["nionswift-tool"]:
        from nion.nionswift_tool import command
        command.launch(sys.argv)
        return

    success = False

    # next attempt to launch using pyqt
    try:
        from PyQt5 import QtCore
        success = True
    except ImportError:
        pass

    # next attempt to launch using pyside2
    try:
        from PySide2 import QtCore
        success = True
    except ImportError:
        pass

    if not success:
        print(
            "Please install either pyqt or PySide2 using pip or conda or use nionswift-tool to launch."
        )

    if success:
        app, error = bootstrap_main(sys.argv)

        if app:
            app.run()
        else:
            print("Error: " + (error or "unknown"))
Esempio n. 6
0
    def scan_for_plugins(self):
        """Scans for available plugins"""
        base_plugin_dir = deluge.common.resource_filename("deluge", "plugins")
        pkg_resources.working_set.add_entry(base_plugin_dir)
        user_plugin_dir = os.path.join(deluge.configmanager.get_config_dir(),
                                       "plugins")

        plugins_dirs = [base_plugin_dir]
        for dirname in os.listdir(base_plugin_dir):
            plugin_dir = os.path.join(base_plugin_dir, dirname)
            pkg_resources.working_set.add_entry(plugin_dir)
            plugins_dirs.append(plugin_dir)
        pkg_resources.working_set.add_entry(user_plugin_dir)
        plugins_dirs.append(user_plugin_dir)

        self.pkg_env = pkg_resources.Environment(plugins_dirs)

        self.available_plugins = []
        for name in self.pkg_env:
            log.debug("Found plugin: %s %s at %s",
                      self.pkg_env[name][0].project_name,
                      self.pkg_env[name][0].version,
                      self.pkg_env[name][0].location)
            self.available_plugins.append(self.pkg_env[name][0].project_name)
Esempio n. 7
0
    def install(self, specs, working_set=None):

        logger.debug('Installing %s.', repr(specs)[1:-1])

        for_buildout_run = bool(working_set)
        path = self._path
        dest = self._dest
        if dest is not None and dest not in path:
            path.insert(0, dest)

        requirements = [
            self._constrain(pkg_resources.Requirement.parse(spec))
            for spec in specs
        ]

        if working_set is None:
            ws = pkg_resources.WorkingSet([])
        else:
            ws = working_set

        for requirement in requirements:
            for dist in self._get_dist(requirement,
                                       ws,
                                       for_buildout_run=for_buildout_run):
                ws.add(dist)
                self._maybe_add_setuptools(ws, dist)

        # OK, we have the requested distributions and they're in the working
        # set, but they may have unmet requirements.  We'll resolve these
        # requirements. This is code modified from
        # pkg_resources.WorkingSet.resolve.  We can't reuse that code directly
        # because we have to constrain our requirements (see
        # versions_section_ignored_for_dependency_in_favor_of_site_packages in
        # zc.buildout.tests).
        requirements.reverse()  # Set up the stack.
        processed = {}  # This is a set of processed requirements.
        best = {}  # This is a mapping of package name -> dist.
        # Note that we don't use the existing environment, because we want
        # to look for new eggs unless what we have is the best that
        # matches the requirement.
        env = pkg_resources.Environment(ws.entries)

        while requirements:
            # Process dependencies breadth-first.
            current_requirement = requirements.pop(0)
            req = self._constrain(current_requirement)
            if req in processed:
                # Ignore cyclic or redundant dependencies.
                continue
            dist = best.get(req.key)
            if dist is None:
                try:
                    dist = env.best_match(req, ws)
                except pkg_resources.VersionConflict as err:
                    logger.debug(
                        "Version conflict while processing requirement %s "
                        "(constrained to %s)", current_requirement, req)
                    # Installing buildout itself and its extensions and
                    # recipes requires the global
                    # ``pkg_resources.working_set`` to be active, which also
                    # includes all system packages. So there might be
                    # conflicts, which are fine to ignore. We'll grab the
                    # correct version a few lines down.
                    if not for_buildout_run:
                        raise VersionConflict(err, ws)
            if dist is None:
                if dest:
                    logger.debug('Getting required %r', str(req))
                else:
                    logger.debug('Adding required %r', str(req))
                _log_requirement(ws, req)
                for dist in self._get_dist(req,
                                           ws,
                                           for_buildout_run=for_buildout_run):
                    ws.add(dist)
                    self._maybe_add_setuptools(ws, dist)
            if dist not in req:
                # Oops, the "best" so far conflicts with a dependency.
                raise VersionConflict(pkg_resources.VersionConflict(dist, req),
                                      ws)

            best[req.key] = dist
            requirements.extend(dist.requires(req.extras)[::-1])
            processed[req] = True
        return ws
Esempio n. 8
0
    def load(self, env, search_path, disable_re, name_re):
        generate_debug_messages = __debug__ and env.log.isEnabledFor(
            logging.DEBUG)
        if not pkg_resources_avail:
            if generate_debug_messages:
                env.log.debug(
                    'The EggLoader service is terminating early because the pkg_resources package is not available on this machine.'
                )
            return

        working_set = pkg_resources.working_set

        env.log.info('BEGIN -  Loading plugins with an EggLoader service')
        distributions, errors = working_set.find_plugins(
            pkg_resources.Environment(search_path))
        for dist in distributions:
            if name_re.match(str(dist)):
                if generate_debug_messages:
                    env.log.debug('Adding plugin %r from %r', dist,
                                  dist.location)
                working_set.add(dist)
            else:
                if generate_debug_messages:
                    env.log.debug('Ignoring plugin %r from %r', dist,
                                  dist.location)

        def _log_error(item, e):
            gen_debug = __debug__ and env.log.isEnabledFor(logging.DEBUG)
            if isinstance(e, pkg_resources.DistributionNotFound):
                if gen_debug:
                    env.log.debug('Skipping "%s": ("%s" not found)', item, e)
            elif isinstance(e, pkg_resources.VersionConflict):
                if gen_debug:
                    env.log.debug('Skipping "%s": (version conflict "%s")',
                                  item, e)
            elif isinstance(e, pkg_resources.UnknownExtra):
                env.log.error('Skipping "%s": (unknown extra "%s")', item, e)
            elif isinstance(e, ImportError):
                env.log.error('Skipping "%s": (can\'t import "%s")', item, e)
            else:
                env.log.error('Skipping "%s": (error "%s")', item, e)

        for dist, e in errors.items():
            _log_error(dist, e)

        for entry in working_set.iter_entry_points(self.entry_point_name):
            if generate_debug_messages:
                env.log.debug('Loading %r from %r', entry.name,
                              entry.dist.location)
            try:
                entry.load(require=True)
            except (ImportError, pkg_resources.DistributionNotFound,
                    pkg_resources.VersionConflict, pkg_resources.UnknownExtra):
                e = sys.exc_info()[1]
                _log_error(entry, e)
            else:
                if not disable_re.match(os.path.dirname(
                        entry.module_name)) is None:
                    #_enable_plugin(env, entry.module_name)
                    pass

        env.log.info('END -    Loading plugins with an EggLoader service')
Esempio n. 9
0
def pkg_environment(path):
    if pkg_resources_avail is None:
        _check_pkg_resources()
    return pkg_resources.Environment(path) if pkg_resources_avail else None
Esempio n. 10
0
    def run(self):
        # dependencies, links, etc...
        develop_indico.run(self)

        env = pkg_resources.Environment()
        easy_install.main(DEVELOP_REQUIRES)
        env.scan()

        local = 'etc/indico.conf'
        if os.path.exists(local):
            print 'Upgrading existing etc/indico.conf..'
            upgrade_indico_conf(local, 'etc/indico.conf.sample')
        else:
            print 'Creating new etc/indico.conf..'
            shutil.copy('etc/indico.conf.sample', local)

        for f in [
                x for x in ('etc/zdctl.conf', 'etc/zodb.conf',
                            'etc/logging.conf') if not os.path.exists(x)
        ]:
            shutil.copy('%s.sample' % f, f)

        print """\nIndico needs to store some information in the filesystem (database, cache, temporary files, logs...)
Please specify the directory where you'd like it to be placed.
(Note that putting it outside of your sourcecode tree is recommended)"""
        prefixDirDefault = os.path.dirname(os.getcwd())
        prefixDir = raw_input('[%s]: ' % prefixDirDefault).strip()

        if prefixDir == '':
            prefixDir = prefixDirDefault

        directories = dict((d, os.path.join(prefixDir, d))
                           for d in ['db', 'log', 'tmp', 'cache', 'archive'])

        print 'Creating directories...',
        for d in directories.values():
            if not os.path.exists(d):
                os.makedirs(d)
        print 'Done!'

        directories['htdocs'] = os.path.join(os.getcwd(), 'indico', 'htdocs')
        directories['bin'] = os.path.join(os.getcwd(), 'bin')
        directories['etc'] = os.path.join(os.getcwd(), 'etc')
        directories['doc'] = os.path.join(os.getcwd(), 'doc')

        self._update_conf_dir_paths(local, directories)

        directories.pop(
            'htdocs'
        )  #avoid modifying the htdocs folder permissions (it brings problems with git)

        from MaKaC.consoleScripts.installBase import _databaseText, _findApacheUserGroup, _checkDirPermissions, _updateDbConfigFiles, _updateMaKaCEggCache

        user = ''

        sourcePath = os.getcwd()

        # find the apache user/group
        user, group = _findApacheUserGroup(self.www_uid, self.www_gid)
        _checkDirPermissions(directories,
                             dbInstalledBySetupPy=directories['db'],
                             accessuser=user,
                             accessgroup=group)

        _updateDbConfigFiles(directories['db'], directories['log'],
                             os.path.join(sourcePath, 'etc'),
                             directories['tmp'], user)

        _updateMaKaCEggCache(
            os.path.join(os.path.dirname(__file__), 'indico', 'MaKaC',
                         '__init__.py'), directories['tmp'])

        updateIndicoConfPathInsideMaKaCConfig(
            os.path.join(os.path.dirname(__file__), ''),
            'indico/MaKaC/common/MaKaCConfig.py')
        compileAllLanguages(self)
        print '''
%s
        ''' % _databaseText('etc')
Esempio n. 11
0
 def _load_dist(self, dist):
     dists = pkg_resources.Environment(dist.location)[dist.project_name]
     assert len(dists) == 1
     return dists[0]
Esempio n. 12
0
def fetch_build_egg(dist, req):
    """Fetch an egg needed for building.

    Use pip/wheel to fetch/build a wheel."""
    # Check pip is available.
    try:
        pkg_resources.get_distribution('pip')
    except pkg_resources.DistributionNotFound:
        dist.announce(
            'WARNING: The pip package is not available, falling back '
            'to EasyInstall for handling setup_requires/test_requires; '
            'this is deprecated and will be removed in a future version.',
            log.WARN
        )
        return _legacy_fetch_build_egg(dist, req)
    # Warn if wheel is not.
    try:
        pkg_resources.get_distribution('wheel')
    except pkg_resources.DistributionNotFound:
        dist.announce('WARNING: The wheel package is not available.', log.WARN)
    # Ignore environment markers; if supplied, it is required.
    req = strip_marker(req)
    # Take easy_install options into account, but do not override relevant
    # pip environment variables (like PIP_INDEX_URL or PIP_QUIET); they'll
    # take precedence.
    opts = dist.get_option_dict('easy_install')
    if 'allow_hosts' in opts:
        raise DistutilsError('the `allow-hosts` option is not supported '
                             'when using pip to install requirements.')
    if 'PIP_QUIET' in os.environ or 'PIP_VERBOSE' in os.environ:
        quiet = False
    else:
        quiet = True
    if 'PIP_INDEX_URL' in os.environ:
        index_url = None
    elif 'index_url' in opts:
        index_url = opts['index_url'][1]
    else:
        index_url = None
    if 'find_links' in opts:
        find_links = _fixup_find_links(opts['find_links'][1])[:]
    else:
        find_links = []
    if dist.dependency_links:
        find_links.extend(dist.dependency_links)
    eggs_dir = os.path.realpath(dist.get_egg_cache_dir())
    environment = pkg_resources.Environment()
    for egg_dist in pkg_resources.find_distributions(eggs_dir):
        if egg_dist in req and environment.can_add(egg_dist):
            return egg_dist
    with TemporaryDirectory() as tmpdir:
        cmd = [
            sys.executable, '-m', 'pip',
            '--disable-pip-version-check',
            'wheel', '--no-deps',
            '-w', tmpdir,
        ]
        if quiet:
            cmd.append('--quiet')
        if index_url is not None:
            cmd.extend(('--index-url', index_url))
        if find_links is not None:
            for link in find_links:
                cmd.extend(('--find-links', link))
        # If requirement is a PEP 508 direct URL, directly pass
        # the URL to pip, as `req @ url` does not work on the
        # command line.
        if req.url:
            cmd.append(req.url)
        else:
            cmd.append(str(req))
        try:
            subprocess.check_call(cmd)
        except subprocess.CalledProcessError as e:
            raise DistutilsError(str(e))
        wheel = Wheel(glob.glob(os.path.join(tmpdir, '*.whl'))[0])
        dist_location = os.path.join(eggs_dir, wheel.egg_name())
        wheel.install_as_egg(dist_location)
        dist_metadata = pkg_resources.PathMetadata(
            dist_location, os.path.join(dist_location, 'EGG-INFO'))
        dist = pkg_resources.Distribution.from_filename(
            dist_location, metadata=dist_metadata)
        return dist
Esempio n. 13
0
def list_distributions():
    import pkg_resources
    env = pkg_resources.Environment()
    for dist_name in env:
        yield dist_name
Esempio n. 14
0
    def __init__(self):
        """init"""

        self.environment = pkg_resources.Environment()
        self.working_set = pkg_resources.WorkingSet()
Esempio n. 15
0
    def _get_dist(self, requirement, ws, always_unzip):
        """The only difference between this and the standard implementation is that it doesn't copy
        eggs from the egg cache but links to them in place."""

        __doing__ = 'Getting distribution for %r.', str(requirement)

        # Maybe an existing dist is already the best dist that satisfies the
        # requirement
        dist, avail = self._satisfied(requirement)

        if dist is None:
            if self._dest is not None:
                easy_install.logger.info(*__doing__)

            # Retrieve the dist:
            if avail is None:
                raise easy_install.MissingDistribution(requirement, ws)

            # We may overwrite distributions, so clear importer
            # cache.
            sys.path_importer_cache.clear()

            tmp = self._download_cache
            if tmp is None:
                tmp = easy_install.tempfile.mkdtemp('get_dist')

            try:
                dist = self._fetch(avail, tmp, self._download_cache)

                if dist is None:
                    raise easy_install.zc.buildout.UserError(
                        "Couldn't download distribution %s." % avail)

                if dist.precedence == pkg_resources.EGG_DIST:
                    # It's already an egg, just fetch it into the dest

                    newloc = os.path.join(self._dest,
                                          os.path.basename(dist.location))

                    # The next 2 lines are new, this is the only bit that is different from the standard
                    if egg_cache.is_from_egg_cache(dist.location):
                        newloc = dist.location
                    elif os.path.isdir(dist.location):
                        # we got a directory. It must have been
                        # obtained locally.  Just copy it.
                        shutil.copytree(dist.location, newloc)
                    else:

                        if self._always_unzip:
                            should_unzip = True
                        else:
                            metadata = pkg_resources.EggMetadata(
                                zipimport.zipimporter(dist.location))
                            should_unzip = (
                                metadata.has_metadata('not-zip-safe')
                                or not metadata.has_metadata('zip-safe'))

                        if should_unzip:
                            easy_install.setuptools.archive_util.unpack_archive(
                                dist.location, newloc)
                        else:
                            shutil.copyfile(dist.location, newloc)

                    easy_install.redo_pyc(newloc)

                    # Getting the dist from the environment causes the
                    # distribution meta data to be read.  Cloning isn't
                    # good enough.
                    dists = pkg_resources.Environment(
                        [newloc],
                        python=easy_install._get_version(self._executable),
                    )[dist.project_name]
                else:
                    # It's some other kind of dist.  We'll let easy_install
                    # deal with it:
                    dists = self._call_easy_install(dist.location, ws,
                                                    self._dest, dist)
                    for dist in dists:
                        easy_install.redo_pyc(dist.location)

            finally:
                if tmp != self._download_cache:
                    shutil.rmtree(tmp)

            self._env.scan([self._dest])
            dist = self._env.best_match(requirement, ws)
            easy_install.logger.info("Got %s.", dist)

        else:
            dists = [dist]

        for dist in dists:
            if (dist.has_metadata('dependency_links.txt')
                    and not self._install_from_cache
                    and self._use_dependency_links):
                for link in dist.get_metadata_lines('dependency_links.txt'):
                    link = link.strip()
                    if link not in self._links:
                        easy_install.logger.debug(
                            'Adding find link %r from %s', link, dist)
                        self._links.append(link)
                        self._index = easy_install._get_index(
                            self._executable, self._index_url, self._links,
                            self._allow_hosts, self._path)

        for dist in dists:
            # Check whether we picked a version and, if we did, report it:
            if not (dist.precedence == pkg_resources.DEVELOP_DIST or
                    (len(requirement.specs) == 1
                     and requirement.specs[0][0] == '==')):
                easy_install.logger.debug('Picked: %s = %s', dist.project_name,
                                          dist.version)
                if not self._allow_picked_versions:
                    raise easy_install.zc.buildout.UserError(
                        'Picked: %s = %s' % (dist.project_name, dist.version))

        return dists
Esempio n. 16
0
def getobedients():
    return [
        pkgname for pkgname in pkg_resources.Environment()
        if pkgname.startswith('obedient.')
    ]
Esempio n. 17
0
    def install(self, working_set=None):
        """installs an egg
        """
        self.logger.info('Installing console scripts.')
        arguments = self.arguments
        # install console scripts
        installed_scripts, install_paths = {}, []
        bin = self.bin
        sitepackages = re.sub(
            'bin.*', 'lib/python%s/site-packages' % self.executable_version,
            self.executable)
        scan_paths = [
            self.buildout['buildout']['develop-eggs-directory'],
            self.buildout['buildout']['eggs-directory'], sitepackages
        ] + self.extra_paths
        entry_points = []
        # parse script key
        scripts = self.options_scripts
        entry_points_options = self.entry_points_options
        if isinstance(scripts, str):
            scripts = scripts.split('\n')
            scripts = dict([('=' in s) and s.split('=', 1) or (s, s)
                            for s in scripts if s])
        console_scripts = scripts.keys()

        # install needed stuff and get according working set
        sreqs, ws = self.working_set(working_set=working_set)
        reqs = [pkg_resources.Requirement.parse(r) for r in sreqs]
        env = pkg_resources.Environment(scan_paths,
                                        python=self.executable_version)
        required_dists = []
        try:
            required_dists = ws.resolve(reqs, env)
        except:
            adists = []
            # try to load from paths
            # when they are versions conflicts, because the main source of them
            # at this stage (and not before) are versions pinned in eggs
            # directlry which we had overidden in buildout.cfg
            for distpath in ws.entries:
                dists = [a for a in pkg_resources.find_distributions(distpath)]
                for dist in dists:
                    if not dist in adists:
                        adists.append(dist)
                        required_dists.append(dist)

        for dist in required_dists:
            if not dist in ws:
                ws.add(dist)
        pypath = [
            os.path.abspath(p) for p in ws.entries + self.extra_paths
            if os.path.exists(p)
        ]
        abs_pypath = pypath[:]

        rpypath, rsetup = pypath, ''
        if self._relative_paths:
            rpypath, rsetup = zc.buildout.easy_install._relative_path_and_setup(
                os.path.join(self.bin, 'i_will_be_a_script'), pypath,
                self._relative_paths)
        else:
            rpypath = "'%s'" % "',\n'".join(rpypath)

        template_vars = {
            'python': self.executable,
            'path': rpypath,
            'rsetup': rsetup,
            'arguments': arguments,
            'initialization': self.initialization,
            'zopepy_initialization': self.initialization,
            'env_initialization': self.env_initialization,
        }

        # parse entry points key
        for s in [item for item in entry_points_options.split() if item]:
            if s.strip():
                parsed = self.parse_entry_point(s)
                if not parsed:
                    logging.getLogger(self.name).error(
                        "Cannot parse the entry point %s.", s)
                    raise zc.buildout.UserError("Invalid entry point")
                entry = parsed.groups()
                entry_points.append(entry)
                scripts[entry[0]] = entry[0]

        # scan eggs for entry point keys
        consumed_ep = []
        for dist in ws:
            for name in pkg_resources.get_entry_map(dist, 'console_scripts'):
                if self.filter(dist, name, entry_points_options, arguments,
                               console_scripts):
                    scripts.setdefault(name, name)
                    entry_point = dist.get_entry_info('console_scripts', name)
                    #consumed_ep.append(name)
                    entry_points.append((name, entry_point.module_name,
                                         '.'.join(entry_point.attrs)))

        # generate interpreter
        interpreter = self.interpreter
        if interpreter:
            interpreter_vars = self.get_script_vars(template_vars, interpreter)
            inst_script = os.path.join(bin, interpreter)
            init_key = '%s-initialization' % self.interpreter
            template_vars['zopepy_initialization'] = self.options.get(
                init_key, template_vars['zopepy_initialization'])
            installed_scripts[
                interpreter] = inst_script, py_script_template % template_vars

        if self.env_file:
            env_vars = self.get_script_vars(template_vars,
                                            os.path.basename(self.env_file))
            env_vars['path'] = ':'.join(abs_pypath)
            env_script = self.env_file
            if not '/' in self.env_file:
                env_script = os.path.join(bin, self.env_file)
            installed_scripts[env_script] = env_script, env_template % env_vars

        # generate console entry pointts
        for name, module_name, attrs in entry_points:
            sname = name
            if scripts:
                sname = scripts.get(name)
                if sname is None:
                    continue
            entry_point_vars = self.get_script_vars(template_vars, sname)
            entry_point_vars.update({
                'module_name': module_name,
                'attrs': attrs,
            })
            installed_scripts[sname] = (os.path.join(
                bin, sname), entry_point_template % entry_point_vars)

        # generate scripts
        option_scripts = self.options_scripts
        # now install classical scripts from the entry script.
        already_installed = [os.path.basename(s) for s in installed_scripts]
        for dist in ws:
            provider = dist._provider
            items = {}
            if dist.has_metadata('scripts'):
                for s in provider.metadata_listdir('scripts'):
                    if not(s in already_installed)\
                       and not(s.endswith('.pyc') or s.endswith('.pyo')):
                        items[s] = provider._fn(provider.egg_info,
                                                'scripts/%s' % s)
            for script in items:
                if self.filter(dist, script, entry_points_options, arguments,
                               console_scripts):
                    # mean to filter by dist, even if the distudoesnt provide
                    # console scripts ;), just add dist.project_name in the
                    # scripts section
                    if dist.project_name in console_scripts:
                        scripts.setdefault(script, script)
                    sname = scripts.get(script, script)
                    # install the script if it exists an entry point which
                    # have generated the same script name with a prefixed cs.
                    destName = sname
                    if sname in installed_scripts:
                        destName = 'cs.%s' % sname
                    script_filename = items[script]
                    inst_script = os.path.join(bin, destName)
                    script_vars = self.get_script_vars(template_vars, destName)
                    script_vars.update({'code': script_filename})
                    code = script_template % script_vars
                    installed_scripts[destName] = inst_script, code

        ls = []
        for script in installed_scripts:
            path, content = installed_scripts[script]
            ls.append(path)
            install_paths.append(path)
            open(path, 'w').writelines(content)
            os.chmod(
                path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
                | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
                | stat.S_IROTH | stat.S_IXOTH)
        l = installed_scripts.keys()
        l.sort()
        msg = 'There were no scripts found to generate or the recipe did not select any one.'
        if (l):
            msg = 'Generated scripts: \'%s\'.' % ("', '".join(l))
        self.logger.info(msg)
        return installed_scripts.keys()
Esempio n. 18
0
    def __init__(self, mpstate):
        super(HelpModule, self).__init__(mpstate,
                                         "mavhelp",
                                         "Help and version information",
                                         public=True)
        self.enabled = False
        self.add_command('mavhelp', self.cmd_help,
                         "help and version information", "<about|site>")
        self.have_list = False

        #versioning info
        #pkg_resources doesn't work in the windows exe build, so read the version file
        try:
            import pkg_resources
            self.version = pkg_resources.Environment()["mavproxy"][0].version
        except:
            start_script = os.path.join(os.environ['LOCALAPPDATA'], "MAVProxy",
                                        "version.txt")
            f = open(start_script, 'r')
            self.version = f.readline()
        self.host = platform.system() + platform.release()
        self.pythonversion = str(platform.python_version())
        if mp_util.has_wxpython:
            self.wxVersion = str(wx.__version__)
        else:
            self.wxVersion = ''

        #check for updates, if able
        import pip
        pypi = xmlrpclib.ServerProxy('https://pypi.python.org/pypi')
        available = pypi.package_releases('MAVProxy')
        if not available:
            self.newversion = 'Error finding update'
        else:
            self.newversion = available[0]

        #and format the update string
        if not isinstance(self.newversion, basestring):
            self.newversion = "Error finding update"
        elif re.search('[a-zA-Z]', self.newversion):
            self.newversion = "Error finding update: " + self.newversion
        elif self.newversion.strip() == self.version.strip():
            self.newversion = "Running latest version"
        else:
            self.newversion = "New version " + self.newversion + " available (currently running " + self.version + ")"

        if mp_util.has_wxpython:
            self.menu_added_console = False
            self.menu = MPMenuSubMenu(
                'Help',
                items=[
                    MPMenuItem('MAVProxy website',
                               'MAVProxy website',
                               '',
                               handler=MPMenuOpenWeblink(
                                   'http://ardupilot.github.io/MAVProxy/')),
                    MPMenuItem('Check for Updates',
                               'Check for Updates',
                               '',
                               handler=MPMenuChildMessageDialog(
                                   title="Updates", message=self.newversion)),
                    MPMenuItem('About',
                               'About',
                               '',
                               handler=MPMenuChildMessageDialog(
                                   title="About MAVProxy",
                                   message=self.about_string()))
                ])
Esempio n. 19
0
def compileKepcart():
    loc = pkg_resources.Environment()['mpcutilities'][0].location
    subprocess.check_call('bash README_COMPILE.txt',
                          cwd=os.path.join(loc, 'mpcutilities'),
                          shell=True)
Esempio n. 20
0
def source_release(args=None):
    if args is None:
        args = sys.argv[1:]

    # set up command line options
    parser = optparse.OptionParser()
    parser.add_option("-n", "--name", dest="filename",
        help="create custom named files", default="None")
    parser.add_option("-s", "--symlinks", dest="symlinks",
        help="if true, symbolic links result in symbolic links in the "
             "destination; if false, the contents of the files pointed "
             "to by symbolic links are copied.",
        action="store_true", default=False)

    # retrieve options
    (options, args) = parser.parse_args(args)

    url = args.pop(0)
    config = args.pop(0)

    clopts = []
    for arg in args:
        name, value = arg.split('=', 1)
        section, option = name.split(':')
        clopts.append((section, option, value))

    name = url.split('/')[-1]

    # use optparse to find custom filename
    if options.filename != 'None':
        name = options.filename

    t1 = tempfile.mkdtemp('source-release1')
    t2 = tempfile.mkdtemp('source-release2')
    co1 = os.path.join(t1, name)
    co2 = os.path.join(t2, name)
    here = os.getcwd()
    print 'Creating source release in %s.tgz' % name
    sys.stdout.flush()
    try:

        if url.startswith('file://'):
            shutil.copytree(
                urlparse.urlparse(url)[2], co1,
                symlinks=options.symlinks
                )
        else:
            _system('svn', 'export', url, co1)
        shutil.copytree(co1, co2, symlinks=options.symlinks)
        cache = os.path.join(co2, 'release-distributions')
        os.mkdir(cache)
        buildout = zc.buildout.buildout.Buildout(
            os.path.join(co1, config), clopts,
            False, False, 'bootstrap',
            )
        eggs_directory = buildout['buildout']['eggs-directory']
        reggs = _relative(eggs_directory, co1)
        if reggs is None:
            print 'Invalid eggs directory (perhaps not a relative path)', \
                eggs_directory
            sys.exit(0)

        buildout.bootstrap([])

        buildargs = args[:]+[
            '-Uvc', os.path.join(co1, config),
            'buildout:download-cache='+cache
            ]

        _system(os.path.join(co1, 'bin', 'buildout'), *buildargs)

        os.chdir(here)

        env = pkg_resources.Environment([eggs_directory])
        dists = [env[project][0].location
                 for project in ('zc.buildout', 'setuptools')]

        eggs = os.path.join(co2, reggs)
        os.mkdir(eggs)
        for dist in dists:
            if os.path.isdir(dist):
                shutil.copytree(dist,
                                os.path.join(eggs, os.path.basename(dist)),
                                symlinks=options.symlinks
                                )
            else:
                shutil.copy(dist, eggs)


        open(os.path.join(co2, 'install.py'), 'w').write(
            install_template % dict(
                path = [os.path.basename(dist) for dist in dists],
                config = config,
                version = sys.version_info[:2],
                eggs_directory = reggs,
                args = args and repr(args)[1:-1]+',' or '',
            ))


        tar = tarfile.open(name+'.tgz', 'w:gz')
        tar.add(co2, name)
        tar.close()


    finally:
        shutil.rmtree(t1)
        shutil.rmtree(t2)
Esempio n. 21
0
#!/usr/bin/env python3

import wpilib
import wpilib.drive

import pkg_resources

for dist in pkg_resources.Environment():
    print(dist)

exit(1)


class MyRobot(wpilib.TimedRobot):
    """Main robot class"""

    def robotInit(self):
        """Robot-wide initialization code should go here"""

        self.lstick = wpilib.Joystick(0)
        self.rstick = wpilib.Joystick(1)

        self.l_motor = wpilib.Jaguar(1)
        self.r_motor = wpilib.Jaguar(2)

        # Position gets automatically updated as robot moves
        self.gyro = wpilib.AnalogGyro(1)

        self.drive = wpilib.drive.DifferentialDrive(self.l_motor, self.r_motor)

        self.motor = wpilib.Jaguar(4)
Esempio n. 22
0
 def test_environment_marker_evaluation_negative(self):
     """Environment markers are evaluated at resolution time."""
     ad = pkg_resources.Environment([])
     ws = WorkingSet([])
     res = ws.resolve(parse_requirements("Foo;python_version<'2'"), ad)
     assert list(res) == []
Esempio n. 23
0
def main():
    server = config.Config().get_musicbrainz_server()
    https_enabled = server['scheme'] == 'https'
    try:
        musicbrainzngs.set_hostname(server['netloc'], https_enabled)
    # Parameter 'use_https' is missing in versions of musicbrainzngs < 0.7
    except TypeError:
        logger.warning("Parameter 'use_https' is missing in versions of "
                       "musicbrainzngs < 0.7. This means whipper will only "
                       "be able to communicate with the configured "
                       "MusicBrainz server ('%s') over plain HTTP. If a "
                       "custom server which speaks HTTPS only has been "
                       "declared, a suitable version of the "
                       "musicbrainzngs module will be needed "
                       "to make it work in whipper.", server['netloc'])
        musicbrainzngs.set_hostname(server['netloc'])

    # Find whipper's plugins paths (local paths have higher priority)
    plugins_p = [directory.data_path('plugins')]  # local path (in $HOME)
    if hasattr(sys, 'real_prefix'):  # no getsitepackages() in virtualenv
        plugins_p.append(
            get_python_lib(plat_specific=False, standard_lib=False,
                           prefix='/usr/local') + '/whipper/plugins')
        plugins_p.append(get_python_lib(plat_specific=False,
                         standard_lib=False) + '/whipper/plugins')
    else:
        plugins_p += [x + '/whipper/plugins' for x in site.getsitepackages()]

    # register plugins with pkg_resources
    distributions, _ = pkg_resources.working_set.find_plugins(
        pkg_resources.Environment(plugins_p)
    )
    list(map(pkg_resources.working_set.add, distributions))
    try:
        cmd = Whipper(sys.argv[1:], os.path.basename(sys.argv[0]), None)
        ret = cmd.do()
    except SystemError as e:
        logger.critical("SystemError: %s", e)
        if (isinstance(e, common.EjectError) and
                cmd.options.eject in ('failure', 'always')):
            # XXX: Pylint, instance of 'SystemError' has no 'device' member
            eject_device(e.device)
        return 255
    except RuntimeError as e:
        print(e)
        return 1
    except KeyboardInterrupt:
        return 2
    except ImportError:
        raise
    except task.TaskException as e:
        if isinstance(e.exception, ImportError):
            raise ImportError(e.exception)
        elif isinstance(e.exception, common.MissingDependencyException):
            logger.critical('missing dependency "%s"', e.exception.dependency)
            return 255

        if isinstance(e.exception, common.EmptyError):
            logger.debug("EmptyError: %s", e.exception)
            logger.critical('could not create encoded file')
            return 255

        # in python3 we can instead do `raise e.exception` as that would show
        # the exception's original context
        logger.critical(e.exceptionMessage)
        return 255
    return ret if ret else 0
Esempio n. 24
0
    def _get_dist(self, requirement, ws):

        __doing__ = 'Getting distribution for %r.', str(requirement)

        # Maybe an existing dist is already the best dist that satisfies the
        # requirement
        dist, avail = self._satisfied(requirement)

        if dist is None:
            if self._dest is None:
                raise zc.buildout.UserError(
                    "We don't have a distribution for %s\n"
                    "and can't install one in offline (no-install) mode.\n" %
                    requirement)

            logger.info(*__doing__)

            # Retrieve the dist:
            if avail is None:
                self._index.obtain(requirement)
                raise MissingDistribution(requirement, ws)

            # We may overwrite distributions, so clear importer
            # cache.
            sys.path_importer_cache.clear()

            tmp = self._download_cache
            if tmp is None:
                tmp = tempfile.mkdtemp('get_dist')

            try:
                dist = self._fetch(avail, tmp, self._download_cache)

                if dist is None:
                    raise zc.buildout.UserError(
                        "Couln't download distribution %s." % avail)

                if dist.precedence == pkg_resources.EGG_DIST:
                    # It's already an egg, just fetch it into the dest

                    newloc = os.path.join(self._dest,
                                          os.path.basename(dist.location))

                    if os.path.isdir(dist.location):
                        # we got a directory. It must have been
                        # obtained locally.  Just copy it.
                        shutil.copytree(dist.location, newloc)
                    else:

                        setuptools.archive_util.unpack_archive(
                            dist.location, newloc)

                    redo_pyc(newloc)

                    # Getting the dist from the environment causes the
                    # distribution meta data to be read.  Cloning isn't
                    # good enough.
                    dists = pkg_resources.Environment([newloc
                                                       ])[dist.project_name]
                else:
                    # It's some other kind of dist.  We'll let easy_install
                    # deal with it:
                    dists = self._call_easy_install(dist.location, ws,
                                                    self._dest, dist)
                    for dist in dists:
                        redo_pyc(dist.location)

            finally:
                if tmp != self._download_cache:
                    shutil.rmtree(tmp)

            self._env.scan([self._dest])
            dist = self._env.best_match(requirement, ws)
            logger.info("Got %s.", dist)

        else:
            dists = [dist]

        for dist in dists:
            if (dist.has_metadata('dependency_links.txt')
                    and not self._install_from_cache
                    and self._use_dependency_links):
                for link in dist.get_metadata_lines('dependency_links.txt'):
                    link = link.strip()
                    if link not in self._links:
                        logger.debug('Adding find link %r from %s', link, dist)
                        self._links.append(link)
                        self._index = _get_index(self._index_url, self._links,
                                                 self._allow_hosts)

        for dist in dists:
            # Check whether we picked a version and, if we did, report it:
            if not (dist.precedence == pkg_resources.DEVELOP_DIST or
                    (len(requirement.specs) == 1
                     and requirement.specs[0][0] == '==')):
                logger.debug('Picked: %s = %s', dist.project_name,
                             dist.version)
                self._picked_versions[dist.project_name] = dist.version

                if not self._allow_picked_versions:
                    raise zc.buildout.UserError(
                        'Picked: %s = %s' % (dist.project_name, dist.version))

        return dists
Esempio n. 25
0
def get_env():
    env = pkg_resources.Environment(search_path='',
                                    platform=pkg_resources.get_platform())
    for dist in pkg_resources.find_distributions(eggs_dir, False):
        env.add(dist)
    return env
Esempio n. 26
0
import os, pickle
import os.path
import pkg_resources
import sys
print(sys.version_info)
SCRIPT_DIR = os.path.join(os.getcwd(), 'Script Bundle')
#SCRIPT_DIR = os.path.join(os.getcwd())
SITE_DIR = os.path.join(SCRIPT_DIR, 'Python27', 'site-packages')
siteEnv = pkg_resources.Environment([SITE_DIR])
packages, pkgErrors = pkg_resources.working_set.find_plugins(siteEnv)
for pkg in packages:
    pkg_resources.working_set.add(pkg)  # add plugins+libs to sys.path
if pkgErrors:
    print("Couldn't load", pkgErrors)  # display errors
# Set Theano variable to disable warning messages
os.environ['THEANO_FLAGS'] = 'cxx='
os.environ['OMP_NUM_THREADS'] = '4'

from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Dense, Flatten, Dropout
from keras.regularizers import l2
import pandas as pd, numpy as np
from keras.layers.noise import GaussianNoise
from scipy.stats import gmean

sys.path.append(r".\\Script Bundle\\")


def preprocessing(data):
    patients_data = {p: {} for p in ['p1', 'p2', 'p3', 'p4']}
Esempio n. 27
0
    def _call_easy_install(self, spec, ws, dest, dist):

        tmp = tempfile.mkdtemp(dir=dest)
        try:
            path = distribute_loc

            args = [sys.executable, '-c', _easy_install_cmd, '-mZUNxd', tmp]
            level = logger.getEffectiveLevel()
            if level > 0:
                args.append('-q')
            elif level < 0:
                args.append('-v')

            args.append(spec)

            if level <= logging.DEBUG:
                logger.debug('Running easy_install:\n"%s"\npath=%s\n',
                             '" "'.join(args), path)

            sys.stdout.flush()  # We want any pending output first

            exit_code = subprocess.call(list(args),
                                        env=dict(os.environ, PYTHONPATH=path))

            dists = []
            env = pkg_resources.Environment([tmp])
            for project in env:
                dists.extend(env[project])

            if exit_code:
                logger.error(
                    "An error occurred when trying to install %s. "
                    "Look above this message for any errors that "
                    "were output by easy_install.", dist)

            if not dists:
                raise zc.buildout.UserError("Couldn't install: %s" % dist)

            if len(dists) > 1:
                logger.warn(
                    "Installing %s\n"
                    "caused multiple distributions to be installed:\n"
                    "%s\n", dist, '\n'.join(map(str, dists)))
            else:
                d = dists[0]
                if d.project_name != dist.project_name:
                    logger.warn(
                        "Installing %s\n"
                        "Caused installation of a distribution:\n"
                        "%s\n"
                        "with a different project name.", dist, d)
                if d.version != dist.version:
                    logger.warn(
                        "Installing %s\n"
                        "Caused installation of a distribution:\n"
                        "%s\n"
                        "with a different version.", dist, d)

            result = []
            for d in dists:
                newloc = os.path.join(dest, os.path.basename(d.location))
                if os.path.exists(newloc):
                    if os.path.isdir(newloc):
                        shutil.rmtree(newloc)
                    else:
                        os.remove(newloc)
                os.rename(d.location, newloc)

                [d] = pkg_resources.Environment([newloc])[d.project_name]

                result.append(d)

            return result

        finally:
            shutil.rmtree(tmp)
Esempio n. 28
0
        # Under Python 3.x, we need to 'build' the source (using 2to3, etc)
        # first.  'python3 setup.py build_tests' will put everything under
        # build/tests (including nose itself, since some tests are inside the
        # nose source)
        # The 'py3where' argument in setup.cfg will take care of making sure we
        # pull our tests only from the build/tests directory.  We just need to
        # make sure the right things are on sys.path.
        lib_dirs = glob.glob(os.path.join(this_dir, 'build', 'lib*'))
        test_dir = os.path.join(this_dir, 'build', 'tests')
        if not os.path.isdir(test_dir):
            raise AssertionError(
                "Error: %s does not exist.  Use the setup.py 'build_tests' command to create it."
                % (test_dir, ))
    try:
        import pkg_resources

        env = pkg_resources.Environment(search_path=lib_dirs)
        distributions = env["nose"]
        assert len(distributions) == 1, (
            "Incorrect usage of selftest.py; please see DEVELOPERS.txt")
        dist = distributions[0]
        dist.activate()
    except ImportError:
        import pkg_resources
        pass
    # Always make sure our chosen test dir is first on the path
    sys.path.insert(0, test_dir)
    import nose

    nose.run_exit()
Esempio n. 29
0
    def take_action(self, opts):
        opts.egg_plugins = []

        if opts.no_sqlalchemy:
            opts.sqlalchemy = False

        if opts.ming:
            opts.sqlalchemy = False
            opts.migrations = False

        if opts.no_auth:
            opts.auth = False

        if not opts.package:
            package = opts.name.lower()
            package = beginning_letter.sub("", package)
            package = valid_only.sub("", package)
            opts.package = package

        if opts.tw1:
            opts.skip_tw = False

        if opts.auth:
            if opts.ming:
                opts.auth = "ming"
                opts.ming = True
            else:
                opts.auth = "sqlalchemy"
                opts.sqlalchemy = True
        else:
            opts.auth = None

        opts.database = opts.sqlalchemy or opts.ming

        opts.name = pkg_resources.safe_name(opts.name)
        opts.project = opts.name

        env = pkg_resources.Environment()
        if opts.name.lower() in env:
            print('The name "%s" is already in use by' % opts.name)
            for dist in env[opts.name]:
                print(dist)
                return

        import imp
        try:
            if imp.find_module(opts.package):
                print('The package name "%s" is already in use' % opts.package)
                return
        except ImportError:
            pass

        if os.path.exists(opts.name):
            print('A directory called "%s" already exists. Exiting.' %
                  opts.name)
            return

        opts.cookiesecret = None
        try:
            import uuid
            opts.cookiesecret = str(uuid.uuid4())
        except ImportError:
            import random
            import base64
            import struct
            opts.cookiesecret = base64.b64encode(''.join([
                struct.pack('i', random.randrange(2**31)) for _n in range(6)
            ])).strip()

        devtools_path = os.path.dirname(
            os.path.os.path.abspath(os.path.dirname(__file__)))

        # Workaround for templates ported from Paste
        # which check for 'True' instead of True
        template_vars = dict(vars(opts))
        #for key, value in template_vars.items():
        #    if value is True:
        #        template_vars[key] = 'True'

        template_vars['PY3'] = PY3
        QuickstartTemplate().run(
            os.path.join(devtools_path, 'templates', 'turbogears'), opts.name,
            template_vars)

        os.chdir(opts.name)

        sys.argv = ['setup.py', 'egg_info']
        imp.load_module('setup', *imp.find_module('setup', ['.']))

        # dirty hack to allow "empty" dirs
        for base, _path, files in os.walk('./'):
            for filename in files:
                if filename == 'empty':
                    os.remove(os.path.join(base, filename))

        if opts.skip_genshi or opts.mako or opts.kajiki or opts.jinja:
            # remove existing template files
            package_template_dir = os.path.abspath(
                os.path.join(opts.package, 'templates'))
            shutil.rmtree(package_template_dir, ignore_errors=True)

        # copy over the alternative templates if appropriate
        if opts.mako or opts.kajiki or opts.jinja:

            def overwrite_templates(template_type):
                print('Writing %s template files to ./%s' %
                      (template_type, os.path.join(opts.package, 'templates')))
                # replace template files with alternative ones
                alt_template_dir = os.path.join(
                    devtools_path, 'commands', 'quickstart_%s' % template_type)
                shutil.copytree(alt_template_dir, package_template_dir)

            if opts.mako:
                overwrite_templates('mako')
            elif opts.jinja:
                overwrite_templates('jinja')
            elif opts.kajiki:
                overwrite_templates('kajiki')

        if opts.ming:
            print('Writing Ming model files to ./%s' %
                  os.path.join(opts.package, 'model'))
            package_model_dir = os.path.abspath(
                os.path.join(opts.package, 'model'))
            ming_model_dir = os.path.join(devtools_path, 'commands',
                                          'model_ming')
            shutil.copy(os.path.join(ming_model_dir, 'session.py'),
                        package_model_dir)

        if not opts.migrations:
            print('Disabling migrations support')
            # remove existing migrations directory
            package_migrations_dir = os.path.abspath('migration')
            shutil.rmtree(package_migrations_dir, ignore_errors=True)
Esempio n. 30
0
    def register(self,
                 name: str,
                 packages: Iterable[str],
                 per_user: bool,
                 is_local: bool,
                 out_file: StringIO = None,
                 package_scripts=None):
        config_file, config_file_path = self._file_path(per_user)

        config = ConfigParser(allow_no_value=True)
        if config_file_path.exists():
            with config_file_path.open() as in_file:
                config.read_file(in_file)

        if not config.has_section(_S):
            config.add_section(_S)
        requirements = self._requirements(config, name) | set(packages)

        if not package_scripts:  # pragma: no cover
            venv = VEnv(name, local=is_local)
            if not venv.exists():
                venv.requirements = requirements
                if not venv.create():
                    echo("Unable to create %s to register %s" %
                         (venv, sorted(packages)))
                    return
            venv.run('pip', ['install'] + list(packages))

            try:
                import pkg_resources
            except ImportError:
                echo("Unable to import pkg_resources to register %s into %s" %
                     (sorted(packages), venv))
                return

            pkg_env = pkg_resources.Environment(
                search_path=[str(venv.abs_path / 'lib' / 'site-packages')])

            def pkg_scripts(p: str) -> Iterable[str]:
                scripts = {}  # type: Dict
                dist = pkg_env[p]
                if len(dist):
                    scripts = dist[0].get_entry_map().get(
                        'console_scripts') or {}
                return scripts.keys()

            package_scripts = pkg_scripts

        for p in packages:
            for s in package_scripts(p):
                echo("Registering %s from %s into %s" % (s, p, name))
                config.set(_S, s, name)

        if not config.has_section(name):
            config.add_section(name)
        config.set(name, _r, '\n'.join(sorted(requirements)))
        if per_user == is_local:
            config.set(name, _l if per_user else _g, None)

        if out_file:
            config.write(out_file)
        else:
            with config_file_path.open('w') as out_config:
                config.write(out_config)