config.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python3
  2. import re
  3. import os
  4. import io
  5. copyright = """// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
  6. // Use of this source code is governed by a MIT license that can
  7. // be found in the LICENSE file.
  8. """
  9. exclude_dirs = [".git", "_docs"]
  10. exclude_files = []
  11. include_dirs = [".", "debug", "extra", "test", "_example"]
  12. def is_target(fpath):
  13. if os.path.splitext(fpath)[-1] == ".go":
  14. return True
  15. return False
  16. def update_copyright(fpath):
  17. print("processing " + fpath)
  18. f = io.open(fpath, 'r', encoding='utf-8')
  19. fstr = f.read()
  20. f.close()
  21. # remove old
  22. m = re.search('^// Copyright .+?\r?\n\r?\n', fstr, re.MULTILINE|re.DOTALL)
  23. if m:
  24. fstr = fstr[m.end():]
  25. # add new
  26. fstr = copyright + fstr
  27. f = io.open(fpath, 'w',encoding='utf-8')
  28. f.write(fstr)
  29. f.close()
  30. def main():
  31. for d in include_dirs:
  32. files = [
  33. os.path.join(d, f) for f in os.listdir(d)
  34. if os.path.isfile(os.path.join(d, f))
  35. ]
  36. for f in files:
  37. if is_target(f):
  38. update_copyright(f)
  39. if __name__ == '__main__':
  40. main()