PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/compile.py

https://github.com/WolfgangSt/PyBuild
Python | 83 lines | 82 code | 1 blank | 0 comment | 0 complexity | 07e89edccf0e26341edbd20385de4706 MD5 | raw file
  1. import os
  2. import subprocess
  3. import sys
  4. import re
  5. cmd = []
  6. cmd_dep = []
  7. out = ""
  8. for i in range(1, len(sys.argv)):
  9. arg = sys.argv[i]
  10. if arg.startswith('-o'):
  11. out = arg
  12. cmd.append(arg)
  13. else:
  14. cmd.append(arg)
  15. cmd_dep.append(arg)
  16. out = out.lstrip('-o')
  17. def Outdated():
  18. global out, cmd_dep
  19. if not os.path.exists(out): #if output does not exist it is outdated
  20. return True
  21. otime = os.path.getmtime(out)
  22. cmd_dep += ['-E', '-M', '-MM']
  23. deps = subprocess.Popen(cmd_dep, stdout=subprocess.PIPE).communicate()[0]
  24. deps = deps.decode()
  25. deps = re.compile('(.+?[^\\\])[ \\r\\n]').findall(deps)
  26. for dep in deps:
  27. if (dep[-1] in ['\n', '\r', ':']):
  28. continue
  29. d = dep.lstrip()
  30. if os.path.exists(d):
  31. dtime = os.path.getmtime(d)
  32. if dtime >= otime:
  33. return True #outdated!
  34. return False
  35. err_match = re.compile('(.*?):(\d+):(.*?):')
  36. """
  37. VC expects output to be in the following format
  38. {filename (line# [, column#]) | toolname} :
  39. [anytext] {error | warning} code####: Localizable String
  40. [
  41. any text
  42. ]
  43. Example:
  44. c:\sources\ndse\qui\qbuild\main.cpp(29) : error C2065: 'error' : undeclared identifier
  45. """
  46. def ReformatLine(m):
  47. file = m.group(1)
  48. line = m.group(2)
  49. type = m.group(3).strip()
  50. #if type == 'error':
  51. return "{0}({1}) : {2} :".format(file, line, type)
  52. #else:
  53. # return m.string[m.start():m.end()]
  54. def Reformat(s):
  55. return err_match.sub(ReformatLine, s)
  56. def Compile():
  57. print("compiling")
  58. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  59. pin, perr = proc.communicate()
  60. pin = Reformat(pin.decode())
  61. perr = Reformat(perr.decode())
  62. sys.stdout.write(pin)
  63. sys.stderr.write(perr)
  64. exit(proc.returncode)
  65. if Outdated():
  66. Compile()