Repository navigation
bpo-34556: Add --upgrade-deps to venv module #13100
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
43614b0
Merge pull request #1 from python/master
cooperlees 42b6f1a
Add --upgrade-deps to venv module
cooperlees 9bd89f4
Fix bin_dir to bin_path + fix unittest pathing
cooperlees 63c4107
Using logger.debug and not a new raw logging.debug in venv/__init__.py
cooperlees c35c469
- Revert all formatting and lint changes
cooperlees e39f6e5
Move logging to f-string
cooperlees 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
Add --upgrade-deps to venv module
- This allows for pip + setuptools to be automatically upgraded to the latest version on PyPI - Update documentation to represent this change bpo-34556: Add --upgrade to venv module
- Loading branch information
commit 42b6f1abbdb560759e22eb25df3c3e653a8827a7
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
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 |
|---|---|---|
|
|
@@ -12,6 +12,8 @@ | |
| import sysconfig | ||
| import types | ||
|
|
||
|
|
||
| CORE_VENV_DEPS = ('pip', 'setuptools') | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
|
|
@@ -38,16 +40,19 @@ class EnvBuilder: | |
| :param with_pip: If True, ensure pip is installed in the virtual | ||
| environment | ||
| :param prompt: Alternative terminal prefix for the environment. | ||
| :param upgrade_deps: Update the base venv modules to the latest on PyPI | ||
| """ | ||
|
|
||
| def __init__(self, system_site_packages=False, clear=False, | ||
| symlinks=False, upgrade=False, with_pip=False, prompt=None): | ||
| symlinks=False, upgrade=False, with_pip=False, prompt=None, | ||
| upgrade_deps=False): | ||
| self.system_site_packages = system_site_packages | ||
| self.clear = clear | ||
| self.symlinks = symlinks | ||
| self.upgrade = upgrade | ||
| self.with_pip = with_pip | ||
| self.prompt = prompt | ||
| self.upgrade_deps = upgrade_deps | ||
|
|
||
| def create(self, env_dir): | ||
| """ | ||
|
|
@@ -74,6 +79,8 @@ def create(self, env_dir): | |
| # restore it and rewrite the configuration | ||
| self.system_site_packages = True | ||
| self.create_configuration(context) | ||
| if self.upgrade_deps: | ||
| self.upgrade_dependencies(context, env_dir) | ||
|
|
||
| def clear_directory(self, path): | ||
| for fn in os.listdir(path): | ||
|
|
@@ -105,7 +112,6 @@ def create_if_needed(d): | |
| prompt = self.prompt if self.prompt is not None else context.env_name | ||
| context.prompt = '(%s) ' % prompt | ||
| create_if_needed(env_dir) | ||
| env = os.environ | ||
| executable = getattr(sys, '_base_executable', sys.executable) | ||
| dirname, exename = os.path.split(os.path.abspath(executable)) | ||
| context.executable = executable | ||
|
|
@@ -250,7 +256,7 @@ def setup_python(self, context): | |
|
|
||
| if sysconfig.is_python_build(True): | ||
| # copy init.tcl | ||
| for root, dirs, files in os.walk(context.python_dir): | ||
| for root, _dirs, files in os.walk(context.python_dir): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dirs is not used in the loop, this is a flake recommended way to indicate that. Will revert so I get my feature landed :) |
||
| if 'init.tcl' in files: | ||
| tcldir = os.path.basename(root) | ||
| tcldir = os.path.join(context.env_dir, 'Lib', tcldir) | ||
|
|
@@ -329,11 +335,11 @@ def install_scripts(self, context, path): | |
| binpath = context.bin_path | ||
| plen = len(path) | ||
| for root, dirs, files in os.walk(path): | ||
| if root == path: # at top-level, remove irrelevant dirs | ||
| if root == path: # at top-level, remove irrelevant dirs | ||
| for d in dirs[:]: | ||
| if d not in ('common', os.name): | ||
| dirs.remove(d) | ||
| continue # ignore files in top level | ||
| continue # ignore files in top level | ||
| for f in files: | ||
| if (os.name == 'nt' and f.startswith('python') | ||
| and f.endswith(('.exe', '.pdb'))): | ||
|
|
@@ -363,15 +369,28 @@ def install_scripts(self, context, path): | |
| f.write(data) | ||
| shutil.copymode(srcfile, dstfile) | ||
|
|
||
| def upgrade_dependencies(self, context, env_dir): | ||
| logging.debug( | ||
| 'Upgrading {} packages in {}'.format(CORE_VENV_DEPS, env_dir) | ||
| ) | ||
| if sys.platform == 'win32': | ||
| pip_exe = os.path.join(context.bin_dir, 'pip.exe') | ||
| else: | ||
| pip_exe = os.path.join(context.bin_dir, 'pip') | ||
| cmd = [pip_exe, 'install', '-U'] | ||
| cmd.extend(CORE_VENV_DEPS) | ||
| subprocess.check_call(cmd) | ||
|
|
||
|
|
||
| def create(env_dir, system_site_packages=False, clear=False, | ||
| symlinks=False, with_pip=False, prompt=None): | ||
| symlinks=False, with_pip=False, prompt=None): | ||
| """Create a virtual environment in a directory.""" | ||
| builder = EnvBuilder(system_site_packages=system_site_packages, | ||
| clear=clear, symlinks=symlinks, with_pip=with_pip, | ||
| prompt=prompt) | ||
| builder.create(env_dir) | ||
|
|
||
|
|
||
| def main(args=None): | ||
| compatible = True | ||
| if sys.version_info < (3, 3): | ||
|
|
@@ -432,6 +451,11 @@ def main(args=None): | |
| parser.add_argument('--prompt', | ||
| help='Provides an alternative prompt prefix for ' | ||
| 'this environment.') | ||
| parser.add_argument('--upgrade-deps', default=False, action='store_true', | ||
| dest='upgrade_deps', | ||
| help='Upgrade core dependencies: {} to the latest ' | ||
| 'version in PyPI'.format( | ||
| ' '.join(CORE_VENV_DEPS))) | ||
| options = parser.parse_args(args) | ||
| if options.upgrade and options.clear: | ||
| raise ValueError('you cannot supply --upgrade and --clear together.') | ||
|
|
@@ -440,10 +464,12 @@ def main(args=None): | |
| symlinks=options.symlinks, | ||
| upgrade=options.upgrade, | ||
| with_pip=options.with_pip, | ||
| prompt=options.prompt) | ||
| prompt=options.prompt, | ||
| upgrade_deps=options.upgrade_deps) | ||
| for d in options.dirs: | ||
| builder.create(d) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| rc = 1 | ||
| try: | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.