Skip to content

Commit 73f6f0d

Browse files
committed
Merge pull request #3 from dave-shawley/add-venv-cleanup
Remove virtual environments
2 parents cd9337b + ac4dc0b commit 73f6f0d

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

HISTORY

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
=========
33

4+
* 0.0.3
5+
6+
- Support removal of virtual environment directories.
7+
48
* 0.0.2 (17-Nov-2014)
59

610
- Support removal of .egg-info and .egg directories.

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ few new command line parameters.
7070
``setup.py clean --egg``
7171
Removes *.egg* and *.egg-info* directories.
7272

73+
``setup.py clean --environment``
74+
Removes the currently active virtual environment as indicated by the
75+
``$VIRTUAL_ENV`` environment variable. The name of the directory can
76+
also be specified using the ``--virtualenv-dir`` command line option.
77+
7378
Where can I get this extension from?
7479
------------------------------------
7580
+---------------+-----------------------------------------------------+

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
'Operating System :: OS Independent',
3838
'Programming Language :: Python',
3939
'Framework :: Setuptools Plugin',
40-
'Development Status :: 1 - Planning',
40+
'Development Status :: 3 - Alpha',
4141
],
4242
entry_points={
4343
'distutils.commands': [
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os.path
44

55

6-
version_info = (0, 0, 2)
6+
version_info = (0, 0, 3)
77
__version__ = '.'.join(str(v) for v in version_info)
88

99

@@ -41,6 +41,8 @@ def initialize_options(self):
4141
self.dist = False
4242
self.eggs = False
4343
self.egg_base = None
44+
self.environment = False
45+
self.virtualenv_dir = None
4446

4547
def finalize_options(self):
4648
_CleanCommand.finalize_options(self)
@@ -53,6 +55,9 @@ def finalize_options(self):
5355
if self.egg_base is None:
5456
self.egg_base = os.curdir
5557

58+
if self.environment and self.virtualenv_dir is None:
59+
self.virtualenv_dir = os.environ.get('VIRTUAL_ENV', None)
60+
5661
def run(self):
5762
_CleanCommand.run(self)
5863

@@ -73,6 +78,9 @@ def run(self):
7378
if name.endswith('.egg'):
7479
dir_names.add(name)
7580

81+
if self.environment and self.virtualenv_dir:
82+
dir_names.add(self.virtualenv_dir)
83+
7684
for dir_name in dir_names:
7785
if os.path.exists(dir_name):
7886
dir_util.remove_tree(dir_name, dry_run=self.dry_run)
@@ -101,12 +109,16 @@ def _set_options():
101109
CleanCommand.user_options.extend([
102110
('dist', 'd', 'remove distribution directory'),
103111
('eggs', None, 'remove egg and egg-info directories'),
112+
('environment', 'E', 'remove virtual environment directory'),
104113

105114
('egg-base=', 'e',
106115
'directory containing .egg-info directories '
107116
'(default: top of the source tree)'),
117+
('virtualenv-dir=', None,
118+
'root directory for the virtual directory '
119+
'(default: value of VIRTUAL_ENV environment variable)'),
108120
])
109121
CleanCommand.boolean_options = _CleanCommand.boolean_options[:]
110-
CleanCommand.boolean_options.extend(['dist', 'eggs'])
122+
CleanCommand.boolean_options.extend(['dist', 'eggs', 'environment'])
111123

112124
_set_options()

tests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,38 @@ def test_that_directories_are_not_removed_in_dry_run_mode(self):
181181
os.rmdir(installed_egg)
182182
raise
183183
os.rmdir(installed_egg)
184+
185+
186+
class VirtualEnvironmentCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
187+
def setUp(self):
188+
super(VirtualEnvironmentCleanupTests, self).setUp()
189+
self._saved_venv = os.environ.get('VIRTUAL_ENV', None)
190+
191+
def tearDown(self):
192+
super(VirtualEnvironmentCleanupTests, self).tearDown()
193+
if self._saved_venv is not None:
194+
os.environ['VIRTUAL_ENV'] = self._saved_venv
195+
else:
196+
os.environ.pop('VIRTUAL_ENV', None)
197+
198+
def test_that_virtualenv_is_removed_when_dir_is_specified(self):
199+
venv_dir = self.create_directory('venv')
200+
run_setup(
201+
'clean', '--environment', '--virtualenv-dir={0}'.format(venv_dir))
202+
self.assert_path_does_not_exist(venv_dir)
203+
204+
def test_that_virtualenv_is_removed_based_on_envvar(self):
205+
venv_dir = self.create_directory('venv')
206+
os.environ['VIRTUAL_ENV'] = venv_dir
207+
run_setup('clean', '--environment')
208+
self.assert_path_does_not_exist(venv_dir)
209+
210+
def test_that_janitor_does_not_fail_when_envdir_is_missing(self):
211+
venv_dir = self.create_directory('venv')
212+
os.environ['VIRTUAL_ENV'] = venv_dir
213+
os.rmdir(venv_dir)
214+
run_setup('clean', '--environment')
215+
216+
def test_that_janitor_does_not_fail_when_no_dir_specified(self):
217+
os.environ.pop('VIRTUAL_ENV', None)
218+
run_setup('clean', '--environment')

0 commit comments

Comments
 (0)