11#!/usr/bin/env python
22
3+
34def do_tar (
45 src_paths : list [str ],
56 dest_path : str ,
67 gcs_base_path : str = "/" ,
78) -> str :
89 """
9- Create a tar.gz archive from source files or directories and save it to the given destination.
10+ Create a tar.gz archive from source files or directories and save it to the
11+ given destination.
1012
1113 Parameters:
1214 src_paths (list[str]): Source paths of files or directories to be archived.
13- dest_path (str): Destination path where the tar.gz archive will be written. This can be either
14- an existing directory or a file path (with the parent directory existing).
15- gcs_base_path (str): The shared GCS collection's configured base path. Default is "/".
15+ dest_path (str): Destination path where the tar.gz archive will be
16+ written. This can be either an existing directory or a
17+ file path (with the parent directory existing).
18+ gcs_base_path (str): The shared GCS collection's configured base path.
19+ Default is "/".
1620
1721 Returns:
1822 str: The output tar archive file path.
1923
2024 Raises:
21- ValueError: If src_paths is empty, dest_path is None, any provided path does not begin with the expected
22- prefix, or if any source path or destination (or its parent) is invalid.
25+ ValueError: If src_paths is empty, dest_path is None, any provided path
26+ does not begin with the expected prefix, or if any source
27+ path or destination (or its parent) is invalid.
2328 RuntimeError: If an error occurs during the creation of the tar archive.
2429
2530 Example:
@@ -48,7 +53,8 @@ def transform_path_from_absolute(path: str) -> str:
4853 """Transform an absolute filesystem path back to a GCS-style path."""
4954 if not path .startswith (gcs_base_path ):
5055 raise ValueError (
51- f"Path '{ path } ' does not start with the expected prefix '{ gcs_base_path } '."
56+ f"Path '{ path } ' does not start with "
57+ f"the expected prefix '{ gcs_base_path } '."
5258 )
5359 return path .replace (gcs_base_path , "/" , 1 )
5460
@@ -72,7 +78,9 @@ def transform_path_from_absolute(path: str) -> str:
7278 # Validate transformed_dest_path
7379 if transformed_dest_path .exists ():
7480 if not (transformed_dest_path .is_dir () or transformed_dest_path .is_file ()):
75- raise ValueError (f"Destination path '{ dest_path } ' is neither a directory nor a file." )
81+ raise ValueError (
82+ f"Destination path '{ dest_path } ' is neither a directory nor a file."
83+ )
7684 else :
7785 if not transformed_dest_path .parent .exists ():
7886 raise ValueError (
@@ -95,7 +103,10 @@ def transform_path_from_absolute(path: str) -> str:
95103 transformed_dest_tar_path = transformed_dest_path .with_name (fn + ".tar.gz" )
96104
97105 # Informative message (could be replaced with logging.info in production code)
98- print (f"Creating tar file at { transformed_dest_tar_path .absolute ()} with { len (transformed_src_paths )} source(s)" )
106+ print (
107+ f"Creating tar file at { transformed_dest_tar_path .absolute ()} "
108+ f"with { len (transformed_src_paths )} source(s)"
109+ )
99110
100111 # Create the tar.gz archive with exception handling.
101112 try :
@@ -108,11 +119,16 @@ def transform_path_from_absolute(path: str) -> str:
108119 try :
109120 transformed_dest_tar_path .unlink ()
110121 except Exception as unlink_err :
111- print (f"Warning: Failed to remove incomplete tar file '{ transformed_dest_tar_path } ': { unlink_err } " )
122+ print (
123+ f"Warning: Failed to remove incomplete tar file "
124+ f"'{ transformed_dest_tar_path } ': { unlink_err } "
125+ )
112126 raise RuntimeError (f"Failed to create tar archive: { e } " ) from e
113127
114128 # Transform the output path back to a GCS-style path and return.
115- result_path = transform_path_from_absolute (str (transformed_dest_tar_path .absolute ()))
129+ result_path = transform_path_from_absolute (
130+ str (transformed_dest_tar_path .absolute ())
131+ )
116132 return result_path
117133
118134
0 commit comments