forked from jupyterhub/jupyter-remote-desktop-proxy
-
Notifications
You must be signed in to change notification settings - Fork 7
/
install.py
152 lines (129 loc) · 4.63 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
import argparse
import hashlib
import os
import platform
import shutil
import tarfile
import textwrap
from urllib.request import HTTPError, urlopen, urlretrieve
from xml.etree import ElementTree
HERE = os.path.dirname(os.path.abspath(__file__))
def checksum_file(path):
"""Compute the md5 checksum of a path"""
hasher = hashlib.md5()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
def fetch_release_info(tigervnc_archive, tigervnc_version):
"""Fetch the download url and md5 checksum from tigervnc release rss"""
url = (
"https://sourceforge.net/projects/tigervnc/rss"
f"?path=/stable/{tigervnc_version}"
)
release_title = f'/stable/{tigervnc_version}/{tigervnc_archive}'
print(f"Fetching md5 and download url from {url}")
with urlopen(url) as f:
tree = ElementTree.parse(f)
root = tree.getroot()
channel = root.find('channel')
for item in channel.iterfind('item'):
title = item.find('title')
if title.text == release_title:
media = item.find('{http://video.search.yahoo.com/mrss/}content')
url = media.get('url')
md5 = media.find('{http://video.search.yahoo.com/mrss/}hash').text
break
else:
raise Exception(f"Could not find a tarball \"{tigervnc_archive}\" on sourceforge.net")
return url, md5
def install_tigervnc(prefix, plat, tigervnc_version):
tigervnc_path = os.path.join(prefix, "bin", "vncconfig")
if os.path.exists(tigervnc_path):
print(f"Tigervnc already exists at {tigervnc_path}. Remove it to re-install.")
print("--- Done ---")
return
tigervnc_archive = f"tigervnc-{tigervnc_version}.{plat}.tar.gz"
tigervnc_archive_path = os.path.join(prefix, tigervnc_archive)
try:
tigervnc_url, tigervnc_md5 = fetch_release_info(tigervnc_archive, tigervnc_version)
except HTTPError as e:
print(f"Failed to retrieve md5 and download url: {e}")
return
except Exception as e:
print(f"Failed to retrieve md5 and download url: {e}")
return
print(f"Downloading tigervnc {tigervnc_version} from {tigervnc_url}...")
urlretrieve(tigervnc_url, tigervnc_archive_path)
md5 = checksum_file(tigervnc_archive_path)
if md5 != tigervnc_md5:
raise OSError(f"Checksum failed {md5} != {tigervnc_md5}")
print("Extracting the archive...")
with tarfile.open(tigervnc_archive_path, "r") as tar_ref:
tar_ref.extractall(prefix)
shutil.copytree(
src=f"{prefix}/tigervnc-{tigervnc_version}.{plat}/usr",
dst=prefix,
dirs_exist_ok=True,
)
print(f"Installed tigervnc {tigervnc_version}")
shutil.rmtree(f"{prefix}/tigervnc-{tigervnc_version}.{plat}")
os.unlink(tigervnc_archive_path)
print("--- Done ---")
def main():
# extract supported and default versions from urls
parser = argparse.ArgumentParser(
description="Dependency installer helper",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--output",
dest="installation_dir",
default=f"{HERE}/share/tigervnc",
help=textwrap.dedent(
"""\
The installation directory (absolute or relative path).
If it doesn't exist, it will be created.
If no directory is provided, it defaults to:
--- %(default)s ---
"""
),
)
machine = platform.machine()
parser.add_argument(
"--platform",
dest="plat",
default=machine,
help=textwrap.dedent(
"""\
The platform to download for.
If no platform is provided, it defaults to:
--- %(default)s ---
"""
),
)
parser.add_argument(
"--tigervnc-version",
dest="tigervnc_version",
default="1.10.1",
choices=["1.10.1", "1.10.0", "1.9.0"],
help=textwrap.dedent(
"""\
The version of tigervnc to download.
If no version is provided, it defaults to:
--- %(default)s ---
"""
),
)
args = parser.parse_args()
deps_dir = args.installation_dir
plat = args.plat
tigervnc_version = args.tigervnc_version.lstrip("v")
if os.path.exists(deps_dir):
print(f"Using existing output directory {deps_dir}...")
else:
print(f"Creating output directory {deps_dir}...")
os.makedirs(deps_dir)
install_tigervnc(deps_dir, plat, tigervnc_version)
if __name__ == "__main__":
main()