test-license.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. # ################################################################
  3. # Copyright (c) Facebook, Inc.
  4. # All rights reserved.
  5. #
  6. # This source code is licensed under both the BSD-style license (found in the
  7. # LICENSE file in the root directory of this source tree) and the GPLv2 (found
  8. # in the COPYING file in the root directory of this source tree).
  9. # You may select, at your option, one of the above-listed licenses.
  10. # ################################################################
  11. import enum
  12. import glob
  13. import os
  14. import re
  15. import sys
  16. ROOT = os.path.join(os.path.dirname(__file__), "..")
  17. RELDIRS = [
  18. "doc",
  19. "examples",
  20. "lib",
  21. "programs",
  22. "tests",
  23. "contrib/linux-kernel",
  24. ]
  25. REL_EXCLUDES = [
  26. "contrib/linux-kernel/test/include",
  27. ]
  28. def to_abs(d):
  29. return os.path.normpath(os.path.join(ROOT, d)) + "/"
  30. DIRS = [to_abs(d) for d in RELDIRS]
  31. EXCLUDES = [to_abs(d) for d in REL_EXCLUDES]
  32. SUFFIXES = [
  33. ".c",
  34. ".h",
  35. "Makefile",
  36. ".mk",
  37. ".py",
  38. ".S",
  39. ]
  40. # License should certainly be in the first 10 KB.
  41. MAX_BYTES = 10000
  42. MAX_LINES = 50
  43. LICENSE_LINES = [
  44. "This source code is licensed under both the BSD-style license (found in the",
  45. "LICENSE file in the root directory of this source tree) and the GPLv2 (found",
  46. "in the COPYING file in the root directory of this source tree).",
  47. "You may select, at your option, one of the above-listed licenses.",
  48. ]
  49. COPYRIGHT_EXCEPTIONS = {
  50. # From zstdmt
  51. "threading.c",
  52. "threading.h",
  53. # From divsufsort
  54. "divsufsort.c",
  55. "divsufsort.h",
  56. }
  57. LICENSE_EXCEPTIONS = {
  58. # From divsufsort
  59. "divsufsort.c",
  60. "divsufsort.h",
  61. # License is slightly different because it references GitHub
  62. "linux_zstd.h",
  63. }
  64. def valid_copyright(lines):
  65. YEAR_REGEX = re.compile("\d\d\d\d|present")
  66. for line in lines:
  67. line = line.strip()
  68. if "Copyright" not in line:
  69. continue
  70. if "present" in line:
  71. return (False, f"Copyright line '{line}' contains 'present'!")
  72. if "Facebook, Inc" not in line:
  73. return (False, f"Copyright line '{line}' does not contain 'Facebook, Inc'")
  74. year = YEAR_REGEX.search(line)
  75. if year is not None:
  76. return (False, f"Copyright line '{line}' contains {year.group(0)}; it should be yearless")
  77. if " (c) " not in line:
  78. return (False, f"Copyright line '{line}' does not contain ' (c) '!")
  79. return (True, "")
  80. return (False, "Copyright not found!")
  81. def valid_license(lines):
  82. for b in range(len(lines)):
  83. if LICENSE_LINES[0] not in lines[b]:
  84. continue
  85. for l in range(len(LICENSE_LINES)):
  86. if LICENSE_LINES[l] not in lines[b + l]:
  87. message = f"""Invalid license line found starting on line {b + l}!
  88. Expected: '{LICENSE_LINES[l]}'
  89. Actual: '{lines[b + l]}'"""
  90. return (False, message)
  91. return (True, "")
  92. return (False, "License not found!")
  93. def valid_file(filename):
  94. with open(filename, "r") as f:
  95. lines = f.readlines(MAX_BYTES)
  96. lines = lines[:min(len(lines), MAX_LINES)]
  97. ok = True
  98. if os.path.basename(filename) not in COPYRIGHT_EXCEPTIONS:
  99. c_ok, c_msg = valid_copyright(lines)
  100. if not c_ok:
  101. print(f"{filename}: {c_msg}", file=sys.stderr)
  102. ok = False
  103. if os.path.basename(filename) not in LICENSE_EXCEPTIONS:
  104. l_ok, l_msg = valid_license(lines)
  105. if not l_ok:
  106. print(f"{filename}: {l_msg}", file=sys.stderr)
  107. ok = False
  108. return ok
  109. def exclude(filename):
  110. for x in EXCLUDES:
  111. if filename.startswith(x):
  112. return True
  113. return False
  114. def main():
  115. invalid_files = []
  116. for directory in DIRS:
  117. for suffix in SUFFIXES:
  118. files = set(glob.glob(f"{directory}/**/*{suffix}", recursive=True))
  119. for filename in files:
  120. if exclude(filename):
  121. continue
  122. if not valid_file(filename):
  123. invalid_files.append(filename)
  124. if len(invalid_files) > 0:
  125. print("Fail!", file=sys.stderr)
  126. for f in invalid_files:
  127. print(f)
  128. return 1
  129. else:
  130. print("Pass!", file=sys.stderr)
  131. return 0
  132. if __name__ == "__main__":
  133. sys.exit(main())