-
Notifications
You must be signed in to change notification settings - Fork 36
/
mxpy-up.py
387 lines (281 loc) · 13.2 KB
/
mxpy-up.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
import logging
import os
import os.path
import shutil
import stat
import subprocess
import sys
from argparse import ArgumentParser
from pathlib import Path
from typing import List, Tuple
logger = logging.getLogger("installer")
MIN_REQUIRED_PYTHON_VERSION = (3, 8, 0)
sdk_path = Path("~/multiversx-sdk").expanduser().resolve()
def main():
parser = ArgumentParser()
parser.add_argument("--exact-version", help="the exact version of mxpy to install")
parser.add_argument("--from-branch", help="use a branch of multiversx/mx-sdk-py-cli")
parser.add_argument("--not-interactive", action="store_true", default=False)
parser.add_argument("--verbose", action="store_true", default=False)
parser.add_argument("--ignore-deprecation", action="store_true", default=False, help="'mxpy-up.py' is obsolete, install using 'pipx': https://docs.multiversx.com/sdk-and-tools/sdk-py/installing-mxpy/#install-using-pipx")
parser.set_defaults(modify_path=True)
args = parser.parse_args()
logger.warning("'mxpy-up.py' is deprecated. Check out the documentation on how to install using `pipx`: https://docs.multiversx.com/sdk-and-tools/sdk-py/installing-mxpy/#install-using-pipx.")
if not args.ignore_deprecation:
raise Exception("'mxpy-up.py' is deprecated, please install using `pipx`: https://docs.multiversx.com/sdk-and-tools/sdk-py/installing-mxpy/#install-using-pipx. If installing using 'mxpy-up` is necessary, provide the `--ignore-deprecation` flag.")
exact_version = args.exact_version
from_branch = args.from_branch
interactive = not args.not_interactive
verbose = args.verbose
log_level = logging.DEBUG if verbose else logging.INFO
logging.basicConfig(level=log_level)
if get_operating_system() == "windows":
print("""
IMPORTANT NOTE
==============
Windows support is limited and experimental.
""")
confirm_continuation(interactive)
guard_non_root_user()
guard_python_version()
migrate_v6(interactive)
# In case of a fresh install:
sdk_path.mkdir(parents=True, exist_ok=True)
create_venv()
logger.info("Installing the necessary dependencies...")
install_mxpy(exact_version, from_branch, verbose)
run_post_install_checks()
if interactive:
guide_system_path_integration()
logger.warning("Installing `mxpy` using `mxpy-up.py` is deprecated. Check out the documentation on how to install using `pipx`: https://docs.multiversx.com/sdk-and-tools/sdk-py/installing-mxpy/#install-using-pipx")
def guard_non_root_user():
logger.debug("Checking user (should not be root).")
operating_system = get_operating_system()
if operating_system == "windows":
return
if os.getuid() == 0:
raise InstallError("You should not install mxpy as root.")
def guard_python_version():
python_version = (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)
logger.debug("Checking Python version.")
logger.debug(f"Python version: {format_version(python_version)}")
if python_version < MIN_REQUIRED_PYTHON_VERSION:
raise InstallError(f"You need Python {format_version(MIN_REQUIRED_PYTHON_VERSION)} or later.")
def format_version(version: Tuple[int, int, int]) -> str:
major, minor, patch = version
return f"{major}.{minor}.{patch}"
def get_operating_system():
aliases = {
"linux": "linux",
"linux1": "linux",
"linux2": "linux",
"darwin": "osx",
"win32": "windows",
"cygwin": "windows",
"msys": "windows"
}
operating_system = aliases.get(sys.platform)
if operating_system is None:
raise InstallError(f"Unknown platform: {sys.platform}")
return operating_system
def migrate_v6(interactive: bool):
nodejs_folder = sdk_path / "nodejs"
if nodejs_folder.exists():
print(f"""
In previous versions of the SDK, the "wasm-opt" tool was installed in the "nodejs" folder.
This is no longer the case - now, "wasm-opt" is a separate module.
The following folder will be removed: {nodejs_folder}.
You may need to reinstall wasm-opt using `mxpy deps install wasm-opt`.
""")
confirm_continuation(interactive)
shutil.rmtree(nodejs_folder)
global_testnet_toml = sdk_path / "testnet.toml"
if global_testnet_toml.exists():
global_testnet_toml.unlink()
def create_venv():
require_python_venv_tools()
venv_folder = get_mxpy_venv_path()
venv_folder.mkdir(parents=True, exist_ok=True)
logger.debug(f"Creating virtual environment in: {venv_folder}.")
import venv
builder = venv.EnvBuilder(with_pip=True, symlinks=True)
builder.clear_directory(venv_folder)
builder.create(venv_folder)
logger.debug(f"Virtual environment has been created in: {venv_folder}.")
def require_python_venv_tools():
operating_system = get_operating_system()
try:
import ensurepip
import venv
logger.debug(f"Packages found: {ensurepip}, {venv}.")
except ModuleNotFoundError:
if operating_system == "linux":
python_venv = f"python{sys.version_info.major}.{sys.version_info.minor}-venv"
raise InstallError(f'Packages [venv] or [ensurepip] not found. Please run "sudo apt install {python_venv}" and then run mxpy-up again.')
else:
raise InstallError("Packages [venv] or [ensurepip] not found, please install them first. See https://docs.python.org/3/tutorial/venv.html.")
def get_mxpy_venv_path():
return sdk_path / "mxpy-venv"
def install_mxpy(exact_version: str, from_branch: str, verbose: bool):
logger.debug("Installing mxpy in virtual environment...")
if from_branch:
package_to_install = f"https://github.com/multiversx/mx-sdk-py-cli/archive/refs/heads/{from_branch}.zip"
else:
package_to_install = "multiversx_sdk_cli" if not exact_version else f"multiversx_sdk_cli=={exact_version}"
venv_path = get_mxpy_venv_path()
return_code = run_in_venv(["python3", "-m", "pip", "install", "--upgrade", "pip"], venv_path, verbose)
if return_code != 0:
raise InstallError("Could not upgrade pip.")
return_code = run_in_venv(["pip3", "install", "--no-cache-dir", package_to_install], venv_path, verbose)
if return_code != 0:
raise InstallError("Could not install mxpy.")
logger.debug("Creating mxpy shortcut...")
shortcut_path = sdk_path / "mxpy"
try:
shortcut_path.unlink()
logger.debug(f"Removed existing shortcut: {shortcut_path}")
except FileNotFoundError:
logger.debug(f"Shortcut does not exist yet: {shortcut_path}")
pass
shortcut_content = get_mxpy_shortcut_content()
shortcut_path.write_text(shortcut_content)
st = os.stat(shortcut_path)
os.chmod(shortcut_path, st.st_mode | stat.S_IEXEC)
logger.info("You have successfully installed mxpy.")
def get_mxpy_shortcut_content():
operating_system = get_operating_system()
venv_path = get_mxpy_venv_path()
if operating_system == "windows":
return f"""#!/bin/sh
. "{venv_path / 'Scripts' / 'activate'}" && python3 -m multiversx_sdk_cli.cli "$@" && deactivate
"""
return f"""#!/bin/sh
. "{venv_path / 'bin' / 'activate'}" && python3 -m multiversx_sdk_cli.cli "$@" && deactivate
"""
def run_in_venv(args: List[str], venv_path: Path, verbose: bool):
env = os.environ.copy()
if "PYTHONHOME" in env:
del env["PYTHONHOME"]
env["PATH"] = str(venv_path / "bin") + ":" + env["PATH"]
env["VIRTUAL_ENV"] = str(venv_path)
if verbose:
process = subprocess.Popen(args, env=env)
else:
process = subprocess.Popen(args, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
return process.wait()
def run_post_install_checks():
multiversx_sdk_path = Path("~/multiversx-sdk").expanduser()
logger.debug("Running post-install checks...")
if multiversx_sdk_path.exists():
logger.debug("~/multiversx-sdk exists OK")
else:
logger.warning("~/multiversx-sdk exists NOK")
if (multiversx_sdk_path / "mxpy").exists():
logger.debug("~/multiversx-sdk/mxpy shortcut created OK")
else:
logger.warning("~/multiversx-sdk/mxpy shortcut created NOK")
def guide_system_path_integration():
interactive = True
operating_system = get_operating_system()
if operating_system == "windows":
print(f"""
###############################################################################
On Windows, for the "mxpy" command shortcut to be available, you MUST ADD the directory "{sdk_path}" to the system PATH.
You can do this by following these steps:
https://superuser.com/questions/949560/how-do-i-set-system-environment-variables-in-windows-10
###############################################################################
Do you UNDERSTAND the above?
###############################################################################
""")
confirm_continuation(interactive)
return
old_export_directive = f'export PATH="{Path("~/elrondsdk").expanduser()}:$PATH"\t# elrond-sdk'
new_export_directive = 'export PATH="${{HOME}}/multiversx-sdk:$PATH"\t# multiversx-sdk'
profile_files = get_profile_files()
if not profile_files:
print(f"""
###############################################################################
No shell profile files have been found.
The "mxpy" command shortcut will not be available until you add the directory "{sdk_path}" to the system PATH.
###############################################################################
Do you UNDERSTAND the above?
""")
confirm_continuation(interactive)
return
profile_files_formatted = "\n".join(f" - {file}" for file in profile_files)
profile_files_contents = [profile_file.read_text() for profile_file in profile_files]
any_old_export_directive = any(old_export_directive in content for content in profile_files_contents)
any_new_export_directive = any(new_export_directive in content for content in profile_files_contents)
if any_old_export_directive:
# We don't perform the removal automatically (a bit risky)
print(f"""
###############################################################################
It seems that the old path "~/elrondsdk" is still configured in shell profile.
Please MANUALLY remove it from the shell profile (now or after the installer script ends).
Your shell profile files:
{profile_files_formatted}
The entry (entries) to remove:
{old_export_directive}
###############################################################################
Make sure you UNDERSTAND the above before proceeding further.
###############################################################################
""")
confirm_continuation(interactive)
if any_new_export_directive:
# Note: in some (rare) cases, here we'll have false positives (e.g. if the export directive is commented out).
print(f"""
###############################################################################
It seems that the path "~/multiversx-sdk" is already configured in shell profile.
To confirm this, CHECK the shell profile (now or after the installer script ends).
Your shell profile files:
{profile_files_formatted}
The entry to check (it should be present):
{new_export_directive}.
###############################################################################
Make sure you UNDERSTAND the above before proceeding further.
###############################################################################
""")
confirm_continuation(interactive)
return
print(f"""
###############################################################################
In order to use the "mxpy" command shortcut, you have to manually extend the PATH variable to include "~/multiversx-sdk".
In order to manually extend the PATH variable, add the following line to your shell profile file upon installation:
export PATH="${{HOME}}/multiversx-sdk:${{PATH}}"
Your shell profile files:
{profile_files_formatted}
Upon editing the shell profile file, you may have to RESTART THE USER SESSION for the changes to take effect.
""")
confirm_continuation(interactive)
def get_profile_files() -> List[Path]:
files = [
Path("~/.profile").expanduser().resolve(),
Path("~/.bashrc").expanduser().resolve(),
Path("~/.bash_profile").expanduser().resolve(),
Path("~/.zshrc").expanduser().resolve()
]
return [file for file in files if file.exists()]
class InstallError(Exception):
def __init__(self, message: str):
super().__init__(message)
def confirm_continuation(interactive: bool):
if not interactive:
return
answer = input("Continue? (y/n)")
if answer.lower() not in ["y", "yes"]:
print("Confirmation not given. Will stop.")
exit(1)
if __name__ == "__main__":
try:
main()
except Exception as err:
logger.fatal(err)
sys.exit(1)
print("""
###############################################################################
Installer script finished successfully.
###############################################################################
For more information go to https://docs.multiversx.com.
For support, please contact us at http://discord.gg/MultiversXBuilders.
###############################################################################
""")