Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Python by denz ( 8 years ago )
#!/usr/bin/env python3
# >=3.6
import sys
import logging
from pathlib import Path
import argparse
from glob import glob
import fileinput
import logging
def get_argparser():
parser = argparse.ArgumentParser()
parser.add_argument('path', type=Path)
parser.add_argument('--namepattern', default='**') #https://docs.python.org/3/library/glob.html
parser.add_argument('--non-recursive', default=True, action='store_false', dest="recursive")
parser.add_argument('--replaced-line', default='create procedure nn();')
parser.add_argument('--replacement-line', default='drop procedure nn;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n')
parser.add_argument('--replace-if-in-first-lines', default=1, type=int)
parser.add_argument('--debug', default=False, action='store_true')
return parser
def main(argv=sys.argv[1:]):
args = get_argparser().parse_args(argv)
logging.basicConfig(level=args.debug and logging.DEBUG or logging.INFO)
search = args.path / args.namepattern
logging.debug(f"Args {args}")
logging.info(f"Replacing in {search}")
filepathes = [ Path(filepath) for filepath in glob(str(search), recursive=args.recursive) if Path(filepath).is_file() ]
for filepath in filepathes:
logging.debug(f"Processing: {filepath}")
loglines = []
with fileinput.FileInput(filepath, inplace=True) as file:
for line in file:
if file.lineno() <= args.replace_if_in_first_lines and line.startswith(args.replaced_line): loglines.append([f'{file.filename()}:{file.lineno()}:{line!r}->{args.replacement_line!r}'])
sys.stdout.write(args.replacement_line)
else:
sys.stdout.write(line)
for logline in loglines:
logging.debug(logline)
if __name__ == '__main__':
main()
Revise this Paste
Parent: 95720