incubator-nuttx/tools/licensing/apachize.py

55 lines
1.7 KiB
Python
Raw Normal View History

2020-09-18 02:55:15 +08:00
#!/usr/bin/env python3
import re
import os
import sys
apache = r"""
/****************************************************************************
* PATH
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/"""
2021-04-05 08:09:12 +08:00
2020-09-18 02:55:15 +08:00
def apachize(path, header):
2021-04-05 08:09:12 +08:00
relpath = os.path.relpath(path, os.environ["TOPDIR"])
header = re.sub("PATH", relpath, header)
2020-09-18 02:55:15 +08:00
with open(path) as f:
s = f.read()
2021-04-05 08:09:12 +08:00
s = re.sub("(?i)/\*\*\*.+?(?:Copyright).+?\*\*\*+/", header, s, 1, re.DOTALL)
2020-09-18 02:55:15 +08:00
print(s)
2021-04-05 08:09:12 +08:00
if len(sys.argv) != 2:
print("Usage: ./apachize.py <file>", file=sys.stderr)
print(
"This will replace the license header of the passed file to that of Apache 2.0 and print it to stdout",
file=sys.sterr,
)
2020-09-18 02:55:15 +08:00
sys.exit(2)
2021-04-05 08:09:12 +08:00
if not "TOPDIR" in os.environ:
print(
"Please define the TOPDIR environment variable to the full path to nuttx/ root",
file=sys.stderr,
)
2020-09-18 02:55:15 +08:00
sys.exit(2)
apachize(sys.argv[1], apache)