-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Implement rwt as pip run #3979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Implement rwt as pip run #3979
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf716e8
Vendor rwt 2.11
jaraco 1883d3b
Add 'pip run' command
jaraco 8091b7e
Ensure help commands match (indicated by tests)
jaraco be2d848
Remove tests from rwt to avoid issues with check-manifest.
jaraco a7d57db
Use rwt 2.12 and pass args directly.
jaraco efcdd9c
Remove unused import
jaraco a4cb1ac
Add docs for pip run
jaraco 21dc670
Update docstring to replace copy/paste artifact with an accurate refl…
jaraco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,4 @@ Reference Guide | |
| pip_search | ||
| pip_wheel | ||
| pip_hash | ||
| pip_run | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| pip run | ||
| ------- | ||
|
|
||
| .. contents:: | ||
|
|
||
| Usage | ||
| ***** | ||
|
|
||
| .. pip-command-usage:: run | ||
|
|
||
| Description | ||
| *********** | ||
|
|
||
| .. pip-command-description:: run | ||
|
|
||
|
|
||
| Overview | ||
| ++++++++ | ||
|
|
||
| Pip run in a specialized invocation of pip install that makes | ||
| packages available only for the duration of a single Python invocation | ||
| for one-off needs. The command is based on the | ||
| `rwt project <https://pypi.org/project/rwt>`_. | ||
|
|
||
| Argument Handling | ||
| +++++++++++++++++ | ||
|
|
||
| As a wrapper around ``pip install``, the arguments to ``run`` are split into | ||
| two segments, separated by a double-dash (``--``). The arguments prior | ||
| to the ``--`` are passed directly to :ref:`pip install`, so should contain | ||
| requirements, requirments files, and index directives. | ||
|
|
||
| The arguments after the ``--`` are passed to a new Python interpreter in the | ||
| context of the installed dependencies. | ||
|
|
||
| For more details and examples, see the `rwt project <https://pypi.org/project/rwt>`_. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import sys | ||
|
|
||
| from . import deps | ||
| from . import commands | ||
| from . import launch | ||
| from . import scripts | ||
|
|
||
|
|
||
| def run(args=None): | ||
| if args is None: | ||
| args = sys.argv[1:] | ||
| pip_args, params = commands.parse_script_args(args) | ||
| commands.intercept(pip_args) | ||
| pip_args.extend(scripts.DepsReader.search(params)) | ||
| with deps.load(*pip_args) as home: | ||
| launch.with_path(home, params) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from . import run | ||
|
|
||
|
|
||
| __name__ == '__main__' and run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import textwrap | ||
|
|
||
|
|
||
| def parse_script_args(args): | ||
| """ | ||
| Separate the command line arguments into arguments for pip | ||
| and arguments to Python. | ||
|
|
||
| >>> parse_script_args(['foo', '--', 'bar']) | ||
| (['foo'], ['bar']) | ||
|
|
||
| >>> parse_script_args(['foo', 'bar']) | ||
| (['foo', 'bar'], []) | ||
| """ | ||
| try: | ||
| pivot = args.index('--') | ||
| except ValueError: | ||
| pivot = len(args) | ||
| return args[:pivot], args[pivot+1:] | ||
|
|
||
|
|
||
| help_doc = textwrap.dedent(""" | ||
| Usage: | ||
|
|
||
| Arguments to rwt prior to `--` are used to specify the requirements | ||
| to make available, just as arguments to pip install. For example, | ||
|
|
||
| rwt -r requirements.txt "requests>=2.0" | ||
|
|
||
| That will launch python after installing the deps in requirements.txt | ||
| and also a late requests. Packages are always installed to a temporary | ||
| location and cleaned up when the process exits. | ||
|
|
||
| Arguments after `--` are passed to the Python interpreter. So to launch | ||
| `script.py`: | ||
|
|
||
| rwt -- script.py | ||
|
|
||
| If the `--` is ommitted or nothing is passed, the python interpreter | ||
| will be launched in interactive mode: | ||
|
|
||
| rwt | ||
| >>> | ||
|
|
||
| For more examples and details, see https://pypi.org/project/rwt. | ||
| """).lstrip() | ||
|
|
||
|
|
||
| def intercept(args): | ||
| """ | ||
| Detect certain args and intercept them. | ||
| """ | ||
| if '--help' in args or '-h' in args: | ||
| print(help_doc) | ||
| raise SystemExit(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| from __future__ import print_function | ||
|
|
||
| import sys | ||
| import contextlib | ||
| import subprocess | ||
| import tempfile | ||
| import shutil | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _update_working_set(): | ||
| """ | ||
| Update the master working_set to include these new packages. | ||
|
|
||
| TODO: would be better to use an officially-supported API, | ||
| but no suitable API is apparent. | ||
| """ | ||
| try: | ||
| pkg_resources = sys.modules['pkg_resources'] | ||
| if not hasattr(pkg_resources, '_initialize_master_working_set'): | ||
| exec(_init_ws_patch, vars(pkg_resources)) | ||
| pkg_resources._initialize_master_working_set() | ||
| except KeyError: | ||
| # it's unnecessary to re-initialize when it hasn't | ||
| # yet been initialized. | ||
| pass | ||
| yield | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def load(*args): | ||
| target = tempfile.mkdtemp(prefix='rwt-') | ||
| cmdline = subprocess.list2cmdline(args) | ||
| print("Loading requirements using", cmdline) | ||
| cmd = ( | ||
| sys.executable, | ||
| '-m', 'pip', | ||
| 'install', | ||
| '-q', | ||
| '-t', target, | ||
| ) + args | ||
| subprocess.check_call(cmd) | ||
| try: | ||
| yield target | ||
| finally: | ||
| shutil.rmtree(target) | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def on_sys_path(*args): | ||
| """ | ||
| Install dependencies via args to pip and ensure they have precedence | ||
| on sys.path. | ||
| """ | ||
| with load(*args) as target: | ||
| sys.path.insert(0, target) | ||
| try: | ||
| with _update_working_set(): | ||
| yield target | ||
| finally: | ||
| sys.path.remove(target) | ||
|
|
||
|
|
||
| # from setuptools 19.6.2 | ||
| _init_ws_patch = ''' | ||
| def _initialize_master_working_set(): | ||
| """ | ||
| Prepare the master working set and make the ``require()`` | ||
| API available. | ||
|
|
||
| This function has explicit effects on the global state | ||
| of pkg_resources. It is intended to be invoked once at | ||
| the initialization of this module. | ||
|
|
||
| Invocation by other packages is unsupported and done | ||
| at their own risk. | ||
| """ | ||
| working_set = WorkingSet._build_master() | ||
| _declare_state('object', working_set=working_set) | ||
|
|
||
| require = working_set.require | ||
| iter_entry_points = working_set.iter_entry_points | ||
| add_activation_listener = working_set.subscribe | ||
| run_script = working_set.run_script | ||
| # backward compatibility | ||
| run_main = run_script | ||
| # Activate all distributions already on sys.path, and ensure that | ||
| # all distributions added to the working set in the future (e.g. by | ||
| # calling ``require()``) will get activated as well. | ||
| add_activation_listener(lambda dist: dist.activate()) | ||
| working_set.entries=[] | ||
| # match order | ||
| list(map(working_set.add_entry, sys.path)) | ||
| globals().update(locals()) | ||
| ''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import textwrap | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| import signal | ||
| import glob | ||
| import itertools | ||
|
|
||
|
|
||
| class PathReader: | ||
| @staticmethod | ||
| def _read_file(filename): | ||
| root = os.path.dirname(filename) | ||
| return ( | ||
| os.path.join(root, path.rstrip()) | ||
| for path in open(filename) | ||
| if path.strip() | ||
| and not path.startswith('#') | ||
| and not path.startswith('import ') | ||
| ) | ||
|
|
||
| @classmethod | ||
| def _read(cls, target): | ||
| """ | ||
| As .pth files aren't honored except in site dirs, | ||
| read the paths indicated by them. | ||
| """ | ||
| pth_files = glob.glob(os.path.join(target, '*.pth')) | ||
| file_items = map(cls._read_file, pth_files) | ||
| return itertools.chain.from_iterable(file_items) | ||
|
|
||
|
|
||
| def _inject_sitecustomize(target): | ||
| """ | ||
| Create a sitecustomize file in the target that will install | ||
| the target as a sitedir. | ||
|
|
||
| Only needed on Python 3.2 and earlier to workaround #1. | ||
| """ | ||
| if sys.version_info > (3, 3): | ||
| return | ||
|
|
||
| hook = textwrap.dedent(""" | ||
| import site | ||
| site.addsitedir({target!r}) | ||
| """).lstrip().format(**locals()) | ||
| sc_fn = os.path.join(target, 'sitecustomize.py') | ||
| with open(sc_fn, 'w') as strm: | ||
| strm.write(hook) | ||
|
|
||
|
|
||
| def _build_env(target): | ||
| """ | ||
| Prepend target and .pth references in target to PYTHONPATH | ||
| """ | ||
| env = dict(os.environ) | ||
| suffix = env.get('PYTHONPATH') | ||
| prefix = target, | ||
| items = itertools.chain( | ||
| prefix, | ||
| PathReader._read(target), | ||
| (suffix,) if suffix else (), | ||
| ) | ||
| joined = os.pathsep.join(items) | ||
| env['PYTHONPATH'] = joined | ||
| return env | ||
|
|
||
|
|
||
| def _setup_env(target): | ||
| _inject_sitecustomize(target) | ||
| return _build_env(target) | ||
|
|
||
|
|
||
| def with_path(target, params): | ||
| """ | ||
| Launch Python with target on the path and params | ||
| """ | ||
| def null_handler(signum, frame): | ||
| pass | ||
|
|
||
| signal.signal(signal.SIGINT, null_handler) | ||
| cmd = [sys.executable] + params | ||
| subprocess.Popen(cmd, env=_setup_env(target)).wait() | ||
|
|
||
|
|
||
| def with_path_overlay(target, params): | ||
| """ | ||
| Overlay Python with target on the path and params | ||
| """ | ||
| cmd = [sys.executable] + params | ||
| os.execve(sys.executable, cmd, _setup_env(target)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this is an issue with
rwtrather than with the vendoring orpip run, but this doesn't pass the exit code of the script back to the caller ofrwt. It probably should.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I've filed jaraco/pip-run#10