def output(cmd, job_id, resource_group_name=None, workspace_name=None, location=None): """ Get the results of running a Q# job. """ import tempfile import json import os from azure.cli.command_modules.storage._client_factory import blob_data_service_factory path = os.path.join(tempfile.gettempdir(), job_id) if os.path.exists(path): logger.debug("Using existing blob from %s", path) else: logger.debug("Downloading job results blob into %s", path) info = WorkspaceInfo(cmd, resource_group_name, workspace_name, location) client = cf_jobs(cmd.cli_ctx, info.subscription, info.resource_group, info.name, info.location) job = client.get(job_id) if job.status != "Succeeded": return f"Job status: {job.status}. Output only available if Succeeded." args = _parse_blob_url(job.output_data_uri) blob_service = blob_data_service_factory(cmd.cli_ctx, args) blob_service.get_blob_to_path(args['container'], args['blob'], path) with open(path) as json_file: data = json.load(json_file) return data
def output(cmd, job_id, resource_group_name=None, workspace_name=None, location=None): """ Get the results of running a Q# job. """ import tempfile import json import os from azure.cli.command_modules.storage._client_factory import blob_data_service_factory path = os.path.join(tempfile.gettempdir(), job_id) info = WorkspaceInfo(cmd, resource_group_name, workspace_name, location) client = cf_jobs(cmd.cli_ctx, info.subscription, info.resource_group, info.name, info.location) job = client.get(job_id) if os.path.exists(path): logger.debug("Using existing blob from %s", path) else: logger.debug("Downloading job results blob into %s", path) if job.status != "Succeeded": return f"Job status: {job.status}. Output only available if Succeeded." args = _parse_blob_url(job.output_data_uri) blob_service = blob_data_service_factory(cmd.cli_ctx, args) blob_service.get_blob_to_path(args['container'], args['blob'], path) with open(path) as json_file: lines = [line.strip() for line in json_file.readlines()] # Receiving an empty response is valid. if len(lines) == 0: return if job.target.startswith("microsoft.simulator"): result_start_line = len(lines) - 1 if lines[-1].endswith('"'): while result_start_line >= 0 and not lines[ result_start_line].startswith('"'): result_start_line -= 1 if result_start_line < 0: raise CLIError( "Job output is malformed, mismatched quote characters.") print('\n'.join(lines[:result_start_line])) result = ' '.join(lines[result_start_line:])[ 1:-1] # seems the cleanest version to display print('_' * len(result) + '\n') json_string = '{ "histogram" : { "' + result + '" : 1 } }' data = json.loads(json_string) else: json_file.seek(0) # Reset the file pointer before loading data = json.load(json_file) return data
def get_url_with_sas(source, account_name, container, blob, share, file_path, local_path): import re import os from azure.cli.command_modules.storage._validators import _query_account_key from azure.cli.command_modules.storage.azcopy.util import _generate_sas_token storage_endpoint = cmd.cli_ctx.cloud.suffixes.storage_endpoint if source is not None: if "?" in source: # sas token exists return source storage_pattern = re.compile(r'https://(.*?)\.(blob|dfs|file).%s' % storage_endpoint) result = re.findall(storage_pattern, source) if result: # source is URL storage_info = result[0] account_name = storage_info[0] if storage_info[1] in ['blob', 'dfs']: service = 'blob' elif storage_info[1] in ['file']: service = 'file' else: raise ValueError('Not supported service type') account_key = _query_account_key(cmd.cli_ctx, account_name) else: # source is path return source elif account_name: account_key = _query_account_key(cmd.cli_ctx, account_name) if container: client = blob_data_service_factory( cmd.cli_ctx, {'account_name': account_name}) if blob is None: blob = '' source = client.make_blob_url(container, blob) service = 'blob' elif share: client = file_data_service_factory(cmd.cli_ctx, { 'account_name': account_name, 'account_key': account_key }) dir_name, file_name = os.path.split( file_path) if file_path else (None, '') dir_name = None if dir_name in ('', '.') else dir_name source = client.make_file_url(share, dir_name, file_name) service = 'file' else: # Only support account trandfer for blob source = 'https://{}.blob.core.windows.net'.format( account_name) service = 'blob' elif local_path is not None: return local_path else: raise ValueError('Not valid file') sas_token = _generate_sas_token(cmd, account_name, account_key, service) return _add_url_sas(source, sas_token)
def validate_azcopy_upload_destination_url(cmd, namespace): client = blob_data_service_factory( cmd.cli_ctx, {'account_name': namespace.account_name}) destination_path = namespace.destination_path if not destination_path: destination_path = '' url = client.make_blob_url(namespace.destination_container, destination_path) namespace.destination = url del namespace.destination_container del namespace.destination_path
def output(cmd, job_id, resource_group_name=None, workspace_name=None, location=None): """ Get the results of running a Q# job. """ import tempfile import json import os from azure.cli.command_modules.storage._client_factory import blob_data_service_factory path = os.path.join(tempfile.gettempdir(), job_id) info = WorkspaceInfo(cmd, resource_group_name, workspace_name, location) client = cf_jobs(cmd.cli_ctx, info.subscription, info.resource_group, info.name, info.location) job = client.get(job_id) if os.path.exists(path): logger.debug("Using existing blob from %s", path) else: logger.debug("Downloading job results blob into %s", path) if job.status != "Succeeded": return f"Job status: {job.status}. Output only available if Succeeded." args = _parse_blob_url(job.output_data_uri) blob_service = blob_data_service_factory(cmd.cli_ctx, args) blob_service.get_blob_to_path(args['container'], args['blob'], path) with open(path) as json_file: if job.target.startswith("microsoft.simulator"): lines = [line.strip() for line in json_file.readlines()] result_start_line = len(lines) - 1 if lines[-1].endswith('"'): while not lines[result_start_line].startswith('"'): result_start_line -= 1 print('\n'.join(lines[:result_start_line])) result = ' '.join(lines[result_start_line:])[ 1:-1] # seems the cleanest version to display print("_" * len(result) + "\n") json_string = "{ \"histogram\" : { \"" + result + "\" : 1 } }" data = json.loads(json_string) else: data = json.load(json_file) return data
def output(cmd, job_id, resource_group_name=None, workspace_name=None, location=None): """ Get the results of running a Q# job. """ import tempfile import json import os from azure.cli.command_modules.storage._client_factory import blob_data_service_factory path = os.path.join(tempfile.gettempdir(), job_id) info = WorkspaceInfo(cmd, resource_group_name, workspace_name, location) client = cf_jobs(cmd.cli_ctx, info.subscription, info.resource_group, info.name, info.location) job = client.get(job_id) if os.path.exists(path): logger.debug("Using existing blob from %s", path) else: logger.debug("Downloading job results blob into %s", path) if job.status != "Succeeded": return job # If "-o table" is specified, this allows transform_output() in commands.py # to format the output, so the error info is shown. If "-o json" or no "-o" # parameter is specified, then the full JSON job output is displayed, being # consistent with other commands. args = _parse_blob_url(job.output_data_uri) blob_service = blob_data_service_factory(cmd.cli_ctx, args) blob_service.get_blob_to_path(args['container'], args['blob'], path) with open(path) as json_file: lines = [line.strip() for line in json_file.readlines()] # Receiving an empty response is valid. if len(lines) == 0: return if job.target.startswith("microsoft.simulator"): result_start_line = len(lines) - 1 is_result_string = lines[-1].endswith('"') if is_result_string: while result_start_line >= 0 and not lines[ result_start_line].startswith('"'): result_start_line -= 1 if result_start_line < 0: raise AzureResponseError( "Job output is malformed, mismatched quote characters.") # Print the job output and then the result of the operation as a histogram. # If the result is a string, trim the quotation marks. print('\n'.join(lines[:result_start_line])) raw_result = ' '.join(lines[result_start_line:]) result = raw_result[1:-1] if is_result_string else raw_result print('_' * len(result) + '\n') json_string = '{ "histogram" : { "' + result + '" : 1 } }' data = json.loads(json_string) else: json_file.seek(0) # Reset the file pointer before loading data = json.load(json_file) return data