naievly copy other includes as well
This commit is contained in:
parent
4e03273544
commit
3d0c098575
197
main.py
197
main.py
@ -6,6 +6,151 @@ import os
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
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():
|
def main():
|
||||||
# parse arguments
|
# parse arguments
|
||||||
parser = argparse.ArgumentParser(description="parse netlist and pack all references to current working directory")
|
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")
|
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=\"(.*?)\"")
|
|
||||||
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("parsing {}".format(file.name))
|
print("parsing {}".format(file.name))
|
||||||
|
|
||||||
content = file.read()
|
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:
|
with open(location.name, "w+") as outfile:
|
||||||
last = 0
|
outfile.write(content)
|
||||||
|
|
||||||
# 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:])
|
|
||||||
|
|
||||||
# do dryrun if it is wanted
|
# do dryrun if it is wanted
|
||||||
if args.d:
|
if args.d:
|
||||||
|
Loading…
Reference in New Issue
Block a user