15
15
"opa_windows_amd64" ,
16
16
]
17
17
18
- T = TypeVar ('T' )
18
+ T = TypeVar ("T" )
19
19
20
20
21
21
def required (value : Optional [T ], name : str ) -> T :
@@ -28,55 +28,65 @@ def required(value: Optional[T], name: str) -> T:
28
28
def get_sha256 (artifact_name : str , version : str ) -> str :
29
29
ext = ".exe" if "windows" in artifact_name else ""
30
30
31
- with urlopen (f"https://github.com/open-policy-agent/opa/releases/download/v{ version } /{ artifact_name } { ext } .sha256" ) as res :
32
- return res .read ().decode ().split (' ' )[0 ]
31
+ with urlopen (
32
+ f"https://github.com/open-policy-agent/opa/releases/download/v{ version } /{ artifact_name } { ext } .sha256"
33
+ ) as res :
34
+ return res .read ().decode ().split (" " )[0 ]
33
35
34
36
35
37
def get_sha256_file (file : str , version : str ) -> str :
36
- file_url = f"https://raw.githubusercontent.com/open-policy-agent/opa/v{ version } /{ file } "
38
+ file_url = (
39
+ f"https://raw.githubusercontent.com/open-policy-agent/opa/v{ version } /{ file } "
40
+ )
37
41
38
42
with urlopen (file_url ) as res :
39
43
return sha256 (res .read ()).hexdigest ()
40
44
41
45
42
46
def main (args : Namespace ):
43
- d = dict ([
44
- (artifact_name , get_sha256 (artifact_name , args .version ))
45
- for artifact_name in ARTIFACTS
46
- ])
47
+ d = dict (
48
+ [
49
+ (artifact_name , get_sha256 (artifact_name , args .version ))
50
+ for artifact_name in ARTIFACTS
51
+ ]
52
+ )
47
53
48
- bzl_path = WORKSPACE_ROOT .joinpath (
49
- 'opa' , 'private' , 'opa_rules_dependencies.bzl' )
54
+ bzl_path = WORKSPACE_ROOT .joinpath ("opa" , "private" , "opa_rules_dependencies.bzl" )
50
55
51
56
with open (bzl_path ) as bzl_file :
52
57
bzl_content = bzl_file .read ()
53
58
54
- version_match = re .search (
55
- r"DEFAULT_VERSION\s*=\s*[\"'](.*?)[\"']" , bzl_content )
59
+ version_match = re .search (r"DEFAULT_VERSION\s*=\s*[\"'](.*?)[\"']" , bzl_content )
56
60
57
61
if version_match is None :
58
62
raise ValueError (f"Could not find DEFAULT_VERSION in file { bzl_path } " )
59
63
60
64
start_version = version_match .start (1 )
61
65
end_version = version_match .end (1 )
62
66
63
- bzl_content = bzl_content [:start_version ] + \
64
- args .version + bzl_content [end_version :]
67
+ bzl_content = bzl_content [:start_version ] + args .version + bzl_content [end_version :]
65
68
66
- dict_match = re .search (
67
- r"^_OPA_SHA256\s*=\s*{\s*$" , bzl_content , re .MULTILINE )
69
+ dict_match = re .search (r"^_OPA_SHA256\s*=\s*{\s*$" , bzl_content , re .MULTILINE )
68
70
69
71
if dict_match is None :
70
72
raise ValueError (f"Could not find _OPA_SHA256 in file { bzl_path } " )
71
73
72
- bzl_content = bzl_content [:dict_match .end (
73
- )] + f"\n \" { args .version } \" : {{\n " + '\n ' .join ([
74
- f" \" { artifact_name } \" : \" { sha256 } \" ,"
75
- for artifact_name , sha256 in d .items ()
76
- ] + [
77
- f" \" opa_capabilities_json\" : \" { get_sha256_file ('capabilities.json' , args .version )} \" ," ,
78
- f" \" opa_builtin_metadata_json\" : \" { get_sha256_file ('builtin_metadata.json' , args .version )} \" ," ,
79
- ]) + "\n }," + bzl_content [dict_match .end ():]
74
+ bzl_content = (
75
+ bzl_content [: dict_match .end ()]
76
+ + f'\n "{ args .version } ": {{\n '
77
+ + "\n " .join (
78
+ [
79
+ f' "{ artifact_name } ": "{ sha256 } ",'
80
+ for artifact_name , sha256 in d .items ()
81
+ ]
82
+ + [
83
+ f" \" opa_capabilities_json\" : \" { get_sha256_file ('capabilities.json' , args .version )} \" ," ,
84
+ f" \" opa_builtin_metadata_json\" : \" { get_sha256_file ('builtin_metadata.json' , args .version )} \" ," ,
85
+ ]
86
+ )
87
+ + "\n },"
88
+ + bzl_content [dict_match .end () :]
89
+ )
80
90
81
91
with open (bzl_path , "w" , encoding = "utf-8" ) as bzl_file :
82
92
bzl_file .write (bzl_content )
@@ -89,13 +99,13 @@ def get_workspace_root(wd: Path) -> Path:
89
99
return wd
90
100
91
101
92
- BUILD_WORKING_DIRECTORY = Path (
93
- required (os .getenv ("BUILD_WORKING_DIRECTORY " ), "BUILD_WORKING_DIRECTORY" ) )
94
- WORKSPACE_ROOT = get_workspace_root ( BUILD_WORKING_DIRECTORY )
102
+ WORKSPACE_ROOT = Path (
103
+ required (os .getenv ("BUILD_WORKSPACE_DIRECTORY " ), "BUILD_WORKSPACE_DIRECTORY" )
104
+ )
95
105
96
- if __name__ == ' __main__' :
106
+ if __name__ == " __main__" :
97
107
98
108
parser = ArgumentParser ()
99
- parser .add_argument (' --version' , '-v' , required = True )
109
+ parser .add_argument (" --version" , "-v" , required = True )
100
110
101
111
main (parser .parse_args ())
0 commit comments