|  | 
|  | 1 | +# August 6, 2018 | 
|  | 2 | +# Create parameters for the ssh/scp remote file upload commands | 
|  | 3 | + | 
|  | 4 | + | 
|  | 5 | +from json import loads, dumps | 
|  | 6 | + | 
|  | 7 | +from qiita_db.sql_connection import TRN | 
|  | 8 | +from qiita_db.software import Software, Command | 
|  | 9 | +from qiita_db.exceptions import (QiitaDBError, QiitaDBDuplicateError) | 
|  | 10 | +from qiita_db.util import convert_to_id | 
|  | 11 | + | 
|  | 12 | + | 
|  | 13 | +# Copied from patch 58.py. Couldn't import due to how patching system works | 
|  | 14 | +def create_command(software, name, description, parameters, outputs=None, | 
|  | 15 | +                   analysis_only=False): | 
|  | 16 | +    r"""Replicates the Command.create code at the time the patch was written""" | 
|  | 17 | +    # Perform some sanity checks in the parameters dictionary | 
|  | 18 | +    if not parameters: | 
|  | 19 | +        raise QiitaDBError( | 
|  | 20 | +            "Error creating command %s. At least one parameter should " | 
|  | 21 | +            "be provided." % name) | 
|  | 22 | +    sql_param_values = [] | 
|  | 23 | +    sql_artifact_params = [] | 
|  | 24 | +    for pname, vals in parameters.items(): | 
|  | 25 | +        if len(vals) != 2: | 
|  | 26 | +            raise QiitaDBError( | 
|  | 27 | +                "Malformed parameters dictionary, the format should be " | 
|  | 28 | +                "{param_name: [parameter_type, default]}. Found: " | 
|  | 29 | +                "%s for parameter name %s" % (vals, pname)) | 
|  | 30 | + | 
|  | 31 | +        ptype, dflt = vals | 
|  | 32 | +        # Check that the type is one of the supported types | 
|  | 33 | +        supported_types = ['string', 'integer', 'float', 'reference', | 
|  | 34 | +                           'boolean', 'prep_template', 'analysis'] | 
|  | 35 | +        if ptype not in supported_types and not ptype.startswith( | 
|  | 36 | +                ('choice', 'mchoice', 'artifact')): | 
|  | 37 | +            supported_types.extend(['choice', 'mchoice', 'artifact']) | 
|  | 38 | +            raise QiitaDBError( | 
|  | 39 | +                "Unsupported parameters type '%s' for parameter %s. " | 
|  | 40 | +                "Supported types are: %s" | 
|  | 41 | +                % (ptype, pname, ', '.join(supported_types))) | 
|  | 42 | + | 
|  | 43 | +        if ptype.startswith(('choice', 'mchoice')) and dflt is not None: | 
|  | 44 | +            choices = set(loads(ptype.split(':')[1])) | 
|  | 45 | +            dflt_val = dflt | 
|  | 46 | +            if ptype.startswith('choice'): | 
|  | 47 | +                # In the choice case, the dflt value is a single string, | 
|  | 48 | +                # create a list with it the string on it to use the | 
|  | 49 | +                # issuperset call below | 
|  | 50 | +                dflt_val = [dflt_val] | 
|  | 51 | +            else: | 
|  | 52 | +                # jsonize the list to store it in the DB | 
|  | 53 | +                dflt = dumps(dflt) | 
|  | 54 | +            if not choices.issuperset(dflt_val): | 
|  | 55 | +                raise QiitaDBError( | 
|  | 56 | +                    "The default value '%s' for the parameter %s is not " | 
|  | 57 | +                    "listed in the available choices: %s" | 
|  | 58 | +                    % (dflt, pname, ', '.join(choices))) | 
|  | 59 | + | 
|  | 60 | +        if ptype.startswith('artifact'): | 
|  | 61 | +            atypes = loads(ptype.split(':')[1]) | 
|  | 62 | +            sql_artifact_params.append( | 
|  | 63 | +                [pname, 'artifact', atypes]) | 
|  | 64 | +        else: | 
|  | 65 | +            if dflt is not None: | 
|  | 66 | +                sql_param_values.append([pname, ptype, False, dflt]) | 
|  | 67 | +            else: | 
|  | 68 | +                sql_param_values.append([pname, ptype, True, None]) | 
|  | 69 | + | 
|  | 70 | +    with TRN: | 
|  | 71 | +        sql = """SELECT EXISTS(SELECT * | 
|  | 72 | +                               FROM qiita.software_command | 
|  | 73 | +                               WHERE software_id = %s AND name = %s)""" | 
|  | 74 | +        TRN.add(sql, [software.id, name]) | 
|  | 75 | +        if TRN.execute_fetchlast(): | 
|  | 76 | +            raise QiitaDBDuplicateError( | 
|  | 77 | +                "command", "software: %d, name: %s" | 
|  | 78 | +                           % (software.id, name)) | 
|  | 79 | +        # Add the command to the DB | 
|  | 80 | +        sql = """INSERT INTO qiita.software_command | 
|  | 81 | +                        (name, software_id, description, is_analysis) | 
|  | 82 | +                 VALUES (%s, %s, %s, %s) | 
|  | 83 | +                 RETURNING command_id""" | 
|  | 84 | +        sql_params = [name, software.id, description, analysis_only] | 
|  | 85 | +        TRN.add(sql, sql_params) | 
|  | 86 | +        c_id = TRN.execute_fetchlast() | 
|  | 87 | + | 
|  | 88 | +        # Add the parameters to the DB | 
|  | 89 | +        sql = """INSERT INTO qiita.command_parameter | 
|  | 90 | +                    (command_id, parameter_name, parameter_type, required, | 
|  | 91 | +                     default_value) | 
|  | 92 | +                 VALUES (%s, %s, %s, %s, %s) | 
|  | 93 | +                 RETURNING command_parameter_id""" | 
|  | 94 | +        sql_params = [[c_id, pname, p_type, reqd, default] | 
|  | 95 | +                      for pname, p_type, reqd, default in sql_param_values] | 
|  | 96 | +        TRN.add(sql, sql_params, many=True) | 
|  | 97 | +        TRN.execute() | 
|  | 98 | + | 
|  | 99 | +        # Add the artifact parameters | 
|  | 100 | +        sql_type = """INSERT INTO qiita.parameter_artifact_type | 
|  | 101 | +                        (command_parameter_id, artifact_type_id) | 
|  | 102 | +                      VALUES (%s, %s)""" | 
|  | 103 | +        supported_types = [] | 
|  | 104 | +        for pname, p_type, atypes in sql_artifact_params: | 
|  | 105 | +            sql_params = [c_id, pname, p_type, True, None] | 
|  | 106 | +            TRN.add(sql, sql_params) | 
|  | 107 | +            pid = TRN.execute_fetchlast() | 
|  | 108 | +            sql_params = [[pid, convert_to_id(at, 'artifact_type')] | 
|  | 109 | +                          for at in atypes] | 
|  | 110 | +            TRN.add(sql_type, sql_params, many=True) | 
|  | 111 | +            supported_types.extend([atid for _, atid in sql_params]) | 
|  | 112 | + | 
|  | 113 | +        # If the software type is 'artifact definition', there are a couple | 
|  | 114 | +        # of extra steps | 
|  | 115 | +        if software.type == 'artifact definition': | 
|  | 116 | +            # If supported types is not empty, link the software with these | 
|  | 117 | +            # types | 
|  | 118 | +            if supported_types: | 
|  | 119 | +                sql = """INSERT INTO qiita.software_artifact_type | 
|  | 120 | +                                (software_id, artifact_type_id) | 
|  | 121 | +                            VALUES (%s, %s)""" | 
|  | 122 | +                sql_params = [[software.id, atid] | 
|  | 123 | +                              for atid in supported_types] | 
|  | 124 | +                TRN.add(sql, sql_params, many=True) | 
|  | 125 | +            # If this is the validate command, we need to add the | 
|  | 126 | +            # provenance and name parameters. These are used internally, | 
|  | 127 | +            # that's why we are adding them here | 
|  | 128 | +            if name == 'Validate': | 
|  | 129 | +                sql = """INSERT INTO qiita.command_parameter | 
|  | 130 | +                            (command_id, parameter_name, parameter_type, | 
|  | 131 | +                             required, default_value) | 
|  | 132 | +                         VALUES (%s, 'name', 'string', 'False', | 
|  | 133 | +                                 'dflt_name'), | 
|  | 134 | +                                (%s, 'provenance', 'string', 'False', NULL) | 
|  | 135 | +                         """ | 
|  | 136 | +                TRN.add(sql, [c_id, c_id]) | 
|  | 137 | + | 
|  | 138 | +        # Add the outputs to the command | 
|  | 139 | +        if outputs: | 
|  | 140 | +            sql = """INSERT INTO qiita.command_output | 
|  | 141 | +                        (name, command_id, artifact_type_id) | 
|  | 142 | +                     VALUES (%s, %s, %s)""" | 
|  | 143 | +            sql_args = [[pname, c_id, convert_to_id(at, 'artifact_type')] | 
|  | 144 | +                        for pname, at in outputs.items()] | 
|  | 145 | +            TRN.add(sql, sql_args, many=True) | 
|  | 146 | +            TRN.execute() | 
|  | 147 | + | 
|  | 148 | +    return Command(c_id) | 
|  | 149 | + | 
|  | 150 | + | 
|  | 151 | +with TRN: | 
|  | 152 | +    qiita_plugin = Software.from_name_and_version('Qiita', 'alpha') | 
|  | 153 | + | 
|  | 154 | +    # Create the 'list_remote_files' command | 
|  | 155 | +    parameters = {'url': ['string', None], | 
|  | 156 | +                  'private_key': ['string', None]} | 
|  | 157 | +    create_command(qiita_plugin, "list_remote_files", | 
|  | 158 | +                   "retrieves list of valid study files from remote dir", | 
|  | 159 | +                   parameters) | 
|  | 160 | + | 
|  | 161 | +    # Create the 'download_remote_files' command | 
|  | 162 | +    parameters = {'url': ['string', None], | 
|  | 163 | +                  'destination': ['string', None], | 
|  | 164 | +                  'private_key': ['string', None]} | 
|  | 165 | +    create_command(qiita_plugin, "download_remote_files", | 
|  | 166 | +                   "downloads valid study files from remote dir", parameters) | 
0 commit comments