Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
codeql/ruby/scripts/generate_model.py at main · github/codeql · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
github
codeql
Repository navigation
Code
Issues
1k
(1k)
Pull requests
481
(481)
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
codeql
/
ruby
/
scripts
/
generate_model.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
146 lines (119 loc) · 4.06 KB
main
Breadcrumbs
codeql
/
ruby
/
scripts
/
generate_model.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
146 lines (119 loc) · 4.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# This script generates a data extensions model for a given library in codeql database form
# Currently only typeModels and summaryModels are generated
# Requires `pyyaml`
import
sys
import
argparse
import
subprocess
from
pathlib
import
Path
import
tempfile
import
json
import
yaml
def
parse_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
"Generates a data extensions model from a Ruby CodeQL database"
)
parser
.
add_argument
(
"database_path"
,
help
=
"filepath to a Ruby CodeQL database"
)
parser
.
add_argument
(
"-o"
,
"--output"
,
required
=
False
,
metavar
=
"output_file"
,
help
=
"if provided, the model will be written to this file"
,
)
parser
.
add_argument
(
"-c"
,
"--codeql"
,
required
=
False
,
metavar
=
"codeql_command"
,
default
=
"codeql"
,
help
=
"if provided, use this command to invoke codeql"
,
)
parser
.
add_argument
(
"-w"
,
"--overwrite"
,
action
=
"store_true"
,
help
=
"if provided, overwrite the file if it already exists"
,
)
return
parser
.
parse_args
()
def
die
(
msg
):
sys
.
stderr
.
write
(
"Error: "
+
msg
+
"
\n
"
)
sys
.
exit
(
1
)
def
main
():
args
=
parse_args
()
output_path
=
args
.
output
check_output_path
(
output_path
,
args
.
overwrite
)
database_path
=
Path
(
args
.
database_path
).
absolute
()
check_database_exists
(
database_path
)
codeql_command
=
args
.
codeql
.
split
(
" "
)
with
tempfile
.
NamedTemporaryFile
()
as
query_output_json_file
:
run_codeql_query
(
codeql_command
,
database_path
,
query_output_json_file
)
generate_output
(
query_output_json_file
,
output_path
)
def
check_output_path
(
output_path
,
overwrite
):
if
output_path
==
None
:
return
# STDOUT
p
=
Path
(
output_path
).
absolute
()
if
p
.
is_file
()
and
not
overwrite
:
die
(
"file already exists at: "
+
str
(
p
))
elif
p
.
is_dir
():
die
(
"specified output path is a directory: "
+
str
(
p
))
def
check_database_exists
(
database_path
):
if
not
database_path
.
exists
():
die
(
"database not found at: "
+
str
(
database_path
))
elif
not
database_path
.
is_dir
():
die
(
"database not found at: "
+
str
(
database_path
)
+
" - not a directory"
)
elif
not
database_path
.
joinpath
(
"db-ruby"
).
exists
():
die
(
"directory: "
+
str
(
database_path
)
+
" doesn't look like a Ruby database"
)
def
run_codeql_query
(
codeql_command
,
database_path
,
query_output_json_file
):
query_path
=
(
Path
(
__file__
)
.
parent
.
parent
.
joinpath
(
"ql/src/queries/modeling/GenerateModel.ql"
)
.
absolute
()
)
with
tempfile
.
NamedTemporaryFile
()
as
bqrs_file
:
subprocess
.
run
(
codeql_command
+
[
"query"
,
"run"
,
"-d"
,
database_path
,
"-o"
,
bqrs_file
.
name
,
query_path
]
)
subprocess
.
run
(
codeql_command
+
[
"bqrs"
,
"decode"
,
"--format"
,
"json"
,
"--output"
,
query_output_json_file
.
name
,
bqrs_file
.
name
,
]
)
def
generate_output
(
query_output_json_file
,
output_path
):
output_string
=
serialize_output
(
query_output_json_file
)
if
not
output_path
==
None
:
Path
(
output_path
).
write_text
(
output_string
)
else
:
print
(
output_string
)
def
model_kinds
():
return
[
"typeModel"
,
"sourceModel"
,
"sinkModel"
,
"summaryModel"
,
"typeVariableModel"
,
]
def
serialize_output
(
query_output_json_file
):
parsed_json
=
json
.
load
(
query_output_json_file
)
serialized_tuples
=
[]
for
extensible_type
in
model_kinds
():
if
not
extensible_type
in
parsed_json
:
continue
tuples
=
parsed_json
[
extensible_type
][
"tuples"
]
if
tuples
:
serialized_tuples
.
append
(
serialize_type
(
tuples
,
extensible_type
))
return
yaml
.
dump
({
"extensions"
:
serialized_tuples
},
default_style
=
'"'
)
def
serialize_type
(
tuples
,
extensible_type
):
return
{
"addsTo"
: {
"pack"
:
"codeql/ruby-all"
,
"extensible"
:
extensible_type
},
"data"
:
tuples
,
}
main
()
You can’t perform that action at this time.