cleanupSource.py

changeset 3597
137370f7114b
child 3613
47e29cc0f29c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cleanupSource.py	Tue May 20 19:26:52 2014 +0200
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2014 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Script for eric5 to clean up the source tree.
+"""
+
+from __future__ import unicode_literals
+from __future__ import print_function
+
+import os
+import sys
+import fnmatch
+import shutil
+
+
+def cleanupSource(dirName):
+    """
+    Cleanup the sources directory to get rid of leftover files
+    and directories.
+    
+    @param dirName name of the directory to prune (string)
+    """
+    # step 1: delete all Ui_*.py files without a corresponding
+    #         *.ui file
+    dirListing = os.listdir(dirName)
+    for formName, sourceName in [
+        (f.replace('Ui_', "").replace(".py", ".ui"), f)
+            for f in dirListing if fnmatch.fnmatch(f, "Ui_*.py")]:
+        if not os.path.exists(os.path.join(dirName, formName)):
+            os.remove(os.path.join(dirName, sourceName))
+            if os.path.exists(os.path.join(dirName, sourceName + "c")):
+                os.remove(os.path.join(dirName, sourceName + "c"))
+    
+    # step 2: delete the __pycache__ directory
+    if os.path.exists(os.path.join(dirName, "__pycache__")):
+        shutil.rmtree(os.path.join(dirName, "__pycache__"))
+    
+    # step 3: descent into subdirectories and delete them if empty
+    for name in os.listdir(dirName):
+        name = os.path.join(dirName, name)
+        if os.path.isdir(name):
+            cleanupSource(name)
+            if len(os.listdir(name)) == 0:
+                os.rmdir(name)
+
+
+def main(argv):
+    """
+    The main function of the script.
+
+    @param argv the list of command line arguments.
+    """
+    print("Cleaning up source ...")
+    sourceDir = os.path.dirname(__file__) or "."
+    cleanupSource(sourceDir)
+    
+    
+if __name__ == "__main__":
+    try:
+        main(sys.argv)
+    except SystemExit:
+        raise
+    except:
+        print(
+            "\nAn internal error occured.  Please report all the output of the"
+            " program, \nincluding the following traceback, to"
+            " eric5-bugs@eric-ide.python-projects.org.\n")
+        raise

eric ide

mercurial