Skip to content

Commit

Permalink
[cmd/file_upgrade] is now part of nixio package
Browse files Browse the repository at this point in the history
  • Loading branch information
jgrewe committed Jun 30, 2021
1 parent e5c74d2 commit 2878e0d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
5 changes: 4 additions & 1 deletion nixio/__init__.py
Expand Up @@ -34,11 +34,14 @@
# version
from .info import VERSION

# cmd
from .cmd.upgrade import file_upgrade

__all__ = ("File", "Block", "Group", "DataArray", "DataFrame", "Tag",
"MultiTag", "Source", "Section", "S", "Feature", "Property",
"OdmlType", "SampledDimension", "RangeDimension", "SetDimension",
"FileMode", "DataSliceMode", "DataType", "DimensionType",
"LinkType", "Compression", "SliceMode", "IndexMode", "validator")
"LinkType", "Compression", "SliceMode", "IndexMode", "validator")
__author__ = ('Christian Kellner, Adrian Stoewer, Andrey Sobolev, Jan Grewe, '
'Balint Morvai, Achilleas Koutsou')
__version__ = VERSION
62 changes: 47 additions & 15 deletions nixio/cmd/upgrade.py
Expand Up @@ -218,18 +218,15 @@ def update_ver():


def collect_tasks(fname):
print(f"{fname}:")

tasks = list()
file_ver = get_file_version(fname)
file_verstr = ".".join(str(v) for v in file_ver)
lib_verstr = ".".join(str(v) for v in nix.file.HDF_FF_VERSION)
if file_ver >= nix.file.HDF_FF_VERSION:
print(f" Up to date ({file_verstr})")
return
return tasks, file_verstr, lib_verstr

# even if the version string indicates the file is old, check format
# details before scheduling tasks
tasks = list()
id_task = add_file_id(fname)
if id_task:
tasks.append(id_task)
Expand All @@ -245,11 +242,7 @@ def collect_tasks(fname):
# always update the format last
tasks.append(update_format_version(fname))

# print task list
print(f" {file_verstr} -> {lib_verstr}")
print(" - " + "\n - ".join(t.__doc__ for t in tasks) + "\n")

return tasks
return tasks, file_verstr, lib_verstr


def create_subcmd_parser(parser):
Expand All @@ -260,12 +253,54 @@ def create_subcmd_parser(parser):
return parser


def print_tasks(fname, tasklist, fileversion, libversion):
if len(tasklist) == 0:
print(f"File {fname} is up to date ({fileversion})")
return
print(f"{fname}: {fileversion} -> {libversion}")
print(" - " + "\n - ".join(t.__doc__ for t in tasklist) + "\n")


def process_tasks(fname, tasklist, quiet=True):
if not quiet:
print(f"Processing {fname} ", end="", flush=True)
for task in tasklist:
task()
if not quiet:
print("done")


def file_upgrade(fname, quiet=True):
"""
Upgrades a file from an old format version to the current version.
:param fname: The fully qualified filename.
:type fname: str
:param quiet: Whether or not the upgrade tool should give feedback on the command line. Defaults to True, no output.
:type quiet: bool
:returns: True if the conversion succeeded, False otherwise, it the file does not need upgrading True is returned.
:rtype : bool
"""
try:
tasklist, fileversion, libversion = collect_tasks(fname)
if not quiet:
print_tasks(fname, tasklist, fileversion, libversion)

process_tasks(fname, tasklist, quiet=quiet)
except Exception as e:
print(f"An Exception occurred while upgrading file {fname}. Error is {e}")
return False
return True


def main(args):
filenames = args.file

tasks = dict()
for fname in filenames:
tasklist = collect_tasks(fname)
tasklist, fileversion, libversion = collect_tasks(fname)
print_tasks(fname, tasklist, fileversion, libversion)
if not tasklist:
continue

Expand Down Expand Up @@ -296,7 +331,4 @@ def main(args):

if conf in ("y", "yes"):
for fname, tasklist in tasks.items():
print(f"Processing {fname} ", end="", flush=True)
for task in tasklist:
task()
print("done")
process_tasks(fname, tasklist, quiet=False)

0 comments on commit 2878e0d

Please sign in to comment.