packer/main.py

212 lines
5.3 KiB
Python
Raw Permalink Normal View History

2020-04-16 11:50:53 +02:00
import argparse
import sys
import re
import os
from pathlib import Path
import shutil
2020-04-16 14:53:59 +02:00
def make_safe(path):
# make sure the path does not climb up
path = Path(path)
while len(path.parts) > 0 and path.parts[0] == "..":
path = Path(*path.parts[1:])
return path
def contains_envvars(path):
for part in path.parts:
if str(part)[0] == "$":
return True
return False
def find_file_refs(content, location):
temp = ""
last = 0
files = {}
fileregex = re.compile(r"(?<!\w)file=\"(.*?)\"")
# iterate over all matches
for match in fileregex.finditer(content):
start = match.start(1)
end = match.end(1)
path = Path(match.group(1))
if contains_envvars(path):
# ignore paths that depend on environment
continue
destpath = path
# build the destination in the current folder
# for the retrieved file reference
if path.is_absolute():
# remove all but 4 layers from the path
destpath = Path(*path.parts[-4:])
if destpath.is_absolute():
# did not help, just remove the absolute part
destpath = Path(*path.parts[1:])
else:
path = location.parent / path
destpath = make_safe(destpath)
# write the new path the new netlist file
temp += content[last:start]
temp += str(destpath)
# remember file to be copied for later
files[path] = destpath
last = end
temp += content[last:]
return files, temp
def find_ahdl_includes(content, location):
fileregex = re.compile(r"ahdl_include *\"(.*?)\"")
files = {}
temp = ""
last = 0
print(len(content))
for match in fileregex.finditer(content):
path = Path(match.group(1))
destpath = path
if contains_envvars(path):
# ignore paths that depend on environment
continue
if not path.is_absolute():
# prepent path with netlist location if absolute
path = location.parent / path
if destpath.is_absolute():
# make destpath relative
destpath = Path(*destpath.parts[-4:])
if destpath.is_absolute():
destpath = Path(*destpath.parts[1:])
# add prefix for ahdl files
destpath = Path("ahdl") / make_safe(destpath)
temp += content[last:match.start(1)]
temp += str(destpath)
last = match.end(1)
destpath = Path.cwd() / destpath
# remember where to copy file to
files[path] = destpath
return files, temp + content[last:]
def find_includes(content, location):
fileregex = re.compile(r"(?<!\w)include *\"(.*?)\"")
files = {}
temp = ""
last = 0
print(len(content))
for match in fileregex.finditer(content):
path = Path(match.group(1))
destpath = path
if contains_envvars(path):
# ignore paths that depend on environment
continue
if not path.is_absolute():
# prepent path with netlist location if absolute
path = location.parent / path
if destpath.is_absolute():
# make destpath relative
destpath = Path(*destpath.parts[-4:])
if destpath.is_absolute():
destpath = Path(*destpath.parts[1:])
# add prefix for ahdl files
destpath = Path("include") / make_safe(destpath)
temp += content[last:match.start(1)]
temp += str(destpath)
last = match.end(1)
destpath = Path.cwd() / destpath
# remember where to copy file to
files[path] = destpath
return files, temp + content[last:]
2020-04-16 12:04:46 +02:00
def main():
# 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"))
parser.add_argument("-d", action="store_const", const=True, default=False, help="Dryrun")
args = parser.parse_args()
2020-04-16 14:53:59 +02:00
2020-04-16 12:04:46 +02:00
files = {}
# process all netlists
for file in args.files:
location = Path(file.name)
2020-04-16 12:24:39 +02:00
print("parsing {}".format(file.name))
2020-04-16 12:04:46 +02:00
content = file.read()
2020-04-16 14:53:59 +02:00
temp, content = find_file_refs(content, location)
for (key, value) in temp.items():
files[key] = value
temp, content = find_ahdl_includes(content, location)
for (key, value) in temp.items():
files[key] = value
temp, content = find_includes(content, location)
for (key, value) in temp.items():
files[key] = value
2020-04-16 12:04:46 +02:00
with open(location.name, "w+") as outfile:
2020-04-16 14:53:59 +02:00
outfile.write(content)
2020-04-16 12:04:46 +02:00
# do dryrun if it is wanted
if args.d:
for file in files:
2020-04-16 12:24:39 +02:00
print("copy {} -> {}".format(file, files[file]))
2020-04-16 12:04:46 +02:00
exit(0)
# copy all files found in netlist earlier
2020-04-16 11:50:53 +02:00
for file in files:
2020-04-16 12:04:46 +02:00
src = Path(file)
dst = Path(files[file])
2020-04-16 11:50:53 +02:00
2020-04-16 12:04:46 +02:00
if not src.exists():
2020-04-16 13:34:14 +02:00
print("src does not exist: {}".format(str(src)))
continue
2020-04-16 11:50:53 +02:00
2020-04-16 12:04:46 +02:00
# create directory
2020-04-16 13:34:14 +02:00
dst.parent.mkdir(parents=True)
shutil.copyfile(str(src), str(dst))
2020-04-16 12:04:46 +02:00
2020-04-16 11:50:53 +02:00
2020-04-16 12:04:46 +02:00
if __name__ == "__main__":
main()