Skip to content

Commit c30e906

Browse files
committed
Merge pull request #4 from dave-shawley/finish-implementation
Finish implementation
2 parents 73f6f0d + f23eb22 commit c30e906

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

HISTORY

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changelog
44
* 0.0.3
55

66
- Support removal of virtual environment directories.
7+
- Support recursive removal of *__pycache__* directories.
8+
- Add support for *--all*.
79

810
* 0.0.2 (17-Nov-2014)
911

README.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ few new command line parameters.
7575
``$VIRTUAL_ENV`` environment variable. The name of the directory can
7676
also be specified using the ``--virtualenv-dir`` command line option.
7777

78+
``setup.py clean --pycache``
79+
Recursively removes directories named *__pycache__*.
80+
81+
``setup.py clean --all``
82+
Remove all of by-products. This is the same as using ``--dist --egg
83+
--environment --pycache``.
84+
7885
Where can I get this extension from?
7986
------------------------------------
8087
+---------------+-----------------------------------------------------+

setupext/janitor.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def initialize_options(self):
4242
self.eggs = False
4343
self.egg_base = None
4444
self.environment = False
45+
self.pycache = False
4546
self.virtualenv_dir = None
4647

4748
def finalize_options(self):
@@ -55,6 +56,10 @@ def finalize_options(self):
5556
if self.egg_base is None:
5657
self.egg_base = os.curdir
5758

59+
if self.all:
60+
for flag in self.boolean_options:
61+
setattr(self, flag, True)
62+
5863
if self.environment and self.virtualenv_dir is None:
5964
self.virtualenv_dir = os.environ.get('VIRTUAL_ENV', None)
6065

@@ -81,6 +86,11 @@ def run(self):
8186
if self.environment and self.virtualenv_dir:
8287
dir_names.add(self.virtualenv_dir)
8388

89+
if self.pycache:
90+
for root, dirs, _ in os.walk(os.curdir):
91+
if '__pycache__' in dirs:
92+
dir_names.add(os.path.join(root, '__pycache__'))
93+
8494
for dir_name in dir_names:
8595
if os.path.exists(dir_name):
8696
dir_util.remove_tree(dir_name, dry_run=self.dry_run)
@@ -110,6 +120,7 @@ def _set_options():
110120
('dist', 'd', 'remove distribution directory'),
111121
('eggs', None, 'remove egg and egg-info directories'),
112122
('environment', 'E', 'remove virtual environment directory'),
123+
('pycache', 'p', 'remove __pycache__ directories'),
113124

114125
('egg-base=', 'e',
115126
'directory containing .egg-info directories '
@@ -119,6 +130,7 @@ def _set_options():
119130
'(default: value of VIRTUAL_ENV environment variable)'),
120131
])
121132
CleanCommand.boolean_options = _CleanCommand.boolean_options[:]
122-
CleanCommand.boolean_options.extend(['dist', 'eggs', 'environment'])
133+
CleanCommand.boolean_options.extend(
134+
['dist', 'eggs', 'environment', 'pycache'])
123135

124136
_set_options()

tests.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def setUpClass(cls):
8484
def create_directory(cls, dir_name):
8585
return tempfile.mkdtemp(dir=cls.temp_dir, prefix=dir_name)
8686

87+
@staticmethod
88+
def mkdirs(*dirs):
89+
for d in dirs:
90+
os.makedirs(d)
91+
return dirs[:]
92+
8793
def assert_path_does_not_exist(self, *trailing_segments):
8894
full_path = os.path.join(*trailing_segments)
8995
if os.path.exists(full_path):
@@ -184,6 +190,7 @@ def test_that_directories_are_not_removed_in_dry_run_mode(self):
184190

185191

186192
class VirtualEnvironmentCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
193+
187194
def setUp(self):
188195
super(VirtualEnvironmentCleanupTests, self).setUp()
189196
self._saved_venv = os.environ.get('VIRTUAL_ENV', None)
@@ -216,3 +223,83 @@ def test_that_janitor_does_not_fail_when_envdir_is_missing(self):
216223
def test_that_janitor_does_not_fail_when_no_dir_specified(self):
217224
os.environ.pop('VIRTUAL_ENV', None)
218225
run_setup('clean', '--environment')
226+
227+
228+
class PycacheCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
229+
230+
def setUp(self):
231+
super(PycacheCleanupTests, self).setUp()
232+
self.test_root = self.create_directory('test-root')
233+
starting_dir = os.curdir
234+
self.addCleanup(os.chdir, starting_dir)
235+
os.chdir(self.test_root)
236+
237+
def test_that_pycache_directories_are_removed(self):
238+
all_dirs = self.mkdirs(
239+
os.path.join(self.test_root, '__pycache__'),
240+
os.path.join(self.test_root, 'a', '__pycache__'),
241+
os.path.join(self.test_root, 'a', 'b', '__pycache__'),
242+
os.path.join(self.test_root, 'b', '__pycache__'),
243+
)
244+
245+
run_setup('clean', '--pycache')
246+
for cache_dir in all_dirs:
247+
self.assert_path_does_not_exist(cache_dir)
248+
249+
def test_that_janitor_does_not_fail_when_cache_parent_is_removed(self):
250+
all_dirs = self.mkdirs(
251+
os.path.join(self.test_root, 'dist'),
252+
os.path.join(self.test_root, 'dist', '__pycache__', '__pycache__'),
253+
os.path.join(self.test_root, 'dist', 'foo', '__pycache__'),
254+
)
255+
256+
run_setup('clean', '--dist', '--pycache')
257+
for cache_dir in all_dirs:
258+
self.assert_path_does_not_exist(cache_dir)
259+
260+
261+
class RemoveAllTests(DirectoryCleanupMixin, unittest.TestCase):
262+
263+
@classmethod
264+
def setUpClass(cls):
265+
super(RemoveAllTests, cls).setUpClass()
266+
test_root = cls.create_directory('test-root')
267+
starting_dir = os.curdir
268+
saved_env_dir = os.environ.get('VIRTUAL_ENV', None)
269+
270+
def restore():
271+
os.chdir(starting_dir)
272+
os.environ.pop('VIRTUAL_ENV', None)
273+
if saved_env_dir is not None:
274+
os.environ['VIRTUAL_ENV'] = saved_env_dir
275+
276+
os.chdir(test_root)
277+
try:
278+
all_dirs = cls.mkdirs(
279+
'__pycache__',
280+
'dist',
281+
'foo.egg-info',
282+
'env',
283+
)
284+
cls.pycache_dir = all_dirs[0]
285+
cls.dist_dir = all_dirs[1]
286+
cls.egg_dir = all_dirs[2]
287+
cls.env_dir = all_dirs[3]
288+
os.environ['VIRTUAL_ENV'] = cls.env_dir
289+
run_setup('clean', '--all')
290+
restore()
291+
except:
292+
restore()
293+
raise
294+
295+
def test_that_pycache_is_removed(self):
296+
self.assert_path_does_not_exist(self.pycache_dir)
297+
298+
def test_that_distdir_is_removed(self):
299+
self.assert_path_does_not_exist(self.dist_dir)
300+
301+
def test_that_eggdir_is_removed(self):
302+
self.assert_path_does_not_exist(self.egg_dir)
303+
304+
def test_that_envdir_is_removed(self):
305+
self.assert_path_does_not_exist(self.env_dir)

0 commit comments

Comments
 (0)