Skip to content

Commit f017afb

Browse files
committed
Format Code and fix typo
1 parent 508db72 commit f017afb

File tree

5 files changed

+51
-35
lines changed

5 files changed

+51
-35
lines changed

charon/config.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
1617
import logging
1718
import os
1819
from typing import Dict, List, Optional
@@ -45,6 +46,7 @@ def __init__(self, data: Dict):
4546
self.__quay_radas_registry: str = data.get("quay_radas_registry", None)
4647
self.__quay_radas_username: str = data.get("quay_radas_username", None)
4748
self.__quay_radas_password: str = data.get("quay_radas_password", None)
49+
self.__radas_sign_enabled: bool = data.get("radas_sign_enabled", False)
4850
self.__radas_sign_timeout_count: int = data.get("radas_sign_timeout_count", 10)
4951
self.__radas_sign_wait_interval_sec: int = data.get("radas_sign_wait_interval_sec", 60)
5052

@@ -93,24 +95,26 @@ def get_quay_radas_username(self) -> str:
9395
def get_quay_radas_password(self) -> str:
9496
return self.__quay_radas_password
9597

98+
def is_radas_sign_enabled(self) -> bool:
99+
return self.__radas_sign_enabled
100+
96101
def get_radas_sign_timeout_count(self) -> int:
97102
return self.__radas_sign_timeout_count
98103

99104
def get_radas_sign_wait_interval_sec(self) -> int:
100105
return self.__radas_sign_wait_interval_sec
101106

107+
102108
def get_config(cfgPath=None) -> CharonConfig:
103109
config_file_path = cfgPath
104110
if not config_file_path or not os.path.isfile(config_file_path):
105111
config_file_path = os.path.join(os.getenv("HOME", ""), ".charon", CONFIG_FILE)
106-
data = read_yaml_from_file_path(config_file_path, 'schemas/charon.json')
112+
data = read_yaml_from_file_path(config_file_path, "schemas/charon.json")
107113
return CharonConfig(data)
108114

109115

110116
def get_template(template_file: str) -> str:
111-
template = os.path.join(
112-
os.getenv("HOME", ''), ".charon/template", template_file
113-
)
117+
template = os.path.join(os.getenv("HOME", ""), ".charon/template", template_file)
114118
if os.path.isfile(template):
115119
with open(template, encoding="utf-8") as file_:
116120
return file_.read()

charon/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@
177177
DEFAULT_REGISTRY = "localhost"
178178
DEFAULT_SIGN_RESULT_LOC = "/tmp/sign"
179179
DEFAULT_RADAS_SIGN_TIMEOUT_COUNT = 10
180-
DEFAULT_RADAS_SIGN_WAIT_INTERVAL_SEC = 60
180+
DEFAULT_RADAS_SIGN_WAIT_INTERVAL_SEC = 60

