cpython: c3c188a0325a (original) (raw)
Mercurial > cpython
changeset 79667:c3c188a0325a
Closes #15776: pyvenv now works with existing directories. [#15776]
Vinay Sajip <vinay_sajip@yahoo.co.uk> | |
---|---|
date | Thu, 11 Oct 2012 17:22:45 +0100 |
parents | ad51ed93377c |
children | 2802b26c64a9 |
files | Doc/library/venv.rst Lib/test/test_venv.py Lib/venv/__init__.py |
diffstat | 3 files changed, 74 insertions(+), 13 deletions(-)[+] [-] Doc/library/venv.rst 5 Lib/test/test_venv.py 59 Lib/venv/__init__.py 23 |
line wrap: on
line diff
--- a/Doc/library/venv.rst
+++ b/Doc/library/venv.rst
@@ -75,8 +75,8 @@ creation according to their needs, the :
* system_site_packages
-- a Boolean value indicating that the system Python
site-packages should be available to the environment (defaults to False
).
directory instead of raising an exception (defaults to ``False``).[](#l1.8)
any existing target directory, before creating the environment.[](#l1.10)
* symlinks
-- a Boolean value indicating whether to attempt to symlink the
Python binary (and any necessary DLLs or other binaries,
@@ -88,7 +88,6 @@ creation according to their needs, the :
upgraded in-place (defaults to False
).
-
Creators of third-party virtual environment tools will be free to use the
provided EnvBuilder
class as a base class.
--- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -113,13 +113,68 @@ class BasicTest(BaseTest): out, err = p.communicate() self.assertEqual(out.strip(), expected.encode())
- if sys.platform == 'win32':
ENV_SUBDIRS = ([](#l2.8)
('Scripts',),[](#l2.9)
('Include',),[](#l2.10)
('Lib',),[](#l2.11)
('Lib', 'site-packages'),[](#l2.12)
)[](#l2.13)
- else:
ENV_SUBDIRS = ([](#l2.15)
('bin',),[](#l2.16)
('include',),[](#l2.17)
('lib',),[](#l2.18)
('lib', 'python%d.%d' % sys.version_info[:2]),[](#l2.19)
('lib', 'python%d.%d' % sys.version_info[:2], 'site-packages'),[](#l2.20)
)[](#l2.21)
- def create_contents(self, paths, filename):
"""[](#l2.24)
Create some files in the environment which are unrelated[](#l2.25)
to the virtual environment.[](#l2.26)
"""[](#l2.27)
for subdirs in paths:[](#l2.28)
d = os.path.join(self.env_dir, *subdirs)[](#l2.29)
os.mkdir(d)[](#l2.30)
fn = os.path.join(d, filename)[](#l2.31)
with open(fn, 'wb') as f:[](#l2.32)
f.write(b'Still here?')[](#l2.33)
+ def test_overwrite_existing(self): """
Test control of overwriting an existing environment directory.[](#l2.37)
Test creating environment in an existing directory.[](#l2.38) """[](#l2.39)
self.assertRaises(ValueError, venv.create, self.env_dir)[](#l2.40)
self.create_contents(self.ENV_SUBDIRS, 'foo')[](#l2.41)
venv.create(self.env_dir)[](#l2.42)
for subdirs in self.ENV_SUBDIRS:[](#l2.43)
fn = os.path.join(self.env_dir, *(subdirs + ('foo',)))[](#l2.44)
self.assertTrue(os.path.exists(fn))[](#l2.45)
with open(fn, 'rb') as f:[](#l2.46)
self.assertEqual(f.read(), b'Still here?')[](#l2.47)
+ builder = venv.EnvBuilder(clear=True) builder.create(self.env_dir)
for subdirs in self.ENV_SUBDIRS:[](#l2.51)
fn = os.path.join(self.env_dir, *(subdirs + ('foo',)))[](#l2.52)
self.assertFalse(os.path.exists(fn))[](#l2.53)
- def clear_directory(self, path):
for fn in os.listdir(path):[](#l2.56)
fn = os.path.join(path, fn)[](#l2.57)
if os.path.islink(fn) or os.path.isfile(fn):[](#l2.58)
os.remove(fn)[](#l2.59)
elif os.path.isdir(fn):[](#l2.60)
shutil.rmtree(fn)[](#l2.61)
- def test_unoverwritable_fails(self):
#create a file clashing with directories in the env dir[](#l2.64)
for paths in self.ENV_SUBDIRS[:3]:[](#l2.65)
fn = os.path.join(self.env_dir, *paths)[](#l2.66)
with open(fn, 'wb') as f:[](#l2.67)
f.write(b'')[](#l2.68)
self.assertRaises((ValueError, OSError), venv.create, self.env_dir)[](#l2.69)
self.clear_directory(self.env_dir)[](#l2.70)
--- a/Lib/venv/init.py +++ b/Lib/venv/init.py @@ -90,6 +90,14 @@ class EnvBuilder: self.setup_scripts(context) self.post_setup(context)
- def clear_directory(self, path):
for fn in os.listdir(path):[](#l3.8)
fn = os.path.join(path, fn)[](#l3.9)
if os.path.islink(fn) or os.path.isfile(fn):[](#l3.10)
os.remove(fn)[](#l3.11)
elif os.path.isdir(fn):[](#l3.12)
shutil.rmtree(fn)[](#l3.13)
+ def ensure_directories(self, env_dir): """ Create the directories for the environment. @@ -101,11 +109,11 @@ class EnvBuilder: def create_if_needed(d): if not os.path.exists(d): os.makedirs(d)
elif os.path.islink(d) or os.path.isfile(d):[](#l3.22)
raise ValueError('Unable to create directory %r' % d)[](#l3.23)
if os.path.exists(env_dir) and not (self.clear or self.upgrade):[](#l3.25)
raise ValueError('Directory exists: %s' % env_dir)[](#l3.26) if os.path.exists(env_dir) and self.clear:[](#l3.27)
shutil.rmtree(env_dir)[](#l3.28)
self.clear_directory(env_dir)[](#l3.29) context = Context()[](#l3.30) context.env_dir = env_dir[](#l3.31) context.env_name = os.path.split(env_dir)[1][](#l3.32)
@@ -369,11 +377,10 @@ def main(args=None): 'when symlinks are not the default for ' 'the platform.') parser.add_argument('--clear', default=False, action='store_true',
dest='clear', help='Delete the environment '[](#l3.37)
'directory if it already '[](#l3.38)
'exists. If not specified and '[](#l3.39)
'the directory exists, an error'[](#l3.40)
' is raised.')[](#l3.41)
dest='clear', help='Delete the contents of the '[](#l3.42)
'environment directory if it '[](#l3.43)
'already exists, before '[](#l3.44)
'environment creation.')[](#l3.45) parser.add_argument('--upgrade', default=False, action='store_true',[](#l3.46) dest='upgrade', help='Upgrade the environment '[](#l3.47) 'directory to use this version '[](#l3.48)