Skip to content

Commit cd9337b

Browse files
committed
Merge pull request #2 from dave-shawley/add-egg-support
Add egg support
2 parents 1aa4eb1 + c5ee0a4 commit cd9337b

File tree

5 files changed

+118
-30
lines changed

5 files changed

+118
-30
lines changed

HISTORY

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

4-
* 0.0.1
4+
* 0.0.2 (17-Nov-2014)
5+
6+
- Support removal of .egg-info and .egg directories.
7+
- Add support for *--dry-run*
8+
9+
* 0.0.1 (16-Nov-2014)
510

611
- Support removal of distribution directories.

README.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ few new command line parameters.
6767
``setup.py clean --dist``
6868
Removes directories that the various *dist* commands produce.
6969

70+
``setup.py clean --egg``
71+
Removes *.egg* and *.egg-info* directories.
72+
7073
Where can I get this extension from?
7174
------------------------------------
7275
+---------------+-----------------------------------------------------+
@@ -85,11 +88,11 @@ Where can I get this extension from?
8588
#extending-and-reusing-setuptools
8689
.. _setuptools: https://pythonhosted.org/setuptools/
8790

88-
.. |Version| image:: https://badge.fury.io/py/setupext-janitor.svg
91+
.. |Version| image:: https://badge.fury.io/py/setupext-janitor.svg?
8992
:target: https://badge.fury.io/
9093
.. |Downloads| image:: https://pypip.in/d/setupext-janitor/badge.svg?
9194
:target: https://pypi.python.org/pypi/setupext-janitor
9295
.. |Status| image:: https://travis-ci.org/dave-shawley/setupext-janitor.svg
9396
:target: https://travis-ci.org/dave-shawley/setupext-janitor
94-
.. |License| image:: https://pypip.in/license/dave-shawley/badge.svg?
95-
:target: https://setupext-dave-shawley.readthedocs.org/
97+
.. |License| image:: https://pypip.in/license/setupext-janitor/badge.svg?
98+
:target: https://setupext-janitor.readthedocs.org/

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@
4444
'clean = setupext.janitor:CleanCommand',
4545
],
4646
},
47+
cmdclass={
48+
'clean': janitor.CleanCommand,
49+
},
4750
)

setupext/janitor/__init__.py

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from distutils import log
1+
from distutils import dir_util, errors
22
from distutils.command.clean import clean as _CleanCommand
3-
import shutil
3+
import os.path
44

55

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

99

@@ -36,30 +36,49 @@ class CleanCommand(_CleanCommand):
3636

3737
# See _set_options for `user_options`
3838

39-
def __init__(self, *args, **kwargs):
40-
_CleanCommand.__init__(self, *args, **kwargs)
41-
self.dist = None
42-
4339
def initialize_options(self):
4440
_CleanCommand.initialize_options(self)
4541
self.dist = False
42+
self.eggs = False
43+
self.egg_base = None
44+
45+
def finalize_options(self):
46+
_CleanCommand.finalize_options(self)
47+
try:
48+
self.set_undefined_options(
49+
'egg_info', ('egg_base', 'egg_base'))
50+
except errors.DistutilsError:
51+
pass
52+
53+
if self.egg_base is None:
54+
self.egg_base = os.curdir
4655

4756
def run(self):
4857
_CleanCommand.run(self)
49-
if not self.dist:
50-
return
5158

52-
dist_dirs = set()
53-
for cmd_name in self.distribution.commands:
54-
if 'dist' in cmd_name:
55-
command = self.distribution.get_command_obj(cmd_name)
56-
command.ensure_finalized()
57-
if getattr(command, 'dist_dir', None):
58-
dist_dirs.add(command.dist_dir)
59-
60-
for dir_name in dist_dirs:
61-
self.announce('removing {0}'.format(dir_name), level=log.DEBUG)
62-
shutil.rmtree(dir_name, ignore_errors=True)
59+
dir_names = set()
60+
if self.dist:
61+
for cmd_name, _ in self.distribution.get_command_list():
62+
if 'dist' in cmd_name:
63+
command = self.distribution.get_command_obj(cmd_name)
64+
command.ensure_finalized()
65+
if getattr(command, 'dist_dir', None):
66+
dir_names.add(command.dist_dir)
67+
68+
if self.eggs:
69+
for name in os.listdir(self.egg_base):
70+
if name.endswith('.egg-info'):
71+
dir_names.add(os.path.join(self.egg_base, name))
72+
for name in os.listdir(os.curdir):
73+
if name.endswith('.egg'):
74+
dir_names.add(name)
75+
76+
for dir_name in dir_names:
77+
if os.path.exists(dir_name):
78+
dir_util.remove_tree(dir_name, dry_run=self.dry_run)
79+
else:
80+
self.announce(
81+
'skipping {0} since it does not exist'.format(dir_name))
6382

6483

