naievly copy other includes as well

This commit is contained in:
Julian Daube 2020-04-16 14:53:59 +02:00
parent 4e03273544
commit 3d0c098575
1 changed files with 162 additions and 35 deletions

197
main.py
View File

@ -6,6 +6,151 @@ 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")
@ -13,51 +158,33 @@ def main():
parser.add_argument("-d", action="store_const", const=True, default=False, help="Dryrun")
args = parser.parse_args()
fileregex = re.compile(r"(?<!\w)file=\"(.*?)\"")
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:
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)
except ValueError:
destpath = Path(*path.parts[-4:])
else:
path = Path.cwd() / path
print(destpath.root)
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:])
outfile.write(content)
# do dryrun if it is wanted
if args.d: