config.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. # use v6;
  3. #
  4. # my $copyright = '// Copyright 2016 Zack Guo <gizak@icloud.com>. All rights reserved.
  5. # // Use of this source code is governed by a MIT license that can
  6. # // be found in the LICENSE file.
  7. #
  8. # ';
  9. #
  10. # sub MAIN('update-docstr', Str $srcp) {
  11. # if $srcp.IO.f {
  12. # $_ = $srcp.IO.slurp;
  13. # if m/^ \/\/\s Copyright .+? \n\n/ {
  14. # unless ~$/ eq $copyright {
  15. # s/^ \/\/\s Copyright .+? \n\n /$copyright/;
  16. # spurt $srcp, $_;
  17. # say "[updated] doc string for:"~$srcp;
  18. # }
  19. # } else {
  20. # say "[added] doc string for "~$srcp~" (no match found)";
  21. # $_ = $copyright ~ $_;
  22. # spurt $srcp, $_;
  23. # }
  24. # }
  25. # }
  26. import re
  27. import os
  28. import io
  29. copyright = """// Copyright 2016 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  30. // Use of this source code is governed by a MIT license that can
  31. // be found in the LICENSE file.
  32. """
  33. exclude_dirs = [".git", "_docs"]
  34. exclude_files = []
  35. include_dirs = [".", "debug", "extra", "test", "_example"]
  36. def is_target(fpath):
  37. if os.path.splitext(fpath)[-1] == ".go":
  38. return True
  39. return False
  40. def update_copyright(fpath):
  41. print("processing " + fpath)
  42. f = io.open(fpath, 'r', encoding='utf-8')
  43. fstr = f.read()
  44. f.close()
  45. # remove old
  46. m = re.search('^// Copyright .+?\r?\n\r?\n', fstr, re.MULTILINE|re.DOTALL)
  47. if m:
  48. fstr = fstr[m.end():]
  49. # add new
  50. fstr = copyright + fstr
  51. f = io.open(fpath, 'w',encoding='utf-8')
  52. f.write(fstr)
  53. f.close()
  54. def main():
  55. for d in include_dirs:
  56. files = [
  57. os.path.join(d, f) for f in os.listdir(d)
  58. if os.path.isfile(os.path.join(d, f))
  59. ]
  60. for f in files:
  61. if is_target(f):
  62. update_copyright(f)
  63. if __name__ == '__main__':
  64. main()