Пример #1
0
def plugin_resolution(rule_runner: RuleRunner,
                      *,
                      interpreter=None,
                      chroot=None,
                      plugins=None,
                      sdist=True):
    @contextmanager
    def provide_chroot(existing):
        if existing:
            yield existing, False
        else:
            with temporary_dir() as new_chroot:
                yield new_chroot, True

    with provide_chroot(chroot) as (root_dir, create_artifacts):
        env: Dict[str, str] = {}
        repo_dir = None
        if plugins:
            repo_dir = os.path.join(root_dir, "repo")
            env.update(
                PANTS_PYTHON_REPOS_REPOS=f"[{repo_dir!r}]",
                PANTS_PYTHON_REPOS_INDEXES="[]",
                PANTS_PYTHON_SETUP_RESOLVER_CACHE_TTL="1",
            )
            plugin_list = []
            for plugin in plugins:
                version = None
                if isinstance(plugin, tuple):
                    plugin, version = plugin
                plugin_list.append(
                    f"{plugin}=={version}" if version else plugin)
                if create_artifacts:
                    setup_py_args = [
                        "sdist" if sdist else "bdist_wheel", "--dist-dir",
                        "dist/"
                    ]
                    _run_setup_py(rule_runner, plugin, version, setup_py_args,
                                  repo_dir)
            env["PANTS_PLUGINS"] = f"[{','.join(map(repr, plugin_list))}]"
            env["PANTS_PLUGIN_CACHE_DIR"] = os.path.join(
                root_dir, "plugin-cache")

        configpath = os.path.join(root_dir, "pants.toml")
        if create_artifacts:
            touch(configpath)
        args = [f"--pants-config-files=['{configpath}']"]

        options_bootstrapper = OptionsBootstrapper.create(env=env,
                                                          args=args,
                                                          allow_pantsrc=False)
        plugin_resolver = PluginResolver(options_bootstrapper,
                                         interpreter=interpreter)
        cache_dir = plugin_resolver.plugin_cache_dir

        working_set = plugin_resolver.resolve(WorkingSet(entries=[]))
        for dist in working_set:
            assert (Path(os.path.realpath(cache_dir))
                    in Path(os.path.realpath(dist.location)).parents)

        yield working_set, root_dir, repo_dir, cache_dir
Пример #2
0
    def plugin_resolution(self,
                          *,
                          interpreter=None,
                          chroot=None,
                          plugins=None,
                          packager_cls=None):
        @contextmanager
        def provide_chroot(existing):
            if existing:
                yield existing, False
            else:
                with temporary_dir() as new_chroot:
                    yield new_chroot, True

        with provide_chroot(chroot) as (root_dir, create_artifacts):
            env = {}
            repo_dir = None
            if plugins:
                repo_dir = os.path.join(root_dir, "repo")
                env.update(
                    PANTS_PYTHON_REPOS_REPOS=f"[{repo_dir!r}]",
                    PANTS_PYTHON_REPOS_INDEXES="[]",
                    PANTS_PYTHON_SETUP_RESOLVER_CACHE_TTL="1",
                )
                plugin_list = []
                for plugin in plugins:
                    version = None
                    if isinstance(plugin, tuple):
                        plugin, version = plugin
                    plugin_list.append(
                        f"{plugin}=={version}" if version else plugin)
                    if create_artifacts:
                        self.create_plugin(repo_dir,
                                           plugin,
                                           version,
                                           packager_cls=packager_cls)
                env["PANTS_PLUGINS"] = f"[{','.join(map(repr, plugin_list))}]"
                env["PANTS_PLUGIN_CACHE_DIR"] = os.path.join(
                    root_dir, "plugin-cache")

            configpath = os.path.join(root_dir, "pants.toml")
            if create_artifacts:
                touch(configpath)
            args = [f"--pants-config-files=['{configpath}']"]

            options_bootstrapper = OptionsBootstrapper.create(env=env,
                                                              args=args)
            plugin_resolver = PluginResolver(options_bootstrapper,
                                             interpreter=interpreter)
            cache_dir = plugin_resolver.plugin_cache_dir

            working_set = plugin_resolver.resolve(WorkingSet(entries=[]))
            for dist in working_set:
                self.assertIn(Path(cache_dir), Path(dist.location).parents)

            yield working_set, root_dir, repo_dir, cache_dir
