66import os .path
77import sqlite3
88import sys
9- from typing import Any , List , Tuple
9+ from typing import Any , List
1010
1111from six .moves .urllib .parse import urlparse
1212
1313from .globus import globus_activate , globus_finalize
1414from .hpss import hpss_put
1515from .hpss_utils import add_files
16- from .settings import DEFAULT_CACHE , config , get_db_filename , logger
16+ from .settings import logger
1717from .utils import (
18+ CommandInfo ,
19+ HPSSType ,
1820 create_tars_table ,
1921 get_files_to_archive ,
2022 run_command ,
2426
2527
2628def create ():
27- cache : str
28- cache , args = setup_create ()
29-
30- # Check config fields
31- if config .path is not None :
32- path : str = config .path
33- else :
34- raise TypeError ("Invalid config.path={}" .format (config .path ))
35- if config .hpss is not None :
36- hpss : str = config .hpss
37- else :
38- raise TypeError ("Invalid config.hpss={}" .format (config .hpss ))
29+ command_info = CommandInfo ("create" )
30+ args = setup_create (command_info )
3931
4032 # Start doing actual work
4133 logger .debug (f"{ ts_utc ()} : Running zstash create" )
42- logger .debug ("Local path : {}" . format ( path ) )
43- logger .debug ("HPSS path : {}" . format ( hpss ) )
44- logger .debug ("Max size : {}" . format ( config . maxsize ) )
45- logger .debug ("Keep local tar files : {}" . format ( args . keep ) )
34+ logger .debug (f "Local path: { command_info . dir_to_archive_absolute } " )
35+ logger .debug (f "HPSS path: { command_info . hpss_path } " )
36+ logger .debug (f "Max size: { command_info . maxsize } " )
37+ logger .debug (f "Keep local tar files: { command_info . keep } " )
4638
4739 # Make sure input path exists and is a directory
4840 logger .debug ("Making sure input path exists and is a directory" )
49- if not os .path .isdir (path ):
41+ if not os .path .isdir (command_info . dir_to_archive_absolute ):
5042 # Input path is not a directory
51- input_path_error_str : str = "Input path should be a directory: {}" . format ( path )
43+ input_path_error_str : str = f "Input path should be a directory: { command_info . dir_to_archive_absolute } "
5244 logger .error (input_path_error_str )
5345 raise NotADirectoryError (input_path_error_str )
5446
55- if hpss != "none" :
56- url = urlparse (hpss )
57- if url .scheme == "globus" :
58- # identify globus endpoints
59- logger .debug (f"{ ts_utc ()} :Calling globus_activate(hpss)" )
60- globus_activate (hpss )
61- else :
62- # config.hpss is not "none", so we need to
63- # create target HPSS directory
64- logger .debug (f"{ ts_utc ()} : Creating target HPSS directory { hpss } " )
65- mkdir_command : str = "hsi -q mkdir -p {}" .format (hpss )
66- mkdir_error_str : str = "Could not create HPSS directory: {}" .format (hpss )
67- run_command (mkdir_command , mkdir_error_str )
47+ if command_info .hpss_type == HPSSType .GLOBUS :
48+ # identify globus endpoints
49+ logger .debug (f"{ ts_utc ()} : Calling globus_activate" )
50+ globus_activate (command_info .globus_info )
51+ elif command_info .hpss_type == HPSSType .SAME_MACHINE_HPSS :
52+ logger .debug (f"{ ts_utc ()} : Creating target HPSS directory { command_info .hpss_path } " )
53+ mkdir_command : str = f"hsi -q mkdir -p { command_info .hpss_path } "
54+ mkdir_error_str : str = f"Could not create HPSS directory: { command_info .hpss_path } "
55+ run_command (mkdir_command , mkdir_error_str )
6856
69- # Make sure it is exists and is empty
70- logger .debug ("Making sure target HPSS directory exists and is empty" )
57+ # Make sure it is exists and is empty
58+ logger .debug ("Making sure target HPSS directory exists and is empty" )
7159
72- ls_command : str = 'hsi -q "cd {}; ls -l"' . format ( hpss )
73- ls_error_str : str = "Target HPSS directory is not empty"
74- run_command (ls_command , ls_error_str )
60+ ls_command : str = f 'hsi -q "cd { command_info . hpss_path } ; ls -l"'
61+ ls_error_str : str = "Target HPSS directory is not empty"
62+ run_command (ls_command , ls_error_str )
7563
7664 # Create cache directory
7765 logger .debug (f"{ ts_utc ()} : Creating local cache directory" )
78- os .chdir (path )
66+ os .chdir (command_info . dir_to_archive_absolute )
7967 try :
80- os .makedirs (cache )
68+ os .makedirs (command_info . cache_dir )
8169 except OSError as exc :
8270 if exc .errno != errno .EEXIST :
8371 cache_error_str : str = "Cannot create local cache directory"
@@ -88,11 +76,12 @@ def create():
8876
8977 # Create and set up the database
9078 logger .debug (f"{ ts_utc ()} : Calling create_database()" )
91- failures : List [str ] = create_database (cache , args )
79+ failures : List [str ] = create_database (command_info , args )
9280
93- # Transfer to HPSS. Always keep a local copy.
94- logger .debug (f"{ ts_utc ()} : calling hpss_put() for { get_db_filename (cache )} " )
95- hpss_put (hpss , get_db_filename (cache ), cache , keep = args .keep , is_index = True )
81+ # Transfer to HPSS. Always keep a local copy of the database.
82+ logger .debug (f"{ ts_utc ()} : calling hpss_put() for { command_info .get_db_name ()} " )
83+ # TODO: (A) Continue refactoring from here
84+ hpss_put (command_info , command_info .get_db_name (), is_index = True )
9685
9786 logger .debug (f"{ ts_utc ()} : calling globus_finalize()" )
9887 globus_finalize (non_blocking = args .non_blocking )
@@ -104,7 +93,7 @@ def create():
10493 logger .error ("Failed to archive {}" .format (file_path ))
10594
10695
107- def setup_create () -> Tuple [ str , argparse .Namespace ] :
96+ def setup_create (ci : CommandInfo ) -> argparse .Namespace :
10897 # Parser
10998 parser : argparse .ArgumentParser = argparse .ArgumentParser (
11099 usage = "zstash create [<args>] path" , description = "Create a new zstash archive"
@@ -175,27 +164,25 @@ def setup_create() -> Tuple[str, argparse.Namespace]:
175164 if args .verbose :
176165 logger .setLevel (logging .DEBUG )
177166
178- # Copy configuration
179- config .path = os .path .abspath (args .path )
180- config .hpss = args .hpss
181- config .maxsize = int (1024 * 1024 * 1024 * args .maxsize )
182- cache : str
183167 if args .cache :
184- cache = args .cache
185- else :
186- cache = DEFAULT_CACHE
168+ ci .cache_dir = args .cache
169+ ci .keep = args .keep
170+ ci .set_dir_to_archive (args .path )
171+ ci .set_maxsize (args .maxsize )
172+ ci .set_hpss_parameters (args .hpss )
187173
188- return cache , args
174+ return args
189175
190176
191- def create_database (cache : str , args : argparse .Namespace ) -> List [str ]:
177+ def create_database (command_info : CommandInfo , args : argparse .Namespace ) -> List [str ]:
192178 # Create new database
193179 logger .debug (f"{ ts_utc ()} :Creating index database" )
194- if os .path .exists (get_db_filename (cache )):
180+ db_name : str = command_info .get_db_name ()
181+ if os .path .exists (db_name ):
195182 # Remove old database
196- os .remove (get_db_filename ( cache ) )
183+ os .remove (db_name )
197184 con : sqlite3 .Connection = sqlite3 .connect (
198- get_db_filename ( cache ) , detect_types = sqlite3 .PARSE_DECLTYPES
185+ db_name , detect_types = sqlite3 .PARSE_DECLTYPES
199186 )
200187 cur : sqlite3 .Cursor = con .cursor ()
201188
@@ -233,8 +220,8 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
233220
234221 # Store configuration in database
235222 # Loop through all attributes of config.
236- for attr in dir (config ):
237- value : Any = getattr (config , attr )
223+ for attr in dir (command_info . config ):
224+ value : Any = getattr (command_info . config , attr )
238225 if not callable (value ) and not attr .startswith ("__" ):
239226 # config.{attr} is not a function.
240227 # The attribute name does not start with "__"
@@ -244,7 +231,7 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
244231 cur .execute ("insert into config values (?,?)" , (attr , value ))
245232 con .commit ()
246233
247- files : List [str ] = get_files_to_archive (cache , args .include , args .exclude )
234+ files : List [str ] = get_files_to_archive (command_info . cache_dir , args .include , args .exclude )
248235
249236 failures : List [str ]
250237 if args .follow_symlinks :
@@ -255,7 +242,7 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
255242 con ,
256243 - 1 ,
257244 files ,
258- cache ,
245+ command_info . cache_dir ,
259246 args .keep ,
260247 args .follow_symlinks ,
261248 skip_tars_md5 = args .no_tars_md5 ,
@@ -270,7 +257,7 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
270257 con ,
271258 - 1 ,
272259 files ,
273- cache ,
260+ command_info . cache_dir ,
274261 args .keep ,
275262 args .follow_symlinks ,
276263 skip_tars_md5 = args .no_tars_md5 ,
0 commit comments