2020from settings_library .s3 import S3Settings
2121from yarl import URL
2222
23- logger = logging .getLogger (__name__ )
23+ _logger = logging .getLogger (__name__ )
2424
2525HTTP_FILE_SYSTEM_SCHEMES : Final = ["http" , "https" ]
2626S3_FILE_SYSTEM_SCHEMES : Final = ["s3" , "s3a" ]
@@ -208,7 +208,7 @@ async def pull_file_from_remote(
208208 await log_publishing_cb (
209209 f"Uncompressing '{ download_dst_path .name } '..." , logging .INFO
210210 )
211- logger .debug (
211+ _logger .debug (
212212 "%s is a zip file and will be now uncompressed" , download_dst_path
213213 )
214214 with repro_zipfile .ReproducibleZipFile (download_dst_path , "r" ) as zip_obj :
@@ -258,7 +258,7 @@ async def _push_file_to_remote(
258258 log_publishing_cb : LogPublishingCB ,
259259 s3_settings : S3Settings | None ,
260260) -> None :
261- logger .debug ("Uploading %s to %s..." , file_to_upload , dst_url )
261+ _logger .debug ("Uploading %s to %s..." , file_to_upload , dst_url )
262262 assert dst_url .path # nosec
263263
264264 storage_kwargs : S3FsSettingsDict | dict [str , Any ] = {}
@@ -306,7 +306,7 @@ async def push_file_to_remote(
306306 await asyncio .get_event_loop ().run_in_executor (
307307 None , zfp .write , src_path , src_path .name
308308 )
309- logger .debug ("%s created." , archive_file_path )
309+ _logger .debug ("%s created." , archive_file_path )
310310 assert archive_file_path .exists () # nosec
311311 file_to_upload = archive_file_path
312312 await log_publishing_cb (
@@ -319,7 +319,7 @@ async def push_file_to_remote(
319319 )
320320
321321 if dst_url .scheme in HTTP_FILE_SYSTEM_SCHEMES :
322- logger .debug ("destination is a http presigned link" )
322+ _logger .debug ("destination is a http presigned link" )
323323 await _push_file_to_http_link (file_to_upload , dst_url , log_publishing_cb )
324324 else :
325325 await _push_file_to_remote (
@@ -330,3 +330,22 @@ async def push_file_to_remote(
330330 f"Upload of '{ src_path .name } ' to '{ dst_url .path .strip ('/' )} ' complete" ,
331331 logging .INFO ,
332332 )
333+
334+
335+ async def log_partial_file_content (
336+ src_path : Path , * , logger : logging .Logger , log_level : int , max_chars : int
337+ ) -> None :
338+ if max_chars < 0 :
339+ msg = "max_chars must be non-negative"
340+ raise ValueError (msg )
341+ if max_chars == 0 :
342+ return
343+ if not src_path .exists ():
344+ logger .log (log_level , "file does not exist: %s" , src_path )
345+ return
346+ async with aiofiles .open (src_path , encoding = "utf-8" ) as f :
347+ content = await f .read (max_chars + 1 )
348+ if len (content ) > max_chars :
349+ logger .log (log_level , "file content (truncated): %s..." , content [:max_chars ])
350+ else :
351+ logger .log (log_level , "file content: %s" , content )
0 commit comments