sourcefix.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/python
  2. #
  3. # Copyright 2010 wkhtmltopdf authors
  4. #
  5. # This file is part of wkhtmltopdf.
  6. #
  7. # wkhtmltopdf is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # wkhtmltopdf is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with wkhtmltopdf. If not, see <http:#www.gnu.org/licenses/>.
  19. from sys import argv, exit
  20. import re
  21. from datetime import date
  22. import os
  23. import difflib
  24. cdate = re.compile(r"Copyright ([0-9 ,]*) wkhtmltopdf authors")
  25. ifdef = re.compile(r"^[\n\r \t]*#ifndef __(.*)__[\t ]*\n#define __(\1)__[\t ]*\n")
  26. endif = re.compile(r"#endif.*[\r\n \t]*$")
  27. ws = re.compile(r"[ \t]*[\r\n]")
  28. branchspace = re.compile(r"([ \t\r\n])(for|if|while|switch|foreach)[\t \r\n]*\(")
  29. hangelse = re.compile(r"}[\r\n\t ]*(else)")
  30. braceup = re.compile(r"(\)|else)[\r\n\t ]*{")
  31. include = re.compile(r"(#include (\"[^\"]*\"|<[^>]*>)\n)+")
  32. def includesort(x):
  33. return "\n".join(sorted(x.group(0)[:-1].split("\n"))+[""])
  34. changes=False
  35. progname="wkhtmltopdf"
  36. for path in argv[1:]:
  37. try:
  38. data = file(path).read()
  39. except:
  40. continue
  41. mo = cdate.search(data)
  42. years = set(mo.group(1).split(", ")) if mo else set()
  43. years.add(str(date.today().year))
  44. ext = path.rsplit(".",2)[-1]
  45. header = ""
  46. cc = "//"
  47. if ext in ["hh","h","c","cc","cpp","inl"]:
  48. header += """// -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  49. // vi:set ts=4 sts=4 sw=4 noet :
  50. //
  51. """
  52. elif ext in ["sh"]:
  53. header += "#!/bin/bash\n#\n"
  54. cc = "#"
  55. elif ext in ["py"]:
  56. header += "#!/usr/bin/python\n#\n"
  57. cc = "#"
  58. elif ext in ["pro","pri"]:
  59. cc = "#"
  60. else:
  61. continue
  62. header += """// Copyright %(years)s %(name)s authors
  63. //
  64. // This file is part of %(name)s.
  65. //
  66. // %(name)s is free software: you can redistribute it and/or modify
  67. // it under the terms of the GNU General Public License as published by
  68. // the Free Software Foundation, either version 3 of the License, or
  69. // (at your option) any later version.
  70. //
  71. // %(name)s is distributed in the hope that it will be useful,
  72. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  73. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  74. // GNU General Public License for more details.
  75. //
  76. // You should have received a copy of the GNU General Public License
  77. // along with %(name)s. If not, see <http://www.gnu.org/licenses/>.
  78. """%{"years": (", ".join(sorted(list(years)))),"name":progname}
  79. #Strip away generated header
  80. hexp = re.compile("^(%s[^\\n]*\\n)*"%(cc))
  81. ndata = hexp.sub("", data,1)
  82. ndata = ws.sub("\n", ndata)+"\n"
  83. if ext in ["hh","h","inl"]:
  84. s=0
  85. e=-1
  86. while ndata[s] in ['\r','\n',' ','\t']: s+=1
  87. while ndata[e] in ['\r','\n',' ','\t']: e-=1
  88. #Strip away generated ifdef
  89. if ifdef.search(ndata):
  90. ndata = endif.sub("",ifdef.sub("",ndata,1),1)
  91. s=0
  92. e=-1
  93. while ndata[s] in ['\r','\n',' ','\t']: s+=1
  94. while ndata[e] in ['\r','\n',' ','\t']: e-=1
  95. ndata=ndata[s:e+1].replace(" ",'\t')
  96. if ext in ["hh","h","c","cc","cpp","inl"]:
  97. ndata = branchspace.sub(r"\1\2 (",ndata)
  98. ndata = hangelse.sub("} else",ndata)
  99. ndata = braceup.sub(r"\1 {",ndata)
  100. ndata = include.sub(includesort, ndata)
  101. if ext in ["hh","h","inl"]:
  102. n = os.path.split(path)[-1].replace(".","_").replace(" ","_").upper()
  103. ndata = """#ifndef __%s__
  104. #define __%s__
  105. %s
  106. #endif //__%s__"""%(n,n,ndata,n)
  107. ndata = header.replace("//",cc)+ndata+"\n"
  108. if ndata != data:
  109. for x in difflib.unified_diff(data.split("\n"),ndata.split("\n"), "a/"+path, "b/"+path):
  110. print x
  111. changes=True
  112. file(path, "w").write(ndata)
  113. if changes: exit(1)