Пример #3
0
    def plugin_resolution(self,
                          *,
                          interpreter=None,
                          chroot=None,
                          plugins=None,
                          packager_cls=None):
        @contextmanager
        def provide_chroot(existing):
            if existing:
                yield existing, False
            else:
                with temporary_dir() as new_chroot:
                    yield new_chroot, True

        with provide_chroot(chroot) as (root_dir, create_artifacts):
            env = {}
            repo_dir = None
            if plugins:
                repo_dir = os.path.join(root_dir, 'repo')
                env.update(PANTS_PYTHON_REPOS_REPOS=f'[{repo_dir!r}]',
                           PANTS_PYTHON_REPOS_INDEXES='[]',
                           PANTS_PYTHON_SETUP_RESOLVER_CACHE_TTL='1')
                plugin_list = []
                for plugin in plugins:
                    version = None
                    if isinstance(plugin, tuple):
                        plugin, version = plugin
                    plugin_list.append(
                        f'{plugin}=={version}' if version else plugin)
                    if create_artifacts:
                        self.create_plugin(repo_dir,
                                           plugin,
                                           version,
                                           packager_cls=packager_cls)
                env['PANTS_PLUGINS'] = f"[{','.join(map(repr, plugin_list))}]"
                env['PANTS_PLUGIN_CACHE_DIR'] = os.path.join(
                    root_dir, 'plugin-cache')

            configpath = os.path.join(root_dir, 'pants.ini')
            if create_artifacts:
                touch(configpath)
            args = [f"--pants-config-files=['{configpath}']"]

            options_bootstrapper = OptionsBootstrapper.create(env=env,
                                                              args=args)
            plugin_resolver = PluginResolver(options_bootstrapper,
                                             interpreter=interpreter)
            cache_dir = plugin_resolver.plugin_cache_dir
            yield plugin_resolver.resolve(
                WorkingSet(entries=[])), root_dir, repo_dir, cache_dir
Пример #4
0
def _initialize_build_configuration(
    plugin_resolver: PluginResolver,
    options_bootstrapper: OptionsBootstrapper,
    env: CompleteEnvironment,
) -> BuildConfiguration:
    """Initialize a BuildConfiguration for the given OptionsBootstrapper.

    NB: This method:
      1. has the side-effect of (idempotently) adding PYTHONPATH entries for this process
      2. is expensive to call, because it might resolve plugins from the network
    """

    bootstrap_options = options_bootstrapper.get_bootstrap_options().for_global_scope()
    working_set = plugin_resolver.resolve(options_bootstrapper, env)

    # Add any extra paths to python path (e.g., for loading extra source backends).
    for path in bootstrap_options.pythonpath:
        if path not in sys.path:
            sys.path.append(path)
            pkg_resources.fixup_namespace_packages(path)

    # Load plugins and backends.
    return load_backends_and_plugins(
        bootstrap_options.plugins,
        working_set,
        bootstrap_options.backend_packages,
    )
Пример #5
0
 def __init__(
     self,
     options_bootstrapper: OptionsBootstrapper,
     executor: PyExecutor | None = None,
 ) -> None:
     self._bootstrap_scheduler = create_bootstrap_scheduler(options_bootstrapper, executor)
     self._plugin_resolver = PluginResolver(self._bootstrap_scheduler)
Пример #6
0
 def __init__(
     self,
     options_bootstrapper: OptionsBootstrapper,
     env: CompleteEnvironment,
     executor: Optional[PyExecutor] = None,
 ) -> None:
     self._bootstrap_scheduler = create_bootstrap_scheduler(
         options_bootstrapper, env, executor=executor
     )
     self._plugin_resolver = PluginResolver(self._bootstrap_scheduler)
Пример #7
0
 def __init__(self, options_bootstrapper, working_set=None, exiter=sys.exit):
   """
   :param OptionsBootStrapper options_bootstrapper: An options bootstrapper instance.
   :param pkg_resources.WorkingSet working_set: The working set of the current run as returned by
                                                PluginResolver.resolve().
   :param func exiter: A function that accepts an exit code value and exits (for tests).
   """
   self._options_bootstrapper = options_bootstrapper
   self._working_set = working_set or PluginResolver(self._options_bootstrapper).resolve()
   self._exiter = exiter
