apachize.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. import sys
  5. apache = r"""
  6. /****************************************************************************
  7. * PATH
  8. *
  9. * Licensed to the Apache Software Foundation (ASF) under one or more
  10. * contributor license agreements. See the NOTICE file distributed with
  11. * this work for additional information regarding copyright ownership. The
  12. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  13. * "License"); you may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  21. * License for the specific language governing permissions and limitations
  22. * under the License.
  23. *
  24. ****************************************************************************/"""
  25. def apachize(path, header):
  26. relpath = os.path.relpath(path, os.environ["TOPDIR"])
  27. header = re.sub("PATH", relpath, header)
  28. with open(path) as f:
  29. s = f.read()
  30. s = re.sub("(?i)/\*\*\*.+?(?:Copyright).+?\*\*\*+/", header, s, 1, re.DOTALL)
  31. print(s)
  32. if len(sys.argv) != 2:
  33. print("Usage: ./apachize.py <file>", file=sys.stderr)
  34. print(
  35. "This will replace the license header of the passed file to that of Apache 2.0 and print it to stdout",
  36. file=sys.stderr,
  37. )
  38. sys.exit(2)
  39. if "TOPDIR" not in os.environ:
  40. print(
  41. "Please define the TOPDIR environment variable to the full path to nuttx/ root",
  42. file=sys.stderr,
  43. )
  44. sys.exit(2)
  45. apachize(sys.argv[1], apache)