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