1
+ # Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates.
2
+ # SPDX-License-Identifier: MIT
3
+ #
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ """
24
+ Example to query resources from a project.
25
+
26
+ - Query values from evaluated jobs, computing some simple statistics on parameter values.
27
+ - Download files from the project
28
+
29
+ """
30
+ import argparse
31
+ import logging
32
+ import os
33
+ import time
34
+
35
+ from ansys .hps .client import Client , HPSError
36
+ from ansys .hps .client .jms import JmsApi , ProjectApi
37
+
38
+ log = logging .getLogger (__name__ )
39
+
40
+
41
+ def download_files (client , project_name ):
42
+ """Download files."""
43
+ out_path = os .path .join (os .path .dirname (__file__ ), "downloads" )
44
+ log .info (f"Downloading files to { out_path } " )
45
+
46
+ jms_api = JmsApi (client )
47
+ project = jms_api .get_project_by_name (name = project_name )
48
+ project = jms_api .get_project (id = project .id )
49
+
50
+ log .info (f"Project id: { project .id } " )
51
+ project_api = ProjectApi (client , project .id )
52
+
53
+ jobs = project_api .get_jobs (eval_status = "evaluated" , fields = ["id" , "values" , "elapsed_time" ])
54
+ log .info (f"# evaluated jobs: { len (jobs )} " )
55
+ num = len (jobs )
56
+
57
+ log .info (
58
+ f"=== Example 1: Downloading output files of { num } jobs using ProjectApi.download_file()"
59
+ )
60
+ for job in jobs [0 :num ]:
61
+ log .info (f"Job { job .id } " )
62
+ for task in project_api .get_tasks (job_id = job .id ):
63
+ log .info (f"Task { task .id } " )
64
+ files = project_api .get_files (id = task .output_file_ids )
65
+ for f in files :
66
+ fpath = os .path .join (out_path , f"task_{ task .id } " )
67
+ log .info (f"Download output file { f .evaluation_path } to { fpath } " )
68
+ start = time .process_time ()
69
+ project_api .download_file (file = f , target_path = fpath )
70
+ log .info (f"Time taken to download output file: { (time .time () - start ):.2f} seconds"
71
+ )
72
+
73
+ if __name__ == "__main__" :
74
+
75
+ parser = argparse .ArgumentParser ()
76
+ parser .add_argument ("-n" , "--name" , type = str , default = "Python Large Output Files" )
77
+ parser .add_argument ("-U" , "--url" , default = "https://127.0.0.1:8443/hps" )
78
+ parser .add_argument ("-u" , "--username" , default = "repuser" )
79
+ parser .add_argument ("-p" , "--password" , default = "repuser" )
80
+ args = parser .parse_args ()
81
+
82
+ logger = logging .getLogger ()
83
+ logging .basicConfig (format = "%(message)s" , level = logging .DEBUG )
84
+
85
+ try :
86
+ log .info ("Connect to HPC Platform Services" )
87
+ client = Client (url = args .url , username = args .username , password = args .password )
88
+ log .info (f"HPS URL: { client .url } " )
89
+
90
+ download_files (client = client , project_name = args .name )
91
+
92
+ except HPSError as e :
93
+ log .error (str (e ))
0 commit comments