30
30
31
31
import abc
32
32
import ctypes
33
- import os
34
- import pathlib
33
+ from pathlib import Path
35
34
import re
36
35
import shutil
37
36
import tempfile
@@ -85,23 +84,23 @@ def update_virtual_environment_for_custom_operators(
85
84
raise NotImplementedError (
86
85
"Updating the dpf-site.zip of a DPF Server is only available when InProcess."
87
86
)
88
- current_dpf_site_zip_path = os . path . join (server .ansys_path , "dpf" , "python" , "dpf-site.zip" )
87
+ current_dpf_site_zip_path = Path (server .ansys_path ) / "dpf" / "python" / "dpf-site.zip"
89
88
# Get the path to where we store the original dpf-site.zip
90
- original_dpf_site_zip_path = os . path . join (
91
- server .ansys_path , "dpf" , "python" , "original" , "dpf-site.zip"
89
+ original_dpf_site_zip_path = (
90
+ Path ( server .ansys_path ) / "dpf" / "python" / "original" / "dpf-site.zip"
92
91
)
93
92
# Restore the original dpf-site.zip
94
93
if restore_original :
95
- if os . path . exists (original_dpf_site_zip_path ):
94
+ if original_dpf_site_zip_path . exists ():
96
95
shutil .move (src = original_dpf_site_zip_path , dst = current_dpf_site_zip_path )
97
- os . rmdir (os . path . dirname ( original_dpf_site_zip_path ) )
96
+ original_dpf_site_zip_path . parent . rmdir ()
98
97
else :
99
98
warnings .warn ("No original dpf-site.zip found. Current is most likely the original." )
100
99
else :
101
100
# Store original dpf-site.zip for this DPF Server if no original is stored
102
- if not os . path .exists (os . path . dirname ( original_dpf_site_zip_path ) ):
103
- os . mkdir (os . path . dirname ( original_dpf_site_zip_path ) )
104
- if not os . path . exists (original_dpf_site_zip_path ):
101
+ if not original_dpf_site_zip_path . parent .exists ():
102
+ original_dpf_site_zip_path . parent . mkdir ()
103
+ if not original_dpf_site_zip_path . exists ():
105
104
shutil .move (src = current_dpf_site_zip_path , dst = original_dpf_site_zip_path )
106
105
# Get the current paths to site_packages
107
106
import site
@@ -111,59 +110,60 @@ def update_virtual_environment_for_custom_operators(
111
110
# Get the first one targeting an actual site-packages folder
112
111
for path_to_site_packages in paths_to_current_site_packages :
113
112
if path_to_site_packages [- 13 :] == "site-packages" :
114
- current_site_packages_path = pathlib . Path (path_to_site_packages )
113
+ current_site_packages_path = Path (path_to_site_packages )
115
114
break
116
115
if current_site_packages_path is None :
117
116
warnings .warn ("Could not find a currently loaded site-packages folder to update from." )
118
117
return
119
118
# If an ansys.dpf.core.path file exists, then the installation is editable
120
- search_path = pathlib . Path ( current_site_packages_path )
119
+ search_path = current_site_packages_path
121
120
potential_editable = list (search_path .rglob ("__editable__.ansys_dpf_core-*.pth" ))
122
121
if potential_editable :
123
122
path_file = potential_editable [0 ]
124
123
else : # Keep for older setuptools versions
125
- path_file = os . path . join ( current_site_packages_path , "ansys.dpf.core.pth" )
126
- if os . path . exists (path_file ):
124
+ path_file = current_site_packages_path / "ansys.dpf.core.pth"
125
+ if path_file . exists ():
127
126
# Treat editable installation of ansys-dpf-core
128
- with open (path_file , "r" ) as f :
127
+ with path_file . open ("r" ) as f :
129
128
current_site_packages_path = f .readline ().strip ()
130
129
with tempfile .TemporaryDirectory () as tmpdir :
131
- os .mkdir (os .path .join (tmpdir , "ansys_dpf_core" ))
132
- ansys_dir = os .path .join (tmpdir , "ansys_dpf_core" )
133
- os .mkdir (os .path .join (ansys_dir , "ansys" ))
134
- os .mkdir (os .path .join (ansys_dir , "ansys" , "dpf" ))
135
- os .mkdir (os .path .join (ansys_dir , "ansys" , "grpc" ))
130
+ tmpdir = Path (tmpdir )
131
+ ansys_dir = tmpdir / "ansys_dpf_core"
132
+ ansys_dir .mkdir ()
133
+ ansys_dir .joinpath ("ansys" ).mkdir ()
134
+ ansys_dir .joinpath ("ansys" , "dpf" ).mkdir ()
135
+ ansys_dir .joinpath ("ansys" , "grpc" ).mkdir ()
136
136
shutil .copytree (
137
- src = os . path . join ( current_site_packages_path , "ansys" , "dpf" , "core" ) ,
138
- dst = os . path . join ( ansys_dir , "ansys" , "dpf" , "core" ) ,
137
+ src = current_site_packages_path / "ansys" / "dpf" / "core" ,
138
+ dst = ansys_dir / "ansys" / "dpf" / "core" ,
139
139
ignore = lambda directory , contents : ["__pycache__" , "result_files" ],
140
140
)
141
141
shutil .copytree (
142
- src = os . path . join ( current_site_packages_path , "ansys" , "dpf" , "gate" ) ,
143
- dst = os . path . join ( ansys_dir , "ansys" , "dpf" , "gate" ) ,
142
+ src = current_site_packages_path / "ansys" / "dpf" / "gate" ,
143
+ dst = ansys_dir / "ansys" / "dpf" / "gate" ,
144
144
ignore = lambda directory , contents : ["__pycache__" ],
145
145
)
146
146
shutil .copytree (
147
- src = os . path . join ( current_site_packages_path , "ansys" , "grpc" , "dpf" ) ,
148
- dst = os . path . join ( ansys_dir , "ansys" , "grpc" , "dpf" ) ,
147
+ src = current_site_packages_path / "ansys" / "grpc" / "dpf" ,
148
+ dst = ansys_dir / "ansys" / "grpc" / "dpf" ,
149
149
ignore = lambda directory , contents : ["__pycache__" ],
150
150
)
151
151
# Find the .dist_info folder
152
152
pattern = re .compile (r"^ansys_dpf_core\S*" )
153
- for p in pathlib . Path ( current_site_packages_path ) .iterdir ():
153
+ for p in current_site_packages_path .iterdir ():
154
154
if p .is_dir ():
155
155
# print(p.stem)
156
156
if re .search (pattern , p .stem ):
157
157
dist_info_path = p
158
158
break
159
159
shutil .copytree (
160
160
src = dist_info_path ,
161
- dst = os . path . join ( ansys_dir , dist_info_path .name ) ,
161
+ dst = ansys_dir / dist_info_path .name ,
162
162
)
163
163
# Zip the files as dpf-site.zip
164
- base_name = os . path . join ( tmpdir , "ansys_dpf_core_zip" )
164
+ base_name = tmpdir / "ansys_dpf_core_zip"
165
165
base_dir = "."
166
- root_dir = os . path . join ( tmpdir , "ansys_dpf_core" ) # OK
166
+ root_dir = tmpdir / "ansys_dpf_core" # OK
167
167
shutil .make_archive (
168
168
base_name = base_name , root_dir = root_dir , base_dir = base_dir , format = "zip"
169
169
)
0 commit comments