forked from polserver/polserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.py
executable file
·154 lines (124 loc) · 3.61 KB
/
clean.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
'''
Cleans the source tree
@author Bodom
'''
import os
import shutil
import re
class Cleaner:
''' Cleans the POL root '''
def __init__(self, root):
self.root = root
def __delFromList(self, list):
''' For internal usage '''
for f in list:
if os.path.isdir(f):
shutil.rmtree(f)
else:
os.unlink(f)
def findDoxygen(self):
''' Returns list of doxygen-generated files '''
doxygen = os.path.join(self.root, 'docs', 'doxygen', 'html')
if os.path.exists(doxygen):
return [doxygen]
return None
def clearDoxygen(self):
self.__delFromList(self.findDoxygen())
def findBoost(self):
''' Returns list of boost extracted files '''
ret = []
lib = os.path.join(self.root, 'lib')
for f in os.listdir(lib):
path = os.path.join(lib, f)
if os.path.isdir(path) and re.match(r'^boost_[0-9_]+$', f):
for f2 in os.listdir(path):
if not f2.startswith('buildboost') and not f2 == '.gitignore':
ret.append(os.path.join(path,f2))
return ret
def clearBoost(self):
self.__delFromList(self.findBoost())
def findCmake(self):
''' Returns list of cmake-generated files '''
ret = []
def readFolder(path):
r = []
for f in os.listdir(path):
fp = os.path.join(path, f)
if os.path.isdir(fp):
if f == 'CMakeFiles':
ret.append(fp)
elif f != 'testsuite':
ret.extend(readFolder(fp))
else:
if f == 'FindSetEnv.cmake':
pass
elif f in ('Makefile', 'CMakeCache.txt', 'pol_global_config.h'):
ret.append(fp)
elif f.endswith('.cmake'):
ret.append(fp)
return r
ret.extend(readFolder(self.root))
return ret
def clearCmake(self):
self.__delFromList(self.findCmake())
def findBinaries(self):
''' Returns list of binary files '''
ret = []
bin = os.path.join(self.root, 'pol-core', 'bin')
for f in os.listdir(bin):
fp = os.path.join(bin, f)
if f != '.gitignore' and os.path.isfile(fp):
ret.append(fp)
return ret
def clearBinaries(self):
self.__delFromList(self.findBinaries())
def findEmpty(self):
''' Returns list of empty folders '''
ret = []
for folder, subfolders, files in os.walk(self.root):
if not subfolders and not files:
ret.append(os.path.join(self.root,folder))
return ret
def clearEmpty(self):
self.__delFromList(self.findEmpty())
if __name__ == '__main__':
import argparse
import subprocess
def yesNo(question, default=None):
''' Asks the user a yes/no question '''
valid = {
"yes": True, "y": True, "ye": True,
"no": False, "n": False
}
if default is None:
prompt = "[y/n]"
elif default:
prompt = "[Y/n]"
else:
prompt = "[y/N]"
while True:
print("{} {} ".format(question,prompt), end='', flush=True)
choice = input().lower()
if default is not None and choice == '':
return default
elif choice in valid.keys():
return valid[choice]
print('Please respond with "y" or "n"')
polRoot = os.path.dirname(os.path.abspath(__file__))
c = Cleaner(polRoot)
print('POL cleaning utility, will delte files inside "{}"'.format(polRoot))
if c.findDoxygen() and yesNo('Delete doxygen generated files?', False):
c.clearDoxygen()
if c.findBoost() and yesNo('Delete boost unpacked files?', False):
c.clearBoost()
if c.findCmake() and yesNo('Delete cmake and intermediate build files?', True):
c.clearCmake()
if c.findBinaries() and yesNo('Delete built binaries?', True):
c.clearBinaries()
if c.findEmpty() and yesNo('Delete empty folders?', True):
c.clearEmpty()
if yesNo('Show list of files ignored by git?', False):
c = ['git', 'ls-files', '--others', '-i', '--exclude-standard']
subprocess.call(c)
print('completed.')