1313from .hpss import hpss_get , hpss_put
1414from .hpss_utils import add_files
1515from .settings import (
16- DEFAULT_CACHE ,
1716 TIME_TOL ,
1817 FilesRow ,
1918 TupleFilesRow ,
2019 config ,
2120 get_db_filename ,
2221 logger ,
2322)
24- from .utils import get_files_to_archive , update_config
23+ from .utils import CommandInfo , HPSSType , get_files_to_archive
2524
2625
2726def update ():
27+ command_info = CommandInfo ("update" )
28+ args : argparse .Namespace = setup_update (command_info )
2829
29- args : argparse .Namespace
30- cache : str
31- args , cache = setup_update ()
32-
33- result : Optional [List [str ]] = update_database (args , cache )
34-
35- if result is None :
30+ failures : Optional [List [str ]] = update_database (command_info , args )
31+ if failures is None :
3632 # There was either nothing to update or `--dry-run` was set.
3733 return
38- else :
39- failures = result
4034
4135 # Transfer to HPSS. Always keep a local copy of the database.
42- if config .hpss is not None :
43- hpss = config .hpss
36+ if command_info . config .hpss is not None :
37+ hpss = command_info . config .hpss
4438 else :
45- raise TypeError ("Invalid config.hpss={}" . format ( config .hpss ) )
46- hpss_put (hpss , get_db_filename ( cache ), cache , keep = args .keep , is_index = True )
39+ raise TypeError (f "Invalid config.hpss={ command_info . config .hpss } " )
40+ hpss_put (hpss , command_info . get_db_name ( ), command_info . cache_dir , keep = command_info .keep )
4741
48- globus_finalize (non_blocking = args .non_blocking )
42+ if command_info .hpss_type == HPSSType .GLOBUS :
43+ globus_finalize (command_info .globus_info , non_blocking = args .non_blocking )
4944
5045 # List failures
5146 if len (failures ) > 0 :
5247 logger .warning ("Some files could not be archived" )
5348 for file_path in failures :
54- logger .error ("Archiving {}" .format (file_path ))
49+ logger .error (f"Archiving { file_path } " )
50+ # TODO: (A) Continue refactor from here
5551
5652
57- def setup_update () -> Tuple [ argparse .Namespace , str ] :
53+ def setup_update (command_info : CommandInfo ) -> argparse .Namespace :
5854 # Parser
5955 parser : argparse .ArgumentParser = argparse .ArgumentParser (
6056 usage = "zstash update [<args>]" , description = "Update an existing zstash archive"
@@ -118,80 +114,69 @@ def setup_update() -> Tuple[argparse.Namespace, str]:
118114 if (not args .hpss ) or (args .hpss .lower () == "none" ):
119115 args .hpss = "none"
120116 args .keep = True
121-
122- # Copy configuration
123- # config.path = os.path.abspath(args.path)
124- config .hpss = args .hpss
125- config .maxsize = int (1024 * 1024 * 1024 * args .maxsize )
126-
127- cache : str
128- if args .cache :
129- cache = args .cache
130- else :
131- cache = DEFAULT_CACHE
132117 if args .verbose :
133118 logger .setLevel (logging .DEBUG )
134119
135- return args , cache
120+ if args .cache :
121+ command_info .cache_dir = args .cache
122+ command_info .keep = args .keep
123+ command_info .set_dir_to_archive (os .getcwd ())
124+ command_info .set_maxsize (args .maxsize )
125+ command_info .set_hpss_parameters (args .hpss )
126+
127+ return args
136128
137129
138130# C901 'update_database' is too complex (20)
139131def update_database ( # noqa: C901
140- args : argparse . Namespace , cache : str
132+ command_info : CommandInfo , args : argparse . Namespace
141133) -> Optional [List [str ]]:
142134 # Open database
143135 logger .debug ("Opening index database" )
144- if not os .path .exists (get_db_filename ( cache )):
136+ if not os .path .exists (command_info . get_db_name ( )):
145137 # The database file doesn't exist in the cache.
146138 # We need to retrieve it from HPSS
147- if args .hpss is not None :
148- config .hpss = args .hpss
149- if config .hpss is not None :
150- hpss : str = config .hpss
151- else :
152- raise TypeError ("Invalid config.hpss={}" .format (config .hpss ))
153- globus_activate (hpss )
154- hpss_get (hpss , get_db_filename (cache ), cache )
139+ if command_info .hpss_type != HPSSType .NO_HPSS :
140+ command_info .update_config ()
141+ if command_info .hpss_type == HPSSType .GLOBUS :
142+ globus_activate (command_info .globus_info )
143+ hpss_get (command_info , command_info .get_db_name ())
155144 else :
145+ # NOTE: while --hpss is required in `create`, it is optional in `update`!
146+ # If --hpss is not provided, we assume it is 'none' => HPSSType.NO_HPSS
156147 error_str : str = (
157- "--hpss argument is required when local copy of database is unavailable"
148+ "--hpss argument (!= none) is required when local copy of database is unavailable"
158149 )
159150 logger .error (error_str )
160151 raise ValueError (error_str )
161152
162153 con : sqlite3 .Connection = sqlite3 .connect (
163- get_db_filename ( cache ), detect_types = sqlite3 .PARSE_DECLTYPES
154+ command_info . get_db_name ( ), detect_types = sqlite3 .PARSE_DECLTYPES
164155 )
165156 cur : sqlite3 .Cursor = con .cursor ()
166157
167- update_config (cur )
158+ command_info . update_config_using_db (cur )
168159
169- if config .maxsize is not None :
170- maxsize = config .maxsize
171- else :
172- raise TypeError ("Invalid config.maxsize={}" .format (config .maxsize ))
173- config .maxsize = int (maxsize )
174-
175- keep : bool
176- # The command line arg should always have precedence
177- if args .hpss == "none" :
178- # If no HPSS is available, always keep the files.
179- keep = True
160+ if command_info .config .maxsize is not None :
161+ command_info .maxsize = command_info .config .maxsize
180162 else :
181- # If HPSS is used, let the user specify whether or not to keep the files.
182- keep = args .keep
163+ raise TypeError (f"Invalid config.maxsize={ command_info .config .maxsize } " )
164+ command_info .config .maxsize = int (command_info .maxsize )
165+
166+ if command_info .hpss_type == HPSSType .NO_HPSS :
167+ # If not using HPSS, always keep the files.
168+ command_info .keep = True
169+ # else: keep command_info.keep set to args.keep
183170
184- if args .hpss is not None :
185- config .hpss = args .hpss
186171
187172 # Start doing actual work
188173 logger .debug ("Running zstash update" )
189- logger .debug ("Local path : {}" . format ( config .path ) )
190- logger .debug ("HPSS path : {}" . format ( config .hpss ) )
191- logger .debug ("Max size : {}" . format ( maxsize ) )
192- logger .debug ("Keep local tar files : {}" . format ( keep ) )
174+ logger .debug (f "Local path : { command_info . config .path } " )
175+ logger .debug (f "HPSS path : { command_info . config .hpss } " )
176+ logger .debug (f "Max size : { command_info . maxsize } " )
177+ logger .debug (f "Keep local tar files : { command_info . keep } " )
193178
194- files : List [str ] = get_files_to_archive (cache , args .include , args .exclude )
179+ files : List [str ] = get_files_to_archive (command_info . get_db_name () , args .include , args .exclude )
195180
196181 # Eliminate files that are already archived and up to date
197182 newfiles : List [str ] = []
@@ -261,8 +246,8 @@ def update_database( # noqa: C901
261246 con ,
262247 itar ,
263248 newfiles ,
264- cache ,
265- keep ,
249+ command_info . get_db_name () ,
250+ command_info . keep ,
266251 args .follow_symlinks ,
267252 non_blocking = args .non_blocking ,
268253 )
@@ -275,8 +260,8 @@ def update_database( # noqa: C901
275260 con ,
276261 itar ,
277262 newfiles ,
278- cache ,
279- keep ,
263+ command_info . get_db_name () ,
264+ command_info . keep ,
280265 args .follow_symlinks ,
281266 non_blocking = args .non_blocking ,
282267 )
0 commit comments