1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#!/usr/bin/env python3
# Utility for detecting incorrect LAMMPS homepage URLs
#
# Written by Richard Berger (Temple University)
from __future__ import print_function
import sys
if sys.version_info.major < 3:
sys.exit('This script must be run with Python 3.5 or later')
if sys.version_info.minor < 5:
sys.exit('This script must be run with Python 3.5 or later')
import os
import glob
import re
import yaml
import argparse
import shutil
DEFAULT_CONFIG = """
recursive: true
include:
- cmake/**
- doc
- doc/src/**
- python
- src/**
- tools/coding_standard
patterns:
- "*.c"
- "*.cmake"
- "*.cpp"
- "*.h"
- "*.md"
- "*.py"
- "*.rst"
- "*.sh"
- ".gitignore"
- "README"
- "requirements.txt"
"""
def check_homepage(f):
pattern = re.compile(r'.*lammps.sandia.gov.*') # lgtm [py/incomplete-hostname-regexp]
lineno = 1
errors = set()
for line in f:
if pattern.match(line):
errors.add(lineno)
lineno += 1
return errors
def check_file(path):
if path.find('homepage.py') >= 0: return { 'homepage_errors' : '' }
encoding = 'UTF-8'
homepage_errors = set()
try:
with open(path, 'r') as f:
homepage_errors = check_homepage(f)
except UnicodeDecodeError:
encoding = 'ISO-8859-1'
try:
with open(path, 'r', encoding=encoding) as f:
homepage_errors = check_homepage(f)
except Exception:
encoding = 'unknown'
return {
'homepage_errors': homepage_errors,
'encoding': encoding
}
def fix_file(path, check_result):
if path.find('homepage.py') >= 0: return
newfile = path + ".modified"
with open(newfile, 'w', encoding='UTF-8') as out:
with open(path, 'r', encoding=check_result['encoding']) as src:
for line in src:
newline = line.replace("lammps.sandia.gov/doc/","docs.lammps.org/")
newline = newline.replace("http://lammps.sandia.gov/","https://www.lammps.org/")
newline = newline.replace("http://lammps.sandia.gov,","https://www.lammps.org/")
newline = newline.replace("lammps.sandia.gov","www.lammps.org")
newline = newline.replace("http://www.lammps.org","https://www.lammps.org")
print(newline, end='', file=out)
shutil.copymode(path, newfile)
shutil.move(newfile, path)
def check_folder(directory, config, fix=False, verbose=False):
success = True
files = []
for base_path in config['include']:
for pattern in config['patterns']:
path = os.path.join(directory, base_path, pattern)
files += glob.glob(path, recursive=config['recursive'])
for f in files:
path = os.path.normpath(f)
if verbose:
print("Checking file:", path)
result = check_file(path)
has_resolvable_errors = False
for lineno in result['homepage_errors']:
print("[Error] Incorrect LAMMPS homepage @ {}:{}".format(path, lineno))
has_resolvable_errors = True
if has_resolvable_errors:
if fix:
print("Applying automatic fixes to file:", path)
fix_file(path, result)
else:
success = False
return success
def main():
parser = argparse.ArgumentParser(description='Utility for detecting and fixing whitespace issues in LAMMPS')
parser.add_argument('-c', '--config', metavar='CONFIG_FILE', help='location of a optional configuration file')
parser.add_argument('-f', '--fix', action='store_true', help='automatically fix URLs')
parser.add_argument('-v', '--verbose', action='store_true', help='verbose output')
parser.add_argument('DIRECTORY', help='directory (or file) that should be checked')
args = parser.parse_args()
lammpsdir = os.path.abspath(os.path.expanduser(args.DIRECTORY))
if args.config:
with open(args.config, 'r') as cfile:
config = yaml.load(cfile, Loader=yaml.FullLoader)
else:
config = yaml.load(DEFAULT_CONFIG, Loader=yaml.FullLoader)
if os.path.isdir(lammpsdir):
if not check_folder(lammpsdir, config, args.fix, args.verbose):
sys.exit(1)
else:
success = True
path = os.path.normpath(lammpsdir)
if args.verbose:
print("Checking file:", path)
result = check_file(path)
has_resolvable_errors = False
for lineno in result['homepage_errors']:
print("[Error] Incorrect LAMMPS homepage @ {}:{}".format(path, lineno))
has_resolvable_errors = True
if has_resolvable_errors:
if args.fix:
print("Applying automatic fixes to file:", path)
fix_file(path, result)
else:
success = False
if not success:
sys.exit(1)
if __name__ == "__main__":
main()
|