PRESUBMIT.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import os
  5. USE_PYTHON3 = True
  6. ANDROID_ALLOWED_LICENSES = [
  7. 'A(pple )?PSL 2(\.0)?',
  8. 'Android Software Development Kit License',
  9. 'Apache( License)?,?( Version)? 2(\.0)?',
  10. '(New )?([23]-Clause )?BSD( [23]-Clause)?( with advertising clause)?',
  11. 'GNU Lesser Public License',
  12. 'L?GPL ?v?2(\.[01])?( or later)?( with the classpath exception)?',
  13. '(The )?MIT(/X11)?(-like)?( License)?',
  14. 'MPL 1\.1 ?/ ?GPL 2(\.0)? ?/ ?LGPL 2\.1',
  15. 'MPL 2(\.0)?',
  16. 'Microsoft Limited Public License',
  17. 'Microsoft Permissive License',
  18. 'Public Domain',
  19. 'Python',
  20. 'SIL Open Font License, Version 1.1',
  21. 'SGI Free Software License B',
  22. 'Unicode, Inc. License',
  23. 'University of Illinois\/NCSA Open Source',
  24. 'X11',
  25. 'Zlib',
  26. ]
  27. def LicenseIsCompatibleWithAndroid(input_api, license):
  28. regex = '^(%s)$' % '|'.join(ANDROID_ALLOWED_LICENSES)
  29. tokens = \
  30. [x.strip() for x in input_api.re.split(' and |,', license) if len(x) > 0]
  31. has_compatible_license = False
  32. for token in tokens:
  33. if input_api.re.match(regex, token, input_api.re.IGNORECASE):
  34. has_compatible_license = True
  35. break
  36. return has_compatible_license
  37. def _CheckThirdPartyReadmesUpdated(input_api, output_api):
  38. """
  39. Checks to make sure that README.chromium files are properly updated
  40. when dependencies in third_party are modified.
  41. """
  42. readmes = []
  43. files = []
  44. errors = []
  45. for f in input_api.AffectedFiles():
  46. local_path = f.LocalPath()
  47. if input_api.os_path.dirname(local_path) == 'third_party':
  48. continue
  49. if (local_path.startswith('third_party' + input_api.os_path.sep) and
  50. not local_path.startswith('third_party' + input_api.os_path.sep +
  51. 'blink' + input_api.os_path.sep) and
  52. not local_path.startswith('third_party' + input_api.os_path.sep +
  53. 'boringssl' + input_api.os_path.sep) and
  54. not local_path.startswith('third_party' + input_api.os_path.sep +
  55. 'closure_compiler' + input_api.os_path.sep +
  56. 'externs' + input_api.os_path.sep) and
  57. not local_path.startswith('third_party' + input_api.os_path.sep +
  58. 'closure_compiler' + input_api.os_path.sep +
  59. 'interfaces' + input_api.os_path.sep) and
  60. not local_path.startswith('third_party' + input_api.os_path.sep +
  61. 'feed_library' + input_api.os_path.sep) and
  62. not local_path.startswith('third_party' + input_api.os_path.sep +
  63. 'ipcz' + input_api.os_path.sep) and
  64. # TODO(danakj): We should look for the README.chromium file in
  65. # third_party/rust/CRATE_NAME/vVERSION/.
  66. not local_path.startswith('third_party' + input_api.os_path.sep +
  67. 'rust' + input_api.os_path.sep) and
  68. not local_path.startswith('third_party' + input_api.os_path.sep +
  69. 'webxr_test_pages' + input_api.os_path.sep)):
  70. files.append(f)
  71. if local_path.endswith("README.chromium"):
  72. readmes.append(f)
  73. if files and not readmes:
  74. errors.append(output_api.PresubmitPromptWarning(
  75. 'When updating or adding third party code the appropriate\n'
  76. '\'README.chromium\' file should also be updated with the correct\n'
  77. 'version and package information.', files))
  78. if not readmes:
  79. return errors
  80. name_pattern = input_api.re.compile(
  81. r'^Name: [a-zA-Z0-9_\-\. \(\)]+\r?$',
  82. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  83. shortname_pattern = input_api.re.compile(
  84. r'^Short Name: [a-zA-Z0-9_\-\.]+\r?$',
  85. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  86. version_pattern = input_api.re.compile(
  87. r'^Version: [a-zA-Z0-9_\-\+\.:/]+\r?$',
  88. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  89. release_pattern = input_api.re.compile(
  90. r'^Security Critical: (yes|no)\r?$',
  91. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  92. license_pattern = input_api.re.compile(
  93. r'^License: (.+)\r?$',
  94. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  95. not_shipped_pattern = input_api.re.compile(
  96. r'^License File: NOT_SHIPPED\r?$',
  97. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  98. license_android_compatible_pattern = input_api.re.compile(
  99. r'^License Android Compatible: (yes|no)\r?$',
  100. input_api.re.IGNORECASE | input_api.re.MULTILINE)
  101. for f in readmes:
  102. if 'D' in f.Action():
  103. _IgnoreIfDeleting(input_api, output_api, f, errors)
  104. continue
  105. contents = input_api.ReadFile(f)
  106. if (not shortname_pattern.search(contents)
  107. and not name_pattern.search(contents)):
  108. errors.append(output_api.PresubmitError(
  109. 'Third party README files should contain either a \'Short Name\' or\n'
  110. 'a \'Name\' which is the name under which the package is\n'
  111. 'distributed. Check README.chromium.template for details.',
  112. [f]))
  113. if not version_pattern.search(contents):
  114. errors.append(output_api.PresubmitError(
  115. 'Third party README files should contain a \'Version\' field.\n'
  116. 'If the package is not versioned or the version is not known\n'
  117. 'list the version as \'unknown\'.\n'
  118. 'Check README.chromium.template for details.',
  119. [f]))
  120. if not release_pattern.search(contents):
  121. errors.append(output_api.PresubmitError(
  122. 'Third party README files should contain a \'Security Critical\'\n'
  123. 'field. This field specifies whether the package is built with\n'
  124. 'Chromium. Check README.chromium.template for details.',
  125. [f]))
  126. license_match = license_pattern.search(contents)
  127. if not license_match:
  128. errors.append(output_api.PresubmitError(
  129. 'Third party README files should contain a \'License\' field.\n'
  130. 'This field specifies the license used by the package. Check\n'
  131. 'README.chromium.template for details.',
  132. [f]))
  133. not_shipped_match = not_shipped_pattern.search(contents)
  134. android_compatible_match = (
  135. license_android_compatible_pattern.search(contents))
  136. if (not not_shipped_match and not android_compatible_match and
  137. not LicenseIsCompatibleWithAndroid(input_api, license_match.group(1))):
  138. errors.append(output_api.PresubmitPromptWarning(
  139. 'Cannot determine whether specified license is compatible with\n' +
  140. 'the Android licensing requirements. Please check that the license\n' +
  141. 'name is spelled according to third_party/PRESUBMIT.py. Please see\n' +
  142. 'README.chromium.template for details.',
  143. [f]))
  144. return errors
  145. def _IgnoreIfDeleting(input_api, output_api, affected_file, errors):
  146. third_party_dir = input_api.os_path.dirname(affected_file.LocalPath()) + \
  147. os.path.sep
  148. for f in input_api.AffectedFiles():
  149. if f.LocalPath().startswith(third_party_dir):
  150. if 'D' not in f.Action():
  151. errors.append(output_api.PresubmitError(
  152. 'Third party README should only be removed when the whole\n'
  153. 'directory is being removed.\n', [f, affected_file]))
  154. def CheckChangeOnUpload(input_api, output_api):
  155. results = []
  156. results.extend(_CheckThirdPartyReadmesUpdated(input_api, output_api))
  157. return results