diff --git a/examples/python_large_output/download_output_file.py b/examples/python_large_output/download_output_file.py index 24953afd..877d0373 100644 --- a/examples/python_large_output/download_output_file.py +++ b/examples/python_large_output/download_output_file.py @@ -21,15 +21,12 @@ # SOFTWARE. """ -Example to query resources from a project. - -- Query values from evaluated jobs, computing some simple statistics on parameter values. -- Download files from the project - +Example to Download files of evaluated jobs from a project """ import argparse import logging import os +import tempfile import time from ansys.hps.client import Client, HPSError @@ -38,15 +35,23 @@ log = logging.getLogger(__name__) -def download_files(client, project_name): +def download_files(client, project_name, dir_path): """Download files.""" - out_path = os.path.join(os.path.dirname(__file__), "downloads") + + out_path = dir_path + if not out_path: + temp_dir = tempfile.TemporaryDirectory() + out_path = os.path.join(temp_dir.name, "downloads") + log.info(f"Downloading files to {out_path}") jms_api = JmsApi(client) - project = jms_api.get_project_by_name(name=project_name) + project = jms_api.get_project_by_name(name=project_name) project = jms_api.get_project(id=project.id) + # start dt client to time dowload of output files correctly + jms_api.client._start_dt_worker() + log.info(f"Project id: {project.id}") project_api = ProjectApi(client, project.id) @@ -54,10 +59,8 @@ def download_files(client, project_name): log.info(f"# evaluated jobs: {len(jobs)}") num = len(jobs) - log.info( - f"=== Example 1: Downloading output files of {num} jobs using ProjectApi.download_file()" - ) - for job in jobs[0:num]: + log.info(f"=== Downloading output files of {num} jobs using ProjectApi.download_file()") + for job in jobs: log.info(f"Job {job.id}") for task in project_api.get_tasks(job_id=job.id): log.info(f"Task {task.id}") @@ -65,10 +68,10 @@ def download_files(client, project_name): for f in files: fpath = os.path.join(out_path, f"task_{task.id}") log.info(f"Download output file {f.evaluation_path} to {fpath}") - start = time.process_time() - project_api.download_file(file=f, target_path=fpath) - log.info(f"Time taken to download output file: {(time.time() - start):.2f} seconds" - ) + start = time.time() + project_api.download_file(file=f, target_path=fpath) + log.info(f"Time taken to download output file: {(time.time() - start):.2f} seconds") + if __name__ == "__main__": @@ -77,17 +80,18 @@ def download_files(client, project_name): parser.add_argument("-U", "--url", default="https://127.0.0.1:8443/hps") parser.add_argument("-u", "--username", default="repuser") parser.add_argument("-p", "--password", default="repuser") + parser.add_argument("-dir", "--download-path", type=str) args = parser.parse_args() logger = logging.getLogger() - logging.basicConfig(format="%(message)s", level=logging.DEBUG) + logging.basicConfig(format="%(message)s", level=logging.INFO) try: log.info("Connect to HPC Platform Services") client = Client(url=args.url, username=args.username, password=args.password) log.info(f"HPS URL: {client.url}") - download_files(client=client, project_name=args.name) + download_files(client=client, project_name=args.name, dir_path=args.download_path) except HPSError as e: - log.error(str(e)) \ No newline at end of file + log.error(str(e)) diff --git a/examples/python_large_output/project_setup.py b/examples/python_large_output/project_setup.py index c79ec844..a39baa6f 100644 --- a/examples/python_large_output/project_setup.py +++ b/examples/python_large_output/project_setup.py @@ -188,6 +188,11 @@ def create_project(client, name, use_exec_script, python_version=None) -> Projec client = Client(url=args.url, username=args.username, password=args.password) try: - create_project(client, name=args.name, use_exec_script=args.use_exec_script, python_version=args.python_version) + create_project( + client, + name=args.name, + use_exec_script=args.use_exec_script, + python_version=args.python_version, + ) except HPSError as e: log.error(str(e))