Skip to content

Commit 5f520cd

Browse files
committed
zstash ls refactored
1 parent f2aa2de commit 5f520cd

File tree

4 files changed

+54
-78
lines changed

4 files changed

+54
-78
lines changed

zstash/create.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ def create():
3131

3232
# Start doing actual work
3333
logger.debug(f"{ts_utc()}: Running zstash create")
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}")
34+
logger.debug(f"Local path: {command_info.config.path}")
35+
logger.debug(f"HPSS path: {command_info.config.hpss}")
36+
logger.debug(f"Max size: {command_info.config.maxsize}")
3737
logger.debug(f"Keep local tar files: {command_info.keep}")
3838

3939
# Make sure input path exists and is a directory
4040
logger.debug("Making sure input path exists and is a directory")
41-
if not os.path.isdir(command_info.dir_to_archive_absolute):
41+
if not os.path.isdir(command_info.config.path):
4242
# Input path is not a directory
43-
input_path_error_str: str = f"Input path should be a directory: {command_info.dir_to_archive_absolute}"
43+
input_path_error_str: str = f"Input path should be a directory: {command_info.config.path}"
4444
logger.error(input_path_error_str)
4545
raise NotADirectoryError(input_path_error_str)
4646

@@ -49,21 +49,21 @@ def create():
4949
logger.debug(f"{ts_utc()}: Calling globus_activate")
5050
globus_activate(command_info.globus_info)
5151
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}"
52+
logger.debug(f"{ts_utc()}: Creating target HPSS directory {command_info.config.hpss}")
53+
mkdir_command: str = f"hsi -q mkdir -p {command_info.config.hpss}"
54+
mkdir_error_str: str = f"Could not create HPSS directory: {command_info.config.hpss}"
5555
run_command(mkdir_command, mkdir_error_str)
5656

5757
# Make sure it is exists and is empty
5858
logger.debug("Making sure target HPSS directory exists and is empty")
5959

60-
ls_command: str = f'hsi -q "cd {command_info.hpss_path}; ls -l"'
60+
ls_command: str = f'hsi -q "cd {command_info.config.hpss}; ls -l"'
6161
ls_error_str: str = "Target HPSS directory is not empty"
6262
run_command(ls_command, ls_error_str)
6363

6464
# Create cache directory
6565
logger.debug(f"{ts_utc()}: Creating local cache directory")
66-
os.chdir(command_info.dir_to_archive_absolute)
66+
os.chdir(command_info.config.path)
6767
try:
6868
os.makedirs(command_info.cache_dir)
6969
except OSError as exc:
@@ -168,7 +168,7 @@ def setup_create(command_info: CommandInfo) -> argparse.Namespace:
168168
command_info.cache_dir = args.cache
169169
command_info.keep = args.keep
170170
command_info.set_dir_to_archive(args.path)
171-
command_info.set_maxsize(args.maxsize)
171+
command_info.set_and_scale_maxsize(args.maxsize)
172172
command_info.set_hpss_parameters(args.hpss)
173173

174174
return args

zstash/ls.py

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,32 @@
99

1010
from .hpss import hpss_get
1111
from .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

2421
def 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

zstash/update.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def update():
4747
logger.warning("Some files could not be archived")
4848
for file_path in failures:
4949
logger.error(f"Archiving {file_path}")
50-
# TODO: (A) Continue refactor from here
5150

5251

5352
def setup_update(command_info: CommandInfo) -> argparse.Namespace:
@@ -121,7 +120,7 @@ def setup_update(command_info: CommandInfo) -> argparse.Namespace:
121120
command_info.cache_dir = args.cache
122121
command_info.keep = args.keep
123122
command_info.set_dir_to_archive(os.getcwd())
124-
command_info.set_maxsize(args.maxsize)
123+
command_info.set_and_scale_maxsize(args.maxsize)
125124
command_info.set_hpss_parameters(args.hpss)
126125

