Some weeks ago I noticed how the qDebug()-output could be enriched, so that in bigger solutions with a lot of different “unknown” components a reported error could be immediately pinned. And you save writing always __FILE__ and __LINE__. Referres to this post.
But the problem was that with the mpc-buildsystem it was unknown to me how to force it to put this DEFINE into the vcxproj-files.
It can be done via the “macros”-statement!
So I worked on my Python-skills and wrote a short script which iterates the given path recursively and fixes all mpc-files by checking for the position of the line with the last closingbrace “}” and then it adds before that position the line. Of course, the experts know several thousand ways to improve that script – but I am currently happy with it. It works, it is debug-able (.sh, I look at you!) and I will use the skeleton also for some other tasks.
It can be found (like most Python-snippets) at: https://github.com/marcelpetrick/pythonCollection
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 |
import fileinput import sys import os #------------------------------------------ def addMacroToFile(filename, newStatement): #print("called addMacroToFile:", filename, determinePositionOfLastBrace(filename)) returnValue = "fail" posLastBrace = determinePositionOfLastBrace(filename) posCurrentLine = 1 for line in fileinput.input(filename, inplace=True): # add additional line in case of matching last brace if posCurrentLine == posLastBrace: sys.stdout.write(newStatement) sys.stdout.write("\n") # newline, else it would be put just before the brace returnValue = "success" sys.stdout.write(line) # prints without additional newline (on windows) posCurrentLine += 1 return returnValue #------------------------------------------ # return the position of the last brace. # possible improvement: in case of some malformed mpc: skip .. def determinePositionOfLastBrace(filename): lastPos = -1 position = 1 with open(filename, "r") as file: #has implicit close - which is nice for line in file: if line.__contains__("}"): lastPos = position position += 1 return lastPos # ------------------------------------------ # find all mpb/mpc files def fixAllFilesRecursively(path, suffix, newStatement): for dirname, dirnames, filenames in os.walk(path): for filename in filenames: full_path = os.path.join(dirname, filename) if full_path.endswith(suffix): print(full_path) wasSuccessful = addMacroToFile(full_path, newStatement) if wasSuccessful == "success": print(" modified :)") # prevent recursion into those subdirs with meta-data if '.svn' in dirnames: dirnames.remove('.svn') if '.git' in dirnames: dirnames.remove('.git') #------------------------------------------ #------------------------------------------ path = "D:\Repo_INS_RADARNX" suffix = "mpc" newStatement = " macros += QT_MESSAGELOGCONTEXT" # TODO possible improvement: prevent double application of the added macro .. fixAllFilesRecursively(path, suffix, newStatement) |
Leave a Reply
You must be logged in to post a comment.