Added removal of pyc files (and their parent folders if they are empty after that) to the python setup.py clean command

This commit is contained in:
Gina Häußge 2014-11-04 10:36:35 +01:00
parent ff96e2e4d6
commit 9e014eba1f

View file

@ -33,6 +33,26 @@ def package_data_dirs(source, sub_folders):
return dirs return dirs
def _recursively_handle_files(directory, file_matcher, folder_handler=None, file_handler=None):
applied_handler = False
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
if file_handler is not None and file_matcher(filename):
file_handler(path)
applied_handler = True
elif os.path.isdir(path):
sub_applied_handler = _recursively_handle_files(path, file_matcher, folder_handler=folder_handler, file_handler=file_handler)
if sub_applied_handler:
applied_handler = True
if folder_handler is not None:
folder_handler(path, sub_applied_handler)
return applied_handler
class CleanCommand(Command): class CleanCommand(Command):
description = "clean build artifacts" description = "clean build artifacts"
user_options = [] user_options = []
@ -45,14 +65,37 @@ class CleanCommand(Command):
pass pass
def run(self): def run(self):
# build folder
if os.path.exists('build'): if os.path.exists('build'):
print "Deleting build directory" print "Deleting build directory"
shutil.rmtree('build') shutil.rmtree('build')
# eggs
eggs = glob.glob('OctoPrint*.egg-info') eggs = glob.glob('OctoPrint*.egg-info')
for egg in eggs: for egg in eggs:
print "Deleting %s directory" % egg print "Deleting %s directory" % egg
shutil.rmtree(egg) shutil.rmtree(egg)
# pyc files
def delete_folder_if_empty(path, applied_handler):
if not applied_handler:
return
if len(os.listdir(path)) == 0:
shutil.rmtree(path)
print "Deleted %s since it was empty" % path
def delete_file(path):
os.remove(path)
print "Deleted %s" % path
import fnmatch
_recursively_handle_files(
os.path.abspath("src"),
lambda name: fnmatch.fnmatch(name.lower(), "*.pyc"),
folder_handler=delete_folder_if_empty,
file_handler=delete_file
)
class NewTranslation(Command): class NewTranslation(Command):
description = "create a new translation" description = "create a new translation"