Skip to content

Commit 34a34e6

Browse files
committed
Add support for languages without a Customizations library
1 parent 7e1d155 commit 34a34e6

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

codeql_bundle/helpers/bundle.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,62 @@ def is_candidate(pack: ResolvedCodeQLPack) -> bool:
213213
with target_copy.path.open("w") as fd:
214214
yaml.dump(qlpack_spec, fd)
215215

216+
logging.debug(
217+
f"Determining if standard library CodeQL library pack {target.name} is customizable."
218+
)
219+
if not (target_copy.path.parent / "Customizations.qll").exists():
220+
logging.debug(
221+
f"Standard library CodeQL pack {target.name} does not have a 'Customizations' library, attempting to add one."
222+
)
223+
# Assume the CodeQL library pack has name `<language>-all`.
224+
target_language = target_copy.get_pack_name().removesuffix("-all")
225+
target_language_library_path = (
226+
target_copy.path.parent / f"{target_language}.qll"
227+
)
228+
logging.debug(
229+
f"Looking for standard library language module {target_language_library_path.name}"
230+
)
231+
if not target_language_library_path.exists():
232+
raise BundleException(
233+
f"Unable to customize {target.name}, because it doesn't have a 'Customizations' library and we cannot determine the language library."
234+
)
235+
logging.debug(
236+
f"Found standard library language module {target_language_library_path.name}, adding import of 'Customizations' library."
237+
)
238+
with target_language_library_path.open("r") as fd:
239+
target_language_library_lines = fd.readlines()
240+
logging.debug(f"Looking for the first import statement.")
241+
242+
first_import_idx = None
243+
for idx, line in enumerate(target_language_library_lines):
244+
if line.startswith("import"):
245+
first_import_idx = idx
246+
break
247+
if first_import_idx == None:
248+
raise BundleException(
249+
f"Unable to customize {target.name}, because we cannot determine the first import statement of {target_language_library_path.name}."
250+
)
251+
logging.debug(
252+
"Found first import statement and prepending import statement importing 'Customizations'"
253+
)
254+
target_language_library_lines.insert(
255+
first_import_idx, "import Customizations\n"
256+
)
257+
with target_language_library_path.open("w") as fd:
258+
fd.writelines(target_language_library_lines)
259+
logging.debug(
260+
f"Writing modified language library to {target_language_library_path}"
261+
)
262+
263+
target_customization_library_path = (
264+
target_copy.path.parent / "Customizations.qll"
265+
)
266+
logging.debug(
267+
f"Creating Customizations library with import of language {target_language}"
268+
)
269+
with target_customization_library_path.open("w") as fd:
270+
fd.write(f"import {target_language}\n")
271+
216272
logging.debug(
217273
f"Updating 'Customizations.qll' with imports of customization libraries."
218274
)

0 commit comments

Comments
 (0)