Пример #1
0
def set_csv_file_name_and_download(csv_filename):
    """Set CSV file path in the app and download the CSV.

    Parameters
    ----------
    csv_filename : str

    Returns
    -------
    str
        CSV file path
    """
    if not os.path.exists(LOCAL_DATA_DIR):
        os.mkdir(LOCAL_DATA_DIR)
    csv_path = os.path.join(LOCAL_DATA_DIR, csv_filename)

    if os.path.exists(csv_path):
        # If the CSV already exists on disk, just use it.
        pass
    else:
        # The CSV has to come from Civis Platform.
        file_id = civis.find_one(CSV_FILES, file_name=csv_filename).id
        if file_id is None:
            raise ValueError(
                f"CSV file not retrievable without a Civis file ID")
        civis.io.civis_to_file(file_id, csv_path)
        logging.info("CSV downloaded to %s", csv_path)
    return csv_path
Пример #2
0
def _decode_train_run(train_job_id, train_run_id, client):
    """Determine correct run ID for use for a given training job ID"""
    try:
        return int(train_run_id)
    except ValueError:
        container = client.scripts.get_containers(int(train_job_id))
        if train_run_id == 'active':
            train_run_id = container.arguments.get('ACTIVE_BUILD', find_one(
                container.params, name='ACTIVE_BUILD'))['default']

        if train_run_id == 'latest':
            return container.last_run.id

        try:
            return int(train_run_id)
        except Exception as exc:
            msg = ('Please provide valid train_run_id! Needs to be '
                   'integer corresponding to a training run ID '
                   'or one of "active" or "latest".')
            six.raise_from(ValueError(msg), exc)
Пример #3
0
def file_id_from_run_output(name, job_id, run_id, regex=False, client=None):
    """Find the file ID of a File run output with the name "name"

    The run output is required to have type "File".
    If using an approximate match and multiple names match the
    provided string, return only the first file ID.

    Parameters
    ----------
    name : str
        The "name" field of the run output you wish to retrieve
    job_id : int
    run_id : int
    regex : bool, optional
        If False (the default), require an exact string match between
        ``name`` and the name of the run output. If True, search for a
        name which matches the regular expression ``name`` and
        retrieve the first found.
    client : :class:`civis.APIClient`, optional
        If not provided, an :class:`civis.APIClient` object will be
        created from the :envvar:`CIVIS_API_KEY`.

    Returns
    -------
    file_id : int
        The ID of a Civis File with name matching ``name``

    Raises
    ------
    IOError
        If the provided job ID and run ID combination can't be found
    FileNotFoundError
        If the run exists, but ``name`` isn't in its run outputs

    See Also
    --------
    APIClient.scripts.list_containers.runs_outputs
    """
    client = APIClient() if client is None else client
    # Retrieve run outputs
    try:
        outputs = client.scripts.list_containers_runs_outputs(job_id, run_id)
    except CivisAPIError as err:
        if err.status_code == 404:
            six.raise_from(
                IOError('Could not find job/run ID {}/{}'.format(
                    job_id, run_id)), err)
        else:
            raise

    # Find file in the run outputs.
    if not regex:
        # Require an exact match on the "name" string.
        obj = find_one(outputs, name=name, object_type='File')
    else:
        # Search for a filename which contains the "name" string
        obj_matches = [
            o for o in outputs
            if re.search(name, o.name) and o.object_type == 'File'
        ]
        if len(obj_matches) > 1:
            log.warning('Found %s matches to "%s". Returning the first.',
                        len(obj_matches), name)
        obj = None if not obj_matches else obj_matches[0]
    if obj is None:
        prefix = "A file containing the pattern" if regex else "File"
        raise FileNotFoundError('{} "{}" is not an output of job/run ID '
                                '{}/{}.'.format(prefix, name, job_id, run_id))
    return obj['object_id']
civis_api_spec.json contains information about the publicly available
API endpoints. This spec is used in both testing and generating
the public Sphinx docs.
"""

import civis
from civis.resources import API_SPEC_PATH


if __name__ == "__main__":
    client = civis.APIClient()
    try:
        job = client.scripts.post_custom(from_template_id=13448)
    except civis.base.CivisAPIError as e:
        if e.status_error == 404:
            raise EnvironmentError(
                "This script can only be run by a Civis employee with their "
                "regular Civis Platform account's API key."
            )
        else:
            raise
    fut = civis.utils.run_job(job.id, client=client, polling_interval=5)
    fut.result()
    print(f"custom script {fut.job_id} run {fut.run_id} has succeeded")
    outputs = client.scripts.list_custom_runs_outputs(fut.job_id, fut.run_id)
    file_id = civis.find_one(outputs, name="civis_api_spec.json").object_id
    with open(API_SPEC_PATH, "wb") as f:
        civis.io.civis_to_file(file_id, f, client=client)
    print("downloaded civis_api_spec.json")