test_licenses.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. import unittest
  5. import os.path
  6. parent = os.path.dirname
  7. test_dir = parent(os.path.abspath(__file__))
  8. sdk_root = parent(parent(parent(test_dir)))
  9. def from_sdk_top(fn):
  10. return os.path.abspath(os.path.join(sdk_root, fn))
  11. MPL2_URL = "http://mozilla.org/MPL/2.0/"
  12. # These files all come with their own license headers
  13. skip = [
  14. "python-lib/cuddlefish/_version.py", # generated, public domain
  15. "doc/static-files/js/jquery.js", # MIT/GPL dual
  16. "examples/annotator/data/jquery-1.4.2.min.js", # MIT/GPL dual
  17. "examples/reddit-panel/data/jquery-1.4.4.min.js", # MIT/GPL dual
  18. "examples/library-detector/data/library-detector.js", # MIT
  19. "python-lib/mozrunner/killableprocess.py", # MIT? BSDish?
  20. "python-lib/mozrunner/winprocess.py", # MIT
  21. "packages/api-utils/tests/test-querystring.js", # MIT
  22. "packages/api-utils/lib/promise.js", # MIT
  23. "packages/api-utils/tests/test-promise.js", # MIT
  24. ]
  25. absskip = [from_sdk_top(os.path.join(*fn.split("/"))) for fn in skip]
  26. class Licenses(unittest.TestCase):
  27. def test(self):
  28. # Examine most SDK files to check if they've got an MPL2 license
  29. # header. We exclude some files that are known to include different
  30. # licenses.
  31. self.missing = []
  32. self.scan_file(from_sdk_top(os.path.join("python-lib", "jetpack_sdk_env.py")))
  33. self.scan(os.path.join("python-lib", "cuddlefish"), [".js", ".py"],
  34. skipdirs=["sdk-docs"], # test_generate.py makes this
  35. )
  36. self.scan(os.path.join("python-lib", "mozrunner"), [".py"])
  37. for sdk_package in ["addon-kit", "api-utils", "test-harness"]:
  38. self.scan(os.path.join("packages", sdk_package),
  39. [".js", ".py", ".md"])
  40. self.scan("examples", [".js", ".css", ".html", ".md"])
  41. self.scan("bin", [".bat", ".ps1"])
  42. for fn in [os.path.join("bin", "activate"),
  43. os.path.join("bin", "cfx"),
  44. os.path.join("bin", "integration-scripts", "buildbot-run-cfx-helper"),
  45. os.path.join("bin", "integration-scripts", "integration-check"),
  46. ]:
  47. self.scan_file(from_sdk_top(fn))
  48. self.scan("doc", [".js", ".css", ".md"], skipdirs=["syntaxhighlighter"])
  49. if self.missing:
  50. print
  51. print "The following files are missing an MPL2 header:"
  52. for fn in sorted(self.missing):
  53. print " "+fn
  54. self.fail("%d files are missing an MPL2 header" % len(self.missing))
  55. def scan(self, start, extensions=[], skipdirs=[]):
  56. # scan a whole subdirectory
  57. start = from_sdk_top(start)
  58. for root, dirs, files in os.walk(start):
  59. for d in skipdirs:
  60. if d in dirs:
  61. dirs.remove(d)
  62. for fn in files:
  63. ext = os.path.splitext(fn)[1]
  64. if extensions and ext not in extensions:
  65. continue
  66. absfn = os.path.join(root, fn)
  67. if absfn in absskip:
  68. continue
  69. self.scan_file(absfn)
  70. def scan_file(self, fn):
  71. # scan a single file
  72. if not MPL2_URL in open(fn, "r").read():
  73. relfile = fn[len(sdk_root)+1:]
  74. self.missing.append(relfile)
  75. if __name__ == '__main__':
  76. unittest.main()