def main()

in Scripts/DeviceFarmScripts/run_ios_ci.py [0:0]


def main():
    args = parser.parse_args()
    run_id = args.run_id
    run_attempt = args.run_attempt
    project_arn = args.project_arn
    device_pool_arn = args.device_pool_arn
    test_spec_file_path = args.test_spec_file_path
    test_file_path = args.test_file_path
    app_file_path = args.app_file_path

    region = os.getenv('AWS_DEVICE_FARM_REGION')

    print("Beginning Device Farm Setup \n")

    # Create Boto3 client for Device Farm
    try:
        client = boto3.client('devicefarm', region_name=region)
    except Exception:
        print("Error - could not make Boto3 client. Credentials likely could not be sourced")
        sys.exit(-1)
    print("Boto3 client established")

    # Upload the crt library shell app to Device Farm
    unique_prefix = 'CI-' + run_id + '-' + run_attempt
    device_farm_app_upload_arn = upload_file(client, project_arn, unique_prefix, app_file_path, 'IOS_APP')
    device_farm_test_upload_arn = upload_file(client, project_arn, unique_prefix, test_file_path, 'APPIUM_PYTHON_TEST_PACKAGE')
    device_farm_test_spec_upload_arn = upload_file(client, project_arn, unique_prefix, test_spec_file_path, 'INSTRUMENTATION_TEST_SPEC')

    print('scheduling run')
    schedule_run_response = client.schedule_run(
        projectArn=project_arn,
        appArn=device_farm_app_upload_arn,
        devicePoolArn=device_pool_arn,
        name=unique_prefix,
        test={
            "type": "APPIUM_PYTHON",
            "testSpecArn": device_farm_test_spec_upload_arn,
            "testPackageArn": device_farm_test_upload_arn
        },
        executionConfiguration={
            'jobTimeoutMinutes': 30
        }
    )

    device_farm_run_arn = schedule_run_response['run']['arn']

    run_start_time = schedule_run_response['run']['started']
    run_start_date_time = run_start_time.strftime("%m/%d/%Y, %H:%M:%S")
    print('run scheduled at ' + run_start_date_time)

    get_run_response = client.get_run(arn=device_farm_run_arn)
    while get_run_response['run']['result'] == 'PENDING':
        time.sleep(10)
        get_run_response = client.get_run(arn=device_farm_run_arn)

    run_end_time = datetime.datetime.now()
    run_end_date_time = run_end_time.strftime("%m/%d/%Y, %H:%M:%S")
    print('Run ended at ' + run_end_date_time + ' with result: ' + get_run_response['run']['result'])

    is_success = True
    if get_run_response['run']['result'] != 'PASSED':
        print('run has failed with result ' + get_run_response['run']['result'])
        is_success = False

    # If Clean up is not executed due to the job being cancelled in CI, the uploaded files will not be deleted
    # from the Device Farm project and must be deleted manually.

    # Clean up
    print('Deleting app file from Device Farm project')
    client.delete_upload(
        arn=device_farm_app_upload_arn
    )
    print('Deleting test package from Device Farm project')
    client.delete_upload(
        arn=device_farm_test_upload_arn
    )
    print('Deleting test spec file from Device Farm project')
    client.delete_upload(
        arn=device_farm_test_spec_upload_arn
    )

    if is_success == False:
        print('Exiting with fail')
        sys.exit(-1)

    print('Exiting with success')