scripts/cleanupSource.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 6957
59927f5be64e
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2014 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
5 #
6
7 """
8 Script for eric6 to clean up the source tree.
9 """
10
11 from __future__ import unicode_literals, print_function
12
13 import os
14 import sys
15 import fnmatch
16 import shutil
17
18
19 def cleanupSource(dirName):
20 """
21 Cleanup the sources directory to get rid of leftover files
22 and directories.
23
24 @param dirName name of the directory to prune (string)
25 """
26 # step 1: delete all Ui_*.py files without a corresponding
27 # *.ui file
28 dirListing = os.listdir(dirName)
29 for formName, sourceName in [
30 (f.replace('Ui_', "").replace(".py", ".ui"), f)
31 for f in dirListing if fnmatch.fnmatch(f, "Ui_*.py")]:
32 if not os.path.exists(os.path.join(dirName, formName)):
33 os.remove(os.path.join(dirName, sourceName))
34 if os.path.exists(os.path.join(dirName, sourceName + "c")):
35 os.remove(os.path.join(dirName, sourceName + "c"))
36
37 # step 2: delete the __pycache__ directory and all remaining *.pyc files
38 if os.path.exists(os.path.join(dirName, "__pycache__")):
39 shutil.rmtree(os.path.join(dirName, "__pycache__"))
40 for name in [f for f in os.listdir(dirName)
41 if fnmatch.fnmatch(f, "*.pyc")]:
42 os.remove(os.path.join(dirName, name))
43
44 # step 3: descent into subdirectories and delete them if empty
45 for name in os.listdir(dirName):
46 name = os.path.join(dirName, name)
47 if os.path.isdir(name):
48 cleanupSource(name)
49 if len(os.listdir(name)) == 0:
50 os.rmdir(name)
51
52
53 def main(argv):
54 """
55 The main function of the script.
56
57 @param argv the list of command line arguments.
58 """
59 print("Cleaning up source ...")
60 sourceDir = os.path.dirname(__file__) or "."
61 cleanupSource(sourceDir)
62
63
64 if __name__ == "__main__":
65 try:
66 main(sys.argv)
67 except SystemExit:
68 raise
69 except Exception:
70 print(
71 "\nAn internal error occured. Please report all the output of the"
72 " program, \nincluding the following traceback, to"
73 " eric-bugs@eric-ide.python-projects.org.\n")
74 raise
75
76 #
77 # eflag: noqa = M801

eric ide

mercurial