CRFile.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #! /usr/bin/env bash
  2. #
  3. # Copyright (C) 2013-2017 Bilibili
  4. # Copyright (C) 2013-2017 Zhang Rui <bbcallen@gmail.com>
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import os
  19. import shutil
  20. from copyrighter.CRCopyright import CRCopyright
  21. class CRFile:
  22. def __init__(self, context, file):
  23. self.dirty = False
  24. self.context = context
  25. self.abs_path = context.get_path_of_file(file)
  26. self.file_ext = os.path.splitext(self.abs_path)[1][1:]
  27. self.copyright_names = {}
  28. self.copyright_urls = {}
  29. self.need_insert_bilibili_copyright = True
  30. def update(self):
  31. if not self.dirty:
  32. self.context.log_file('~ remain', self.abs_path)
  33. return
  34. tmp_path = self.abs_path + '.tmp'
  35. if self.context.dryrun:
  36. src = open(self.abs_path, 'r')
  37. else:
  38. shutil.copy2(self.abs_path, tmp_path)
  39. src = open(tmp_path, 'r')
  40. tmp = open(self.abs_path, 'w')
  41. did_insert_bilibili_copyright = False
  42. for line in src:
  43. if self.need_insert_bilibili_copyright and not did_insert_bilibili_copyright:
  44. copyright = CRCopyright.scan_line(self.context, line)
  45. if copyright:
  46. copyright.name = 'Bilibili'
  47. copyright.url = None
  48. if not self.context.dryrun:
  49. tmp.write(copyright.get_line())
  50. tmp.write("\n")
  51. # print ' insert %s' % copyright.get_line()
  52. did_insert_bilibili_copyright = True
  53. if not self.context.dryrun:
  54. tmp.write(line)
  55. src.close()
  56. if not self.context.dryrun:
  57. tmp.close()
  58. os.remove(tmp_path)
  59. if self.need_insert_bilibili_copyright and did_insert_bilibili_copyright:
  60. self.context.log_file('+ update', self.abs_path)
  61. else:
  62. self.context.log_file('~ missing', self.abs_path)
  63. def copyright_names(self):
  64. return self.copyright_names.keys()
  65. def copyright_urls(self):
  66. return self.copyright_urls.keys()
  67. def __parse_line(self, line):
  68. copyright = CRCopyright.scan_line(self.context, line)
  69. if copyright:
  70. # print "match %s" % copyright.name
  71. self.copyright_names[copyright.name.lower()] = copyright
  72. self.copyright_urls[copyright.url.lower()] = copyright
  73. return True
  74. @staticmethod
  75. def load_from_file(context, file):
  76. parsed_lines = 0;
  77. crf = CRFile(context = context, file = file)
  78. f = open(crf.abs_path, 'r')
  79. for line in f:
  80. if parsed_lines > 20:
  81. break
  82. parsed_lines += 1
  83. crf.__parse_line(line)
  84. f.close()
  85. # TODO: use a visitor
  86. if 'bilibili' not in crf.copyright_names:
  87. if 'zhang rui' in crf.copyright_names or 'bbcallen@gmail.com' in crf.copyright_urls:
  88. crf.need_insert_bilibili_copyright = True
  89. crf.dirty = True
  90. return crf
  91. @staticmethod
  92. def update_path(context, file):
  93. base_name = os.path.basename(file)
  94. abs_path = context.get_path_of_file(file)
  95. if base_name.startswith('.'):
  96. context.log_file('- hidden', abs_path)
  97. return
  98. elif context.is_black_path(abs_path):
  99. context.log_file('- black', abs_path)
  100. return
  101. elif os.path.islink(abs_path):
  102. context.log_file('- link', abs_path)
  103. return
  104. elif os.path.isdir(abs_path):
  105. for sub_file in os.listdir(abs_path):
  106. sub_path = os.path.realpath(os.path.join(abs_path, sub_file))
  107. CRFile.update_path(context, sub_path)
  108. elif not context.need_copyright(abs_path):
  109. context.log_file('- nohead', abs_path)
  110. return
  111. elif os.path.isfile(abs_path):
  112. src_file = CRFile.load_from_file(context, abs_path)
  113. src_file.update()