more comments, less debug

This commit is contained in:
Julian Daube 2020-04-16 11:59:58 +02:00
parent e5285bf94c
commit 768b5d81b4
1 changed files with 14 additions and 6 deletions

20
main.py
View File

@ -6,16 +6,18 @@ import os
from pathlib import Path
import shutil
# parse arguments
parser = argparse.ArgumentParser(description="parse netlist and pack all references to current working directory")
parser.add_argument("files", nargs="*", type=argparse.FileType("r"), default=sys.stdin)
parser.add_argument("-d", action="store_const", const=True, default=False, help="Dryrun")
args = parser.parse_args()
print(args.d)
fileregex = re.compile(r"(?<!\w)file=\"(.*?)\"")
files = {}
# process all netlists
for file in args.files:
location = Path(file.name)
print(f"parsing {file.name}")
@ -25,12 +27,16 @@ for file in args.files:
with open(location.name, "w+") as outfile:
last = 0
# iterate over all matches
for match in fileregex.finditer(content):
start = match.start(1)
end = match.end(1)
path = Path(match.group(1))
destpath = path
# build the destination in the current folder
# for the retrieved file reference
if path.is_absolute():
try:
destpath = path.relative_to(location.parent)
@ -42,35 +48,37 @@ for file in args.files:
if destpath.parts[0] == "..":
destpath = Path(*destpath.parts[1:])
# write the new path the new netlist file
outfile.write(content[last:start])
outfile.write(str(destpath))
# remember file to be copied for later
files[path] = destpath
last = end
outfile.write(content[last:])
# do dryrun
# do dryrun if it is wanted
if args.d:
for file in files:
print(f"copy {file} -> {files[file]}")
exit(0)
# copy all files found in netlist earlier
for file in files:
src = Path(file)
dst = Path(files[file])
if not src.exists():
# create dir
print(f"src does not exist: {str(src)}")
continue
# create directory
dst.parent.mkdir(parents=True, exist_ok=True)
print(file, files[file])
shutil.copyfile(src, dst)