1
1
import json
2
2
import requests
3
- from typing import Optional , Dict , List
3
+ from typing import Optional , Dict , List , Tuple
4
4
from semantic_version import Version
5
5
from pathlib import Path
6
+ import yaml
6
7
7
8
SCRIPT_PATH = Path (__file__ )
8
- SUPPORTED_VERSIONS_PATH = SCRIPT_PATH .parent .parent .parent / "supported_codeql_configs.json"
9
+ CODING_STANDARDS_ROOT = SCRIPT_PATH .parent .parent .parent
10
+ SUPPORTED_VERSIONS_PATH = CODING_STANDARDS_ROOT / "supported_codeql_configs.json"
9
11
10
- def get_compatible_stdlib (version : Version ) -> Optional [str ]:
12
+ def get_compatible_stdlib (version : Version ) -> Optional [Tuple [ str , str ] ]:
11
13
tag = f"codeql-cli/v{ version } "
12
14
response = requests .get (f"https://raw.githubusercontent.com/github/codeql/{ tag } /cpp/ql/lib/qlpack.yml" )
13
15
14
16
if response .status_code == 200 :
15
- return tag
17
+ # Parse the qlpack.yml returned in the response as a yaml file to read the version property
18
+ qlpack = yaml .safe_load (response .text )
19
+ if qlpack is not None and "version" in qlpack :
20
+ return (tag , qlpack ["version" ])
16
21
return None
17
22
18
23
def get_compatible_bundle (version : Version , token : str ) -> Optional [str ]:
@@ -30,15 +35,17 @@ def get_compatible_bundle(version: Version, token: str) -> Optional[str]:
30
35
def main (cli_version : str , github_token : str ) -> None :
31
36
try :
32
37
parsed_cli_version = Version (cli_version )
33
- compatible_stdlib = get_compatible_stdlib (parsed_cli_version )
34
- if compatible_stdlib is None :
38
+ compatible_stdlib_return = get_compatible_stdlib (parsed_cli_version )
39
+ if compatible_stdlib_return is None :
35
40
print (f"Unable to find compatible standard library for: { parsed_cli_version } " )
36
41
exit (1 )
37
42
compatible_bundle = get_compatible_bundle (parsed_cli_version , github_token )
38
43
if compatible_bundle is None :
39
44
print (f"Unable to find compatible bundle for: { parsed_cli_version } " )
40
45
exit (1 )
41
46
47
+ compatible_stdlib_tag , compatible_stdlib_version = compatible_stdlib_return
48
+
42
49
with SUPPORTED_VERSIONS_PATH .open ("r" ) as f :
43
50
supported_versions = json .load (f )
44
51
@@ -49,10 +56,36 @@ def main(cli_version : str, github_token: str) -> None:
49
56
supported_env = supported_envs [0 ]
50
57
supported_env ["codeql_cli" ] = str (parsed_cli_version )
51
58
supported_env ["codeql_cli_bundle" ] = compatible_bundle
52
- supported_env ["codeql_standard_library" ] = compatible_stdlib
59
+ supported_env ["codeql_standard_library" ] = compatible_stdlib_tag
53
60
54
61
with SUPPORTED_VERSIONS_PATH .open ("w" ) as f :
55
62
json .dump (supported_versions , f , indent = 2 )
63
+
64
+ # Find every qlpack.yml file in the repository
65
+ qlpack_files = list (CODING_STANDARDS_ROOT .rglob ("qlpack.yml" ))
66
+ # Filter out any files that are in a hidden directory
67
+ qlpack_files = [f for f in qlpack_files if not any (part for part in f .parts if part .startswith ("." ))]
68
+
69
+ # Update the "codeql/cpp-all" entries in the "dependencies" property in every qlpack.yml file
70
+ updated_qlpacks = []
71
+ for qlpack_file in qlpack_files :
72
+ with qlpack_file .open ("r" ) as f :
73
+ qlpack = yaml .safe_load (f )
74
+ print ("Updating dependencies in " + str (qlpack_file ))
75
+ if "codeql/cpp-all" in qlpack ["dependencies" ]:
76
+ qlpack ["dependencies" ]["codeql/cpp-all" ] = compatible_stdlib_version
77
+ with qlpack_file .open ("w" ) as f :
78
+ yaml .safe_dump (qlpack , f )
79
+ updated_qlpacks .append (qlpack_file .parent )
80
+
81
+ # Call CodeQL to update the lock files by running codeql pack upgrade
82
+ # Note: we need to do this after updating all the qlpack files,
83
+ # otherwise we may get dependency resolution errors
84
+ for qlpack in updated_qlpacks :
85
+ print ("Updating lock files for " + str (qlpack ))
86
+ os .system (f"codeql pack upgrade { qlpack } " )
87
+
88
+
56
89
except ValueError as e :
57
90
print (e )
58
91
exit (1 )
0 commit comments