Esempio n. 1
0
 def test_context_required(self):
     """Verify that accessing a property of an Archive outside of a context manager raises."""
     with self.assertRaises(InvalidOperation):
         filepath = get_archive_file('export_v0.1_simple.aiida',
                                     filepath='export/migrate')
         archive = Archive(filepath)
         archive.version_format  # pylint: disable=pointless-statement
Esempio n. 2
0
def inspect(archive, version, data, meta_data):
    """Inspect contents of an exported archive without importing it.

    By default a summary of the archive contents will be printed. The various options can be used to change exactly what
    information is displayed.
    """
    from aiida.tools.importexport import Archive, CorruptArchive

    with Archive(archive) as archive_object:
        try:
            if version:
                echo.echo(archive_object.version_format)
            elif data:
                echo.echo_dictionary(archive_object.data)
            elif meta_data:
                echo.echo_dictionary(archive_object.meta_data)
            else:
                info = archive_object.get_info()
                data = sorted([(k.capitalize(), v) for k, v in info.items()])
                data.extend(
                    sorted([(k.capitalize(), v) for k, v in
                            archive_object.get_data_statistics().items()]))
                echo.echo(tabulate.tabulate(data))
        except CorruptArchive as exception:
            echo.echo_critical('corrupt archive: {}'.format(exception))
Esempio n. 3
0
    def test_migrate_recursively_specific_version(self):
        """Test the `version` argument of the `migrate_recursively` function."""
        filepath_archive = get_archive_file('export_v0.3_simple.aiida',
                                            **self.core_archive)

        with Archive(filepath_archive) as archive:

            # Incorrect type
            with self.assertRaises(TypeError):
                migrate_recursively(archive.meta_data,
                                    archive.data,
                                    None,
                                    version=0.2)

            # Backward migrations are not supported
            with self.assertRaises(ArchiveMigrationError):
                migrate_recursively(archive.meta_data,
                                    archive.data,
                                    None,
                                    version='0.2')

            migrate_recursively(archive.meta_data,
                                archive.data,
                                None,
                                version='0.3')

            migrated_version = '0.5'
            version = migrate_recursively(archive.meta_data,
                                          archive.data,
                                          None,
                                          version=migrated_version)
            self.assertEqual(version, migrated_version)
Esempio n. 4
0
    def test_migrate_in_place(self):
        """Test that passing the -i/--in-place option will overwrite the passed file."""
        archive = 'export_v0.1_simple.aiida'
        target_version = '0.2'
        filename_input = get_archive_file(archive,
                                          filepath=self.fixture_archive)
        filename_tmp = next(tempfile._get_candidate_names())  # pylint: disable=protected-access

        try:
            # copy file (don't want to overwrite test data)
            shutil.copy(filename_input, filename_tmp)

            # specifying both output and in-place should except
            options = [
                filename_tmp, '--in-place', '--output-file', 'test.aiida'
            ]
            result = self.cli_runner.invoke(cmd_export.migrate, options)
            self.assertIsNotNone(result.exception, result.output)

            # specifying neither output nor in-place should except
            options = [filename_tmp]
            result = self.cli_runner.invoke(cmd_export.migrate, options)
            self.assertIsNotNone(result.exception, result.output)

            # check that in-place migration produces a valid archive in place of the old file
            options = [filename_tmp, '--in-place', '--version', target_version]
            result = self.cli_runner.invoke(cmd_export.migrate, options)
            self.assertIsNone(result.exception, result.output)
            self.assertTrue(os.path.isfile(filename_tmp))
            # check that files in zip file are ok
            self.assertEqual(zipfile.ZipFile(filename_tmp).testzip(), None)
            with Archive(filename_tmp) as archive_object:
                self.assertEqual(archive_object.version_format, target_version)
        finally:
            os.remove(filename_tmp)
Esempio n. 5
0
    def test_migrate_version_specific(self):
        """Test the `-v/--version` option to migrate to a specific version instead of the latest."""
        archive = 'export_v0.1_simple.aiida'
        target_version = '0.2'

        filename_input = get_archive_file(archive,
                                          filepath=self.fixture_archive)
        filename_output = next(tempfile._get_candidate_names())  # pylint: disable=protected-access

        try:
            options = [
                filename_input, filename_output, '--version', target_version
            ]
            result = self.cli_runner.invoke(cmd_export.migrate, options)
            self.assertIsNone(result.exception, result.output)
            self.assertTrue(os.path.isfile(filename_output))
            self.assertEqual(zipfile.ZipFile(filename_output).testzip(), None)

            with Archive(filename_output) as archive_object:
                self.assertEqual(archive_object.version_format, target_version)
        finally:
            delete_temporary_file(filename_output)
Esempio n. 6
0
 def test_empty_archive(self):
     """Verify that attempting to unpack an empty archive raises a `CorruptArchive` exception."""
     filepath = get_archive_file('empty.aiida', filepath='export/migrate')
     with self.assertRaises(CorruptArchive):
         with Archive(filepath) as archive:
             archive.version_format  # pylint: disable=pointless-statement
Esempio n. 7
0
 def test_version_format(self):
     """Verify that `version_format` return the correct archive format version."""
     filepath = get_archive_file('export_v0.1_simple.aiida',
                                 filepath='export/migrate')
     with Archive(filepath) as archive:
         self.assertEqual(archive.version_format, '0.1')