Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfa9bf3

Browse files
committed
C#: Add nuget based stubbing script
1 parent fa215bc commit bfa9bf3

3 files changed

Lines changed: 108 additions & 45 deletions

File tree

csharp/ql/src/Stubs/helpers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
import os
3+
import subprocess
4+
5+
6+
def run_cmd(cmd, msg="Failed to run command"):
7+
print('Running ' + ' '.join(cmd))
8+
if subprocess.check_call(cmd):
9+
print(msg)
10+
exit(1)
11+
12+
13+
def get_argv(index, default):
14+
if len(sys.argv) > index:
15+
return sys.argv[index]
16+
return default
17+
18+
19+
def trim_output_file(file):
20+
# Remove the leading and trailing bytes from the file
21+
length = os.stat(file).st_size
22+
if length < 20:
23+
contents = b''
24+
else:
25+
f = open(file, "rb")
26+
try:
27+
pre = f.read(2)
28+
print("Start characters in file skipped.", pre)
29+
contents = f.read(length - 5)
30+
post = f.read(3)
31+
print("End characters in file skipped.", post)
32+
finally:
33+
f.close()
34+
35+
f = open(file, "wb")
36+
f.write(contents)
37+
f.close()

csharp/ql/src/Stubs/make_stubs.py

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sys
1212
import os
1313
import subprocess
14+
import helpers
1415

1516
print('Script to generate stub.cs files for C# qltest projects')
1617

@@ -45,66 +46,34 @@
4546

4647
csharpQueries = os.path.abspath(os.path.dirname(sys.argv[0]))
4748
outputFile = os.path.join(testDir, 'stubs.cs')
49+
bqrsFile = os.path.join(testDir, 'stubs.bqrs')
4850

4951
print("Stubbing qltest in", testDir)
5052

5153
if os.path.isfile(outputFile):
5254
os.remove(outputFile) # It would interfere with the test.
5355
print("Removed previous", outputFile)
5456

55-
cmd = ['codeql', 'test', 'run', '--keep-databases', testDir]
56-
print('Running ' + ' '.join(cmd))
57-
if subprocess.check_call(cmd):
58-
print("codeql test failed. Please fix up the test before proceeding.")
59-
exit(1)
57+
helpers.run_cmd(['codeql', 'test', 'run', '--keep-databases', testDir],
58+
"codeql test failed. Please fix up the test before proceeding.")
6059

6160
dbDir = os.path.join(testDir, os.path.basename(testDir) + ".testproj")
6261

6362
if not os.path.isdir(dbDir):
6463
print("Expected database directory " + dbDir + " not found.")
6564
exit(1)
6665

67-
cmd = ['codeql', 'query', 'run', os.path.join(
68-
csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', outputFile]
69-
print('Running ' + ' '.join(cmd))
70-
if subprocess.check_call(cmd):
71-
print('Failed to run the query to generate output file.')
72-
exit(1)
66+
helpers.run_cmd(['codeql', 'query', 'run', os.path.join(
67+
csharpQueries, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', bqrsFile], 'Failed to run the query to generate output file.')
68+
69+
helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
70+
outputFile, '--format=text', '--no-titles'], 'Failed to run the query to generate output file.')
71+
72+
helpers.trim_output_file(outputFile)
7373

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()
104-
105-
f = open(outputFile, "wb")
106-
f.write(contents)
107-
f.close()
74+
if os.path.isfile(bqrsFile):
75+
os.remove(bqrsFile) # Cleanup
76+
print("Removed temp BQRS file", bqrsFile)
10877

10978
cmd = ['codeql', 'test', 'run', testDir]
11079
print('Running ' + ' '.join(cmd))
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import sys
2+
import os
3+
import helpers
4+
5+
6+
print('Script to generate stub.cs file from a nuget package')
7+
print('Usage: python ' + sys.argv[0] +
8+
' NUGET_PACKAGE_NAME [VERSION=latest] [WORK_DIR=tempDir] [OUTPUT_NAME=stub]')
9+
10+
if len(sys.argv) < 2:
11+
print("\nPlease supply a nuget package name.")
12+
exit(1)
13+
14+
thisScript = sys.argv[0]
15+
thisDir = os.path.abspath(os.path.dirname(thisScript))
16+
nuget = sys.argv[1]
17+
18+
workDir = os.path.abspath(helpers.get_argv(3, "tempDir"))
19+
projectName = "proj"
20+
projectDir = os.path.join(workDir, projectName)
21+
dbName = 'db'
22+
dbDir = os.path.join(workDir, dbName)
23+
outputName = helpers.get_argv(4, "stub")
24+
outputFile = os.path.join(workDir, outputName + '.cs')
25+
bqrsFile = os.path.join(workDir, outputName + '.bqrs')
26+
version = helpers.get_argv(2, "latest")
27+
28+
print("\nCreating new project")
29+
helpers.run_cmd(['dotnet', 'new', 'classlib', "--language", "C#", '--name',
30+
projectName, '--output', projectDir])
31+
32+
print("\nAdding reference")
33+
cmd = ['dotnet', 'add', projectDir, 'package', nuget]
34+
if (version != "latest"):
35+
cmd.append('--version')
36+
cmd.append(version)
37+
helpers.run_cmd(cmd)
38+
39+
print("\nCreating DB")
40+
helpers.run_cmd(['codeql', 'database', 'create', dbDir, '--language=csharp',
41+
'--command', 'dotnet build /t:rebuild ' + projectDir])
42+
43+
if not os.path.isdir(dbDir):
44+
print("Expected database directory " + dbDir + " not found.")
45+
exit(1)
46+
47+
print("\nRunning query")
48+
helpers.run_cmd(['codeql', 'query', 'run', os.path.join(
49+
thisDir, 'MinimalStubsFromSource.ql'), '--database', dbDir, '--output', bqrsFile])
50+
51+
helpers.run_cmd(['codeql', 'bqrs', 'decode', bqrsFile, '--output',
52+
outputFile, '--format=text', '--no-titles'])
53+
54+
helpers.trim_output_file(outputFile)
55+
56+
print("\nOutput: " + outputFile)
57+
exit(0)

0 commit comments

Comments
 (0)