Skip to content

Instantly share code, notes, and snippets.

@stefanschmidt
Created April 22, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanschmidt/4b71dd0730c40c0e3b50c6c05234b580 to your computer and use it in GitHub Desktop.
Save stefanschmidt/4b71dd0730c40c0e3b50c6c05234b580 to your computer and use it in GitHub Desktop.
Clean Python build directory
import os
import glob
import shutil
from io import open
from setuptools import Command, setup
# Slightly modified and extended version of this approach:
# https://github.com/pypa/setuptools/issues/1347#issuecomment-387802255
class CleanCommand(Command):
"""Custom clean command to tidy up the project root."""
CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info'.split(' ')
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
here = os.path.dirname(os.path.realpath(__file__))
for path_spec in self.CLEAN_FILES:
# Make paths absolute and relative to this path
abs_paths = glob.glob(os.path.normpath(os.path.join(here, path_spec)))
for path in [str(p) for p in abs_paths]:
if not path.startswith(here):
# Die if path in CLEAN_FILES is absolute + outside this directory
raise ValueError("%s is not a path inside %s" % (path, here))
print('removing %s' % os.path.relpath(path))
shutil.rmtree(path)
setup(
cmdclass={"clean": CleanCommand},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment