| 1 |
#! /usr/bin/env python3 |
| 2 |
# SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 3 |
# SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 4 |
|
| 5 |
import argparse |
| 6 |
import dataclasses |
| 7 |
import errno |
| 8 |
import logging |
| 9 |
import re |
| 10 |
|
| 11 |
# REUSE-IgnoreStart |
| 12 |
parser = argparse.ArgumentParser( |
| 13 |
description='add SPDX header to DUNE project modules' |
| 14 |
) |
| 15 |
parser.add_argument( |
| 16 |
'--license', '-l', dest='license', |
| 17 |
help='SPDX license name', |
| 18 |
default='LicenseRef-GPL-2.0-only-with-DUNE-exception', |
| 19 |
) |
| 20 |
parser.add_argument( |
| 21 |
'--copyright', '-c', dest='copyright', |
| 22 |
help='copyright information', |
| 23 |
default='Copyright © DUNE Project contributors, see file LICENSE.md in module root', |
| 24 |
) |
| 25 |
parser.add_argument( |
| 26 |
'--like', dest='like', |
| 27 |
help='handle files like a file named like this', |
| 28 |
), |
| 29 |
parser.add_argument( |
| 30 |
'--log', dest='log', |
| 31 |
help='set log level', |
| 32 |
), |
| 33 |
parser.add_argument( |
| 34 |
'files', nargs='+', |
| 35 |
help='files to add copyright and license headers to', |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
@dataclasses.dataclass |
| 40 |
class Rule: |
| 41 |
pattern: str |
| 42 |
prefix: str = "" |
| 43 |
suffix: str = "" |
| 44 |
line_prefix: str = "" |
| 45 |
line_suffix: str = "" |
| 46 |
use_license_file: bool = False |
| 47 |
skip_lines: re.Pattern = None |
| 48 |
|
| 49 |
def match(self, filename) -> bool: |
| 50 |
return bool(re.search(self.pattern, filename)) |
| 51 |
|
| 52 |
|
| 53 |
rules = [ |
| 54 |
Rule( |
| 55 |
pattern=r"\.(?:c|cc|cc\.in|h|hh|hh\.in|ct|ht|t|geo)$|config\.h\.cmake$", |
| 56 |
line_prefix="// ", |
| 57 |
), |
| 58 |
Rule( |
| 59 |
pattern=r"\.(?:rst|rst\.in)$", |
| 60 |
prefix="::\n", |
| 61 |
line_prefix=" ", |
| 62 |
suffix="\n", |
| 63 |
), |
| 64 |
Rule( |
| 65 |
pattern=r"\.bib$", |
| 66 |
line_prefix="@Comment ", |
| 67 |
suffix="\n", |
| 68 |
), |
| 69 |
Rule( |
| 70 |
pattern=r"\.dgf$", |
| 71 |
line_prefix="% ", |
| 72 |
skip_lines=re.compile("^DGF"), |
| 73 |
), |
| 74 |
Rule( |
| 75 |
pattern=r"\.tex$", |
| 76 |
line_prefix="% ", |
| 77 |
suffix="\n", |
| 78 |
), |
| 79 |
Rule( |
| 80 |
pattern=r"(?:\.amc|\.cmake|\.cmake\.in|\.gitignore|\.ini|\.pc\.in" |
| 81 |
r"|\.pl|\.py|\.py\.in|\.sh|\.toml|\.toml\.in|\.yml" |
| 82 |
r"|CMakeLists.txt|Doxylocal|dune.module|MANIFEST\.in)$", |
| 83 |
line_prefix="# ", |
| 84 |
suffix="\n", |
| 85 |
), |
| 86 |
Rule( |
| 87 |
pattern=r"/doxygen/.*\.txt$", |
| 88 |
line_prefix="// ", |
| 89 |
suffix="\n", |
| 90 |
), |
| 91 |
Rule( |
| 92 |
pattern=r"(?:\.md|INSTALL)$", |
| 93 |
prefix="<!--\n", |
| 94 |
suffix="-->\n\n", |
| 95 |
), |
| 96 |
Rule( |
| 97 |
pattern=r"\.(?:cel|eps|fig|pdf|png|svg|vrt)$", |
| 98 |
use_license_file=True, |
| 99 |
), |
| 100 |
Rule( |
| 101 |
# gmsh's MSH file format support comments via unknown sections |
| 102 |
# ($Comment ... $EndComment), but DUNE does not handle that. |
| 103 |
# Reference: https://gmsh.info/doc/texinfo/gmsh.html#MSH-file-format |
| 104 |
pattern=r"\.msh", |
| 105 |
use_license_file=True, |
| 106 |
), |
| 107 |
] |
| 108 |
|
| 109 |
|
| 110 |
skip_lines = re.compile(r'-\*-|vi(?:m)?:|^#!') |
| 111 |
|
| 112 |
|
| 113 |
class Notice: |
| 114 |
def __init__(self, copyright, license): |
| 115 |
self.copyright = f'SPDX-FileCopyrightInfo: {copyright}' |
| 116 |
self.license = f'SPDX-License-Identifier: {license}' |
| 117 |
|
| 118 |
def match(self, lines) -> bool: |
| 119 |
pattern = re.compile(f'(?:{re.escape(self.copyright)}|{re.escape(self.license)})') |
| 120 |
return any(pattern.search(line) for line in lines) |
| 121 |
|
| 122 |
|
| 123 |
def rule_for_file(filename) -> Rule: |
| 124 |
for rule in rules: |
| 125 |
if rule.match(filename): |
| 126 |
return rule |
| 127 |
return None |
| 128 |
|
| 129 |
|
| 130 |
def line_for_notice(lines, rule: Rule) -> int: |
| 131 |
for index, line in enumerate(lines): |
| 132 |
if not skip_lines.search(line) \ |
| 133 |
and (rule.skip_lines is None or not rule.skip_lines.search(line)): |
| 134 |
return index |
| 135 |
return len(lines) |
| 136 |
|
| 137 |
|
| 138 |
def apply_rule_to_file(filename, rule: Rule, notice: Notice): |
| 139 |
if rule.use_license_file: |
| 140 |
try: |
| 141 |
with open(f"{filename}.license", "xt") as fh: |
| 142 |
logging.debug(f"{filename}: create separate .license file") |
| 143 |
print(notice.copyright, file=fh) |
| 144 |
print(notice.license, file=fh) |
| 145 |
except OSError as e: |
| 146 |
if e.errno == errno.EEXIST: |
| 147 |
logging.info(f"{filename}: separate .license file already exists") |
| 148 |
else: |
| 149 |
raise |
| 150 |
else: |
| 151 |
with open(filename, "rt") as fh: |
| 152 |
lines = fh.readlines() |
| 153 |
if notice.match(lines): |
| 154 |
logging.info(f"{filename}: already contains a notice") |
| 155 |
return |
| 156 |
index = line_for_notice(lines, rule) |
| 157 |
logging.debug(f"{filename}: Will insert notice at line {index}") |
| 158 |
if rule.suffix: |
| 159 |
lines.insert(index, rule.suffix) |
| 160 |
lines.insert(index, f"{rule.line_prefix}{notice.license}{rule.line_suffix}\n") |
| 161 |
lines.insert(index, f"{rule.line_prefix}{notice.copyright}{rule.line_suffix}\n") |
| 162 |
if rule.prefix: |
| 163 |
lines.insert(index, rule.prefix) |
| 164 |
with open(filename, "wt") as fh: |
| 165 |
print(*lines, sep='', end='', file=fh) |
| 166 |
|
| 167 |
|
| 168 |
if __name__ == '__main__': |
| 169 |
args = parser.parse_args() |
| 170 |
if args.log: |
| 171 |
logging.basicConfig(level=getattr(logging, args.log.upper())) |
| 172 |
notice = Notice(args.copyright, args.license) |
| 173 |
for filename in args.files: |
| 174 |
logging.debug(f"Processing {filename}") |
| 175 |
if args.like: |
| 176 |
rule = rule_for_file(args.like) |
| 177 |
else: |
| 178 |
rule = rule_for_file(filename) |
| 179 |
if rule is None: |
| 180 |
logging.warning(f"{filename}: No rule found for this file. Add a rule or try --like.") |
| 181 |
continue |
| 182 |
apply_rule_to_file(filename, rule, notice) |
| 183 |
|
| 184 |
|
| 185 |
# add-spdx() { |
| 186 |
# local file line_prefix prefix suffix |
| 187 |
# local copying_file="LICENSE.md" |
| 188 |
# local license="LicenseRef-GPL-2.0-only-with-DUNE-exception" |
| 189 |
# xxlicense="LGPL-2.1-or-later" |
| 190 |
# for file in "$@"; do |
| 191 |
# prefix="" |
| 192 |
# line_prefix="" |
| 193 |
# suffix="" |
| 194 |
# license_file="${file}" |
| 195 |
# case "${file}" in |
| 196 |
# *.cc|*.hh|*config.h.cmake|*.cc.in|*.ct|*.ht|*.t) line_prefix="// " ;; |
| 197 |
# */doxygen/*.txt) line_prefix="// "; suffix=$'\n' ;; |
| 198 |
# *CMakeLists.txt|*.py|*.gitignore|*.yml|*Doxylocal|*.toml|*.pc.in|*dune.module|*.cmake|*.ini) line_prefix="# "; suffix=$'\n' ;; |
| 199 |
# *.md|*INSTALL) prefix=$'<!--\n'; suffix=$'\n-->\n' ;; |
| 200 |
# *.tex) line_prefix="% "; suffix=$'\n' ;; |
| 201 |
# *.svg|*.png|*.eps|*.pdf) license_file="${file}.license"; file="" ;; |
| 202 |
# esac |
| 203 |
# |
| 204 |
# ed ${file} <<EOT |
| 205 |
# 0i |
| 206 |
# ${prefix}${line_prefix}SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file ${copying_file} in module root |
| 207 |
# ${line_prefix}SPDX-License-Identifier: ${license}${suffix} |
| 208 |
# . |
| 209 |
# w ${license_file} |
| 210 |
# q |
| 211 |
# EOT |
| 212 |
# done |
| 213 |
# } |
| 214 |
# REUSE-IgnoreEnd |