mirror of
https://github.com/fergalmoran/picard.git
synced 2025-12-25 10:48:18 +00:00
Add support for fix-header: skip, to ignore a file
Comment formats supported: # /* */ //
This commit is contained in:
@@ -28,6 +28,7 @@ import itertools
|
||||
import os
|
||||
import re
|
||||
import subprocess # nosec: B404
|
||||
import sys
|
||||
|
||||
|
||||
ALIASES = {
|
||||
@@ -113,6 +114,7 @@ def parse_file(path):
|
||||
start = end = None
|
||||
authors_from_file = {}
|
||||
|
||||
skip_pattern = re.compile(r'^(?:#|/\*|//)\s+(fix-header:\s*skip|Automatically\s+generated)', re.IGNORECASE)
|
||||
with open(path) as f:
|
||||
lines = f.readlines()
|
||||
found = defaultdict(lambda: None)
|
||||
@@ -120,8 +122,9 @@ def parse_file(path):
|
||||
found["shebang"] = lines[0].rstrip()
|
||||
del lines[0]
|
||||
for num, line in enumerate(lines):
|
||||
if line.startswith("# Automatically generated"):
|
||||
found['autogenerated'] = num
|
||||
match = skip_pattern.search(line)
|
||||
if match:
|
||||
found['skip'] = match.group(1)
|
||||
return (found, {}, {}, '', "".join(lines))
|
||||
|
||||
for num, line in enumerate(lines):
|
||||
@@ -206,8 +209,8 @@ LICENSE_BOTTOM = """#
|
||||
|
||||
def fix_header(path):
|
||||
found, authors_from_file, authors_from_log, before, after = parse_file(path)
|
||||
if found['autogenerated'] is not None:
|
||||
return None
|
||||
if found['skip'] is not None:
|
||||
return None, found['skip']
|
||||
|
||||
authors = {}
|
||||
for a in authors_from_log:
|
||||
@@ -245,7 +248,7 @@ def fix_header(path):
|
||||
before.strip(),
|
||||
after.strip(),
|
||||
]))
|
||||
return "\n".join(parts)
|
||||
return "\n".join(parts), None
|
||||
|
||||
|
||||
def main():
|
||||
@@ -269,10 +272,14 @@ def main():
|
||||
else:
|
||||
if args.recursive:
|
||||
paths += glob.glob(path + '/*')
|
||||
if not files:
|
||||
print("No valid file found", file=sys.stderr)
|
||||
sys.exit(0)
|
||||
|
||||
for path in files:
|
||||
new_content = fix_header(path)
|
||||
new_content, info = fix_header(path)
|
||||
if new_content is None:
|
||||
print("Skipping %s (%s)" % (path, info), file=sys.stderr)
|
||||
continue
|
||||
if args.in_place:
|
||||
with open(path, 'w') as f:
|
||||
|
||||
Reference in New Issue
Block a user