Skip to content

Commit dfa3438

Browse files
committed
zstash extract refactored
1 parent 5f520cd commit dfa3438

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed

zstash/extract.py

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,19 @@
2626
TIME_TOL,
2727
FilesRow,
2828
TupleFilesRow,
29-
config,
30-
get_db_filename,
3129
logger,
3230
)
33-
from .utils import tars_table_exists, update_config
31+
from .utils import CommandInfo, HPSSType, tars_table_exists
3432

3533

3634
def extract(keep_files: bool = True):
3735
"""
3836
Given an HPSS path in the zstash database or passed via the command line,
3937
extract the archived data based on the file pattern (if given).
4038
"""
41-
args: argparse.Namespace
42-
cache: str
43-
args, cache = setup_extract()
44-
45-
failures: List[FilesRow] = extract_database(args, cache, keep_files)
39+
command_info = CommandInfo("extract")
40+
args: argparse.Namespace = setup_extract(command_info)
41+
failures: List[FilesRow] = extract_database(command_info, args, keep_files)
4642

4743
if failures:
4844
logger.error("Encountered an error for files:")
@@ -64,7 +60,7 @@ def extract(keep_files: bool = True):
6460
)
6561

6662

67-
def setup_extract() -> Tuple[argparse.Namespace, str]:
63+
def setup_extract(command_info: CommandInfo) -> Tuple[argparse.Namespace, str]:
6864
parser: argparse.ArgumentParser = argparse.ArgumentParser(
6965
usage="zstash extract [<args>] [files]",
7066
description="Extract files from existing archive",
@@ -103,19 +99,22 @@ def setup_extract() -> Tuple[argparse.Namespace, str]:
10399
)
104100
parser.add_argument("files", nargs="*", default=["*"])
105101
args: argparse.Namespace = parser.parse_args(sys.argv[2:])
106-
if args.hpss and args.hpss.lower() == "none":
102+
103+
if args.hpss and (args.hpss.lower() == "none"):
107104
args.hpss = "none"
108-
if args.cache:
109-
cache = args.cache
110-
else:
111-
cache = DEFAULT_CACHE
112105
# Note: setting logging level to anything other than DEBUG doesn't work with
113106
# multiple workers. This must have someting to do with the custom logger
114107
# implemented for multiple workers.
115108
if args.verbose or args.workers > 1:
116109
logger.setLevel(logging.DEBUG)
117110

118-
return args, cache
111+
if args.cache:
112+
command_info.cache_dir = args.cache
113+
command_info.keep = args.keep
114+
command_info.set_dir_to_archive(os.getcwd())
115+
command_info.set_hpss_parameters(args.hpss, null_hpss_allowed=True)
116+
117+
return args
119118

120119

121120
def parse_tars_option(tars: str, first_tar: str, last_tar: str) -> List[str]:
@@ -156,58 +155,43 @@ def parse_tars_option(tars: str, first_tar: str, last_tar: str) -> List[str]:
156155
return tar_list
157156

158157

159-
def extract_database(
160-
args: argparse.Namespace, cache: str, keep_files: bool
158+
def extract_database(command_info: CommandInfo,
159+
args: argparse.Namespace, keep_files: bool
161160
) -> List[FilesRow]:
162161

163162
# Open database
164163
logger.debug("Opening index database")
165-
if not os.path.exists(get_db_filename(cache)):
164+
if not os.path.exists(command_info.get_db_name()):
166165
# Will need to retrieve from HPSS
167-
if args.hpss is not None:
168-
config.hpss = args.hpss
169-
if config.hpss is not None:
170-
hpss: str = config.hpss
171-
else:
172-
raise TypeError("Invalid config.hpss={}".format(config.hpss))
173-
hpss_get(hpss, get_db_filename(cache), cache)
166+
if command_info.hpss_type != HPSSType.UNDEFINED:
167+
hpss_get(command_info.config.hpss, command_info.get_db_name())
174168
else:
175169
error_str: str = (
176170
"--hpss argument is required when local copy of database is unavailable"
177171
)
178172
logger.error(error_str)
179-
180173
raise ValueError(error_str)
181174
con: sqlite3.Connection = sqlite3.connect(
182-
get_db_filename(cache), detect_types=sqlite3.PARSE_DECLTYPES
175+
command_info.get_db_name(), detect_types=sqlite3.PARSE_DECLTYPES
183176
)
184177
cur: sqlite3.Cursor = con.cursor()
185178

186-
update_config(cur)
187-
if config.maxsize is not None:
188-
maxsize = config.maxsize
189-
else:
190-
raise TypeError("Invalid config.maxsize={}".format(config.maxsize))
191-
config.maxsize = int(maxsize)
192-
193-
# The command line arg should always have precedence
194-
if args.hpss is not None:
195-
config.hpss = args.hpss
196-
keep: bool
197-
if config.hpss == "none":
198-
# If no HPSS is available, always keep the files.
199-
keep = True
200-
else:
201-
keep = args.keep
179+
command_info.update_config_using_db(cur)
180+
command_info.validate_maxsize()
181+
182+
if command_info.hpss_type == HPSSType.NO_HPSS:
183+
# If not using HPSS, always keep the files.
184+
command_info.keep = True
185+
# else: keep command_info.keep set to args.keep
202186

203187
# Start doing actual work
204188
cmd: str = "extract" if keep_files else "check"
205189

206190
logger.debug("Running zstash " + cmd)
207-
logger.debug("Local path : {}".format(config.path))
208-
logger.debug("HPSS path : {}".format(config.hpss))
209-
logger.debug("Max size : {}".format(config.maxsize))
210-
logger.debug("Keep local tar files : {}".format(keep))
191+
logger.debug(f"Local path : {command_info.config.path}")
192+
logger.debug(f"HPSS path : {command_info.config.hpss}")
193+
logger.debug(f"Max size : {command_info.config.maxsize}")
194+
logger.debug(f"Keep local tar files : {command_info.keep}")
211195

212196
matches_: List[TupleFilesRow] = []
213197
if args.tars is not None:
@@ -277,10 +261,10 @@ def extract_database(
277261
if args.workers > 1:
278262
logger.debug("Running zstash {} with multiprocessing".format(cmd))
279263
failures = multiprocess_extract(
280-
args.workers, matches, keep_files, keep, cache, cur, args
264+
args.workers, matches, keep_files, command_info.keep, command_info.cache_dir, cur, args
281265
)
282266
else:
283-
failures = extractFiles(matches, keep_files, keep, cache, cur, args)
267+
failures = extractFiles(matches, keep_files, command_info.keep, command_info.cache_dir, cur, args)
284268

285269
# Close database
286270
logger.debug("Closing index database")

0 commit comments

Comments
 (0)