Skip to content

Commit cd2d7fa

Browse files
committed
Add support for recursive pycache removal.
1 parent 73f6f0d commit cd2d7fa

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

HISTORY

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changelog
44
* 0.0.3
55

66
- Support removal of virtual environment directories.
7+
- Support recursive removal of *__pycache__* directories.
78

89
* 0.0.2 (17-Nov-2014)
910

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ 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+
7881
Where can I get this extension from?
7982
------------------------------------
8083
+---------------+-----------------------------------------------------+

setupext/janitor.py

Lines changed: 9 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):
@@ -81,6 +82,11 @@ def run(self):
8182
if self.environment and self.virtualenv_dir:
8283
dir_names.add(self.virtualenv_dir)
8384

85+
if self.pycache:
86+
for root, dirs, _ in os.walk(os.curdir):
87+
if '__pycache__' in dirs:
88+
dir_names.add(os.path.join(root, '__pycache__'))
89+
8490
for dir_name in dir_names:
8591
if os.path.exists(dir_name):
8692
dir_util.remove_tree(dir_name, dry_run=self.dry_run)
@@ -110,6 +116,7 @@ def _set_options():
110116
('dist', 'd', 'remove distribution directory'),
111117
('eggs', None, 'remove egg and egg-info directories'),
112118
('environment', 'E', 'remove virtual environment directory'),
119+
('pycache', 'p', 'remove __pycache__ directories'),
113120

114121
('egg-base=', 'e',
115122
'directory containing .egg-info directories '
@@ -119,6 +126,7 @@ def _set_options():
119126
'(default: value of VIRTUAL_ENV environment variable)'),
120127
])
121128
CleanCommand.boolean_options = _CleanCommand.boolean_options[:]
122-
CleanCommand.boolean_options.extend(['dist', 'eggs', 'environment'])
129+
CleanCommand.boolean_options.extend(
130+
['dist', 'eggs', 'environment', 'pycache'])
123131

124132
_set_options()

tests.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def test_that_directories_are_not_removed_in_dry_run_mode(self):
184184

185185

186186
class VirtualEnvironmentCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
187+
187188
def setUp(self):
188189
super(VirtualEnvironmentCleanupTests, self).setUp()
189190
self._saved_venv = os.environ.get('VIRTUAL_ENV', None)
@@ -216,3 +217,41 @@ def test_that_janitor_does_not_fail_when_envdir_is_missing(self):
216217
def test_that_janitor_does_not_fail_when_no_dir_specified(self):
217218
os.environ.pop('VIRTUAL_ENV', None)
218219
run_setup('clean', '--environment')
220+
221+
222+
class PycacheCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
223+
224+
def setUp(self):
225+
super(PycacheCleanupTests, self).setUp()
226+
self.test_root = self.create_directory('test-root')
227+
starting_dir = os.curdir
228+
self.addCleanup(os.chdir, starting_dir)
229+
os.chdir(self.test_root)
230+
231+
def mkdirs(self, *dirs):
232+
for d in dirs:
233+
os.makedirs(d)
234+
return dirs[:]
235+
236+
def test_that_pycache_directories_are_removed(self):
237+
all_dirs = self.mkdirs(
238+
os.path.join(self.test_root, '__pycache__'),
239+
os.path.join(self.test_root, 'a', '__pycache__'),
240+
os.path.join(self.test_root, 'a', 'b', '__pycache__'),
241+
os.path.join(self.test_root, 'b', '__pycache__'),
242+
)
243+
244+
run_setup('clean', '--pycache')
245+
for cache_dir in all_dirs:
246+
self.assert_path_does_not_exist(cache_dir)
247+
248+
def test_that_janitor_does_not_fail_when_cache_parent_is_removed(self):
249+
all_dirs = self.mkdirs(
250+
os.path.join(self.test_root, 'dist'),
251+
os.path.join(self.test_root, 'dist', '__pycache__', '__pycache__'),
252+
os.path.join(self.test_root, 'dist', 'foo', '__pycache__'),
253+
)
254+
255+
run_setup('clean', '--dist', '--pycache')
256+
for cache_dir in all_dirs:
257+
self.assert_path_does_not_exist(cache_dir)

0 commit comments

Comments
 (0)