-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcleanupSource.py
75 lines (61 loc) · 2.19 KB
/
cleanupSource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2014 - 2016 Detlev Offenbach <[email protected]>
#
"""
Script for eric6 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 and all remaining *.pyc files
if os.path.exists(os.path.join(dirName, "__pycache__")):
shutil.rmtree(os.path.join(dirName, "__pycache__"))
for name in [f for f in os.listdir(dirName)
if fnmatch.fnmatch(f, "*.pyc")]:
os.remove(os.path.join(dirName, name))
# 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"
" [email protected].\n")
raise