|
1 |
| -#!/usr/bin/python3 |
| 1 | +#!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import errno
|
4 | 4 | import json
|
|
47 | 47 |
|
48 | 48 | try:
|
49 | 49 | os.makedirs(sys.argv[3])
|
50 |
| -except Exception as e: |
| 50 | +except OSError as e: |
51 | 51 | if e.errno != errno.EEXIST:
|
52 | 52 | print("Failed to create output directory %s: %s" % (sys.argv[3], e))
|
53 | 53 | sys.exit(1)
|
|
75 | 75 | (sys.argv[2], e), file=sys.stderr)
|
76 | 76 | sys.exit(1)
|
77 | 77 |
|
78 |
| -commentRegex = re.compile("^\s*(//|#)") |
| 78 | +commentRegex = re.compile(r"^\s*(//|#)") |
79 | 79 |
|
80 | 80 |
|
81 | 81 | def isComment(s):
|
82 | 82 | return commentRegex.match(s) is not None
|
83 | 83 |
|
84 | 84 |
|
85 |
| -try: |
86 |
| - with open(sys.argv[1], "r") as f: |
87 |
| - specs = [l for l in f if not isComment(l)] |
88 |
| -except Exception as e: |
89 |
| - print("Failed to open %s: %s\n" % (sys.argv[1], e)) |
| 85 | +def readCsv(file): |
| 86 | + try: |
| 87 | + with open(file, "r") as f: |
| 88 | + specs = [l for l in f if not isComment(l)] |
| 89 | + except Exception as e: |
| 90 | + print("Failed to open %s: %s\n" % (file, e)) |
| 91 | + sys.exit(1) |
| 92 | + |
| 93 | + specs = [row.split(";") for row in specs] |
| 94 | + return specs |
| 95 | + |
| 96 | + |
| 97 | +def readYml(file): |
| 98 | + try: |
| 99 | + import yaml |
| 100 | + with open(file, "r") as f: |
| 101 | + doc = yaml.load(f.read(), yaml.Loader) |
| 102 | + specs = [] |
| 103 | + for ext in doc['extensions']: |
| 104 | + if ext['addsTo']['extensible'] == 'summaryModel': |
| 105 | + for row in ext['data']: |
| 106 | + if isinstance(row[2], bool): |
| 107 | + row[2] = str(row[2]).lower() |
| 108 | + specs.append(row) |
| 109 | + return specs |
| 110 | + except ImportError: |
| 111 | + print("PyYAML not found - try \n pip install pyyaml") |
| 112 | + sys.exit(1) |
| 113 | + except ValueError as e: |
| 114 | + print("Invalid yaml model in %s: %s\n" % (file, e)) |
| 115 | + sys.exit(1) |
| 116 | + except OSError as e: |
| 117 | + print("Failed to open %s: %s\n" % (file, e)) |
| 118 | + sys.exit(1) |
| 119 | + |
| 120 | + |
| 121 | +def readYmlDir(dirname): |
| 122 | + specs = [] |
| 123 | + for f in os.listdir(dirname): |
| 124 | + if f.endswith('.yml'): |
| 125 | + specs += readYml(f"{dirname}/{f}") |
| 126 | + return specs |
| 127 | + |
| 128 | + |
| 129 | +specsFile = sys.argv[1] |
| 130 | +if os.path.isdir(specsFile): |
| 131 | + specs = readYmlDir(specsFile) |
| 132 | +elif specsFile.endswith(".yml") or specsFile.endswith(".yaml"): |
| 133 | + specs = readYml(specsFile) |
| 134 | +elif specsFile.endswith(".csv"): |
| 135 | + spcs = readCsv(specsFile) |
| 136 | +else: |
| 137 | + print(f"Invalid specs {specsFile}. Must be a csv file, a yml file, or a directory of yml files.") |
90 | 138 | sys.exit(1)
|
91 | 139 |
|
| 140 | + |
92 | 141 | projectTestPkgDir = os.path.join(projectDir, "src", "main", "java", "test")
|
93 | 142 | projectTestFile = os.path.join(projectTestPkgDir, "Test.java")
|
94 | 143 |
|
95 | 144 | os.makedirs(projectTestPkgDir)
|
96 | 145 |
|
97 | 146 |
|
98 |
| -def qualifiedOuterNameFromCsvRow(row): |
99 |
| - cells = row.split(";") |
100 |
| - if len(cells) < 2: |
| 147 | +def qualifiedOuterNameFromRow(row): |
| 148 | + if len(row) < 2: |
101 | 149 | return None
|
102 |
| - return cells[0] + "." + cells[1].replace("$", ".") |
| 150 | + return row[0] + "." + row[1].replace("$", ".") |
103 | 151 |
|
104 | 152 |
|
105 | 153 | with open(projectTestFile, "w") as testJava:
|
106 | 154 | testJava.write("package test;\n\npublic class Test {\n\n")
|
107 | 155 |
|
108 | 156 | for i, spec in enumerate(specs):
|
109 |
| - outerName = qualifiedOuterNameFromCsvRow(spec) |
| 157 | + outerName = qualifiedOuterNameFromRow(spec) |
110 | 158 | if outerName is None:
|
111 | 159 | print("A taint specification has the wrong format: should be 'package;classname;methodname....'", file=sys.stderr)
|
112 | 160 | print("Mis-formatted row: " + spec, file=sys.stderr)
|
@@ -140,7 +188,7 @@ def qualifiedOuterNameFromCsvRow(row):
|
140 | 188 | with open(qlFile, "w") as f:
|
141 | 189 | f.write(
|
142 | 190 | "import java\nimport utils.flowtestcasegenerator.GenerateFlowTestCase\n\nclass GenRow extends TargetSummaryModelCsv {\n\n\toverride predicate row(string r) {\n\t\tr = [\n")
|
143 |
| - f.write(",\n".join('\t\t\t"%s"' % spec.strip() for spec in specs)) |
| 191 | + f.write(",\n".join('\t\t\t"%s"' % ';'.join(spec) for spec in specs)) |
144 | 192 | f.write("\n\t\t]\n\t}\n}\n")
|
145 | 193 |
|
146 | 194 | print("Generating tests")
|
@@ -221,7 +269,7 @@ def copyfile(fromName, toFileHandle):
|
221 | 269 | # Make a test extension file
|
222 | 270 | with open(resultYml, "w") as f:
|
223 | 271 | models = "\n".join(' - [%s]' %
|
224 |
| - modelSpecRow[0].strip() for modelSpecRow in supportModelRows) |
| 272 | + modelSpecRow[0].strip() for modelSpecRow in supportModelRows) |
225 | 273 | dataextensions = f"""extensions:
|
226 | 274 | - addsTo:
|
227 | 275 | pack: codeql/java-tests
|
|
0 commit comments