6584
def _set_options():
@@ -81,8 +100,13 @@ def _set_options():
81100
CleanCommand.user_options = _CleanCommand.user_options[:]
82101
CleanCommand.user_options.extend([
83102
('dist', 'd', 'remove distribution directory'),
103+
('eggs', None, 'remove egg and egg-info directories'),
104+
105+
('egg-base=', 'e',
106+
'directory containing .egg-info directories '
107+
'(default: top of the source tree)'),
84108
])
85109
CleanCommand.boolean_options = _CleanCommand.boolean_options[:]
86-
CleanCommand.boolean_options.append('dist')
110+
CleanCommand.boolean_options.extend(['dist', 'eggs'])
87111

88112
_set_options()

tests.py

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import shutil
66
import sys
77
import tempfile
8+
import uuid
89

910
if sys.version_info >= (2, 7):
1011
import unittest
@@ -71,26 +72,31 @@ def test_that_janitor_defines_dist_command(self):
7172
janitor.CleanCommand.user_options)
7273

7374

74-
class DirectoryCleanupTests(unittest.TestCase):
75-
temp_dir = tempfile.mkdtemp()
75+
class DirectoryCleanupMixin(object):
7676

7777
@classmethod
7878
def setUpClass(cls):
79-
super(DirectoryCleanupTests, cls).setUpClass()
79+
super(DirectoryCleanupMixin, cls).setUpClass()
80+
cls.temp_dir = tempfile.mkdtemp()
8081
atexit.register(shutil.rmtree, cls.temp_dir)
8182

8283
@classmethod
8384
def create_directory(cls, dir_name):
8485
return tempfile.mkdtemp(dir=cls.temp_dir, prefix=dir_name)
8586

86-
def assert_path_does_not_exist(self, full_path):
87+
def assert_path_does_not_exist(self, *trailing_segments):
88+
full_path = os.path.join(*trailing_segments)
8789
if os.path.exists(full_path):
8890
raise AssertionError('{0} should not exist'.format(full_path))
8991

90-
def assert_path_exists(self, full_path):
92+
def assert_path_exists(self, *trailing_segments):
93+
full_path = os.path.join(*trailing_segments)
9194
if not os.path.exists(full_path):
9295
raise AssertionError('{0} should exist'.format(full_path))
9396

97+
98+
class DistDirectoryCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
99+
94100
def test_that_dist_directory_is_removed_for_sdist(self):
95101
dist_dir = self.create_directory('dist-dir')
96102
run_setup(
@@ -128,3 +134,50 @@ def test_that_directories_are_not_removed_without_parameter(self):
128134
)
129135
self.assert_path_exists(sdist_dir)
130136
self.assert_path_exists(bdist_dir)
137+
138+
def test_that_directories_are_not_removed_in_dry_run_mode(self):
139+
sdist_dir = self.create_directory('sdist-dir')
140+
run_setup(
141+
'sdist', '--dist-dir={0}'.format(sdist_dir),
142+
'clean', '--dist', '--dry-run'
143+
)
144+
self.assert_path_exists(sdist_dir)
145+
146+
147+
class EggDirectoryCleanupTests(DirectoryCleanupMixin, unittest.TestCase):
148+
149+
def test_that_egg_info_directories_are_removed(self):
150+
egg_root = self.create_directory('egg-info-root')
151+
os.mkdir(os.path.join(egg_root, 'bah.egg-info'))
152+
os.mkdir(os.path.join(egg_root, 'foo.egg-info'))
153+
run_setup('clean', '--egg-base={0}'.format(egg_root), '--eggs')
154+
self.assert_path_exists(egg_root)
155+
self.assert_path_does_not_exist(egg_root, 'bah.egg-info')
156+
self.assert_path_does_not_exist(egg_root, 'foo.egg-info')
157+
158+
def test_that_egg_directories_are_removed(self):
159+
dir_name = uuid.uuid4().hex + '.egg'
160+
os.mkdir(dir_name)
161+
try:
162+
run_setup('clean', '--eggs')
163+
self.assert_path_does_not_exist(dir_name)
164+
except:
165+
os.rmdir(dir_name)
166+
raise
167+
168+
def test_that_directories_are_not_removed_in_dry_run_mode(self):
169+
egg_root = self.create_directory('egg-info-root')
170+
os.mkdir(os.path.join(egg_root, 'package.egg-info'))
171+
installed_egg = uuid.uuid4().hex + '.egg'
172+
os.mkdir(installed_egg)
173+
try:
174+
run_setup(
175+
'clean', '--egg-base={0}'.format(egg_root), '--eggs',
176+
'--dry-run',
177+
)
178+
self.assert_path_exists(installed_egg)
179+
self.assert_path_exists(egg_root, 'package.egg-info')
180+
except:
181+
os.rmdir(installed_egg)
182+
raise
183+
os.rmdir(installed_egg)

0 commit comments

Comments
 (0)