Skip to content
Permalink
Browse files

Added removal of pyc files (and their parent folders if they are empt…

…y after that) to the python setup.py clean command
  • Loading branch information...
foosel committed Nov 4, 2014
1 parent ff96e2e commit 9e014eba1feffde11ed0601d9c911b8cac9f3fb0
Showing with 43 additions and 0 deletions.
  1. +43 −0 setup.py
@@ -33,6 +33,26 @@ def package_data_dirs(source, sub_folders):
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):
description = "clean build artifacts"
user_options = []
@@ -45,14 +65,37 @@ def finalize_options(self):
pass

def run(self):
# build folder
if os.path.exists('build'):
print "Deleting build directory"
shutil.rmtree('build')

# eggs
eggs = glob.glob('OctoPrint*.egg-info')
for egg in eggs:
print "Deleting %s directory" % 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):
description = "create a new translation"

0 comments on commit 9e014eb

Please sign in to comment.
You can’t perform that action at this time.