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

Improve error message for verdi import when archive version is incompatible #1960

Merged
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
3 changes: 2 additions & 1 deletion aiida/backends/tests/export_and_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ def test_2(self):
import shutil
import tempfile

from aiida.common import exceptions
from aiida.orm import DataFactory
from aiida.orm.importexport import export

Expand Down Expand Up @@ -310,7 +311,7 @@ def test_2(self):
self.tearDownClass()
self.setUpClass()

with self.assertRaises(ValueError):
with self.assertRaises(exceptions.IncompatibleArchiveVersionError):
import_data(filename, silent=True)
finally:
# Deleting the created temporary folders
Expand Down
45 changes: 30 additions & 15 deletions aiida/cmdline/commands/cmd_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aiida.cmdline.commands.cmd_verdi import verdi
from aiida.cmdline.params.options import MultipleValueOption
from aiida.cmdline.utils import decorators, echo
from aiida.common import exceptions


@verdi.command('import')
Expand All @@ -31,7 +32,7 @@ def cmd_import(archives, webpages):

The ARCHIVES can be specified by their relative or absolute file path, or their HTTP URL.
"""
# pylint: disable=too-many-branches,broad-except
# pylint: disable=too-many-branches,too-many-statements,broad-except
import traceback
from six.moves import urllib

Expand Down Expand Up @@ -64,9 +65,15 @@ def cmd_import(archives, webpages):
echo.echo_critical('no valid exported archives were found')

for archive in archives_file:

echo.echo_info('importing archive {}'.format(archive))

try:
echo.echo_info('importing archive {}'.format(archive))
import_data(archive)
except exceptions.IncompatibleArchiveVersionError as exception:
echo.echo_warning('{} cannot be imported: {}'.format(archive, exception))
echo.echo_warning('run `verdi export migrate {}` to update it'.format(archive))
continue
except Exception:
echo.echo_error('an exception occurred while importing the archive {}'.format(archive))
echo.echo(traceback.format_exc())
Expand All @@ -75,20 +82,28 @@ def cmd_import(archives, webpages):
echo.echo_success('imported archive {}'.format(archive))

for archive in archives_url:
try:
echo.echo_info('downloading archive {}'.format(archive))

echo.echo_info('downloading archive {}'.format(archive))

try:
response = urllib.request.urlopen(archive)
except Exception as exception:
echo.echo_warning('downloading archive {} failed: {}'.format(archive, exception))

with SandboxFolder() as temp_folder:
temp_file = 'importfile.tar.gz'
temp_folder.create_file_from_filelike(response, temp_file)
echo.echo_success('archive downloaded, proceeding with import')
import_data(temp_folder.get_abs_path(temp_file))
with SandboxFolder() as temp_folder:
temp_file = 'importfile.tar.gz'
temp_folder.create_file_from_filelike(response, temp_file)
echo.echo_success('archive downloaded, proceeding with import')

except Exception:
echo.echo_error('an exception occurred while importing the archive {}'.format(archive))
echo.echo(traceback.format_exc())
click.confirm('do you want to continue?', abort=True)
else:
echo.echo_success('imported archive {}'.format(archive))
try:
import_data(temp_folder.get_abs_path(temp_file))
except exceptions.IncompatibleArchiveVersionError as exception:
echo.echo_warning('{} cannot be imported: {}'.format(archive, exception))
echo.echo_warning('download the archive file and run `verdi export migrate` to update it')
continue
except Exception:
echo.echo_error('an exception occurred while importing the archive {}'.format(archive))
echo.echo(traceback.format_exc())
click.confirm('do you want to continue?', abort=True)
else:
echo.echo_success('imported archive {}'.format(archive))
7 changes: 7 additions & 0 deletions aiida/common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,10 @@ class TransportTaskException(Exception):
Raised when a TransportTask, an task to be completed by the engine that requires transport, fails
"""
pass


class IncompatibleArchiveVersionError(Exception):
"""
Raised when trying to import an export archive with an incompatible schema version.
"""
pass
11 changes: 4 additions & 7 deletions aiida/orm/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ def import_data_dj(in_path, ignore_unknown_nodes=False,
# PRELIMINARY CHECKS #
######################
if metadata['export_version'] != expected_export_version:
raise ValueError("File export version is {}, but I can import only "
"version {}".format(metadata['export_version'],
expected_export_version))
raise exceptions.IncompatibleArchiveVersionError('Archive schema version {} is incompatible with the '
'currently supported schema version {}'.format(metadata['export_version'], expected_export_version))

##########################################################################
# CREATE UUID REVERSE TABLES AND CHECK IF I HAVE ALL NODES FOR THE LINKS #
Expand Down Expand Up @@ -928,10 +927,8 @@ def import_data_sqla(in_path, ignore_unknown_nodes=False, silent=False):
# PRELIMINARY CHECKS #
######################
if metadata['export_version'] != expected_export_version:
raise ValueError("File export version is {}, but I can "
"import only version {}"
.format(metadata['export_version'],
expected_export_version))
raise exceptions.IncompatibleArchiveVersionError('Archive schema version {} is incompatible with the '
'currently supported schema version {}'.format(metadata['export_version'], expected_export_version))

###################################################################
# CREATE UUID REVERSE TABLES AND CHECK IF #
Expand Down