Пример #8
0
    def plugin_resolution(self, chroot=None, plugins=None):
        @contextmanager
        def provide_chroot(existing):
            if existing:
                yield existing, False
            else:
                with temporary_dir() as new_chroot:
                    yield new_chroot, True

        with provide_chroot(chroot) as (root_dir, create_artifacts):
            env = {'PANTS_BOOTSTRAPDIR': root_dir}
            repo_dir = None
            if plugins:
                repo_dir = os.path.join(root_dir, 'repo')
                env.update(PANTS_PYTHON_REPOS_REPOS='[{!r}]'.format(repo_dir),
                           PANTS_PYTHON_REPOS_INDEXES='[]',
                           PANTS_PYTHON_SETUP_RESOLVER_CACHE_TTL='1')
                plugin_list = []
                for plugin in plugins:
                    version = None
                    if isinstance(plugin, tuple):
                        plugin, version = plugin
                    plugin_list.append('{}=={}'.format(plugin, version)
                                       if version else plugin)
                    if create_artifacts:
                        self.create_plugin(repo_dir, plugin, version)
                env['PANTS_PLUGINS'] = '[{}]'.format(','.join(
                    map(repr, plugin_list)))

            configpath = os.path.join(root_dir, 'pants.ini')
            if create_artifacts:
                touch(configpath)
            args = ["--pants-config-files=['{}']".format(configpath)]

            options_bootstrapper = OptionsBootstrapper(env=env, args=args)
            plugin_resolver = PluginResolver(options_bootstrapper)
            cache_dir = plugin_resolver.plugin_cache_dir
            yield plugin_resolver.resolve(
                WorkingSet(entries=[])), root_dir, repo_dir, cache_dir
Пример #9
0
  def plugin_resolution(self, chroot=None, plugins=None, packager_cls=None):
    @contextmanager
    def provide_chroot(existing):
      if existing:
        yield existing, False
      else:
        with temporary_dir() as new_chroot:
          yield new_chroot, True

    with provide_chroot(chroot) as (root_dir, create_artifacts):
      env = {'PANTS_BOOTSTRAPDIR': root_dir}
      repo_dir = None
      if plugins:
        repo_dir = os.path.join(root_dir, 'repo')
        env.update(PANTS_PYTHON_REPOS_REPOS='[{!r}]'.format(repo_dir),
                   PANTS_PYTHON_REPOS_INDEXES='[]',
                   PANTS_PYTHON_SETUP_RESOLVER_CACHE_TTL='1')
        plugin_list = []
        for plugin in plugins:
          version = None
          if isinstance(plugin, tuple):
            plugin, version = plugin
          plugin_list.append('{}=={}'.format(plugin, version) if version else plugin)
          if create_artifacts:
            self.create_plugin(repo_dir, plugin, version, packager_cls=packager_cls)
        env['PANTS_PLUGINS'] = '[{}]'.format(','.join(map(repr, plugin_list)))

      configpath = os.path.join(root_dir, 'pants.ini')
      if create_artifacts:
        touch(configpath)
      args = ["--pants-config-files=['{}']".format(configpath)]

      options_bootstrapper = OptionsBootstrapper(env=env, args=args)
      plugin_resolver = PluginResolver(options_bootstrapper)
      cache_dir = plugin_resolver.plugin_cache_dir
      yield plugin_resolver.resolve(WorkingSet(entries=[])), root_dir, repo_dir, cache_dir
Пример #10
0
 def __init__(self, options_bootstrapper: OptionsBootstrapper) -> None:
     self._options_bootstrapper = options_bootstrapper
     self._bootstrap_options = options_bootstrapper.get_bootstrap_options(
     ).for_global_scope()
     self._working_set = PluginResolver(
         self._options_bootstrapper).resolve()
Пример #11
0
 def __init__(self, options_bootstrapper, working_set=None):
     self._options_bootstrapper = options_bootstrapper
     self._bootstrap_options = options_bootstrapper.get_bootstrap_options(
     ).for_global_scope()
     self._working_set = working_set or PluginResolver(
         self._options_bootstrapper).resolve()
