| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
> |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
''' |
| 13 |
Script to add inclusion of mocs to files recursively. |
| 14 |
''' |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
import os |
| 19 |
import re |
| 20 |
import argparse |
| 21 |
import sys |
| 22 |
|
| 23 |
dirty = False |
| 24 |
|
| 25 |
|
| 26 |
def stripInitialSlash(path): |
| 27 |
if path and path.startswith("/"): |
| 28 |
path = path[1:] |
| 29 |
return path |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
def shouldExclude(root, path): |
| 35 |
|
| 36 |
if not args.excludes: |
| 37 |
return False |
| 38 |
|
| 39 |
assert root.startswith(args.root) |
| 40 |
root = stripInitialSlash(root[len(args.root):]) |
| 41 |
|
| 42 |
if args.headerPrefix: |
| 43 |
assert root.startswith(args.headerPrefix) |
| 44 |
root = stripInitialSlash(root[len(args.headerPrefix):]) |
| 45 |
|
| 46 |
return (path in args.excludes) or (root + "/" + path in args.excludes) |
| 47 |
|
| 48 |
|
| 49 |
regexp = re.compile("\\s*(Q_OBJECT|Q_GADGET|Q_NAMESPACE)\\s*") |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
def hasMacro(fileName): |
| 54 |
with open(fileName, "r", encoding="ISO-8859-1") as fileHandle: |
| 55 |
for line in fileHandle: |
| 56 |
if regexp.match(line): |
| 57 |
return True |
| 58 |
return False |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def matchingCPPFile(root, fileName): |
| 64 |
assert root.startswith(args.root) |
| 65 |
root = stripInitialSlash(root[len(args.root):]) |
| 66 |
|
| 67 |
if args.headerPrefix: |
| 68 |
assert root.startswith(args.headerPrefix) |
| 69 |
root = stripInitialSlash(root[len(args.headerPrefix):]) |
| 70 |
|
| 71 |
if args.sourcePrefix: |
| 72 |
root = args.sourcePrefix + "/" + root |
| 73 |
|
| 74 |
return args.root + "/" \ |
| 75 |
+ root + ("/" if root != "" else "") \ |
| 76 |
+ fileNameWithoutExtension(fileName) + ".cpp" |
| 77 |
|
| 78 |
|
| 79 |
def fileNameWithoutExtension(fileName): |
| 80 |
return os.path.splitext(os.path.basename(fileName))[0] |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def cppHasMOCInclude(fileName): |
| 86 |
includeStatement = '#include "moc_%s.cpp"' % fileNameWithoutExtension(fileName) |
| 87 |
with open(fileName, encoding="utf8") as fileHandle: |
| 88 |
return includeStatement in fileHandle.read() |
| 89 |
|
| 90 |
|
| 91 |
def getMocInsertionLocation(filename, content): |
| 92 |
headerIncludeRegex = re.compile(r'#include "%s\.h".*\n' % fileNameWithoutExtension(filename), re.M) |
| 93 |
match = headerIncludeRegex.search(content) |
| 94 |
if match: |
| 95 |
return match.end() |
| 96 |
return 0 |
| 97 |
|
| 98 |
|
| 99 |
def trimExistingMocInclude(content, cppFileName): |
| 100 |
mocStrRegex = re.compile(r'#include "moc_%s\.cpp"\n' % fileNameWithoutExtension(cppFileName)) |
| 101 |
match = mocStrRegex.search(content) |
| 102 |
if match: |
| 103 |
return content[:match.start()] + content[match.end():] |
| 104 |
return content |
| 105 |
|
| 106 |
|
| 107 |
def processFile(root, fileName): |
| 108 |
|
| 109 |
global dirty |
| 110 |
macroFound = hasMacro(root+"/"+fileName) |
| 111 |
logVerbose("Inspecting %s %s" % |
| 112 |
(root+"/"+fileName, "[Has Q_OBJECT / Q_GADGET / Q_NAMESPACE]" if macroFound else "")) |
| 113 |
|
| 114 |
if macroFound: |
| 115 |
cppFileName = matchingCPPFile(root, fileName) |
| 116 |
logVerbose(" -> %s" % cppFileName) |
| 117 |
|
| 118 |
if not os.path.exists(cppFileName): |
| 119 |
log("file %s didn't exist (which might not be an error)" % cppFileName) |
| 120 |
return |
| 121 |
|
| 122 |
if args.replaceExisting or not cppHasMOCInclude(cppFileName): |
| 123 |
dirty = True |
| 124 |
if args.dryRun: |
| 125 |
log("Missing moc include file: %s" % cppFileName) |
| 126 |
else: |
| 127 |
log("Updating %s" % cppFileName) |
| 128 |
|
| 129 |
with open(cppFileName, "r", encoding="utf8") as f: |
| 130 |
content = f.read() |
| 131 |
|
| 132 |
if args.replaceExisting: |
| 133 |
content = trimExistingMocInclude(content, cppFileName) |
| 134 |
|
| 135 |
loc = getMocInsertionLocation(cppFileName, content) |
| 136 |
if args.insertAtEnd: |
| 137 |
with open(cppFileName, "a", encoding="utf8") as f: |
| 138 |
f.write('\n#include "moc_%s.cpp"\n' % fileNameWithoutExtension(cppFileName)) |
| 139 |
else: |
| 140 |
with open(cppFileName, "w", encoding="utf8") as f: |
| 141 |
f.write(content[:loc] + ('#include "moc_%s.cpp"\n' % |
| 142 |
fileNameWithoutExtension(cppFileName)) + content[loc:]) |
| 143 |
|
| 144 |
|
| 145 |
def log(content): |
| 146 |
if not args.quiet: |
| 147 |
print(content) |
| 148 |
|
| 149 |
|
| 150 |
def logVerbose(content): |
| 151 |
if args.verbose: |
| 152 |
print(content) |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
parser = argparse.ArgumentParser(description="""Script to add inclusion of mocs to files recursively. |
| 158 |
The source files either need to be in the same directories as the header files or in parallel directories, |
| 159 |
where the root of the headers are specified using --header-prefix and the root of the sources are specified using --source-prefix. |
| 160 |
If either header-prefix or source-prefix is the current directory, then they may be omitted.""") |
| 161 |
parser.add_argument("--dry-run", "-n", dest="dryRun", action='store_true', help="only report files to be updated") |
| 162 |
parser.add_argument("--quiet", "-q", dest="quiet", action='store_true', help="suppress output") |
| 163 |
parser.add_argument("--verbose", "-v", dest="verbose", action='store_true') |
| 164 |
parser.add_argument("--header-prefix", metavar="directory", dest="headerPrefix", |
| 165 |
help="This directory will be replaced with source-prefix when " |
| 166 |
"searching for matching source files") |
| 167 |
parser.add_argument("--source-prefix", metavar="directory", dest="sourcePrefix", help="see --header-prefix") |
| 168 |
parser.add_argument("--excludes", metavar="directory", dest="excludes", nargs="*", |
| 169 |
help="directories to be excluded, might either be in the form of a directory name, " |
| 170 |
"e.g. 3rdparty or a partial directory prefix from the root, e.g 3rdparty/parser") |
| 171 |
parser.add_argument("--insert-at-end", dest="insertAtEnd", action='store_true', |
| 172 |
help="insert the moc include at the end of the file instead of the beginning") |
| 173 |
parser.add_argument("--replace-existing", dest="replaceExisting", action='store_true', |
| 174 |
help="delete and readd existing MOC include statements") |
| 175 |
parser.add_argument(dest="root", default=".", metavar="directory", |
| 176 |
nargs="?", help="root directory for the operation") |
| 177 |
|
| 178 |
args = parser.parse_args() |
| 179 |
|
| 180 |
root = args.root |
| 181 |
if args.headerPrefix: |
| 182 |
root += "/" + args.headerPrefix |
| 183 |
|
| 184 |
path = os.walk(root) |
| 185 |
for root, directories, files in path: |
| 186 |
|
| 187 |
directories[:] = [d for d in directories if not shouldExclude(root, d)] |
| 188 |
|
| 189 |
for file in files: |
| 190 |
if file.endswith(".h") or file.endswith(".hpp"): |
| 191 |
processFile(root, file) |
| 192 |
|
| 193 |
if not dirty: |
| 194 |
log("No changes needed") |
| 195 |
|
| 196 |
sys.exit(-1 if dirty else 0) |
| 197 |
|