From 6b71b818534d5b3c909c53ddbf111066a5660ebc Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 15 Apr 2023 17:43:42 -0400 Subject: [PATCH] port-jobs: Changed method used for removing comments Former-commit-id: 954de0d020fb579fb1355fd6918aea898038e751 Former-commit-id: 263ea195e60daddeca5c11fef2e2f5943e9a6eff --- port-jobs/COMMENTS.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/port-jobs/COMMENTS.py b/port-jobs/COMMENTS.py index a5969bd..1890576 100755 --- a/port-jobs/COMMENTS.py +++ b/port-jobs/COMMENTS.py @@ -1,23 +1,24 @@ #!/usr/bin/env python3 from os import listdir -import re +from re import match as rematch from sys import argv # read arguments arguments = argv[1:] -# define regular expression depending on arguments -if len(arguments) > 0 and arguments[0] == 'ALL': - regex = re.compile(r'^\s*#.*$\n?', re.MULTILINE) -else: - regex = re.compile(r'^\s*# PORT.*$\n?', re.MULTILINE) - +# loop through all Python files in the working directory for filename in listdir('working'): if filename.endswith('.py'): filepath = 'working/' + filename # read input file - text = open(filepath, 'r').read() - # run the defined regular expression - new_text = re.sub(regex, '', text) - # write updated text - open(filepath, 'w').write(new_text) + with open(filepath, 'r') as file: + new_file = [] + for line in file: + # determine whether to remove all or only PORT comments + if len(arguments) > 0 and arguments[0] == 'ALL': + if not rematch(r'^\s*#', line): + new_file.append(line) + elif not rematch(r'^\s*# PORT', line): + new_file.append(line) + # write the updated file contents + open(filepath, 'w').writelines(new_file)