-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.py
127 lines (99 loc) · 4.48 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
import contextlib
import subprocess
import sys
import traceback
from pathlib import Path
HERE = Path(__file__).parent.resolve()
def show_failure_message_and_exist(message: str):
try:
import click # noqa
click.echo(click.style(message, fg='red', bold=True))
except ImportError:
print(message)
exit(1)
def install_package_from_directory(directory: Path, what):
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", '-e', what], cwd=str(directory))
except subprocess.CalledProcessError as e:
show_failure_message_and_exist(f'Failed to install. Exception :\n{e}')
def install_package_from_conda(package: str):
import click # noqa
click.echo(f'Installing: {package}')
try:
need_shell = sys.platform == 'win32'
subprocess.check_call(["conda", "install", "-y", package], shell=need_shell)
except subprocess.CalledProcessError as e:
show_failure_message_and_exist(f'Failed to install. Exception :\n{e}')
@contextlib.contextmanager
def exit_on_fail_with_message(message: str):
try:
yield
except Exception as e:
print(traceback.format_exc())
print(f'exception : \n{e}')
show_failure_message_and_exist(message)
def install_click_if_necessary():
print("Checking if click is installed...")
try:
print("Click is installed, proceeding...")
import click # noqa
except ImportError:
print("Click is not installed, installing click...")
subprocess.check_call([sys.executable, "-m", "pip", "install", 'click'])
print()
def is_jupyter_installed():
try:
output = subprocess.check_output(["jupyter", "lab", "--version"], stderr=subprocess.STDOUT)
version = output.decode().strip()
if not version.startswith('3.'):
show_failure_message_and_exist("PyQuibbler is only compatible with Jupyter lab version 3.x.")
except subprocess.CalledProcessError as e:
show_failure_message_and_exist(f'Failed to get jupyter lab version. Exception :\n{e}')
return True
def install_pyquibbler():
import click # noqa
click.echo("Installing pyquibbler (development)...")
pyquibbler_directory = HERE / 'pyquibbler'
with exit_on_fail_with_message("Failed to install pyquibbler"):
install_package_from_directory(pyquibbler_directory, ".[dev, sphinx]")
click.echo(click.style("\nSuccessfully installed pyquibbler!\n", fg='green', bold=True))
def install_labextension():
import click # noqa
click.echo("Installing pyquibbler jupyter extension (development)...")
lab_extension_directory = HERE / 'pyquibbler-labextension'
with exit_on_fail_with_message("Failed to install pyquibbler jupyter extension"):
install_package_from_conda('cookiecutter')
install_package_from_conda('nodejs')
install_package_from_conda('jupyter-packaging')
click.echo("-- Running `pip install`...")
install_package_from_directory(lab_extension_directory, '.')
click.echo("-- Symlinking jupyter extension...")
subprocess.check_output(['jupyter', 'labextension', 'develop', '--overwrite', '.'],
cwd=str(lab_extension_directory))
click.echo("-- Building jupyter extension...")
subprocess.check_output(['jupyter', 'labextension', 'build'],
cwd=str(lab_extension_directory))
click.echo(click.style("\nSuccessfully installed pyquibbler jupyter-lab extension!\n", fg='green', bold=True))
def run_click_command_line():
import click # noqa
@click.command()
def install():
click.echo("Checking if Jupyter lab is installed...")
if is_jupyter_installed():
click.echo("Jupyter lab is installed.")
should_install_extension = click.prompt(
"Would you like to install also the pyquibbler Jupyter Lab extension? [y/N]",
type=bool)
else:
click.echo("Jupyter lab is not installed.\n"
"Continuing installing pyquibbler without the Jupyter Lab extension.")
should_install_extension = False
install_pyquibbler()
if should_install_extension:
install_labextension()
click.echo(click.style("\nInstallation completed successfully!", fg='green', bold=True))
click.echo(click.style('Welcome to pyquibbler!\n', fg='green', bold=True))
install()
if __name__ == '__main__':
install_click_if_necessary()
run_click_command_line()