Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/file_upgrade] is now part of nixio package #512

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion nixio/__init__.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)