127126
return args
@@ -156,12 +155,7 @@ def update_database( # noqa: C901
156155
cur: sqlite3.Cursor = con.cursor()
157156

158157
command_info.update_config_using_db(cur)
159-
160-
if command_info.config.maxsize is not None:
161-
command_info.maxsize = command_info.config.maxsize
162-
else:
163-
raise TypeError(f"Invalid config.maxsize={command_info.config.maxsize}")
164-
command_info.config.maxsize = int(command_info.maxsize)
158+
command_info.validate_maxsize()
165159

166160
if command_info.hpss_type == HPSSType.NO_HPSS:
167161
# If not using HPSS, always keep the files.
@@ -173,7 +167,7 @@ def update_database( # noqa: C901
173167
logger.debug("Running zstash update")
174168
logger.debug(f"Local path : {command_info.config.path}")
175169
logger.debug(f"HPSS path : {command_info.config.hpss}")
176-
logger.debug(f"Max size : {command_info.maxsize}")
170+
logger.debug(f"Max size : {command_info.config.maxsize}")
177171
logger.debug(f"Keep local tar files : {command_info.keep}")
178172

179173
files: List[str] = get_files_to_archive(command_info.get_db_name(), args.include, args.exclude)

zstash/utils.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class HPSSType(Enum):
1717
NO_HPSS = 1
1818
SAME_MACHINE_HPSS = 2
1919
GLOBUS = 3
20+
UNDEFINED = 4
2021

2122

2223
class GlobusInfo(object):
@@ -56,12 +57,8 @@ def __init__(self, command_name: str):
5657
self.curr_transfers = []
5758

5859
# Use set_dir_to_archive
59-
self.dir_to_archive_absolute = None
6060
self.dir_to_archive_relative = None
6161

62-
# Use set_maxsize
63-
self.maxsize = None
64-
6562
# Use set_hpss_parameters
6663
self.hpss_path = None
6764
self.hpss_type = None
@@ -70,16 +67,22 @@ def __init__(self, command_name: str):
7067
def set_dir_to_archive(self, path: str):
7168
abs_path = os.path.abspath(path)
7269
if abs_path is not None:
73-
self.dir_to_archive_absolute = abs_path
70+
self.config.path = abs_path
7471
self.dir_to_archive_relative = path
7572
else:
7673
raise ValueError(f"Invalid path={path}")
7774

78-
def set_maxsize(self, maxsize):
79-
self.maxsize = int(1024 * 1024 * 1024 * maxsize)
75+
def set_and_scale_maxsize(self, maxsize):
76+
self.config.maxsize = int(1024 * 1024 * 1024 * maxsize)
8077

81-
def set_hpss_parameters(self, hpss_path: str):
82-
self.hpss_path = hpss_path
78+
def validate_maxsize(self):
79+
if self.config.maxsize is not None:
80+
self.config.maxsize = int(self.config.maxsize)
81+
else:
82+
raise ValueError("config.maxsize is undefined")
83+
84+
def set_hpss_parameters(self, hpss_path: str, null_hpss_allowed=False):
85+
self.config.hpss = hpss_path
8386
if hpss_path == "none":
8487
self.hpss_type = HPSSType.NO_HPSS
8588
elif hpss_path is not None:
@@ -89,13 +92,10 @@ def set_hpss_parameters(self, hpss_path: str):
8992
self.globus_info = GlobusInfo(hpss_path)
9093
else:
9194
self.hpss_type = HPSSType.SAME_MACHINE_HPSS
95+
elif null_hpss_allowed:
96+
self.hpss_type = HPSSType.UNDEFINED
9297
else:
93-
raise ValueError(f"Invalid hpss_path={hpss_path}")
94-
95-
def update_config(self):
96-
self.config.path = self.dir_to_archive_absolute
97-
self.config.hpss = self.hpss_path
98-
self.config.maxsize = self.maxsize
98+
raise ValueError(f"hpss_path is undefined")
9999

100100
def update_config_using_db(self, cur: sqlite3.Cursor):
101101
# Retrieve some configuration settings from database

0 commit comments

Comments
 (0)