sourcefix.py 3.7 KB

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