33
44Copyright (C) 20011-2012 Vinay Sajip. All Rights Reserved.
55
6- usage: python -m venv [-h] [--no-distribute ] [--system-site-packages ]
7- [--symlinks] [--clear] [-- upgrade]
6+ usage: python -m venv [-h] [--system-site-packages ] [--symlinks] [--clear ]
7+ [--upgrade]
88 ENV_DIR [ENV_DIR ...]
99
1010Creates virtual Python environments in one or more target directories.
1414
1515optional arguments:
1616 -h, --help show this help message and exit
17- --no-distribute Don't install Distribute in the virtual environment.*
1817 --system-site-packages
1918 Give the virtual environment access to the system
2019 site-packages dir.
2423 raised.
2524 --upgrade Upgrade the environment directory to use this version
2625 of Python, assuming Python has been upgraded in-place.
27-
28- *Note: Distribute support will be available during the alpha phase to
29- facilitate testing third-party packages with venvs created using this package.
30- This support will be removed after the alpha phase.
3126"""
3227import base64
3328import io
3631import os .path
3732import shutil
3833import sys
34+ import sysconfig
3935try :
4036 import threading
4137except ImportError :
@@ -88,11 +84,11 @@ def create(self, env_dir):
8884 """
8985 if (self .symlinks and
9086 sys .platform == 'darwin' and
91- 'Library/Framework' in sys . base_prefix ):
87+ sysconfig . get_config_var ( 'PYTHONFRAMEWORK' ) ):
9288 # Symlinking the stub executable in an OSX framework build will
9389 # result in a broken virtual environment.
9490 raise ValueError (
95- " Symlinking is not supported on OSX framework Python." )
91+ ' Symlinking is not supported on OSX framework Python.' )
9692 env_dir = os .path .abspath (env_dir )
9793 context = self .ensure_directories (env_dir )
9894 self .create_configuration (context )
@@ -206,9 +202,10 @@ def setup_python(self, context):
206202 if os .name != 'nt' :
207203 if not os .path .islink (path ):
208204 os .chmod (path , 0o755 )
209- path = os .path .join (binpath , 'python' )
210- if not os .path .exists (path ):
211- os .symlink (exename , path )
205+ for suffix in ('python' , 'python3' ):
206+ path = os .path .join (binpath , suffix )
207+ if not os .path .exists (path ):
208+ os .symlink (exename , path )
212209 else :
213210 subdir = 'DLLs'
214211 include = self .include_binary
@@ -322,104 +319,6 @@ def install_scripts(self, context, path):
322319 os .chmod (dstfile , 0o755 )
323320
324321
325- # This class will not be included in Python core; it's here for now to
326- # facilitate experimentation and testing, and as proof-of-concept of what could
327- # be done by external extension tools.
328- class DistributeEnvBuilder (EnvBuilder ):
329- """
330- By default, this builder installs Distribute so that you can pip or
331- easy_install other packages into the created environment.
332-
333- :param nodist: If True, Distribute is not installed into the created
334- environment.
335- :param progress: If Distribute is installed, the progress of the
336- installation can be monitored by passing a progress
337- callable. If specified, it is called with two
338- arguments: a string indicating some progress, and a
339- context indicating where the string is coming from.
340- The context argument can have one of three values:
341- 'main', indicating that it is called from virtualize()
342- itself, and 'stdout' and 'stderr', which are obtained
343- by reading lines from the output streams of a subprocess
344- which is used to install Distribute.
345-
346- If a callable is not specified, default progress
347- information is output to sys.stderr.
348- """
349-
350- def __init__ (self , * args , ** kwargs ):
351- self .nodist = kwargs .pop ("nodist" , False )
352- self .progress = kwargs .pop ("progress" , None )
353- super ().__init__ (* args , ** kwargs )
354-
355- def post_setup (self , context ):
356- """
357- Set up any packages which need to be pre-installed into the
358- environment being created.
359-
360- :param context: The information for the environment creation request
361- being processed.
362- """
363- if not self .nodist :
364- if threading :
365- self .install_distribute (context )
366-
367- def reader (self , stream , context ):
368- """
369- Read lines from a subprocess' output stream and either pass to a progress
370- callable (if specified) or write progress information to sys.stderr.
371- """
372- progress = self .progress
373- while True :
374- s = stream .readline ()
375- if not s :
376- break
377- if progress is not None :
378- progress (s , context )
379- else :
380- sys .stderr .write ('.' )
381- #sys.stderr.write(s.decode('utf-8'))
382- sys .stderr .flush ()
383- stream .close ()
384-
385- def install_distribute (self , context ):
386- """
387- Install Distribute in the environment.
388-
389- :param context: The information for the environment creation request
390- being processed.
391- """
392- from subprocess import Popen , PIPE
393- from urllib .request import urlretrieve
394-
395- url = 'http://python-distribute.org/distribute_setup.py'
396- binpath = context .bin_path
397- distpath = os .path .join (binpath , 'distribute_setup.py' )
398- # Download Distribute in the env
399- urlretrieve (url , distpath )
400- progress = self .progress
401- if progress is not None :
402- progress ('Installing distribute' , 'main' )
403- else :
404- sys .stderr .write ('Installing distribute ' )
405- sys .stderr .flush ()
406- # Install Distribute in the env
407- args = [context .env_exe , 'distribute_setup.py' ]
408- p = Popen (args , stdout = PIPE , stderr = PIPE , cwd = binpath )
409- t1 = threading .Thread (target = self .reader , args = (p .stdout , 'stdout' ))
410- t1 .start ()
411- t2 = threading .Thread (target = self .reader , args = (p .stderr , 'stderr' ))
412- t2 .start ()
413- p .wait ()
414- t1 .join ()
415- t2 .join ()
416- if progress is not None :
417- progress ('done.' , 'main' )
418- else :
419- sys .stderr .write ('done.\n ' )
420- # Clean up - no longer needed
421- os .unlink (distpath )
422-
423322def create (env_dir , system_site_packages = False , clear = False , symlinks = False ):
424323 """
425324 Create a virtual environment in a directory.
@@ -436,8 +335,7 @@ def create(env_dir, system_site_packages=False, clear=False, symlinks=False):
436335 :param symlinks: If True, attempt to symlink rather than copy files into
437336 virtual environment.
438337 """
439- # XXX This should be changed to EnvBuilder.
440- builder = DistributeEnvBuilder (system_site_packages = system_site_packages ,
338+ builder = EnvBuilder (system_site_packages = system_site_packages ,
441339 clear = clear , symlinks = symlinks )
442340 builder .create (env_dir )
443341
@@ -460,17 +358,12 @@ def main(args=None):
460358 'directories.' )
461359 parser .add_argument ('dirs' , metavar = 'ENV_DIR' , nargs = '+' ,
462360 help = 'A directory to create the environment in.' )
463- # XXX This option will be removed.
464- parser .add_argument ('--no-distribute' , default = False ,
465- action = 'store_true' , dest = 'nodist' ,
466- help = "Don't install Distribute in the virtual "
467- "environment." )
468361 parser .add_argument ('--system-site-packages' , default = False ,
469362 action = 'store_true' , dest = 'system_site' ,
470- help = " Give the virtual environment access to the "
471- " system site-packages dir. " )
363+ help = ' Give the virtual environment access to the '
364+ ' system site-packages dir.' )
472365 if os .name == 'nt' or (sys .platform == 'darwin' and
473- 'Library/Framework' in sys . base_prefix ):
366+ sysconfig . get_config_var ( 'PYTHONFRAMEWORK' ) ):
474367 use_symlinks = False
475368 else :
476369 use_symlinks = True
@@ -491,12 +384,9 @@ def main(args=None):
491384 options = parser .parse_args (args )
492385 if options .upgrade and options .clear :
493386 raise ValueError ('you cannot supply --upgrade and --clear together.' )
494- # XXX This will be changed to EnvBuilder
495- builder = DistributeEnvBuilder (system_site_packages = options .system_site ,
496- clear = options .clear ,
497- symlinks = options .symlinks ,
498- upgrade = options .upgrade ,
499- nodist = options .nodist )
387+ builder = EnvBuilder (system_site_packages = options .system_site ,
388+ clear = options .clear , symlinks = options .symlinks ,
389+ upgrade = options .upgrade )
500390 for d in options .dirs :
501391 builder .create (d )
502392
0 commit comments