-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathprogen.py
executable file
·111 lines (99 loc) · 4.48 KB
/
progen.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
#!/usr/bin/python
#
# Copyright 2011 Tom Klein.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Create a .pro file if one doesn't exist; populate a .pro with some
# default configuration parameters (if they don't exist); add CONFIG
# parameters specified on the command line
import subprocess, sys, os, re, time
def insertString(inputString, searchRE, insertionRE, stringToInsert):
"""
If <searchRE> is empty or doesn't match <inputString> then insert
<stringToInsert> into <inputString> at the first match of
<insertionRE> (if there is one). Return a pair whose first
coordinate is True if the string was inserted and False otherwise,
and whose second coordinate is the "new" string.
"""
if searchRE == '' or re.search(searchRE, inputString) == None:
insertLocation = inputString.find(insertionRE)
if insertLocation != -1:
print(" Inserting '" + stringToInsert.replace("\n","") + "'...")
inputString = (inputString[:insertLocation] + stringToInsert +
inputString[insertLocation:])
return (True, inputString)
else:
print("\n\n!! Couldn't find insert location: " + insertionRE + "\n\n")
sys.exit(-1)
else:
#print "'" + insertString.replace("\n","") + "' already exists"
return (False, inputString)
###############################################################################
# process command line args
###############################################################################
help = [arg for arg in sys.argv if arg.find('--help') != -1]
if help != []:
print("""\n Usage: {0} [--help | config1 config2 ...]
where config1, config2, ... will be added to the pro file as
CONFIG += config1 config2 ...
""".format(sys.argv[0]))
sys.exit(0)
configArgs = " ".join(sys.argv[1:])
###############################################################################
# create .pro if necessary, then initialize it if necessary
###############################################################################
projectName = os.path.split(os.getcwd())[1] # current directory name
proFilename = projectName + '.pro'
print("")
try:
projectFile = open(proFilename)
except:
print(" Recreating project file...")
# subprocess.Popen(["qmake", "-project"])
# block until qmake returns
subprocess.call(["qmake", "-project"])
projectFile = open(proFilename)
projectString = projectFile.read()
projectFile.close()
updateNeeded = False
# insert new project configuration in front of here
insertionPointRE = "\n# Input\nHEADERS"
# note that it's okay to repeat config lines/options, so we do that freely
if configArgs:
addString = "CONFIG += " + configArgs + "\n"
(configWriteNeeded, projectString) = insertString(projectString,
re.escape(addString),
insertionPointRE,
addString)
updateNeeded = updateNeeded or configWriteNeeded
qtIncludes = "QT += xml widgets printsupport\n"
(xmlWriteNeeded, projectString) = insertString(projectString,
re.escape(qtIncludes),
insertionPointRE,
qtIncludes)
updateNeeded = updateNeeded or xmlWriteNeeded
(iconWriteNeeded, projectString) = insertString(projectString,
r'RC_FILE',
insertionPointRE,
'RC_FILE += cstitch.rc\n',)
updateNeeded = updateNeeded or iconWriteNeeded
if updateNeeded:
projectFile = open(proFilename, 'w')
projectFile.write(projectString)
projectFile.close()
print(" " + proFilename + " updated.")
else:
print(" No updates required.")
print("")