Skip to content

Commit

Permalink
Addressing code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
szoupanos committed Aug 13, 2018
1 parent bfaa84a commit ac085f2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
5 changes: 0 additions & 5 deletions aiida/backends/tests/export_and_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,11 +1808,6 @@ def test_links_for_workflows(self):
o1.add_link_from(w1, 'output', link_type=LinkType.CREATE)
o1.add_link_from(w1, 'return', link_type=LinkType.RETURN)

uuids_wanted = set(_.uuid for _ in (w1,o1,i1))
# links_wanted = [l for l in self.get_all_node_links() if l[3] in
# ('createlink', 'inputlink', 'returnlink', 'calllink')]
# links_wanted = self.get_all_node_links()

links_wanted = [l for l in self.get_all_node_links() if l[3] in
(LinkType.CREATE.value,
LinkType.INPUT.value,
Expand Down
28 changes: 15 additions & 13 deletions aiida/cmdline/commands/exportfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ def cli(self, *args):
help='Export the given computers by pk')
@click.option('-G', '--groups', multiple=True, type=int,
help='Export the given groups by pk')
@click.option('-g', '--group_names', multiple=True, type=str,
@click.option('-g', '--group-names', multiple=True, type=str,
help='Export the given groups by group name')
@click.option('-IF', '--input_forward', is_flag=True, default=False,
help='Follow forward INPUT links (recursively) when calculating the node '
"set to export. By default this is switched off.")
@click.option('-CrR', '--create_reversed', is_flag=True, default=True,
help='Follow reverse CREATE links (recursively) when calculating the node '
'set to export. By default this is switched on.')
@click.option('-RR', '--return_reversed', is_flag=True, default=False,
help='Follow reverse RETURN links (recursively) when calculating the node '
'set to export. By default this is switched off.')
@click.option('-CaR', '--call_reversed', is_flag=True, default=False,
help='Follow reverse CALL links (recursively) when calculating the node '
'set to export. By default this is switched off.')
@click.option('-I', '--input-forward', is_flag=True, default=False,
show_default=True, help='Follow forward INPUT links (recursively) when '
'calculating the node set to export.')
@click.option('-C', '--create-reversed', is_flag=True, default=True,
show_default=True, help='Follow reverse CREATE links (recursively) when '
'calculating the node set to export.')
@click.option('-R', '--return-reversed', is_flag=True, default=False,
show_default=True, help='Follow reverse RETURN links (recursively) when '
'calculating the node set to export.')
@click.option('-X', '--call-reversed', is_flag=True, default=False,
show_default=True, help='Follow reverse CALL links (recursively) when '
'calculating the node set to export.')
@click.option('-f', '--overwrite', is_flag=True, default=False,
help='Overwrite the output file, if it exists')
@click.option('-a', '--archive-format',
Expand All @@ -66,6 +66,8 @@ def create(outfile, computers, groups, nodes, group_names, input_forward,
"""
import sys
from aiida.backends.utils import load_dbenv, is_dbenv_loaded
# TODO: Replace with aiida.cmdline.utils.decorators.with_dbenv decocator
# TODO: when we merge to develop
if not is_dbenv_loaded():
load_dbenv()
from aiida.orm import Group, Node, Computer
Expand Down
6 changes: 5 additions & 1 deletion aiida/cmdline/commands/importfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def run(self, *args):

from aiida.common.folders import SandboxFolder
from aiida.orm.importexport import get_valid_import_links, import_data
from aiida.common.exceptions import ContentNotExistent

parser = argparse.ArgumentParser(
prog=self.get_full_command_name(),
Expand Down Expand Up @@ -82,7 +83,10 @@ def run(self, *args):
for filename in files:
try:
print "**** Importing file {}".format(filename)
import_data(filename)
try:
import_data(filename)
except ContentNotExistent as ce:
print ce
except Exception:
traceback.print_exc()

Expand Down
2 changes: 0 additions & 2 deletions aiida/orm/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ def add_link_from(self, src, label=None, link_type=LinkType.UNSPECIFIED):

if link_type is LinkType.CREATE and \
len(self.get_inputs(link_type=LinkType.CREATE)) > 0:
for l in self.get_inputs(link_type=LinkType.CREATE):
print "=====> ", l, l.dbnode
raise ValueError("At most one CREATE node can enter a data node")

if not isinstance(src, Calculation):
Expand Down
61 changes: 34 additions & 27 deletions aiida/orm/importexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,9 @@ def import_data_dj(in_path,ignore_unknown_nodes=False,
"nor a zip file.")

if not folder.get_content_list():
print "The provided file/folder is empty. Exiting silently"
return

from aiida.common.exceptions import ContentNotExistent
raise ContentNotExistent("The provided file/folder ({}) is empty"
.format(in_path))
try:
with open(folder.get_abs_path('metadata.json')) as f:
metadata = json.load(f)
Expand Down Expand Up @@ -869,7 +869,6 @@ def import_data_sqla(in_path, ignore_unknown_nodes=False, silent=False):

from aiida.orm import Node, Group
from aiida.common.archive import extract_tree, extract_tar, extract_zip, extract_cif
from aiida.common.links import LinkType
from aiida.common.folders import SandboxFolder, RepositoryFolder
from aiida.common.utils import get_object_from_string
from aiida.common.datastructures import calc_states
Expand Down Expand Up @@ -910,6 +909,10 @@ def import_data_sqla(in_path, ignore_unknown_nodes=False, silent=False):
"is neither a (possibly compressed) tar "
"file, nor a zip file.")

if not folder.get_content_list():
from aiida.common.exceptions import ContentNotExistent
raise ContentNotExistent("The provided file/folder ({}) is empty"
.format(in_path))
try:
with open(folder.get_abs_path('metadata.json')) as f:
metadata = json.load(f)
Expand Down Expand Up @@ -1683,10 +1686,10 @@ def export_tree(what, folder,allowed_licenses=None, forbidden_licenses=None,
silent=False, input_forward=False, create_reversed=True,
return_reversed=False, call_reversed=False, **kwargs):
"""
Export the DB entries passed in the 'what' list to a file tree.
Export the entries passed in the 'what' list to a file tree.
:todo: limit the export to finished or failed calculations.
:param what: a list of Django database entries; they can belong to
different models.
:param what: a list of entity instances; they can belong to
different models/entities.
:param folder: a :py:class:`Folder <aiida.common.folders.Folder>` object
:param input_forward: Follow forward INPUT links (recursively) when
calculating the node set to export.
Expand Down Expand Up @@ -1733,7 +1736,7 @@ def export_tree(what, folder,allowed_licenses=None, forbidden_licenses=None,
given_node_entry_ids = set()
given_group_entry_ids = set()

# I store a list of the actual dbnodes
# I keep the pks of the given entities
for entry in what:
if issubclass(entry.__class__, Group):
given_group_entry_ids.add(entry.pk)
Expand Down Expand Up @@ -1908,7 +1911,6 @@ def export_tree(what, folder,allowed_licenses=None, forbidden_licenses=None,
else:
curr_node_id = to_be_visited[Data].pop()
# If it is already visited continue to the next node
# If it is already visited continue to the next node
if curr_node_id in to_be_exported:
continue
# Otherwise say that it is a node to be exported
Expand Down Expand Up @@ -2599,16 +2601,29 @@ def export_zip(what, outfile = 'testzip', overwrite = False,
print "File written in {:10.3g} s.".format(time.time() - t)


def export(what, outfile='export_data.aiida.tar.gz', overwrite=False, silent=False, **kwargs):
def export(what, outfile='export_data.aiida.tar.gz', overwrite=False,
silent=False, **kwargs):
"""
Export the DB entries passed in the 'what' list on a file.
Export the entries passed in the 'what' list to a file tree.
:todo: limit the export to finished or failed calculations.
:param what: a list of Django database entries; they can belong to different
models.
:param also_parents: if True, also all the parents are stored (from the transitive closure)
:param also_calc_outputs: if True, any output of a calculation is also exported
:param what: a list of entity instances; they can belong to
different models/entities.
:param input_forward: Follow forward INPUT links (recursively) when
calculating the node set to export.
:param create_reversed: Follow reversed CREATE links (recursively) when
calculating the node set to export.
:param return_reversed: Follow reversed RETURN links (recursively) when
calculating the node set to export.
:param call_reversed: Follow reversed CALL links (recursively) when
calculating the node set to export.
:param allowed_licenses: a list or a function. If a list, then checks
whether all licenses of Data nodes are in the list. If a function,
then calls function for licenses of Data nodes expecting True if
license is allowed, False otherwise.
:param forbidden_licenses: a list or a function. If a list, then checks
whether all licenses of Data nodes are in the list. If a function,
then calls function for licenses of Data nodes expecting True if
license is allowed, False otherwise.
:param outfile: the filename of the file on which to export
:param overwrite: if True, overwrite the output file without asking.
if False, raise an IOError in this case.
Expand All @@ -2635,25 +2650,17 @@ def export(what, outfile='export_data.aiida.tar.gz', overwrite=False, silent=Fal
if not silent:
print "COMPRESSING..."

# PAX_FORMAT: virtually no limitations, better support for unicode
# characters
# dereference=True: at the moment, we should not have any symlink or
# hardlink in the AiiDA repository; therefore, do not store symlinks
# or hardlinks, but store the actual destinations.
# This also simplifies the checks on import.
t3 = time.time()
with tarfile.open(outfile, "w:gz", format=tarfile.PAX_FORMAT,
dereference=True) as tar:
tar.add(folder.abspath, arcname="")

# import shutil
# shutil.make_archive(outfile, 'zip', folder.abspath)#, base_dir='aiida')
t4 = time.time()

if not silent:
filecr_time = t2-t1
filecomp_time = t4-t3
print "Exported in {:6.2g}s, compressed in {:6.2g}s, total: {:6.2g}s.".format(filecr_time, filecomp_time, filecr_time + filecomp_time)
print("Exported in {:6.2g}s, compressed in {:6.2g}s, total: {:6.2g}s."
.format(filecr_time, filecomp_time, filecr_time + filecomp_time))

if not silent:
print "DONE."
Expand Down

0 comments on commit ac085f2

Please sign in to comment.