forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoirpacker.py
executable file
·59 lines (48 loc) · 1.52 KB
/
noirpacker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python3
import os
import re
import sys
def read_dir(path):
ret = ""
modules = {}
# First find all modules and group together modules of the same name.
# This can happen when we have a `foo.nr` file and `foo` directory.
for entry in os.scandir(path):
if entry.is_dir():
append_entry(modules, entry.name, entry)
elif entry.is_file() and entry.name.endswith(".nr"):
if entry.name != "main.nr":
append_entry(modules, entry.name[:-3], entry)
else:
append_entry(modules, "", entry)
for name, entries in modules.items():
# No mod construct for the main module
if name != "":
ret += f"mod {name} {{\n"
for entry in entries:
if entry.is_dir():
ret += read_dir(entry)
elif entry.is_file():
ret += sanitize(open(entry).read())
if name != "":
ret += "\n}\n"
return ret
def append_entry(modules, name, entry):
if name in modules:
modules[name].append(entry)
else:
modules[name] = [entry]
# Remove 'mod foo;' declarations from files
def sanitize(contents):
return re.sub(r'\bmod \w+;', '', contents)
if len(sys.argv) < 2:
print('usage: ./noirpacker.py <path-to-src-dir> [output-file]')
else:
path = sys.argv[1]
packed_dir = read_dir(path)
if len(sys.argv) == 2:
print(packed_dir)
else:
f = open(sys.argv[2], 'w')
f.write(packed_dir)
f.close()