Skip to content

Commit 8adaee0

Browse files
authored
Merge pull request github#5453 from tamasvajk/feature/use_codeql_stubs
C#: Adjust make_stubs.py to use codeql instead of odasa
2 parents 6109ef5 + e516092 commit 8adaee0

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

csharp/ql/src/Stubs/make_stubs.py

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@
4343
print("Test directory does not contain .cs files. Please specify a working qltest directory.")
4444
exit(1)
4545

46-
cmd = ['odasa', 'selfTest']
47-
print('Running ' + ' '.join(cmd))
48-
if subprocess.check_call(cmd):
49-
print("odasa selfTest failed. Ensure odasa is on your current path.")
50-
exit(1)
51-
5246
csharpQueries = os.path.abspath(os.path.dirname(sys.argv[0]))
5347
outputFile = os.path.join(testDir, 'stubs.cs')
5448

@@ -58,56 +52,75 @@
5852
os.remove(outputFile) # It would interfere with the test.
5953
print("Removed previous", outputFile)
6054

61-
cmd = ['odasa', 'qltest', '--optimize', '--leave-temp-files', testDir]
55+
cmd = ['codeql', 'test', 'run', '--keep-databases', testDir]
6256
print('Running ' + ' '.join(cmd))
6357
if subprocess.check_call(cmd):
64-
print("qltest failed. Please fix up the test before proceeding.")
58+
print("codeql test failed. Please fix up the test before proceeding.")
6559
exit(1)
6660

67-
dbDir = os.path.join(testDir, os.path.basename(testDir) + ".testproj", "db-csharp")
61+
dbDir = os.path.join(testDir, os.path.basename(testDir) + ".testproj")
6862

6963
if not os.path.isdir(dbDir):
70-
print("Expected database directory " + dbDir + " not found. Please contact Semmle.")
64+
print("Expected database directory " + dbDir + " not found.")
7165
exit(1)
7266

73-
cmd = ['odasa', 'runQuery', '--query', os.path.join(csharpQueries, 'MinimalStubsFromSource.ql'), '--db', dbDir, '--output-file', outputFile]
67+
cmd = ['codeql', 'query', 'run', os.path.join(
68+
csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', outputFile]
7469
print('Running ' + ' '.join(cmd))
7570
if subprocess.check_call(cmd):
76-
print('Failed to run the query to generate output file. Please contact Semmle.')
71+
print('Failed to run the query to generate output file.')
7772
exit(1)
7873

79-
# Remove the leading " and trailing " bytes from the file
80-
len = os.stat(outputFile).st_size
81-
f = open(outputFile, "rb")
82-
try:
83-
quote = f.read(1)
84-
if quote != b'"':
85-
print("Unexpected character in file. Please contact Semmle.")
86-
contents = f.read(len-3)
87-
quote = f.read(1)
88-
if quote != b'"':
89-
print("Unexpected end character. Please contact Semmle.", quote)
90-
finally:
91-
f.close()
74+
# Remove the leading and trailing bytes from the file
75+
length = os.stat(outputFile).st_size
76+
if length < 20:
77+
contents = b''
78+
else:
79+
f = open(outputFile, "rb")
80+
try:
81+
countTillSlash = 0
82+
foundSlash = False
83+
slash = f.read(1)
84+
while slash != b'':
85+
if slash == b'/':
86+
foundSlash = True
87+
break
88+
countTillSlash += 1
89+
slash = f.read(1)
90+
91+
if not foundSlash:
92+
countTillSlash = 0
93+
94+
f.seek(0)
95+
quote = f.read(countTillSlash)
96+
print("Start characters in file skipped.", quote)
97+
post = b'\x0e\x01\x08#select\x01\x01\x00s\x00'
98+
contents = f.read(length - len(post) - countTillSlash)
99+
quote = f.read(len(post))
100+
if quote != post:
101+
print("Unexpected end character in file.", quote)
102+
finally:
103+
f.close()
92104

93105
f = open(outputFile, "wb")
94106
f.write(contents)
95107
f.close()
96108

97-
cmd = ['odasa', 'qltest', '--optimize', testDir]
109+
cmd = ['codeql', 'test', 'run', testDir]
98110
print('Running ' + ' '.join(cmd))
99111
if subprocess.check_call(cmd):
100-
print('\nTest failed. You may need to fix up', outputFile)
101-
print('It may help to view', outputFile, ' in Visual Studio')
102-
print("Next steps:")
103-
print('1. Look at the compilation errors, and fix up', outputFile, 'so that the test compiles')
104-
print('2. Re-run odasa qltest --optimize "' + testDir + '"')
105-
print('3. git add "' + outputFile + '"')
106-
exit(1)
112+
print('\nTest failed. You may need to fix up', outputFile)
113+
print('It may help to view', outputFile, ' in Visual Studio')
114+
print("Next steps:")
115+
print('1. Look at the compilation errors, and fix up',
116+
outputFile, 'so that the test compiles')
117+
print('2. Re-run codeql test run "' + testDir + '"')
118+
print('3. git add "' + outputFile + '"')
119+
exit(1)
107120

108121
print("\nStub generation successful! Next steps:")
109122
print('1. Edit "semmle-extractor-options" in the .cs files to remove unused references')
110-
print('2. Re-run odasa qltest --optimize "' + testDir + '"')
123+
print('2. Re-run codeql test run "' + testDir + '"')
111124
print('3. git add "' + outputFile + '"')
112125
print('4. Commit your changes.')
113126

0 commit comments

Comments
 (0)