From 24e7b2f13ac45bd67a084343625954bb6d68636b Mon Sep 17 00:00:00 2001 From: Peter Bittner Date: Sun, 15 May 2016 19:18:27 +0200 Subject: [PATCH] Add clean command to setup.py --- README.rst | 6 ++++++ setup.py | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index db1d93d..1073910 100644 --- a/README.rst +++ b/README.rst @@ -87,6 +87,12 @@ virtualenv, which for the moment you still may have to install manually: pip install "virtualenv<14.0.0" # <14.0.0 needed for Python 3.2 only +You can use the ``clean`` command to remove build and test files and folders: + +.. code-block:: bash + + python setup.py clean + .. _pull request: https://github.com/k-bx/python-semver/pulls .. _py.test: http://pytest.org/ diff --git a/setup.py b/setup.py index ee3d426..ea363bc 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,12 @@ #!/usr/bin/env python import semver as package +from glob import glob +from os import remove +from os.path import dirname, join from setuptools import setup from setuptools.command.test import test as TestCommand from shlex import split +from shutil import rmtree class Tox(TestCommand): @@ -26,8 +30,43 @@ class Tox(TestCommand): exit(errno) +class Clean(TestCommand): + def run(self): + delete_in_root = [ + 'build', + '.cache', + 'dist', + '.eggs', + '*.egg-info', + '.tox', + ] + delete_everywhere = [ + '__pycache__', + '*.pyc', + ] + for candidate in delete_in_root: + rmtree_glob(candidate) + for visible_dir in glob('[A-Za-z0-9]*'): + for candidate in delete_everywhere: + rmtree_glob(join(visible_dir, candidate)) + rmtree_glob(join(visible_dir, '*', candidate)) + + +def rmtree_glob(file_glob): + for fobj in glob(file_glob): + try: + rmtree(fobj) + print('%s/ removed ...' % fobj) + except OSError: + try: + remove(fobj) + print('%s removed ...' % fobj) + except OSError: + pass + + def read_file(filename): - with open(filename) as f: + with open(join(dirname(__file__), filename)) as f: return f.read() setup( @@ -60,6 +99,7 @@ setup( ], tests_require=['tox', 'virtualenv<14.0.0'], cmdclass={ + 'clean': Clean, 'test': Tox, }, )