-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.py
326 lines (236 loc) · 10 KB
/
install.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Windows installer
Blatantly stollen from https://github.com/geoadmin/tool-topgis/blob/master/topgis/src/topgis/installer/install_topgis.py
"""
import ctypes
import glob
import itertools
import os
import platform
import shutil
import socket
import string
import subprocess
import sys
import tarfile
import logging
from six import BytesIO
from six.moves import configparser, input
HOME = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH")
CONDA_EXE = os.path.expandvars(
r"C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\conda.exe"
)
CONDA_BASE_DIR = r"D:\conda\envs"
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
def get_available_drives():
if "Windows" not in platform.system():
return []
drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
return list(
itertools.compress(
string.ascii_uppercase,
map(lambda x: ord(x) - ord("0"), bin(drive_bitmask)[:1:-1]),
)
)
def mapDrive(drive, networkPath):
if not drive.endswith(":"):
drive + ":"
drive_letter = drive.replace(":", "")
if drive_letter in get_available_drives():
logging.warning(" Drive {} in use, trying to unmap...".format(drive))
return -1
else:
logging.info(" Drive {} is free".format(drive))
if os.path.exists(networkPath):
logging.info("{} is found...".format(networkPath))
logging.info("Trying to map {} to drive {}".format(networkPath, drive))
winCMD = ["NET", "USE", drive, networkPath]
try:
cmdOutPut = subprocess.Popen(
winCMD, stdout=subprocess.PIPE, shell=True
).communicate()
return drive_letter in get_available_drives()
except:
logging.error("Unexpected error...")
return -1
logging.info("Mapping to {} successful".format(drive))
return 1
else:
logging.info("Network path {} unreachable...".format(networkPath))
return -1
def quote(str):
return '"{}"'.format(str)
class Installer:
dirname = os.path.dirname(os.path.abspath(__file__))
conda_base_dir = r"\\v0t0020a.adr.admin.ch\prod\lgX\TOPGIS\conda"
@classmethod
def clone_conda(cls, envname):
dest_env = r"D:\conda\envs\{}".format(envname)
src_env = r"TIE"
yaml_file = os.path.join(cls.dirname, "environment_from_yaml_list_explicit.txt")
# cmd = """"C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\conda.exe" create --clone "T:\TIE" -p "D:\conda\envs\TIE_TEST" - vv - -no - shortcuts --pinned 1"""
cmd = """"C:/Program Files/ArcGIS/Pro/bin/Python/scripts/conda.exe" create -p {} --file {}""".format(
dest_env, yaml_file
)
args = [
quote(CONDA_EXE),
"create",
"--clone",
quote(src_env),
"-p",
quote(dest_env),
"-vv",
"--no-shortcuts",
"--pinned",
"1",
]
logging.info(cmd)
proc = subprocess.Popen(cmd)
logging.info(proc.communicate())
return dest_env
@classmethod
def create_from_scratch(cls, envname):
dest_env = r"D:\conda\envs\{}".format(envname)
PYTHON_VERSION="3.9.18"
pkgs_list = [ 'conda', 'pip', 'setuptools', 'geopandas', 'scikit-image', 'scipy', 'rasterio', 'shapely', 'mayavi', 'geocube', 'tqdm', 'requests', 'gdal', 'pyproj', 'geojson', 'pyyaml', 'pyogrio', 'dask', 'ipycytoscape' ]
# cmd = """"C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\conda.exe" create --clone "T:\TIE" -p "D:\conda\envs\TIE_TEST" - vv - -no - shortcuts --pinned 1"""
cmd = """"C:/Program Files/ArcGIS/Pro/bin/Python/scripts/conda.exe" create -p {} -v --override-channels -c conda-forge -y --show-channel-urls python={} {} """.format(
dest_env, PYTHON_VERSION, " ".join(pkgs_list)
)
logging.info(cmd)
proc = subprocess.Popen(cmd)
logging.info(proc.communicate())
return dest_env
@classmethod
def download_conda_env(cls, envname='TIE'):
conda_env_path = r"D:\conda\envs"
archive_name = "conda-TIE-win10.tar.gz"
if not os.path.isdir(conda_env_path):
os.makedirs(conda_env_path)
tar_archive_path = os.path.join(base_dir, archive_name)
logging.info(" Copy tar archive {} to {}".format(tar_archive_path, conda_env_path))
shutil.copy(tar_archive_path, conda_env_path)
os.chdir(conda_env_path)
logging.info(" Gunziping archive")
with tarfile.open(archive_name, "r:gz") as tar:
print(os.path.commonprefix(tar.getnames()))
tar.extractall()
tar.close()
return conda_env_path
@classmethod
def update_env(cls, conda_env_path):
python_exe = os.path.join(conda_env_path, 'python.exe')
for pkg in ("https://bitbucket.org/procrastinatio/lg-tie-lib/get/packaging.zip",
"https://bitbucket.org/procrastinatio/geocover-utils/get/master.zip"):
proc = subprocess.call(
[python_exe, "-m", "pip", "install", "--trusted", "bitbucket.org", pkg]
)
conda_exe = os.path.join(conda_env_path, 'condabin', 'conda.bat')
for pkg in ("swisstopo::tietoolbox", ):
proc = subprocess.call(
[conda_exe, "install", "--yes", pkg]
)
logging.info(r"Toolbox installed in '{}\Lib\site-packages\tietoolbox\esri\toolboxes'".format(conda_env_path))
@classmethod
def map_drive(cls, drive, unc_path):
logging.info("===== Mapping Drive for conda env =====")
mapDrive(drive, unc_path)
return os.path.join(os.sep, drive + os.sep, os.path.basename(unc_path))
@classmethod
def ini_file(cls, conda_env_path):
logging.info("===== geocover.ini =====")
base_dir = cls.conda_base_dir
user_config_dir = os.path.join(HOME, r"config\geocover")
user_config_path = os.path.join(user_config_dir, "geocover.ini")
if os.path.isfile(user_config_path):
logging.info(
"geocover.ini file already exists in {}. Will not overwrite it!".format(
user_config_dir
)
)
return 2
if not os.path.isdir(user_config_dir):
os.makedirs(user_config_dir)
data = {
"hostname": socket.gethostname(),
"username": os.environ.get("USERNAME"),
"conda_env_path": conda_env_path,
}
try:
with open(os.path.join(base_dir, "geocover.ini.in"), "r") as f:
tpl = f.read()
ini_str = tpl.format(**data)
except (IOError, KeyError, ValueError) as e:
logging.info("Cannot read or create the ini file: {}".format(e))
try:
config = configparser.RawConfigParser(allow_no_value=True)
config.readfp(BytesIO(ini_str))
with open(user_config_path, "wb") as configfile:
config.write(configfile)
logging.info("Config written to {}".format(user_config_path))
except Exception as e:
logging.error("Error: {}".format(e))
@classmethod
def condarc_file(cls):
logging.info("===== .condarc =====")
base_dir = cls.conda_base_dir
user_config_dir = HOME
user_config_path = os.path.join(user_config_dir, ".condarc")
if os.path.isfile(user_config_path):
logging.warning("File {} already exists. Will not overwrite".format(user_config_path))
return 2
if not os.path.isdir(user_config_dir):
os.makedir(user_config_dir)
try:
shutil.copy2(os.path.join(base_dir, "_condarc"), user_config_path)
except (IOError, KeyError, ValueError) as e:
logging.error("Cannot write file to {}: {}".format(user_config_path, e))
@classmethod
def install_from_setup(cls):
curdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(curdir)
for python_exe in glob.glob(
r"C:\Python27" + os.sep + "**" + os.sep + "python.exe"
):
logging.info("===== Installing for {} =====".format(python_exe))
# TODO: proper install for python2
subprocess.call(
[python_exe, "-m", "pip", "install", "-r", "requirements.txt"]
)
@classmethod
def main(cls):
envname = "TIE"
try:
logging.info("=== Preparing conda env ===")
cls.condarc_file()
if not os.path.isdir(CONDA_BASE_DIR):
try:
os.makedirs(CONDA_BASE_DIR)
except Exception as e:
logging.error("Cannot create dir {}. Exit".format(CONDA_BASE_DIR))
sys.exit(2)
# Use one of them
# conda_env_unc_path = os.path.join(cls.conda_base_dir, "envs\TIE")
conda_env_path = r"D:\conda\envs\EMPTY_FROM_YAML"
# Download and unzipped an archive containing the conda en
# conda_env_path = cls.download_conda_env(envname=envname)
# Map a network drive containing the conda env
# conda_env_path = cls.map_drive("T:", conda_env_unc_path)
conda_env_path = cls.clone_conda(envname)
# Create the env from scratch (very long)
#conda_env_path = cls.create_from_scratch(envname)
cls.update_env(conda_env_path)
logging.info(conda_env_path)
cls.ini_file(conda_env_path)
logging.info("Installing geocover and dependencies from setup.py")
cls.install_from_setup()
logging.info("\nPython geocover successfully installed")
input("Press enter...")
except Exception as e:
print(
"\nCould not install required python modules. Please contact GeoCover support [email protected]"
)
logging.error(e)
input("Press enter...")
if __name__ == "__main__":
i = Installer()
i.main()