working version

This commit is contained in:
Julian Daube 2020-04-16 11:50:53 +02:00
commit e5285bf94c

76
main.py Normal file
View File

@ -0,0 +1,76 @@
import argparse
import sys
import re
import os
from pathlib import Path
import shutil
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 = {}
for file in args.files:
location = Path(file.name)
print(f"parsing {file.name}")
content = file.read()
with open(location.name, "w+") as outfile:
last = 0
for match in fileregex.finditer(content):
start = match.start(1)
end = match.end(1)
path = Path(match.group(1))
destpath = path
if path.is_absolute():
try:
destpath = path.relative_to(location.parent)
except ValueError:
destpath = Path(*path.parts[-4:])
else:
path = Path.cwd() / path
print(destpath.root)
if destpath.parts[0] == "..":
destpath = Path(*destpath.parts[1:])
outfile.write(content[last:start])
outfile.write(str(destpath))
files[path] = destpath
last = end
outfile.write(content[last:])
# do dryrun
if args.d:
for file in files:
print(f"copy {file} -> {files[file]}")
exit(0)
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
dst.parent.mkdir(parents=True, exist_ok=True)
print(file, files[file])
shutil.copyfile(src, dst)