From 7f7010c73e24c998e840a257ae46922aafb71546 Mon Sep 17 00:00:00 2001 From: Adrian L Lange Date: Mon, 24 Apr 2023 17:40:10 +0200 Subject: [PATCH] Use argparse to handle args, moving input file paths to an option as multiline strings --- action.yml | 4 ++-- update.py | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index b72108a..9936713 100644 --- a/action.yml +++ b/action.yml @@ -5,10 +5,10 @@ branding: color: white inputs: files: - description: Space-separated list of license files to update + description: Multiline string of license files to update required: true runs: using: composite steps: - - run: python3 $GITHUB_ACTION_PATH/update.py ${{ inputs.files }} + - run: python3 $GITHUB_ACTION_PATH/update.py -f "${{ inputs.files }}" shell: bash diff --git a/update.py b/update.py index cf8bf47..99586bd 100644 --- a/update.py +++ b/update.py @@ -1,18 +1,36 @@ #!/usr/bin/env python3 +import os import re import sys import fileinput +import argparse from datetime import datetime -if len(sys.argv) <= 1: - print('Please tell me which license file(s) to update') - sys.exit(1) +parser = argparse.ArgumentParser( + usage='%(prog)s [OPTION]', + description='Updates license file(s) to the current year.' +) + +parser.add_argument('-f', '--files', help='Multiline string of file paths', required=True) +args = parser.parse_args() + +paths = [] +for line in iter(args.files.splitlines()): + if line != '': + if not os.path.isfile(line): + print(f'file not found at path "{line}"') + sys.exit(1) + elif line in paths: + print(f'duplicate path "{line}"') + sys.exit(1) + else: + paths.append(line) PATTERN = re.compile(r'^([Cc]opyright.+) ([0-9-]{4,9}) (.+)$') NEW_YEAR = datetime.now().year -for line in fileinput.input(inplace=True): +for line in fileinput.input(files=paths, inplace=True): match = PATTERN.match(line) if match: sys.stdout.write(f'{match.group(1)} {match.group(2)[:4]}-{NEW_YEAR} {match.group(3)}\n')