Файли
rocm-systems/utils/update_license.py
T

131 рядки
3.8 KiB
Python
Неформатований Звичайний вигляд Історія

#!/usr/bin/env python3
# -------------------------------------------------------------------------------
# Support script for license header management.
# -------------------------------------------------------------------------------
import argparse
import logging
import glob
import os
import sys
import re
import filecmp
import shutil
2023-02-13 14:28:25 -06:00
begDelim = "######bl$"
endDelim = "######el$"
maxHeaderLines = 200
2023-02-13 14:28:25 -06:00
def cacheLicenseFile(infile, comment="#"):
if not os.path.isfile(infile):
logging.error("Unable to access license file - >%s" % infile)
sys.exit(1)
license = ""
2023-02-13 14:28:25 -06:00
with open(infile, "r") as file_in:
for line in file_in:
license += comment
if line.strip() != "":
license += " "
license += line
2023-02-13 14:28:25 -06:00
return license
parser = argparse.ArgumentParser()
2023-02-13 14:28:25 -06:00
parser.add_argument("--license", required=True, help="License File")
parser.add_argument("--source", required=True, help="Source directory")
parser.add_argument("--dryrun", help="enable dryrun mode", action="store_true")
group = parser.add_mutually_exclusive_group(required=True)
2023-02-13 14:28:25 -06:00
group.add_argument("--extension", help="file extension to parse")
group.add_argument("--files", help="specific file(s) to parse")
2023-02-13 14:28:25 -06:00
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
args = parser.parse_args()
srcDir = args.source
fileExtension = None
specificFiles = None
if args.extension:
fileExtension = args.extension
if args.files:
specificFiles = args.files.split(",")
print("")
logging.info("Source directory = %s" % srcDir)
if fileExtension:
logging.info("File extension = %s" % fileExtension)
if specificFiles:
logging.info("Specific files = %s" % specificFiles)
# cache license file
license = cacheLicenseFile(args.license)
# Scan files in provided source directory...
2023-02-13 14:28:25 -06:00
for filename in glob.iglob(srcDir + "/**", recursive=True):
# skip directories
if os.path.isdir(filename):
2023-02-13 14:28:25 -06:00
continue
# File matching options:
2023-02-13 14:28:25 -06:00
# (1) filter non-matching extensions
if fileExtension:
if not filename.endswith(fileExtension):
continue
# or, (2) filter for specific filename
if specificFiles:
found = False
for file in specificFiles:
2023-02-13 14:28:25 -06:00
fullPath = os.path.join(srcDir, file)
if fullPath == filename:
found = True
break
if not found:
continue
2023-02-13 14:28:25 -06:00
logging.debug("Examining %s for license..." % filename)
# Update license header contents if delimiters are found
2023-02-13 14:28:25 -06:00
with open(filename, "r") as file_in:
baseName = os.path.basename(filename)
2023-02-13 14:28:25 -06:00
dirName = os.path.dirname(filename)
tmpFile = dirName + "/." + baseName + ".tmp"
file_out = open(tmpFile, "w")
for line in file_in:
2023-02-13 14:28:25 -06:00
if re.search(begDelim, line):
logging.debug("Found beginning delimiter")
file_out.write(line)
file_out.write(license)
foundEnd = False
for i in range(maxHeaderLines):
line = file_in.readline()
2023-02-13 14:28:25 -06:00
if re.search(endDelim, line):
logging.debug("Found ending delimiter")
file_out.write(line)
foundEnd = True
break
if not foundEnd:
logging.error("Unable to find end of delimited header")
sys.exit(1)
else:
file_out.write(line)
file_out.close()
# Check if file changed and update
2023-02-13 14:28:25 -06:00
if not filecmp.cmp(filename, tmpFile, shallow=False):
logging.info("%s changed" % filename)
2023-02-13 14:28:25 -06:00
shutil.copystat(filename, tmpFile)
if not args.dryrun:
2023-02-13 14:28:25 -06:00
os.rename(tmpFile, filename)
else:
os.unlink(tmpFile)