packer/main.py

212 lines
5.3 KiB
Python

import argparse
import sys
import re
import os
from pathlib import Path
import shutil
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:]
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()
files = {}
# process all netlists
for file in args.files:
location = Path(file.name)
print("parsing {}".format(file.name))
content = file.read()
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
with open(location.name, "w+") as outfile:
outfile.write(content)
# do dryrun if it is wanted
if args.d:
for file in files:
print("copy {} -> {}".format(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():
print("src does not exist: {}".format(str(src)))
continue
# create directory
dst.parent.mkdir(parents=True)
shutil.copyfile(str(src), str(dst))
if __name__ == "__main__":
main()