forked from rpavlik/cmake-multitool
/
cmake-bulk-decrufter.py
executable file
·143 lines (114 loc) · 4.59 KB
/
cmake-bulk-decrufter.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
#!/usr/bin/env python
"""
Main application to use the CMakeScript packages to output cleaner code.
Original Author:
2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
http://academic.cleardefinition.com
Iowa State University HCI Graduate Program/VRAC
Copyright Iowa State University 2010.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
"""
###
# standard packages
import sys
import os
import subprocess
from optparse import OptionParser
###
# third-party packages
# - none
###
# internal packages
import cmakescript
from mergetool import MergeTool
class App:
def __init__(self, args_in=sys.argv[1:]):
self.args_in = args_in
self.mergetool = None
def main(self):
parser = OptionParser(usage="usage: %prog [options] [[file|dir]...]",
version="%prog 0.5, part of the cmakescript tools")
parser.add_option("-m", "--merge",
type="choice",
choices=MergeTool.mergetools.keys(),
metavar="APPNAME",
dest="mergetool",
default=None,
help="open a diff/merge app APPNAME for each file "
"processed. Supported APPNAME options are: " +
" ".join(MergeTool.mergetools.keys())
)
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
parser.add_option("-i", "--inplace",
action="store_true", dest="inplace", default=False,
help="overwrite files with changes inplace")
(self.options, args) = parser.parse_args(self.args_in)
if len(args) == 0:
args.append(os.getcwd())
inputfiles = []
for arg in args:
inputfiles.extend(cmakescript.find_cmake_scripts(arg))
for infile, number in zip(inputfiles, range(1, len(inputfiles) + 1)):
print
"------------------------"
print
infile + " - " + str(number) + " of " + str(len(inputfiles))
print
"------------------------"
output = self.processFile(infile)
if output is not None:
# A trailing newline
output = output + "\n"
self.runMergeTool(infile, output)
if self.options.inplace:
open(infile, 'wb').writelines(output)
def processFile(self, filename):
try:
parser = cmakescript.parse_file(filename)
except cmakescript.IncompleteStatementError:
print
"Error parsing file: IncompleteStatementError"
return None
except cmakescript.UnclosedChildBlockError:
print
"Error parsing file: UnclosedChildBlockError"
return None
# formatter = cmakescript.CMakeFormatter(parser.parsetree)
cleaned = cmakescript.apply_all_cleanup_visitors(parser.parsetree)
formatter = cmakescript.NiceFormatter(cleaned)
return formatter.output_as_cmake()
def runMergeTool(self, filename, formatted):
if self.mergetool is None and self.options.mergetool is not None:
self.mergetool = MergeTool(self.options.mergetool)
orig = open(filename, 'r')
originalscript = orig.read()
orig.close()
if originalscript == formatted:
return
if self.mergetool is not None:
modname = os.path.splitext(os.path.basename(filename))[0]
t1 = subprocess.Popen(["mktemp", "-d"], stdout=subprocess.PIPE)
tempdir = t1.communicate()[0].strip()
tempclean = os.path.join(tempdir, modname + ".Decrufted.cmake")
temporig = os.path.join(tempdir, modname + ".Original.cmake")
temporigfile = open(temporig, 'w', False)
temporigfile.write(originalscript)
temporigfile.close()
tempcleanfile = open(tempclean, 'w', False)
tempcleanfile.write(formatted)
tempcleanfile.close()
self.mergetool.run(tempclean, filename, temporig)
else:
# If we aren't merging, print the formatted output
print
formatted
###
# __main__
if __name__ == "__main__":
## Can be used as a tool when executed directly
app = App()
app.main()