remove broken stdin

This commit is contained in:
Julian Daube 2020-04-16 12:04:46 +02:00
parent 768b5d81b4
commit c836bdee49

119
main.py
View File

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