charon/pkgs/maven.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,13 @@ def handle_maven_uploading(
409409
if cf_enable:
410410
cf_invalidate_paths.extend(archetype_files)
411411

412-
# 10. Generate signature file if radas sign is enabled, or do detached sign if contain_signature is set to True
412+
# 10. Generate signature file if radas sign is enabled,
413+
# or do detached sign if contain_signature is set to True
413414
conf = get_config(config)
414415
if not conf:
415416
sys.exit(1)
416417

417-
if conf.get_radas_sign_enabled():
418+
if conf.is_radas_sign_enabled():
418419
logger.info("Start generating radas signature files for s3 bucket %s\n", bucket_name)
419420
(_failed_metas, _generated_signs) = radas_signature.generate_radas_sign(top_level)
420421
if not _generated_signs:

charon/pkgs/oras_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
1617
import oras.client
1718
import logging
1819
from charon.config import get_config
1920
from typing import List
2021

2122
logger = logging.getLogger(__name__)
2223

24+
2325
class OrasClient:
2426
"""
2527
Wrapper for oras‑py’s OrasClient, deciding whether to login based on config.
@@ -49,16 +51,19 @@ def pull(self, result_reference_url: str, sign_result_loc: str) -> List[str]:
4951
"""
5052
Call oras‑py’s pull method to pull the remote file to local.
5153
Args:
52-
result_reference_url (str): Reference of the remote file (e.g. “quay.io/repository/signing/radas@hash”).
53-
sign_result_loc (str): Local save path (e.g. “/tmp/sign”).
54+
result_reference_url (str):
55+
Reference of the remote file (e.g. “quay.io/repository/signing/radas@hash”).
56+
sign_result_loc (str):
57+
Local save path (e.g. “/tmp/sign”).
5458
"""
5559
files = []
5660
try:
5761
self.login_if_needed()
58-
# the filename should be possibly named by the digest hash value based on the oras source code
5962
files = self.client.pull(target=result_reference_url, outdir=sign_result_loc)
6063
logger.info("Pull file from %s to %s", result_reference_url, sign_result_loc)
6164
except Exception as e:
62-
logger.error("Failed to pull file from %s to %s: %s", result_reference_url, sign_result_loc, e)
65+
logger.error(
66+
"Failed to pull file from %s to %s: %s", result_reference_url, sign_result_loc, e
67+
)
6368
finally:
64-
return files
69+
return files

charon/pkgs/radas_signature_handler.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16+
1617
import proton
1718
import proton.handlers
1819
import threading
1920
import logging
2021
import json
2122
import os
2223
import asyncio
24+
import sys
25+
import time
2326
from typing import List, Any, Tuple, Callable, Dict
2427
from charon.config import get_config
2528
from charon.constants import DEFAULT_SIGN_RESULT_LOC
@@ -29,10 +32,12 @@
2932

3033
logger = logging.getLogger(__name__)
3134

35+
3236
class SignHandler:
3337
"""
3438
Handle the sign result status management
3539
"""
40+
3641
_is_processing: bool = True
3742
_downloaded_files: List[str] = []
3843

@@ -52,6 +57,7 @@ def set_processing(cls, value: bool) -> None:
5257
def set_downloaded_files(cls, files: List[str]) -> None:
5358
cls._downloaded_files = files
5459

60+
5561
class UmbListener(proton.handlers.MessagingHandler):
5662
"""
5763
UmbListener class (AMQP version), register this when setup UmbClient
@@ -74,10 +80,7 @@ def on_message(self, event: proton.Event) -> None:
7480
On message callback
7581
"""
7682
# handle response from radas in a thread
77-
thread = threading.Thread(
78-
target=self._process_message,
79-
args=[event.message.body]
80-
)
83+
thread = threading.Thread(target=self._process_message, args=[event.message.body])
8184
thread.start()
8285

8386
def on_error(self, event: proton.Event) -> None:
@@ -103,8 +106,8 @@ def _process_message(msg: Any) -> None:
103106
result_reference_url = msg_dict.get("result_reference")
104107

105108
if not result_reference_url:
106-
logger.warning("Not found result_reference in message,ignore.")
107-
return
109+
logger.warning("Not found result_reference in message,ignore.")
110+
return
108111

109112
conf = get_config()
110113
if not conf:
@@ -117,20 +120,24 @@ def _process_message(msg: Any) -> None:
117120

118121
oras_client = OrasClient()
119122
files = oras_client.pull(
120-
result_reference_url=result_reference_url,
121-
sign_result_loc=sign_result_loc
123+
result_reference_url=result_reference_url, sign_result_loc=sign_result_loc
122124
)
123125
SignHandler.set_downloaded_files(files)
124126
finally:
125127
SignHandler.set_processing(False)
126128

129+
127130
def generate_radas_sign(top_level: str) -> Tuple[List[str], List[str]]:
128131
"""
129132
Generate .asc files based on RADAS sign result json file
130133
"""
131134
conf = get_config()
132-
timeout_count = conf.get_radas_sign_timeout_count() if conf else DEFAULT_RADAS_SIGN_TIMEOUT_COUNT
133-
wait_interval_sec = conf.get_radas_sign_wait_interval_sec() if conf else DEFAULT_RADAS_SIGN_WAIT_INTERVAL_SEC
135+
timeout_count = (
136+
conf.get_radas_sign_timeout_count() if conf else DEFAULT_RADAS_SIGN_TIMEOUT_COUNT
137+
)
138+
wait_interval_sec = (
139+
conf.get_radas_sign_wait_interval_sec() if conf else DEFAULT_RADAS_SIGN_WAIT_INTERVAL_SEC
140+
)
134141
wait_count = 0
135142
while SignHandler.is_processing():
136143
wait_count += 1
@@ -146,23 +153,25 @@ def generate_radas_sign(top_level: str) -> Tuple[List[str], List[str]]:
146153
# should only have the single sign result json file from the radas registry
147154
json_file_path = files[0]
148155
try:
149-
with open(json_file_path, 'r') as f:
156+
with open(json_file_path, "r") as f:
150157
data = json.load(f)
151158
except Exception as e:
152159
logger.error(f"Failed to read or parse the JSON file: {e}")
153160
raise
154161

155162
async def generate_single_sign_file(
156-
file_path: str, signature: str, failed_paths: List[str], generated_signs: List[str],
157-
sem: asyncio.BoundedSemaphore
163+
file_path: str,
164+
signature: str,
165+
failed_paths: List[str],
166+
generated_signs: List[str],
167+
sem: asyncio.BoundedSemaphore,
158168
):
159169
async with sem:
160170
if not file_path or not signature:
161-
logger.error(f"Invalid JSON entry")
171+
logger.error("Invalid JSON entry")
162172
return
163173
# remove the root path maven-repository
164174
filename = file_path.split("/", 1)[1]
165-
signature = item.get("signature")
166175

167176
artifact_path = os.path.join(top_level, filename)
168177
asc_filename = f"{filename}.asc"
@@ -173,7 +182,7 @@ async def generate_single_sign_file(
173182
return
174183

175184
try:
176-
with open(signature_path, 'w') as asc_file:
185+
with open(signature_path, "w") as asc_file:
177186
asc_file.write(signature)
178187
generated_signs.append(signature_path)
179188
logger.info(f"Generated .asc file: {signature_path}")
@@ -182,14 +191,11 @@ async def generate_single_sign_file(
182191
logger.error(f"Failed to write .asc file for {artifact_path}: {e}")
183192

184193
result = data.get("result", [])
185-
return __do_path_cut_and(
186-
path_handler=generate_single_sign_file,
187-
data=result
188-
)
194+
return __do_path_cut_and(path_handler=generate_single_sign_file, data=result)
195+
189196

190197
def __do_path_cut_and(
191-
path_handler: Callable,
192-
data: List[Dict[str, str]]
198+
path_handler: Callable, data: List[Dict[str, str]]
193199
) -> Tuple[List[str], List[str]]:
194200

195201
failed_paths: List[str] = []
@@ -207,4 +213,4 @@ def __do_path_cut_and(
207213

208214
loop = asyncio.get_event_loop()
209215
loop.run_until_complete(asyncio.gather(*tasks))
210-
return (failed_paths, generated_signs)
216+
return (failed_paths, generated_signs)

0 commit comments

Comments
 (0)