99
1010from .hpss import hpss_get
1111from .settings import (
12- DEFAULT_CACHE ,
1312 FilesRow ,
1413 TarsRow ,
1514 TupleFilesRow ,
1615 TupleTarsRow ,
17- config ,
18- get_db_filename ,
1916 logger ,
2017)
21- from .utils import tars_table_exists , update_config
18+ from .utils import CommandInfo , HPSSType , tars_table_exists
2219
2320
2421def ls ():
2522 """
2623 List all of the files in the HPSS path.
2724 Supports the '-l' argument for more information.
2825 """
29-
30- args : argparse .Namespace
31- cache : str
32- args , cache = setup_ls ()
33-
34- matches : List [FilesRow ] = ls_database (args , cache )
26+ command_info = CommandInfo ("ls" )
27+ args : argparse .Namespace = setup_ls (command_info )
28+ matches : List [FilesRow ] = ls_database (command_info , args )
3529
3630 print_matches (args , matches )
3731
3832 if args .tars :
39- tar_matches : List [TarsRow ] = ls_tars_database (args , cache )
33+ tar_matches : List [TarsRow ] = ls_tars_database (command_info , args )
4034 print_matches (args , tar_matches )
4135
4236
43- def setup_ls () -> Tuple [argparse .Namespace , str ]:
37+ def setup_ls (command_info : CommandInfo ) -> Tuple [argparse .Namespace , str ]:
4438 parser : argparse .ArgumentParser = argparse .ArgumentParser (
4539 usage = "zstash ls [<args>] [files]" ,
4640 description = "List the files from an existing archive. If `files` is specified, then only the files specified will be listed. If `hpss=none`, then this will list the directories and files in the current directory excluding the cache." ,
@@ -76,33 +70,30 @@ def setup_ls() -> Tuple[argparse.Namespace, str]:
7670
7771 parser .add_argument ("files" , nargs = "*" , default = ["*" ])
7872 args : argparse .Namespace = parser .parse_args (sys .argv [2 :])
79- if args .hpss and args .hpss .lower () == "none" :
73+
74+ if args .hpss and (args .hpss .lower () == "none" ):
8075 args .hpss = "none"
81- cache : str
82- if args .cache :
83- cache = args .cache
84- else :
85- cache = DEFAULT_CACHE
8676 if args .verbose :
8777 logger .setLevel (logging .DEBUG )
8878
89- return args , cache
79+ if args .cache :
80+ command_info .cache_dir = args .cache
81+ command_info .keep = args .keep
82+ command_info .set_dir_to_archive (os .getcwd ())
83+ command_info .set_hpss_parameters (args .hpss , null_hpss_allowed = True )
84+
85+ return args
9086
9187
92- def ls_database (args : argparse . Namespace , cache : str ) -> List [FilesRow ]:
88+ def ls_database (command_info : CommandInfo , args : argparse . Namespace ) -> List [FilesRow ]:
9389 # Open database
9490 logger .debug ("Opening index database" )
95- if not os .path .exists (get_db_filename ( cache )):
91+ if not os .path .exists (command_info . get_db_name ( )):
9692 # Will need to retrieve from HPSS
97- if args .hpss is not None :
98- config .hpss = args .hpss
99- if config .hpss is not None :
100- hpss = config .hpss
101- else :
102- raise TypeError ("Invalid config.hpss={}" .format (config .hpss ))
93+ if command_info .hpss_type != HPSSType .UNDEFINED :
10394 try :
10495 # Retrieve from HPSS
105- hpss_get (hpss , get_db_filename ( cache ), cache )
96+ hpss_get (command_info , command_info . get_db_name () )
10697 except RuntimeError :
10798 raise FileNotFoundError ("There was nothing to ls." )
10899 else :
@@ -113,25 +104,16 @@ def ls_database(args: argparse.Namespace, cache: str) -> List[FilesRow]:
113104 raise ValueError (error_str )
114105
115106 con : sqlite3 .Connection = sqlite3 .connect (
116- get_db_filename ( cache ), detect_types = sqlite3 .PARSE_DECLTYPES
107+ command_info . get_db_name ( ), detect_types = sqlite3 .PARSE_DECLTYPES
117108 )
118109 cur : sqlite3 .Cursor = con .cursor ()
119110
120- update_config (cur )
121-
122- if config .maxsize is not None :
123- maxsize : int = config .maxsize
124- else :
125- raise TypeError ("Invalid config.maxsize={}" .format (config .maxsize ))
126- config .maxsize = maxsize
127-
128- # The command line arg should always have precedence
129- if args .hpss is not None :
130- config .hpss = args .hpss
111+ command_info .update_config_using_db (cur )
112+ command_info .validate_maxsize ()
131113
132114 # Start doing actual work
133115 logger .debug ("Running zstash ls" )
134- logger .debug ("HPSS path : %s" % ( config .hpss ) )
116+ logger .debug (f "HPSS path : { command_info . config .hpss } " )
135117
136118 # Find matching files
137119 matches_ : List [TupleFilesRow ] = []
@@ -165,9 +147,9 @@ def ls_database(args: argparse.Namespace, cache: str) -> List[FilesRow]:
165147 return matches
166148
167149
168- def ls_tars_database (args : argparse . Namespace , cache : str ) -> List [TarsRow ]:
150+ def ls_tars_database (command_info : CommandInfo , args : argparse . Namespace ) -> List [TarsRow ]:
169151 con : sqlite3 .Connection = sqlite3 .connect (
170- get_db_filename ( cache ), detect_types = sqlite3 .PARSE_DECLTYPES
152+ command_info . get_db_name ( ), detect_types = sqlite3 .PARSE_DECLTYPES
171153 )
172154 cur : sqlite3 .Cursor = con .cursor ()
173155
0 commit comments