From d7dae4809f409f82d9f081a4c94da945ae1aec9a Mon Sep 17 00:00:00 2001 From: Lionel Untereiner Date: Wed, 15 Jul 2020 15:12:30 +0200 Subject: [PATCH] put config object as parameter to functions --- h5pyd/_apps/hsacl.py | 22 +++++++++++----------- h5pyd/_apps/hscopy.py | 20 +++++++++----------- h5pyd/_apps/hsdel.py | 22 +++++++++++----------- h5pyd/_apps/hsdiff.py | 16 ++++++++-------- h5pyd/_apps/hsget.py | 17 ++++++++--------- h5pyd/_apps/hsinfo.py | 17 ++++++++--------- h5pyd/_apps/hsload.py | 18 +++++++++--------- h5pyd/_apps/hsls.py | 39 +++++++++++++++++++-------------------- h5pyd/_apps/hsmv.py | 30 ++++++++++++++---------------- h5pyd/_apps/hstouch.py | 34 +++++++++++++++++----------------- 10 files changed, 114 insertions(+), 121 deletions(-) diff --git a/h5pyd/_apps/hsacl.py b/h5pyd/_apps/hsacl.py index c0cd897e..684ed3b6 100755 --- a/h5pyd/_apps/hsacl.py +++ b/h5pyd/_apps/hsacl.py @@ -18,8 +18,6 @@ else: from .config import Config -cfg = Config() - # # get given ACL, return None if not found # @@ -46,7 +44,7 @@ def getACL(f, username="default"): # # Usage # -def printUsage(): +def printUsage(cfg): print("") print("Usage: {} [options] domain [+crudep] [-crudep] [userid1 userid2 ...]".format(cfg["cmd"])) print("") @@ -75,6 +73,8 @@ def printUsage(): def main(): + cfg = Config() + cfg["cmd"] = sys.argv[0].split('/')[-1] if cfg["cmd"].endswith(".py"): cfg["cmd"] = "python " + cfg["cmd"] @@ -91,7 +91,7 @@ def main(): remove_list = set() if len(sys.argv) == 1 or sys.argv[1] == "-h": - printUsage() + printUsage(cfg) # setup logging logging.basicConfig(filename=logfname, format='%(levelname)s %(asctime)s %(message)s', level=loglevel) @@ -120,13 +120,13 @@ def main(): elif val == "ERROR": loglevel = logging.ERROR else: - printUsage() + printUsage(cfg) argn += 2 elif domain is None and arg == '--logfile': logfname = val argn += 2 elif domain is None and arg in ("-h", "--help"): - printUsage() + printUsage(cfg) elif domain is None and arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val argn += 2 @@ -141,7 +141,7 @@ def main(): argn += 2 elif domain is None and arg[0] in ('-', '+'): print("No domain given") - printUsage() + printUsage(cfg) elif domain is None: logging.debug("get domain") domain = arg @@ -154,7 +154,7 @@ def main(): logging.debug("got plus") if len(usernames) > 0: logging.debug("usernames:", usernames) - printUsage() + printUsage(cfg) add_list = set(arg[1:]) logging.info("add_list:", add_list) argn += 1 @@ -162,7 +162,7 @@ def main(): elif arg[0] == '-': logging.debug("got minus") if len(usernames) > 0: - printUsage() + printUsage(cfg) remove_list = set(arg[1:]) logging.info("remove_list:", remove_list) argn += 1 @@ -170,7 +170,7 @@ def main(): logging.info("got username:", arg) if arg.find('/') >= 0: print("Invalid username:", arg) - printUsage() + printUsage(cfg) usernames.append(arg) argn += 1 @@ -181,7 +181,7 @@ def main(): if len(usernames) == 0 and (add_list or remove_list): print("At least one username must be given to add/remove permissions") - printUsage() + printUsage(cfg) if domain is None: print("no domain specified") diff --git a/h5pyd/_apps/hscopy.py b/h5pyd/_apps/hscopy.py index e79971ea..cc095694 100755 --- a/h5pyd/_apps/hscopy.py +++ b/h5pyd/_apps/hscopy.py @@ -32,11 +32,8 @@ from .utillib import load_file -cfg = Config() - - #---------------------------------------------------------------------------------- -def usage(): +def usage(cfg): print("Usage:\n") print((" {} [ OPTIONS ] source destination".format(cfg["cmd"]))) print("") @@ -71,6 +68,7 @@ def print_config_example(): #---------------------------------------------------------------------------------- def main(): + cfg = Config() loglevel = logging.ERROR verbose = False @@ -91,7 +89,7 @@ def main(): if arg[0] == '-' and len(src_files) > 0: # options must be placed before filenames print("options must precead source files") - usage() + usage(cfg) sys.exit(-1) if len(sys.argv) > argn + 1: val = sys.argv[argn+1] @@ -112,14 +110,14 @@ def main(): loglevel = logging.ERROR else: print("unknown loglevel") - usage() + usage(cfg) sys.exit(-1) argn += 2 elif arg == '--logfile': logfname = val argn += 2 elif arg in ("-h", "--help"): - usage() + usage(cfg) sys.exit(0) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val @@ -147,7 +145,7 @@ def main(): deflate = compressLevel argn += 1 elif arg[0] == '-': - usage() + usage(cfg) sys.exit(-1) else: src_files.append(arg) @@ -164,7 +162,7 @@ def main(): if len(src_files) < 2: # need at least a src and destination - usage() + usage(cfg) sys.exit(-1) src_domain = src_files[0] des_domain = src_files[1] @@ -173,12 +171,12 @@ def main(): logging.info("target domain: {}".format(des_domain)) if src_domain[0] != '/' or src_domain[-1] == '/': print("source domain must be an absolute path, non-folder domain") - usage() + usage(cfg) sys.exit(-1) if des_domain[0] != '/' or des_domain[-1] == '/': print("source domain must be an absolute path, non-folder domain") - usage() + usage(cfg) sys.exit(-1) diff --git a/h5pyd/_apps/hsdel.py b/h5pyd/_apps/hsdel.py index 3ad35a66..89cc0c56 100644 --- a/h5pyd/_apps/hsdel.py +++ b/h5pyd/_apps/hsdel.py @@ -11,9 +11,7 @@ # Delete domain # -cfg = Config() - -def getFolder(domain, mode='r'): +def getFolder(cfg, domain, mode='r'): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -22,7 +20,7 @@ def getFolder(domain, mode='r'): return dir -def deleteDomain(domain): +def deleteDomain(cfg, domain): # get handle to parent folder if domain.endswith('/'): @@ -39,7 +37,7 @@ def deleteDomain(domain): if not parent_domain.endswith('/'): parent_domain += '/' try: - hparent = getFolder(parent_domain, mode='a') + hparent = getFolder(cfg, parent_domain, mode='a') except IOError as oe: if oe.errno == 404: # Not Found sys.exit("Parent domain: {} not found".format(parent_domain)) @@ -79,7 +77,7 @@ def deleteDomain(domain): # # Usage # -def printUsage(): +def printUsage(cfg): print("usage: {} [-v] [-e endpoint] [-u username] [-p password] [--loglevel debug|info|warning|error] [--logfile ] [--bucket ] domains".format(cfg["cmd"])) print("example: {} -e http://hsdshdflab.hdfgroup.org /hdfgroup/data/test/deleteme.h5".format(cfg["cmd"])) sys.exit() @@ -88,6 +86,8 @@ def printUsage(): # Main # def main(): + cfg = Config() + domains = [] argn = 1 loglevel = logging.ERROR @@ -104,7 +104,7 @@ def main(): val = sys.argv[argn+1] if arg in ("-h", "--help"): - printUsage() + printUsage(cfg) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val argn += 2 @@ -131,20 +131,20 @@ def main(): elif val == "ERROR": loglevel = logging.ERROR else: - printUsage() + printUsage(cfg) argn += 2 elif arg == '--logfile': logfname = val argn += 2 elif arg[0] == '-': - printUsage() + printUsage(cfg) else: domains.append(arg) argn += 1 if len(domains) == 0: # need a domain - printUsage() + printUsage(cfg) logging.basicConfig(filename=logfname, format='%(levelname)s %(asctime)s %(message)s', level=loglevel) @@ -154,7 +154,7 @@ def main(): if not domain.startswith('/'): sys.exit("domain: {} must start with a slash".format(domain)) - deleteDomain(domain) + deleteDomain(cfg, domain) if __name__ == "__main__": diff --git a/h5pyd/_apps/hsdiff.py b/h5pyd/_apps/hsdiff.py index a56e2cd0..13321a76 100755 --- a/h5pyd/_apps/hsdiff.py +++ b/h5pyd/_apps/hsdiff.py @@ -28,7 +28,6 @@ from .config import Config from .chunkiter import ChunkIterator -cfg = Config() def diff_attrs(src, tgt, ctx): """ compare attributes of src and tgt """ @@ -360,7 +359,7 @@ def object_diff_helper(name, obj): #---------------------------------------------------------------------------------- -def usage(): +def usage(cfg): print("Usage:\n") print((" {} [ OPTIONS ] file domain".format(cfg["cmd"]))) print("") @@ -397,6 +396,7 @@ def print_config_example(): #---------------------------------------------------------------------------------- def main(): + cfg = Config() loglevel = logging.ERROR verbose = False @@ -419,7 +419,7 @@ def main(): if arg[0] == '-' and len(src_files) > 0: # options must be placed before filenames print("options must precead source files") - usage() + usage(cfg) sys.exit(-1) if len(sys.argv) > argn + 1: val = sys.argv[argn+1] @@ -446,7 +446,7 @@ def main(): loglevel = logging.ERROR else: print("unknown loglevel") - usage() + usage(cfg) sys.exit(-1) argn += 2 elif arg == '--logfile': @@ -456,7 +456,7 @@ def main(): cfg["hs_bucket"] = val argn += 2 elif arg in ("-h", "--help"): - usage() + usage(cfg) sys.exit(0) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val @@ -471,7 +471,7 @@ def main(): print_config_example() sys.exit(0) elif arg[0] == '-': - usage() + usage(cfg) sys.exit(-1) else: src_files.append(arg) @@ -488,7 +488,7 @@ def main(): if len(src_files) < 2: # need at least a src and destination - usage() + usage(cfg) sys.exit(-1) file_path = src_files[0] domain_path = src_files[1] @@ -497,7 +497,7 @@ def main(): logging.info("domain: {}".format(domain_path)) if domain_path[0] != '/' or domain_path[-1] == '/': print("domain must be an absolute path, non-folder domain") - usage() + usage(cfg) sys.exit(-1) diff --git a/h5pyd/_apps/hsget.py b/h5pyd/_apps/hsget.py index 3e9619cd..82bd5262 100755 --- a/h5pyd/_apps/hsget.py +++ b/h5pyd/_apps/hsget.py @@ -27,10 +27,8 @@ from .config import Config from .utillib import load_file -cfg = Config() # config object - #---------------------------------------------------------------------------------- -def usage(): +def usage(cfg): print("Usage:\n") print((" {} [ OPTIONS ] domain filepath".format(cfg["cmd"]))) print("") @@ -65,6 +63,7 @@ def print_config_example(): #---------------------------------------------------------------------------------- def main(): + cfg = Config() # config object loglevel = logging.ERROR verbose = False @@ -91,7 +90,7 @@ def main(): if arg[0] == '-' and src_domain is not None: # options must be placed before filenames print("options must precead source files") - usage() + usage(cfg) sys.exit(-1) if len(sys.argv) > argn + 1: val = sys.argv[argn+1] @@ -112,7 +111,7 @@ def main(): loglevel = logging.ERROR else: print("unknown loglevel") - usage() + usage(cfg) sys.exit(-1) argn += 2 elif arg == '--logfile': @@ -122,7 +121,7 @@ def main(): bucket = val argn += 2 elif arg in ("-h", "--help"): - usage() + usage(cfg) sys.exit(0) elif arg in ("-e", "--endpoint"): endpoint = val @@ -137,7 +136,7 @@ def main(): print_config_example() sys.exit(0) elif arg[0] == '-': - usage() + usage(cfg) sys.exit(-1) elif src_domain is None: src_domain = arg @@ -146,7 +145,7 @@ def main(): des_file = arg argn += 1 else: - usage() + usage(cfg) sys.exit(-1) # setup logging @@ -161,7 +160,7 @@ def main(): if src_domain is None or des_file is None: # need at least a src and destination - usage() + usage(cfg) sys.exit(-1) logging.info("source domain: {}".format(src_domain)) diff --git a/h5pyd/_apps/hsinfo.py b/h5pyd/_apps/hsinfo.py index 3ef72869..2d37ea45 100644 --- a/h5pyd/_apps/hsinfo.py +++ b/h5pyd/_apps/hsinfo.py @@ -14,13 +14,10 @@ # -cfg = Config() - - # # Usage # -def printUsage(): +def printUsage(cfg): print("Usage: {} [-h] [--loglevel debug|info|warning|error] [--logfile ] [-c oonf_file] [-e endpoint] [-u username] [-p password] [-b bucket] [domain]".format(cfg["cmd"])) print("") print("Description:") @@ -115,7 +112,7 @@ def getServerInfo(cfg): else: print("Error: {}".format(ioe)) -def getDomainInfo(domain, cfg): +def getDomainInfo(cfg, domain): """ get info about the domain and print """ username = cfg["hs_username"] password = cfg["hs_password"] @@ -195,7 +192,7 @@ def getDomainInfo(domain, cfg): # # Get folder in /home/ that is owned by given user # -def getHomeFolder(): +def getHomeFolder(cfg): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -229,6 +226,8 @@ def getHomeFolder(): # Main # def main(): + cfg = Config() + argn = 1 cfg["cmd"] = sys.argv[0].split('/')[-1] if cfg["cmd"].endswith(".py"): @@ -255,13 +254,13 @@ def main(): elif val == "ERROR": cfg["loglevel"] = logging.ERROR else: - printUsage() + printUsage(cfg) argn += 2 elif arg == '--logfile': cfg["logfname"] = val argn += 2 elif arg in ("-h", "--help"): - printUsage() + printUsage(cfg) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val argn += 2 @@ -297,7 +296,7 @@ def main(): getServerInfo(cfg) else: for domain in domains: - getDomainInfo(domain, cfg) + getDomainInfo(cfg, domain) if __name__ == "__main__": diff --git a/h5pyd/_apps/hsload.py b/h5pyd/_apps/hsload.py index 7d8da862..c2abcf87 100755 --- a/h5pyd/_apps/hsload.py +++ b/h5pyd/_apps/hsload.py @@ -47,11 +47,10 @@ else: from urlparse import urlparse -cfg = Config() #---------------------------------------------------------------------------------- -def stage_file(uri, netfam=None, sslv=True): +def stage_file(cfg, uri, netfam=None, sslv=True): if PYCRUL is None: logging.warn("pycurl not available for inline staging of input %s, see pip search pycurl." % uri) return None @@ -84,7 +83,7 @@ def stage_file(uri, netfam=None, sslv=True): #---------------------------------------------------------------------------------- -def usage(): +def usage(cfg): print("Usage:\n") print((" {} [ OPTIONS ] sourcefile domain".format(cfg["cmd"]))) print((" {} [ OPTIONS ] sourcefile folder".format(cfg["cmd"]))) @@ -143,6 +142,7 @@ def print_config_example(): #---------------------------------------------------------------------------------- def main(): + cfg = Config() loglevel = logging.ERROR verbose = False @@ -167,7 +167,7 @@ def main(): if arg[0] == '-' and len(src_files) > 0: # options must be placed before filenames sys.stderr.write("options must precede source files") - usage() + usage(cfg) sys.exit(-1) if len(sys.argv) > argn + 1: @@ -199,7 +199,7 @@ def main(): loglevel = logging.ERROR else: sys.stderr.write("unknown loglevel") - usage() + usage(cfg) sys.exit(-1) argn += 2 elif arg == '--logfile': @@ -213,7 +213,7 @@ def main(): elif arg == '-6': ipvfam = 6 elif arg in ("-h", "--help"): - usage() + usage(cfg) sys.exit(0) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val @@ -241,7 +241,7 @@ def main(): deflate = compressLevel argn += 1 elif arg[0] == '-': - usage() + usage(cfg) sys.exit(-1) else: src_files.append(arg) @@ -258,7 +258,7 @@ def main(): if len(src_files) < 2: # need at least a src and destination - usage() + usage(cfg) sys.exit(-1) domain = src_files[-1] src_files = src_files[:-1] @@ -267,7 +267,7 @@ def main(): logging.info("target domain: {}".format(domain)) if len(src_files) > 1 and (domain[0] != '/' or domain[-1] != '/'): sys.stderr.write("target must be a folder if multiple source files are provided") - usage() + usage(cfg) sys.exit(-1) if cfg["hs_endpoint"] is None: diff --git a/h5pyd/_apps/hsls.py b/h5pyd/_apps/hsls.py index 50c54bf7..fa593efa 100644 --- a/h5pyd/_apps/hsls.py +++ b/h5pyd/_apps/hsls.py @@ -14,10 +14,7 @@ # Print objects in a domain in the style of the hsls utilitiy # -cfg = Config() - - -def intToStr(n): +def intToStr(cfg, n): if cfg["human_readable"]: s = "{:,}".format(n) else: @@ -25,7 +22,7 @@ def intToStr(n): return s -def format_size(n): +def format_size(cfg, n): if n is None or n == ' ': return ' ' * 8 symbol = ' ' @@ -43,7 +40,7 @@ def format_size(n): return "{:7.1f}{}".format(n, symbol) -def getShapeText(dset): +def getShapeText(cfg, dset): shape_text = "Scalar" shape = dset.shape if shape is not None: @@ -68,7 +65,7 @@ def getShapeText(dset): return shape_text -def visititems(name, grp, visited): +def visititems(cfg, name, grp, visited): for k in grp: item = grp.get(k, getlink=True) class_name = item.__class__.__name__ @@ -77,7 +74,7 @@ def visititems(name, grp, visited): # follow hardlinks try: item = grp.get(k) - dump(item_name, item, visited=visited) + dump(cfg, item_name, item, visited=visited) except IOError: # object deleted but hardlink left? desc = "{Missing hardlink object}" @@ -99,7 +96,7 @@ def getTypeStr(dt): return str(dt) -def dump(name, obj, visited=None): +def dump(cfg, name, obj, visited=None): class_name = obj.__class__.__name__ desc = None obj_id = None @@ -244,7 +241,7 @@ def dumpAcls(obj): dumpACL(acl) -def getFolder(domain): +def getFolder(cfg, domain): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -257,7 +254,7 @@ def getFolder(domain): return dir -def getFile(domain): +def getFile(cfg, domain): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -267,7 +264,7 @@ def getFile(domain): return fh -def visitDomains(domain, depth=1): +def visitDomains(cfg, domain, depth=1): if depth == 0: return 0 @@ -348,7 +345,7 @@ def visitDomains(domain, depth=1): # # Usage # -def printUsage(): +def printUsage(cfg): print("usage: {} [-r] [-v] [-h] [--showacls] [--showattrs] [--loglevel debug|info|warning|error] [--logfile ] [-e endpoint] [-u username] [-p password] [--bucket bucketname] domains".format(cfg["cmd"])) print("example: {} -r -e http://hsdshdflab.hdfgroup.org /shared/tall.h5".format(cfg["cmd"])) print("") @@ -374,6 +371,8 @@ def printUsage(): # Main # def main(): + cfg = Config() + domains = [] argn = 1 depth = 1 @@ -414,7 +413,7 @@ def main(): elif val == "ERROR": loglevel = logging.ERROR else: - printUsage() + printUsage(cfg) argn += 2 elif arg == '--logfile': logfname = val @@ -426,7 +425,7 @@ def main(): cfg["showattrs"] = True argn += 1 elif arg in ("-h", "--help"): - printUsage() + printUsage(cfg) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val argn += 2 @@ -447,7 +446,7 @@ def main(): argn += 2 elif arg[0] == '-': - printUsage() + printUsage(cfg) else: domains.append(arg) argn += 1 @@ -464,12 +463,12 @@ def main(): for domain in domains: if domain.endswith('/'): # given a folder path - count = visitDomains(domain, depth=depth) + count = visitDomains(cfg, domain, depth=depth) print("{} items".format(count)) else: try: - f = getFile(domain) + f = getFile(cfg, domain) except IOError as ioe: if ioe.errno == 401: print("Username/Password missing or invalid") @@ -491,7 +490,7 @@ def main(): if grp is None: print("{}: No such domain".format(domain)) domain += '/' - count = visitDomains(domain, depth=depth) + count = visitDomains(cfg, domain, depth=depth) print("{} items".format(count)) continue dump('/', grp) @@ -500,7 +499,7 @@ def main(): # recursive visited = {} # dict of id to h5path visited[grp.id.id] = '/' - visititems('/', grp, visited) + visititems(cfg, '/', grp, visited) else: for k in grp: item = grp.get(k, getlink=True) diff --git a/h5pyd/_apps/hsmv.py b/h5pyd/_apps/hsmv.py index 036cf59e..1b9217c6 100755 --- a/h5pyd/_apps/hsmv.py +++ b/h5pyd/_apps/hsmv.py @@ -26,12 +26,9 @@ else: from .config import Config -cfg = Config() - - #---------------------------------------------------------------------------------- -def usage(): +def usage(cfg): print("Usage:\n") print((" {} [ OPTIONS ] source_domain des_domain".format(cfg["cmd"]))) print((" {} [ OPTIONS ] source_domain folder".format(cfg["cmd"]))) @@ -69,7 +66,7 @@ def print_config_example(): #---------------------------------------------------------------------------------- -def getFolder(domain, mode="r"): +def getFolder(cfg, domain, mode="r"): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -79,7 +76,7 @@ def getFolder(domain, mode="r"): return dir -def getFile(domain, mode="r"): +def getFile(cfg, domain, mode="r"): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -88,7 +85,7 @@ def getFile(domain, mode="r"): password=password, bucket=bucket, use_cache=True) return fh -def createFile(domain, linked_domain=None): +def createFile(cfg, domain, linked_domain=None): #print("createFile", domain) username = cfg["hs_username"] password = cfg["hs_password"] @@ -143,6 +140,7 @@ def deleteDomain(domain, keep_root=False): print("Domain: {} deleted".format(domain)) def main(): + cfg = Config() loglevel = logging.ERROR verbose = False @@ -161,7 +159,7 @@ def main(): if arg[0] == '-' and len(src_files) > 0: # options must be placed before filenames print("options must precead source files") - usage() + usage(cfg) sys.exit(-1) if len(sys.argv) > argn + 1: val = sys.argv[argn+1] @@ -179,14 +177,14 @@ def main(): loglevel = logging.ERROR else: print("unknown loglevel") - usage() + usage(cfg) sys.exit(-1) argn += 2 elif arg == '--logfile': logfname = val argn += 2 elif arg in ("-h", "--help"): - usage() + usage(cfg) sys.exit(0) elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = val @@ -204,7 +202,7 @@ def main(): print_config_example() sys.exit(0) elif arg[0] == '-': - usage() + usage(cfg) sys.exit(-1) else: src_files.append(arg) @@ -221,14 +219,14 @@ def main(): if len(src_files) < 2: # need at least a src and destination - usage() + usage(cfg) sys.exit(-1) src_domain = src_files[0] des_domain = src_files[1] if src_domain[0] != '/' or des_domain[0] != '/': print("absolute paths must be used") - usage() + usage(cfg) sys.exit(-1) if src_domain[-1] == '/': @@ -250,7 +248,7 @@ def main(): # get root id of source file try: - fin = getFile(src_domain, "r") + fin = getFile(cfg, src_domain, "r") except IOError as oe: # this will fail if we try to open a folder msg = "Error: {} getting domain: {}".format(oe.errno, src_domain) @@ -263,7 +261,7 @@ def main(): # create a new file using the src domain for the root group try: - fout = createFile(des_domain, linked_domain=src_domain) + fout = createFile(cfg, des_domain, linked_domain=src_domain) except IOError as oe: msg = "Error: {} creating domain: {}".format(oe.errno, des_domain) logging.error(msg) @@ -273,7 +271,7 @@ def main(): logging.info("des root id: {}".format(fout.id.id)) try: - deleteDomain(src_domain, keep_root=True) + deleteDomain(cfg, src_domain, keep_root=True) logging.info("domain: {} removed".format(src_domain)) except IOError as oe: msg = "Error: {} removing source domain: {}".format(oe.errno, src_domain) diff --git a/h5pyd/_apps/hstouch.py b/h5pyd/_apps/hstouch.py index b38dcf90..abb28ecb 100644 --- a/h5pyd/_apps/hstouch.py +++ b/h5pyd/_apps/hstouch.py @@ -11,9 +11,7 @@ # Create domain or update timestamp if domain already exist # -cfg = Config() - -def getFolder(domain): +def getFolder(cfg, domain): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -22,7 +20,7 @@ def getFolder(domain): dir = h5py.Folder(domain, endpoint=endpoint, username=username, password=password, bucket=bucket) return dir -def createFolder(domain): +def createFolder(cfg, domain): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -34,7 +32,7 @@ def createFolder(domain): dir = h5py.Folder(domain, mode='x', endpoint=endpoint, username=username, password=password, bucket=bucket, owner=owner) return dir -def getFile(domain): +def getFile(cfg, domain): username = cfg["hs_username"] password = cfg["hs_password"] endpoint = cfg["hs_endpoint"] @@ -43,7 +41,7 @@ def getFile(domain): fh = h5py.File(domain, mode='r', endpoint=endpoint, username=username, password=password, bucket=bucket) return fh -def createFile(domain): +def createFile(cfg, domain): #print("createFile", domain) username = cfg["hs_username"] password = cfg["hs_password"] @@ -57,7 +55,7 @@ def createFile(domain): -def touchDomain(domain): +def touchDomain(cfg, domain): make_folder = False if domain[-1] == '/': @@ -79,7 +77,7 @@ def touchDomain(domain): if not parent_domain.endswith('/'): parent_domain += '/' try: - getFolder(parent_domain) + getFolder(cfg, parent_domain) except IOError as oe: #print("errno:", oe.errno) if oe.errno in (404, 410): # Not Found @@ -93,7 +91,7 @@ def touchDomain(domain): hdomain = None try: - hdomain = getFile(domain) + hdomain = getFile(cfg, domain) except IOError as oe: #print("errno:", oe.errno) if oe.errno in (404, 410): # Not Found @@ -117,7 +115,7 @@ def touchDomain(domain): # create domain if not make_folder: try: - fh = createFile(domain) + fh = createFile(cfg, domain) if cfg["verbose"]: print("domain created: {}, root id: {}".format(domain, fh.id.id)) fh.close() @@ -126,7 +124,7 @@ def touchDomain(domain): else: # make folder try: - fh = createFolder(domain + '/') + fh = createFolder(cfg, domain + '/') if cfg["verbose"]: print("folder created", domain + '/') fh.close() @@ -136,7 +134,7 @@ def touchDomain(domain): # # Usage # -def printUsage(): +def printUsage(cfg): print("usage: {} [-v] [-e endpoint] [-u username] [-p password] [-o owner] [--loglevel debug|info|warning|error] [--logfile ] [--bucket ] domains".format(cfg["cmd"])) print("example: {} -e http://hsdshdflab.hdfgroup.org /home/myfolder/emptydomain.h5".format(cfg["cmd"])) sys.exit() @@ -145,6 +143,8 @@ def printUsage(): # Main # def main(): + cfg = Config() + domains = [] argn = 1 loglevel = logging.ERROR @@ -161,7 +161,7 @@ def main(): val = sys.argv[argn+1] if arg in ("-h", "--help"): - printUsage() + printUsage(cfg) elif arg in ("-v", "--verbose"): cfg["verbose"] = True argn += 1 @@ -176,7 +176,7 @@ def main(): elif val == "ERROR": loglevel = logging.ERROR else: - printUsage() + printUsage(cfg) argn += 2 elif arg in ("-e", "--endpoint"): cfg["hs_endpoint"] = sys.argv[argn+1] @@ -194,14 +194,14 @@ def main(): cfg["hs_owner"] = sys.argv[argn+1] argn += 2 elif arg[0] == '-': - printUsage() + printUsage(cfg) else: domains.append(arg) argn += 1 if len(domains) == 0: # need a domain - printUsage() + printUsage(cfg) # setup logging logging.basicConfig(filename=logfname, format='%(levelname)s %(asctime)s %(message)s', level=loglevel) @@ -211,7 +211,7 @@ def main(): if not domain.startswith('/'): sys.exit("domain: {} must start with a slash".format(domain)) - touchDomain(domain) + touchDomain(cfg, domain) if __name__ == "__main__": main()