def calculation_kill(self, *args): """ Kill a calculation. Pass a list of calculation PKs to kill them. If you also pass the -f option, no confirmation will be asked. """ if not is_dbenv_loaded(): load_dbenv() from aiida.cmdline import wait_for_confirmation from aiida.orm.calculation.job import JobCalculation as Calc from aiida.common.exceptions import NotExistent, InvalidOperation, \ RemoteOperationError import argparse parser = argparse.ArgumentParser( prog=self.get_full_command_name(), description='Kill AiiDA calculations.') parser.add_argument( 'calcs', metavar='PK', type=int, nargs='+', help='The principal key (PK) of the calculations to kill') parser.add_argument('-f', '--force', help='Force the kill of calculations', action="store_true") args = list(args) parsed_args = parser.parse_args(args) if not parsed_args.force: sys.stderr.write( "Are you sure to kill {} calculation{}? [Y/N] ".format( len(parsed_args.calcs), "" if len(parsed_args.calcs) == 1 else "s")) if not wait_for_confirmation(): sys.exit(0) counter = 0 for calc_pk in parsed_args.calcs: try: c = load_node(calc_pk, parent_class=Calc) c.kill() # Calc.kill(calc_pk) counter += 1 except NotExistent: print >> sys.stderr, ("WARNING: calculation {} " "does not exist.".format(calc_pk)) except (InvalidOperation, RemoteOperationError) as e: print >> sys.stderr, (e.message) print >> sys.stderr, "{} calculation{} killed.".format( counter, "" if counter == 1 else "s")
def group_delete(self, *args): """ Delete an existing group. """ if not is_dbenv_loaded(): load_dbenv() import argparse from aiida.common.exceptions import NotExistent from aiida.orm import Group as G from aiida.cmdline import wait_for_confirmation parser = argparse.ArgumentParser( prog=self.get_full_command_name(), description='Delete an existing group.') parser.add_argument('-f', '--force', dest='force', action='store_true', help="Force deletion of the group even if it " "is not empty. Note that this deletes only the " "group and not the nodes.") parser.add_argument('GROUP', help="The name or PK of the group to delete") parser.set_defaults(force=False) args = list(args) parsed_args = parser.parse_args(args) group = parsed_args.GROUP force = parsed_args.force try: group_pk = int(group) except ValueError: group_pk = None group_name = group if group_pk is not None: try: group = G(dbgroup=group_pk) except NotExistent as e: print >> sys.stderr, "Error: {}.".format(e.message) sys.exit(1) else: try: group = G.get_from_string(group_name) except NotExistent as e: print >> sys.stderr, "Error: {}.".format(e.message) sys.exit(1) group_pk = group.pk group_name = group.name num_nodes = len(group.nodes) if num_nodes > 0 and not force: print >> sys.stderr, ("Group '{}' is not empty (it contains {} " "nodes). Pass the -f option if you really want to delete " "it.".format(group_name, num_nodes)) sys.exit(1) sys.stderr.write("Are you sure to kill the group with PK = {} ({})? " "[Y/N] ".format(group_pk, group_name)) if not wait_for_confirmation(): sys.exit(0) group.delete() print "Group '{}' (PK={}) deleted.".format(group_name, group_pk)
def group_removenodes(self, *args): """ Remove nodes from a given group. """ from aiida.cmdline import delayed_load_node as load_node from aiida.cmdline import wait_for_confirmation if not is_dbenv_loaded(): load_dbenv() import argparse from aiida.common.exceptions import NotExistent from aiida.orm import Group as G parser = argparse.ArgumentParser( prog=self.get_full_command_name(), description='Remove nodes from a given AiiDA group.') parser.add_argument('-g', '--group', dest='group', required=True, help="The name or PK of the group you want to " "remove a node from.") parser.add_argument('nodes', nargs='+', help="The PK or UUID of the nodes to remove. An " "error is raised if the node does not exist. " "No message is shown if the node does not belong " "to the group.") parser.set_defaults(raw=False) args = list(args) parsed_args = parser.parse_args(args) group = parsed_args.group try: group_pk = int(group) except ValueError: group_pk = None group_name = group if group_pk is not None: try: group = G(dbgroup=group_pk) except NotExistent as e: print >> sys.stderr, "Error: {}.".format(e.message) sys.exit(1) else: try: group = G.get_from_string(group_name) except NotExistent as e: print >> sys.stderr, "Error: {}.".format(e.message) sys.exit(1) group_pk = group.pk group_name = group.name nodes = [] for node in parsed_args.nodes: try: node = int(node) except ValueError: pass # I leave it as a string and let load_node complain # if it is not a UUID try: nodes.append(load_node(node)) except NotExistent as e: print >> sys.stderr, "Error: {}.".format(e.message) sys.exit(1) sys.stderr.write("Are you sure to remove {} nodes from the group " "with PK = {} " "({})? [Y/N] ".format(len(nodes), group_pk, group_name)) if not wait_for_confirmation(): sys.exit(0) group.remove_nodes(nodes)
def workflow_kill(self, *args): """ Kill a workflow. Pass a list of workflow PKs to kill them. If you also pass the -f option, no confirmation will be asked. If you pass the -q option, additional information will be suppressed """ from aiida.backends.utils import load_dbenv, is_dbenv_loaded if not is_dbenv_loaded(): load_dbenv() from aiida.cmdline import wait_for_confirmation from aiida.orm.workflow import kill_from_pk from aiida.common.exceptions import NotExistent from aiida.orm.workflow import WorkflowKillError, WorkflowUnkillable force = False verbose = True wfs = [] args = list(args) while args: param = args.pop() if param == '-f': force = True elif param == '-q': verbose = False else: try: wfs.append(int(param)) except ValueError: print >> sys.stderr, ( "'{}' is not a valid workflow PK.".format(param)) sys.exit(2) if not wfs: print >> sys.stderr, "Pass a list of PKs of workflows to kill." print >> sys.stderr, ("You can pass -f if you do not want to see " "a confirmation message") sys.exit(1) if not force: sys.stderr.write( "Are you sure to kill {} workflow{}? [Y/N] ".format( len(wfs), "" if len(wfs) == 1 else "s")) if not wait_for_confirmation(): sys.exit(0) counter = 0 for wf_pk in wfs: try: kill_from_pk(wf_pk, verbose=verbose) counter += 1 except NotExistent: print >> sys.stderr, ("WARNING: workflow {} " "does not exist.".format(wf_pk)) except WorkflowKillError as e: to_print = "" for msg in e.error_message_list: to_print += msg + "\n" to_print += "{}: {}\n".format(e.__class__.__name__, e.message) sys.stdout.write(to_print) except WorkflowUnkillable as e: sys.stdout.write("{}: {}\n".format(e.__class__.__name__, e.message)) if verbose: print >> sys.stderr, "{} workflow{} killed.".format( counter, "" if counter <= 1 else "s")
def run(self, *args): if not is_dbenv_loaded(): load_dbenv() import argparse from aiida.cmdline import wait_for_confirmation parser = argparse.ArgumentParser(prog=self.get_full_command_name(), description="See description of Nodes. If no node " "description or label is found, prints n.a.") # parameters for display parser.add_argument('-n', '--no-labels', action='store_false', default=True, help="Don't show the labels.") parser.add_argument('-r', '--raw', action='store_true', default=False, help="If set, prints only the description without " "pks or labels.") # pks parser.add_argument('pks', type=int, nargs='+', help="a list of node pks to show.") # parameters for description modifications parser.add_argument('-s', '--set', action='store_true', default=False, help="If present, set a new label, otherwise only " "show the labels.") parser.add_argument('-a', '--add-to-description', action='store_true', default=False, help="If -s, the string passed in -d is appended " "to the current description.") parser.add_argument('-f', '--force', action='store_true', default=False, help="Force the reset of the description.") parser.add_argument('-d', '--description', type=str, help="The new description to be set on the node. " "Note: pass it between quotes.") parsed_args = parser.parse_args(args) pks = parsed_args.pks if not parsed_args.set: also_labels = parsed_args.no_labels for pk in pks: n = load_node(pk) if not self._node_class_ok(n): print "Node {} is not a subclass of {}. Exiting...".format( pk, self._node_subclass) sys.exit(1) label = n.label description = n.description if parsed_args.raw: print '"{}"'.format(n.description) print "" else: print "Node pk: {}".format(pk) if also_labels: if label: print 'Label: "{}"'.format(label) else: print 'Label: n.a.' if description: print 'Description: "{}"'.format(description) else: print 'Description: n.a.' print "" else: # check that only one pk is present if len(pks) > 1: sys.stderr.write( "More than one node found to set one description" ". Exiting...\n") sys.exit(1) else: pk = pks[0] new_description = parsed_args.description if new_description is None: sys.stderr.write("No description was found. Exiting...\n") sys.exit(1) if not parsed_args.add_to_description: n = load_node(pk) if not self._node_class_ok(n): print "Node {} is not a subclass of {}. Exiting...".format( pk, self._node_subclass) sys.exit(1) old_description = n.description if not parsed_args.force: sys.stderr.write( "Current description is: {}\n".format(old_description)) sys.stderr.write( "New description is: {}\n".format(new_description)) sys.stderr.write( "Are you sure you want to reset the description? " "[Y/N] ") if not wait_for_confirmation(): sys.exit(0) n.description = new_description else: n = load_node(pk) if not self._node_class_ok(n): print "Node {} is not a subclass of {}. Exiting...".format( pk, self._node_subclass) sys.exit(1) old_description = n.description new_description = old_description + "\n" + new_description n.description = new_description
def run(self, *args): if not is_dbenv_loaded(): load_dbenv() import argparse from aiida.cmdline import wait_for_confirmation parser = argparse.ArgumentParser(prog=self.get_full_command_name(), description="See/modify the labels of Nodes.") # display parameters parser.add_argument('-r', '--raw', action='store_true', default=False, help="Display only the labels, without the pk.") # pks parser.add_argument('pks', type=int, nargs='+', help="a list of nodes to show.") # parameters for label modification parser.add_argument('-s', '--set', action='store_true', default=False, help="If present, set a new label, otherwise only " "show the labels.") parser.add_argument('-f', '--force', action='store_true', default=False, help="Force the reset of the label.") parser.add_argument('-l', '--label', type=str, default=None, help="The new label to be set on the node. Note: " "pass it between quotes.") parsed_args = parser.parse_args(args) raw = parsed_args.raw pks = parsed_args.pks if not parsed_args.set: for pk in pks: n = load_node(pk) if not self._node_class_ok(n): print "Node {} is not a subclass of {}. Exiting...".format( pk, self._node_subclass) sys.exit(1) if raw: print '"{}"'.format(n.label) else: if not n.label: print 'Node {}, label: n.a.'.format(pk) else: print 'Node {}, label: "{}"'.format(pk, n.label) else: if len(pks) > 1: sys.stderr.write("More than one node found to set one label" ". Exiting...\n") sys.exit(1) else: pk = pks[0] new_label = parsed_args.label if new_label is None: sys.stderr.write("A new label is required" ". Exiting...\n") sys.exit(1) n = load_node(pk) if not self._node_class_ok(n): print "Node {} is not a subclass of {}. Exiting...".format(pk, self._node_subclass) sys.exit(1) old_label = n.label if not parsed_args.force: sys.stderr.write("Current label is: {}\n".format(old_label)) sys.stderr.write("New label is: {}\n".format(new_label)) sys.stderr.write("Are you sure you want to reset the label? " "[Y/N] ") if not wait_for_confirmation(): sys.exit(0) n.label = new_label