- Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathpackage.py
111 lines (90 loc) · 3.72 KB
/
package.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
importos
importre
fromargparseimportArgumentParser
DIR="."
CURVES= {"SecP256r1", "SecP256k1", "SecP512r1", "Wei25519"}
FILTERED_FILES= {"UnitTests.java", "Example.java", "Integer.java"}
defload_imports(files):
imports=set()
forfileinfiles:
withopen(file, "r") asf:
forlineinf:
ifre.search(r"import .*;", line):
imports.add(line.strip())
# Remove all imports that are already imported by a wildcard
imports_copy=imports.copy()
forimpinfilter(lambdax: "*;"inx, imports_copy):
forotherinimports_copy:
ifother!=impandother.startswith(imp[:-2]):
imports.remove(other)
returnimports
defpackage_file(file, keep_locks=False):
lines= []
skip=False
withopen(file, "r") asf:
forlineinf:
if"[DependencyEnd:ObjectLocker]"inline:
skip=False
continue
ifnotkeep_locksand"[DependencyBegin:ObjectLocker]"inline:
skip=True
ifskip:
continue
ifre.search(r"import .*;", line) orre.search(r"package .*;", line):
continue
ifnotkeep_locksandre.search(r"(un)?lock\(.*\)", line) orre.search(r"registerLock\(", line):
continue
lines.append(
(" "+line.replace("public class ", "public static class ")).rstrip())
# Remove empty starting and last lines
whilelines[0].strip() =="":
lines.pop(0)
whilelines[-1].strip() =="":
lines.pop(-1)
returnos.linesep.join(lines)
defget_version(file):
withopen(file, "r") asf:
forlineinf:
if (matches:=re.search(r"version \'(.*)\'", line)):
returnmatches.group(1)
return"0.0"
defmain():
parser=ArgumentParser(
prog="package.py",
description="Package the JCMathLib library into a single file."
)
parser.add_argument(
"-d", "--dir", help="Directory to package", default=DIR)
parser.add_argument("-k", "--keep-locks", help="Keep locks",
action="store_true", default=False)
parser.add_argument("-c", "--curves", help="Curves to include",
default=["SecP256k1"], nargs="+", choices=sorted(CURVES))
parser.add_argument("-p", "--package",
help="Package name", default="your_package")
parser.add_argument("-o", "--output", help="Output file",
default="jcmathlib.java")
args=parser.parse_args()
version=get_version(f"{args.dir}/applet/build.gradle")
sources=f"{args.dir}/applet/src/main/java/opencrypto/jcmathlib/"
filtered_files=FILTERED_FILES.copy()
ifnotargs.keep_locks:
filtered_files=filtered_files.union({"ObjectLocker.java"})
included_files=sorted(map(
lambdax: sources+x, filter(lambdax: x.endswith(".java") andxnotinfiltered_files, os.listdir(sources))))
included_files+=list(map(lambdax: f"{sources}curves/{x}.java", args.curves))
imports=load_imports(included_files)
withopen(args.output, "w") asf:
print("package "+args.package+";", file=f)
print(file=f)
forimpinsorted(imports):
print(imp, file=f)
print(file=f)
print("/**", file=f)
print(f" * Packaged JCMathLib v{version} (https://github.com/OpenCryptoProject/JCMathLib).", file=f)
print(" */", file=f)
print("public class jcmathlib {", file=f)
print((os.linesep*2).join(map(lambdax: package_file(x,
args.keep_locks), included_files)), file=f)
print("}", file=f)
if__name__=="__main__":
main()