Пример #12
0
def plugin_resolution(rule_runner: RuleRunner,
                      *,
                      interpreter=None,
                      chroot=None,
                      plugins=None,
                      sdist=True):
    @contextmanager
    def provide_chroot(existing):
        if existing:
            yield existing, False
        else:
            with temporary_dir() as new_chroot:
                yield new_chroot, True

    interpreter_constraints = (PexInterpreterConstraints([
        f"=={interpreter.identity.version_str}"
    ]) if interpreter else PexInterpreterConstraints([">=3.7"]))

    with provide_chroot(chroot) as (root_dir, create_artifacts):
        env: Dict[str, str] = {}
        repo_dir = None
        if plugins:
            repo_dir = os.path.join(root_dir, "repo")
            env.update(
                PANTS_PYTHON_REPOS_REPOS=f"['file://{repo_dir}']",
                PANTS_PYTHON_REPOS_INDEXES="[]",
                PANTS_PYTHON_SETUP_RESOLVER_CACHE_TTL="1",
            )
            plugin_list = []
            for plugin in plugins:
                version = None
                if isinstance(plugin, tuple):
                    plugin, version = plugin
                plugin_list.append(
                    f"{plugin}=={version}" if version else plugin)
                if create_artifacts:
                    setup_py_args = [
                        "sdist" if sdist else "bdist_wheel", "--dist-dir",
                        "dist/"
                    ]
                    _run_setup_py(
                        rule_runner,
                        plugin,
                        interpreter_constraints,
                        version,
                        setup_py_args,
                        repo_dir,
                    )
            env["PANTS_PLUGINS"] = f"[{','.join(map(repr, plugin_list))}]"

        configpath = os.path.join(root_dir, "pants.toml")
        if create_artifacts:
            touch(configpath)
        args = [f"--pants-config-files=['{configpath}']"]

        options_bootstrapper = OptionsBootstrapper.create(env=env,
                                                          args=args,
                                                          allow_pantsrc=False)
        complete_env = CompleteEnvironment({
            **{
                k: os.environ[k]
                for k in ["PATH", "HOME", "PYENV_ROOT"] if k in os.environ
            },
            **env
        })
        bootstrap_scheduler = create_bootstrap_scheduler(
            options_bootstrapper, complete_env)
        plugin_resolver = PluginResolver(
            bootstrap_scheduler,
            interpreter_constraints=interpreter_constraints)
        cache_dir = options_bootstrapper.bootstrap_options.for_global_scope(
        ).named_caches_dir

        working_set = plugin_resolver.resolve(options_bootstrapper,
                                              complete_env,
                                              WorkingSet(entries=[]))
        for dist in working_set:
            assert (Path(os.path.realpath(cache_dir))
                    in Path(os.path.realpath(dist.location)).parents)

        yield working_set, root_dir, repo_dir
Пример #13
0
def plugin_resolution(
    rule_runner: RuleRunner,
    *,
    interpreter: PythonInterpreter | None = None,
    chroot: str | None = None,
    plugins: Sequence[Plugin] = (),
    sdist: bool = True,
    working_set_entries: Sequence[Distribution] = (),
    use_pypi: bool = False,
):
    @contextmanager
    def provide_chroot(existing):
        if existing:
            yield existing, False
        else:
            with temporary_dir() as new_chroot:
                yield new_chroot, True

    # Default to resolving with whatever we're currently running with.
    interpreter_constraints = (InterpreterConstraints(
        [f"=={interpreter.identity.version_str}"]) if interpreter else None)
    artifact_interpreter_constraints = interpreter_constraints or InterpreterConstraints(
        [f"=={'.'.join(map(str, sys.version_info[:3]))}"])

    with provide_chroot(chroot) as (root_dir, create_artifacts):
        env: Dict[str, str] = {}
        repo_dir = None
        if plugins:
            repo_dir = os.path.join(root_dir, "repo")
            env.update(
                PANTS_PYTHON_REPOS_REPOS=f"['file://{repo_dir}']",
                PANTS_PYTHON_RESOLVER_CACHE_TTL="1",
            )
            if not use_pypi:
                env.update(PANTS_PYTHON_REPOS_INDEXES="[]")
            plugin_list = []
            for plugin in plugins:
                version = plugin.version
                plugin_list.append(
                    f"{plugin.name}=={version}" if version else plugin.name)
                if create_artifacts:
                    setup_py_args = [
                        "sdist" if sdist else "bdist_wheel", "--dist-dir",
                        "dist/"
                    ]
                    _run_setup_py(
                        rule_runner,
                        plugin.name,
                        artifact_interpreter_constraints,
                        version,
                        plugin.install_requires,
                        setup_py_args,
                        repo_dir,
                    )
            env["PANTS_PLUGINS"] = f"[{','.join(map(repr, plugin_list))}]"

        configpath = os.path.join(root_dir, "pants.toml")
        if create_artifacts:
            touch(configpath)
        args = [f"--pants-config-files=['{configpath}']"]

        options_bootstrapper = OptionsBootstrapper.create(env=env,
                                                          args=args,
                                                          allow_pantsrc=False)
        complete_env = CompleteEnvironment({
            **{
                k: os.environ[k]
                for k in ["PATH", "HOME", "PYENV_ROOT"] if k in os.environ
            },
            **env
        })
        bootstrap_scheduler = create_bootstrap_scheduler(options_bootstrapper)
        cache_dir = options_bootstrapper.bootstrap_options.for_global_scope(
        ).named_caches_dir

        input_working_set = WorkingSet(entries=[])
        for dist in working_set_entries:
            input_working_set.add(dist)
        plugin_resolver = PluginResolver(bootstrap_scheduler,
                                         interpreter_constraints,
                                         input_working_set)
        working_set = plugin_resolver.resolve(
            options_bootstrapper,
            complete_env,
        )
        for dist in working_set:
            assert (Path(os.path.realpath(cache_dir))
                    in Path(os.path.realpath(dist.location)).parents)

        yield working_set, root_dir, repo_dir