Skip to content

Commit bd08c60

Browse files
committed
Store state with objects
1 parent 0761d4b commit bd08c60

File tree

10 files changed

+538
-530
lines changed

10 files changed

+538
-530
lines changed

zstash/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ def check():
99
"""
1010
# This basically just goes through the process of extracting the files,
1111
# but doesn't actually save the output.
12-
extract.extract(keep_files=False)
12+
extract.extract(do_extract_files=False)

zstash/create.py

Lines changed: 63 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import os.path
77
import sqlite3
88
import sys
9-
from typing import Any, List, Tuple
10-
11-
from six.moves.urllib.parse import urlparse
9+
from typing import Any, List
1210

1311
from .globus import globus_activate, globus_finalize
1412
from .hpss import hpss_put
1513
from .hpss_utils import add_files
16-
from .settings import DEFAULT_CACHE, config, get_db_filename, logger
14+
from .settings import logger
1715
from .utils import (
16+
CommandInfo,
17+
HPSSType,
1818
create_tars_table,
1919
get_files_to_archive,
2020
run_command,
@@ -24,60 +24,54 @@
2424

2525

2626
def 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))
27+
command_info = CommandInfo("create")
28+
args = setup_create(command_info)
3929

4030
# Start doing actual work
4131
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))
32+
logger.debug(f"Local path: {command_info.config.path}")
33+
logger.debug(f"HPSS path: {command_info.config.hpss}")
34+
logger.debug(f"Max size: {command_info.config.maxsize}")
35+
logger.debug(f"Keep local tar files: {command_info.keep}")
4636

4737
# Make sure input path exists and is a directory
4838
logger.debug("Making sure input path exists and is a directory")
49-
if not os.path.isdir(path):
39+
if not command_info.config.path:
40+
raise ValueError("config.path is undefined")
41+
if not os.path.isdir(command_info.config.path):
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 = (
44+
f"Input path should be a directory: {command_info.config.path}"
45+
)
5246
logger.error(input_path_error_str)
5347
raise NotADirectoryError(input_path_error_str)
5448

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)
49+
if command_info.globus_info:
50+
# identify globus endpoints
51+
logger.debug(f"{ts_utc()}: Calling globus_activate")
52+
globus_activate(command_info.globus_info)
53+
elif command_info.hpss_type == HPSSType.SAME_MACHINE_HPSS:
54+
logger.debug(
55+
f"{ts_utc()}: Creating target HPSS directory {command_info.config.hpss}"
56+
)
57+
mkdir_command: str = f"hsi -q mkdir -p {command_info.config.hpss}"
58+
mkdir_error_str: str = (
59+
f"Could not create HPSS directory: {command_info.config.hpss}"
60+
)
61+
run_command(mkdir_command, mkdir_error_str)
6862

69-
# Make sure it is exists and is empty
70-
logger.debug("Making sure target HPSS directory exists and is empty")
63+
# Make sure it is exists and is empty
64+
logger.debug("Making sure target HPSS directory exists and is empty")
7165

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)
66+
ls_command: str = f'hsi -q "cd {command_info.config.hpss}; ls -l"'
67+
ls_error_str: str = "Target HPSS directory is not empty"
68+
run_command(ls_command, ls_error_str)
7569

7670
# Create cache directory
7771
logger.debug(f"{ts_utc()}: Creating local cache directory")
78-
os.chdir(path)
72+
os.chdir(command_info.config.path)
7973
try:
80-
os.makedirs(cache)
74+
os.makedirs(command_info.cache_dir)
8175
except OSError as exc:
8276
if exc.errno != errno.EEXIST:
8377
cache_error_str: str = "Cannot create local cache directory"
@@ -88,23 +82,24 @@ def create():
8882

8983
# Create and set up the database
9084
logger.debug(f"{ts_utc()}: Calling create_database()")
91-
failures: List[str] = create_database(cache, args)
85+
failures: List[str] = create_database(command_info, args)
9286

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)
87+
# Transfer to HPSS. Always keep a local copy of the database.
88+
logger.debug(f"{ts_utc()}: calling hpss_put() for {command_info.get_db_name()}")
89+
hpss_put(command_info, command_info.get_db_name())
9690

97-
logger.debug(f"{ts_utc()}: calling globus_finalize()")
98-
globus_finalize(non_blocking=args.non_blocking)
91+
if command_info.globus_info:
92+
logger.debug(f"{ts_utc()}: calling globus_finalize()")
93+
globus_finalize(command_info.globus_info, non_blocking=args.non_blocking)
9994

10095
if len(failures) > 0:
10196
# List the failures
10297
logger.warning("Some files could not be archived")
10398
for file_path in failures:
104-
logger.error("Failed to archive {}".format(file_path))
99+
logger.error(f"Failed to archive {file_path}")
105100

106101

107-
def setup_create() -> Tuple[str, argparse.Namespace]:
102+
def setup_create(command_info: CommandInfo) -> argparse.Namespace:
108103
# Parser
109104
parser: argparse.ArgumentParser = argparse.ArgumentParser(
110105
usage="zstash create [<args>] path", description="Create a new zstash archive"
@@ -175,27 +170,25 @@ def setup_create() -> Tuple[str, argparse.Namespace]:
175170
if args.verbose:
176171
logger.setLevel(logging.DEBUG)
177172

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
183173
if args.cache:
184-
cache = args.cache
185-
else:
186-
cache = DEFAULT_CACHE
174+
command_info.cache_dir = args.cache
175+
command_info.keep = args.keep
176+
command_info.set_dir_to_archive(args.path)
177+
command_info.set_and_scale_maxsize(args.maxsize)
178+
command_info.set_hpss_parameters(args.hpss)
187179

188-
return cache, args
180+
return args
189181

190182

191-
def create_database(cache: str, args: argparse.Namespace) -> List[str]:
183+
def create_database(command_info: CommandInfo, args: argparse.Namespace) -> List[str]:
192184
# Create new database
193185
logger.debug(f"{ts_utc()}:Creating index database")
194-
if os.path.exists(get_db_filename(cache)):
186+
db_name: str = command_info.get_db_name()
187+
if os.path.exists(db_name):
195188
# Remove old database
196-
os.remove(get_db_filename(cache))
189+
os.remove(db_name)
197190
con: sqlite3.Connection = sqlite3.connect(
198-
get_db_filename(cache), detect_types=sqlite3.PARSE_DECLTYPES
191+
db_name, detect_types=sqlite3.PARSE_DECLTYPES
199192
)
200193
cur: sqlite3.Cursor = con.cursor()
201194

@@ -233,8 +226,8 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
233226

234227
# Store configuration in database
235228
# Loop through all attributes of config.
236-
for attr in dir(config):
237-
value: Any = getattr(config, attr)
229+
for attr in dir(command_info.config):
230+
value: Any = getattr(command_info.config, attr)
238231
if not callable(value) and not attr.startswith("__"):
239232
# config.{attr} is not a function.
240233
# The attribute name does not start with "__"
@@ -244,19 +237,20 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
244237
cur.execute("insert into config values (?,?)", (attr, value))
245238
con.commit()
246239

247-
files: List[str] = get_files_to_archive(cache, args.include, args.exclude)
240+
files: List[str] = get_files_to_archive(
241+
command_info.cache_dir, args.include, args.exclude
242+
)
248243

249244
failures: List[str]
250245
if args.follow_symlinks:
251246
try:
252247
# Add files to archive
253248
failures = add_files(
249+
command_info,
254250
cur,
255251
con,
256252
-1,
257253
files,
258-
cache,
259-
args.keep,
260254
args.follow_symlinks,
261255
skip_tars_md5=args.no_tars_md5,
262256
non_blocking=args.non_blocking,
@@ -266,12 +260,11 @@ def create_database(cache: str, args: argparse.Namespace) -> List[str]:
266260
else:
267261
# Add files to archive
268262
failures = add_files(
263+
command_info,
269264
cur,
270265
con,
271266
-1,
272267
files,
273-
cache,
274-
args.keep,
275268
args.follow_symlinks,
276269
skip_tars_md5=args.no_tars_md5,
277270
non_blocking=args.non_blocking,

0 commit comments

Comments
 (0)