forked from deephaven-examples/deephaven-ib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdhib_env.py
executable file
·619 lines (461 loc) · 21.5 KB
/
dhib_env.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/usr/bin/env python3
""" A script to build a virtual environment for Deephaven-IB development or release."""
import atexit
import logging
import os
import re
import shutil
from pathlib import Path
from types import ModuleType
from typing import Optional, Dict, Union
import click
import pkginfo
import requests
IB_VERSION_DEFAULT="10.19.04"
DH_VERSION_DEFAULT="0.34.1"
########################################################################################################################
# Version Numbers
########################################################################################################################
def version_tuple(version: str) -> tuple[int, ...]:
"""Convert a version string to a tuple of integers.
Args:
version: The version string to convert.
Returns:
A tuple of integers representing the version.
"""
return tuple(map(int, (version.split("."))))
def version_str(version: tuple[int, ...], wide: bool) -> str:
"""Convert a version tuple to a string.
Args:
version: The version tuple to convert.
wide: Whether to use a wide format that includes leading zeros.
Returns:
A string representing the version.
"""
if wide:
return ".".join(f"{x:02d}" for x in version)
else:
return ".".join(map(str, version))
def version_assert_format(version: str) -> None:
"""Assert that a version string is formatted correctly.
Args:
version: The version string to check.
Raises:
ValueError: If the version string is not formatted correctly.
"""
if not version:
raise ValueError("Version string is empty.")
# check if the version string is in semver format
pattern1 = re.compile(r"^([0-9]\d*)\.([0-9]\d*)\.([0-9]\d*)$")
pattern2 = re.compile(r"^([0-9]\d*)\.([0-9]\d*)\.([0-9]\d*)\.dev([0-9]\d*)$")
is_semver = bool(pattern1.match(version)) or bool(pattern2.match(version))
if not is_semver:
raise ValueError(f"Version string is not in semver format: {version}")
########################################################################################################################
# Shell
########################################################################################################################
def shell_exec(cmd: str) -> None:
"""Execute a shell command.
Args:
cmd: The command to execute.
"""
logging.warning(f"Executing shell command: {cmd}")
e = os.system(cmd)
if e != 0:
raise Exception(f"Error executing shell command: {cmd}")
########################################################################################################################
# URL
########################################################################################################################
def url_download(url: str, path: Union[str, Path]) -> None:
"""Download a file from a URL.
Args:
url: The URL to download from.
path: The path to save the downloaded file to.
"""
logging.warning(f"Downloading file: {url}, path: {path}")
response = requests.get(url)
response.raise_for_status()
with open(path, "wb") as f:
f.write(response.content)
########################################################################################################################
# Package Query Functions
########################################################################################################################
def delete_file_on_exit(file_path: Union[str, Path]) -> None:
"""Register a file to be deleted on program exit."""
def delete_file():
if os.path.exists(file_path):
os.remove(file_path)
logging.debug(f"{file_path} has been deleted.")
atexit.register(delete_file)
def download_wheel(python: str, package: str, version: Optional[str], delete_on_exit: bool = True) -> Path:
"""Download a wheel file for a package with a specific version.
Args:
python: The path to the Python executable to use.
package: The name of the package to download.
version: The version of the package to download. If None, the latest version will be downloaded.
delete_on_exit: Whether to delete the wheel file on program exit.
Returns:
The path of the downloaded wheel file.
Raises:
subprocess.CalledProcessError: If the download process fails.
"""
logging.warning(f"Downloading wheel for package: {package}, version: {version}, delete_on_exit: {delete_on_exit}")
if not version:
logging.warning(f"Determining latest version of package: {package}")
response = requests.get(f"https://pypi.org/pypi/{package}/json")
response.raise_for_status()
version = response.json()["info"]["version"]
ver = f"=={version}" if version else ""
shell_exec(f"{python} -m pip download {package}{ver} --no-deps")
p = Path(f"{package}-{version}-py3-none-any.whl").absolute()
if delete_on_exit:
delete_file_on_exit(str(p))
return p
def pkg_dependencies(path_or_module: Union[str, Path, ModuleType]) -> Dict[str, Optional[str]]:
"""Get the dependencies of a package.
Args:
path_or_module: The path to the package or the module object.
Returns:
A dictionary containing the dependencies of the package and their version specifications.
"""
if isinstance(path_or_module, Path):
path_or_module = str(path_or_module)
meta = pkginfo.get_metadata(path_or_module)
if not meta:
raise ValueError(f"Package could not be found: {path_or_module}")
rst = {}
for req in meta.requires_dist:
s = req.split(" ")
name = s[0]
if len(s) > 1:
version = s[1].strip("()")
else:
version = None
rst[name] = version
return rst
########################################################################################################################
# Venv
########################################################################################################################
class Pyenv:
"""A python environment."""
def __init__(self, python: str):
"""Create a python environment.
Args:
python: The path to the Python executable.
"""
self._python = python
@property
def python(self) -> str:
"""The path to the Python executable in the virtual environment."""
return self._python
def pip_install(self, package: Union[str, Path], version: str = "") -> None:
"""Install a package into the virtual environment.
Args:
package: The name of the package to install.
version: The version constraint of the package to install. If None, the latest version will be installed.
For example, provide "==1.2.3" to install version 1.2.3.
"""
logging.warning(f"Installing package in environment: {package}, version: {version}, python: {self.python}")
if isinstance(package, Path):
package = package.absolute()
cmd = f"""{self.python} -m pip install {package}{version}"""
shell_exec(cmd)
class Venv(Pyenv):
"""A Python virtual environment."""
def __init__(self, path: Path):
"""Create a virtual environment.
Args:
path: The path to the virtual environment.
"""
super().__init__(os.path.join(path, "bin", "python"))
self.path = path
def new_venv(path: Path, python: str, delete_if_exists: bool) -> Venv:
"""Create a new virtual environment.
Args:
path: The path to the virtual environment.
python: The path to the Python executable to use.
delete_if_exists: Whether to delete the virtual environment if it already exists.
Returns:
The new virtual environment.
"""
logging.warning(f"Building new virtual environment: {path}")
if delete_if_exists and path.exists():
logging.warning(f"Deleting existing virtual environment: {path}")
shutil.rmtree(path)
if path.exists():
logging.error(
f"Virtual environment already exists. Please remove it before running this script. venv={path}")
raise FileExistsError(
f"Virtual environment already exists. Please remove it before running this script. venv={path}")
logging.warning(f"Creating virtual environment: {path}")
shell_exec(f"{python} -m venv {path}")
v = Venv(path)
logging.warning(f"Updating virtual environment: {path}")
shell_exec(f"{v.python} -m pip install --upgrade pip")
shell_exec(f"{v.python} -m pip install --upgrade build")
return v
def venv_path(is_release: bool, dh_version: str, dh_ib_version: str) -> Path:
"""Get the standard path to a new virtual environment.
Args:
is_release: Whether the virtual environment is for a release.
dh_version: The version of Deephaven.
dh_ib_version: The version of deephaven-ib.
Returns:
The path to the new virtual environment.
"""
if is_release:
return Path(f"venv-release-dhib={dh_version}").absolute()
else:
return Path(f"venv-dev-dhib={dh_ib_version}-dh={dh_version}").absolute()
########################################################################################################################
# IB Wheel
########################################################################################################################
class IbWheel:
def __init__(self, version: str):
"""Create an IB wheel.
Args:
version: The version of the IB wheel.
"""
self.version = version_tuple(version)
def build(self, pyenv: Pyenv) -> None:
"""Build the IB wheel.
Interactive Brokers does not make their Python wheels available via PyPI,
and the wheels are not redistributable.
As a result, we need to build the IB wheel locally.
Args:
pyenv: The python environment to build the wheel in.
"""
logging.warning(f"Building IB wheel: {self.version}")
shutil.rmtree("build/ib", ignore_errors=True)
shutil.rmtree("dist/ib", ignore_errors=True)
os.makedirs("build/ib", exist_ok=True)
os.makedirs("dist/ib", exist_ok=True)
logging.warning(f"Downloading IB API version {self.version}")
ver_ib = f"{self.version[0]:02d}{self.version[1]:02d}.{self.version[2]:02d}"
url_download(f"https://interactivebrokers.github.io/downloads/twsapi_macunix.{ver_ib}.zip", "build/ib/api.zip")
logging.warning(f"Unzipping IB API")
shell_exec("cd build/ib && unzip api.zip")
logging.warning(f"Building IB Python API")
shell_exec(f"cd build/ib/IBJts/source/pythonclient && {pyenv.python} -m build --wheel")
shell_exec("cp build/ib/IBJts/source/pythonclient/dist/* dist/ib/")
@property
def path(self) -> Path:
"""The path to the IB wheel."""
return Path(f"dist/ib/ibapi-{version_str(self.version, False)}-py3-none-any.whl").absolute()
def install(self, pyenv: Pyenv) -> None:
"""Install the IB wheel into a virtual environment.
Args:
pyenv: The python environment to install the wheel into.
"""
logging.warning(f"Installing IB wheel in python environment: {self.version} python: {pyenv.python}")
ver_narrow = version_str(self.version, False)
pyenv.pip_install(self.path)
########################################################################################################################
# deephaven-ib
########################################################################################################################
class DhIbWheel:
def __init__(self, version: str, dh_version: str, ib_version: str):
"""Create a deephaven-ib wheel.
Args:
version: The version of the deephaven-ib wheel.
dh_version: The version of Deephaven.
ib_version: The version of ibapi.
"""
self.version = version
self.dh_version = dh_version
self.ib_version = ib_version
def build(self, pyenv: Pyenv) -> None:
"""Build the deephaven-ib wheel."""
logging.warning(f"Building deephaven-ib: {self.version}")
shell_exec(f"DH_IB_VERSION={self.version} DH_VERSION={self.dh_version} IB_VERSION={self.ib_version} {pyenv.python} -m build --wheel")
@property
def path(self) -> Path:
"""The path to the deephaven-ib wheel."""
return Path(f"dist/deephaven_ib-{self.version}-py3-none-any.whl").absolute()
def install(self, pyenv: Pyenv) -> None:
"""Install the deephaven-ib wheel into a virtual environment."""
logging.warning(f"Installing deephaven-ib in python environment: {self.version} python: {pyenv.python}")
pyenv.pip_install(self.path)
########################################################################################################################
# Messages
########################################################################################################################
def success(pyenv: Pyenv) -> None:
"""Print a success message.
Args:
pyenv: The python environment.
"""
logging.warning("Deephaven-ib environment created successfully.")
logging.warning(f"Python environment: {pyenv.python}")
if isinstance(pyenv, Venv):
logging.warning(f"Success! Virtual environment created: {pyenv.path}")
logging.warning(f"Activate the virtual environment with: source {pyenv.path}/bin/activate")
logging.warning(f"Deactivate the virtual environment with: deactivate")
########################################################################################################################
# Click CLI
########################################################################################################################
@click.group()
def cli():
"""A script to build Deephaven-IB virtual environments."""
pass
@click.command()
@click.option('--python', default="python3", help='The path to the Python executable to use.')
@click.option('--ib_version', default=IB_VERSION_DEFAULT, help='The version of ibapi.')
def ib_wheel(
python: str,
ib_version: str,
):
"""Create an ibapi wheel."""
logging.warning(f"Creating an ib wheel: python={python}, ib_version={ib_version}")
version_assert_format(ib_version)
python = Path(python).absolute() if python.startswith("./") else python
logging.warning(f"Using system python: {python}")
pyenv = Pyenv(python)
ib_wheel = IbWheel(ib_version)
ib_wheel.build(pyenv)
logging.warning(f"IB wheel created successfully.")
logging.warning(f"IB wheel path: {ib_wheel.path}")
@click.command()
@click.option('--python', default="python3", help='The path to the Python executable to use.')
@click.option('--dh_version', default=DH_VERSION_DEFAULT, help='The version of Deephaven.')
@click.option('--ib_version', default=IB_VERSION_DEFAULT, help='The version of ibapi.')
@click.option('--dh_ib_version', default=None, help='The version of deephaven-ib.')
def dhib_wheel(
python: str,
dh_version: str,
ib_version: str,
dh_ib_version: Optional[str],
):
"""Create a deephaven-ib wheel."""
logging.warning(f"Creating a deephaven-ib wheel: python={python}, ib_version={ib_version} dh_version={dh_version}, dh_ib_version={dh_ib_version}")
if dh_ib_version is None:
dh_ib_version = "0.0.0.dev0"
version_assert_format(ib_version)
version_assert_format(dh_version)
version_assert_format(dh_ib_version)
python = Path(python).absolute() if python.startswith("./") else python
logging.warning(f"Using system python: {python}")
pyenv = Pyenv(python)
logging.warning(f"Building deephaven-ib from source: {dh_ib_version}")
dh_ib_wheel = DhIbWheel(dh_ib_version, dh_version, ib_version)
dh_ib_wheel.build(pyenv)
logging.warning(f"Deephaven-ib wheel created successfully.")
logging.warning(f"Deephaven-ib wheel path: {dh_ib_wheel.path}")
@click.command()
@click.option('--python', default="python3", help='The path to the Python executable to use.')
@click.option('--dh_version', default=DH_VERSION_DEFAULT, help='The version of Deephaven.')
@click.option('--dh_version_exact', default=None, help='The exact version of Deephaven.')
@click.option('--ib_version', default=IB_VERSION_DEFAULT, help='The version of ibapi.')
@click.option('--dh_ib_version', default=None, help='The version of deephaven-ib.')
@click.option('--use_venv', default=True, help='Whether to use a python virtual environment or system python.')
@click.option('--path_venv', default=None, help='The path to the virtual environment.')
@click.option('--create_venv', default=True, help='Whether to create the virtual environment if it does not already exist.')
@click.option('--delete_venv', default=False, help='Whether to delete the virtual environment if it already exists.')
@click.option('--install_dhib', default=True, help='Whether to install deephaven-ib. If set to false, the resulting venv can be used to develop deephaven-ib in PyCharm or other development environments.')
def dev(
python: str,
dh_version: str,
dh_version_exact: str,
ib_version: str,
dh_ib_version: Optional[str],
use_venv: bool,
path_venv: Optional[str],
create_venv: bool,
delete_venv: bool,
install_dhib: bool
):
"""Create a development environment."""
logging.warning(f"Creating development environment: python={python} dh_version={dh_version}, dh_version_exact={dh_version_exact}, ib_version={ib_version}, dh_ib_version={dh_ib_version}, delete_vm_if_exists={delete_venv}")
python = Path(python).absolute() if python.startswith("./") else python
if dh_version_exact:
if dh_version != DH_VERSION_DEFAULT:
raise ValueError(f"Cannot specify both dh_version={dh_version} and dh_version_exact={dh_version_exact}")
dh_version = dh_version_exact
dh_version_pip = f"=={dh_version}"
else:
dh_version_pip = f"~={dh_version}"
use_dev = dh_ib_version is None
if dh_ib_version is None:
dh_ib_version = "0.0.0.dev0"
version_assert_format(dh_version)
version_assert_format(ib_version)
version_assert_format(dh_ib_version)
if use_venv:
if path_venv:
v_path = Path(path_venv).absolute()
else:
v_path = venv_path(False, dh_version, dh_ib_version)
if create_venv:
pyenv = new_venv(v_path, python, delete_venv)
else:
pyenv = Venv(v_path)
else:
logging.warning(f"Using system python: {python}")
pyenv = Pyenv(python)
ib_wheel = IbWheel(ib_version)
ib_wheel.build(pyenv)
ib_wheel.install(pyenv)
pyenv.pip_install("deephaven-server", dh_version_pip)
if install_dhib:
if use_dev:
logging.warning(f"Building deephaven-ib from source: {dh_ib_version}")
dh_ib_wheel = DhIbWheel(dh_ib_version, dh_version, ib_version)
dh_ib_wheel.build(pyenv)
dh_ib_wheel.install(pyenv)
else:
logging.warning(f"Installing deephaven-ib from PyPI: {dh_ib_version}")
logging.warning(f"*** INSTALLED deephaven-ib MAY BE INCONSISTENT WITH INSTALLED DEPENDENCIES ***")
pyenv.pip_install("deephaven-ib", f"=={dh_ib_version}")
success(pyenv)
@click.command()
@click.option('--python', default="python3", help='The path to the Python executable to use.')
@click.option('--dh_ib_version', default=None, help='The version of deephaven-ib.')
@click.option('--use_venv', default=True, help='Whether to use a python virtual environment or system python.')
@click.option('--path_venv', default=None, help='The path to the virtual environment.')
@click.option('--create_venv', default=True, help='Whether to create the virtual environment if it does not already exist.')
@click.option('--delete_venv', default=False, help='Whether to delete the virtual environment if it already exists.')
def release(
python: str,
dh_ib_version: Optional[str],
use_venv: bool,
path_venv: Optional[str],
create_venv: bool,
delete_venv: bool
):
"""Create a release environment."""
logging.warning(f"Creating release environment: python={python} dh_ib_version={dh_ib_version}")
python = Path(python).absolute() if python.startswith("./") else python
wheel = download_wheel(python, "deephaven_ib", dh_ib_version)
deps = pkg_dependencies(wheel)
ib_version = deps["ibapi"].replace("==", "")
dh_version = deps["deephaven-server"].replace("==", "").replace("~=", "")
version_assert_format(dh_version)
version_assert_format(ib_version)
if dh_ib_version:
version_assert_format(dh_ib_version)
dh_ib_version_pip = f"=={dh_ib_version}"
else:
dh_ib_version_pip = ""
if use_venv:
if path_venv:
v_path = Path(path_venv).absolute()
else:
v_path = venv_path(True, dh_version, dh_ib_version)
if create_venv:
pyenv = new_venv(v_path, python, delete_venv)
else:
pyenv = Venv(v_path)
else:
logging.warning(f"Using system python: {python}")
pyenv = Pyenv(python)
ib_wheel = IbWheel(ib_version)
ib_wheel.build(pyenv)
ib_wheel.install(pyenv)
logging.warning(f"Installing deephaven-ib from PyPI: {dh_ib_version}")
pyenv.pip_install("deephaven-ib", dh_ib_version_pip)
success(pyenv)
cli.add_command(ib_wheel)
cli.add_command(dhib_wheel)
cli.add_command(dev)
cli.add_command(release)
if __name__ == '__main__':
cli()