forked from mishoo/UglifyJS
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathextract_features.py
executable file
·61 lines (50 loc) · 1.42 KB
/
extract_features.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
#!/usr/bin/python
import multiprocessing
import os
import sys
import shutil
def PrintUsage():
print """
Usage:
extract_features.py --filelist <file>
OR
extract_features.py --dir <directory>
"""
exit(1)
def GetJSFilesInDir(d):
for root, _, files in os.walk(d):
for f in files:
fname = os.path.join(root, f)
if fname.endswith('.js'):
yield fname
TMP_DIR = ""
def ExtractFeaturesForFile(f):
global TMP_DIR
os.system("nodejs bin/unuglifyjs --extract_features --skip_minified '%s' >> %s/%d" % (f, TMP_DIR, os.getpid()))
def ExtractFeaturesForFileList(files):
global TMP_DIR
TMP_DIR = "/tmp/feature_extractor%d" % (os.getpid())
if os.path.exists(TMP_DIR):
shutil.rmtree(TMP_DIR)
os.makedirs(TMP_DIR)
try:
p = multiprocessing.Pool(multiprocessing.cpu_count())
p.map(ExtractFeaturesForFile, files)
output_files = os.listdir(TMP_DIR)
for f in output_files:
os.system("cat %s/%s" % (TMP_DIR, f))
finally:
shutil.rmtree(TMP_DIR)
if __name__ == '__main__':
if (len(sys.argv) <= 1):
PrintUsage()
# Process command line arguments
if (sys.argv[1] == "--filelist"):
files = open(sys.argv[2], 'r').read().split('\n')
elif (sys.argv[1] == "--dir"):
files = [f for f in GetJSFilesInDir(sys.argv[2])]
else:
PrintUsage()
# Remove files that say they are minified.
files = [f for f in files if not f.endswith('.min.js')]
ExtractFeaturesForFileList(files)