CRContext.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 datetime
  19. import os
  20. import fnmatch
  21. from sets import Set
  22. from copyrighter.CRCopyright import CRCopyright
  23. class CRContext:
  24. file_ext_white_list = Set([
  25. # '*',
  26. 'c',
  27. 'h',
  28. 'java',
  29. 'm',
  30. 'mk',
  31. 'py',
  32. 'sh',
  33. ])
  34. rel_path_black_list = Set([
  35. '*.a',
  36. '*.d',
  37. '*.jar',
  38. '*.lib',
  39. '*.lproj',
  40. '*.o',
  41. '*.patch',
  42. '*.pbxproj',
  43. '*.pch',
  44. '*.png',
  45. '*.pyc',
  46. '*.so',
  47. '*.strings',
  48. '*.xcassets',
  49. '*.xcodeproj',
  50. '*.xcuserdatad',
  51. '*.xcworkspace',
  52. '*.xib',
  53. 'android/contrib',
  54. 'android/ijkplayer/build',
  55. 'android/ijkplayer/*/build',
  56. 'android/ijkplayer/*/libs',
  57. 'android/ijkplayer/*/obj',
  58. 'android/ijkplayer/*/res',
  59. 'android/patches',
  60. 'extra',
  61. 'ijkmedia/ijkyuv',
  62. 'ijkmedia/ijkj4a',
  63. 'ijkprof/android-ndk-profiler',
  64. 'ios/build',
  65. 'ios/contrib',
  66. 'ios/ffmpeg-*',
  67. 'ios/openssl-*',
  68. ])
  69. def __init__(self, verbose = True, dryrun = True):
  70. self.verbose = verbose
  71. self.dryrun = dryrun
  72. self.root_path = os.path.realpath(os.path.join(__file__, '../../..'))
  73. self.year = datetime.date.today().year
  74. def get_path_of_file(self, file):
  75. if not os.path.isabs(file):
  76. file = os.path.realpath(os.path.join(self.root_path, file))
  77. return file
  78. def get_relpath(self, file):
  79. return os.path.relpath(file, self.root_path)
  80. def is_black_path(self, path):
  81. rel_path = self.get_relpath(path)
  82. for black_item in CRContext.rel_path_black_list:
  83. if fnmatch.fnmatch(rel_path, black_item):
  84. # print ' + fnmatch %s %s' % (rel_path, black_item)
  85. return True
  86. # else:
  87. # print ' - fnmatch %s %s' % (rel_path, black_item)
  88. return False
  89. def need_copyright(self, file):
  90. if '*' in CRContext.file_ext_white_list:
  91. return True
  92. ext = os.path.splitext(file)[1][1:]
  93. return ext in CRContext.file_ext_white_list
  94. def log_file(self, tag, file):
  95. if self.verbose:
  96. rel_path = self.get_relpath(file)
  97. print '%-10s %s' % (tag, rel_path)