Uh oh!
There was an error while loading. Please reload this page.
forked from nats-io/nsc
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.py
More file actions
Latest commit
executable file
·198 lines (165 loc) · 5.32 KB
/
Copy pathinstall.py
File metadata and controls
executable file
·198 lines (165 loc) · 5.32 KB
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
#!/usr/bin/env python
# Copyright 2018-2021 The NATS Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This installer mostly inspired by
# https://github.com/denoland/deno_install/blob/master/install.py
from __future__ importprint_function
importio
importos
importre
importsys
importzipfile
importzlib
try:
fromurllib.requestimporturlopen
exceptImportError:
fromurllib2importurlopen
NSC_REPO_URL="https://github.com/nats-io/nsc"
FILENAME_LOOKUP= {
"darwin": {
"amd64": "nsc-darwin-amd64.zip",
"arm64": "nsc-darwin-arm64.zip",
},
"linux": {
"amd64": "nsc-linux-amd64.zip",
"arm64": "nsc-linux-arm64.zip",
"armv7": "nsc-linux-armv7.zip",
},
"win32": {
"amd64": "nsc-windows-amd64.zip",
},
}
defrelease_url(platform, arch, tag):
if"linux"inplatform:
# convert any linux regardless of version reported to "linux"
platform="linux"
ifarch=="x86_64":
arch="amd64"
elifarch=="aarch64":
arch="arm64"
elifarch=="armv7l": # reported by debian Buster on Raspberry Pi 3B+
arch="armv7"
try:
filename=FILENAME_LOOKUP[platform][arch]
exceptKeyError:
print("Unable to locate appropriate filename for", platform, "with archtecture", arch)
sys.exit(1)
release_base=NSC_REPO_URL+'/releases/'
iftag:
returnrelease_base+'download/'+tag+'/'+filename
returnrelease_base+'latest/download/'+filename
defdownload_with_progress(url):
print("Downloading", url)
remote_file=urlopen(url)
total_size=int(remote_file.headers['Content-Length'].strip())
data= []
bytes_read=0.0
whileTrue:
d=remote_file.read(8192)
ifnotd:
print()
break
bytes_read+=len(d)
data.append(d)
sys.stdout.write('\r%2.2f%% downloaded'% (bytes_read/total_size*100))
sys.stdout.flush()
returnb''.join(data)
defmain():
url=release_url(sys.platform, os.uname()[4], sys.argv[1] iflen(sys.argv) >1elseNone)
bin_dir=nsc_bin_dir()
exe_fn=os.path.join(bin_dir, "nsc")
windows="windows"inurl
ifwindows:
exe_fn=os.path.join(bin_dir, "nsc.exe")
compressed=download_with_progress(url)
ifurl.endswith(".zip"):
withzipfile.ZipFile(io.BytesIO(compressed), 'r') asz:
withopen(exe_fn, 'wb+') asexe:
ifwindows:
exe.write(z.read('nsc.exe'))
else:
exe.write(z.read('nsc'))
else:
# Note: gzip.decompress is not available in python2.
content=zlib.decompress(compressed, 15+32)
withopen(exe_fn, 'wb+') asexe:
exe.write(content)
os.chmod(exe_fn, 0o744)
print("NSC: "+exe_fn)
ifmaybe_symlink(exe_fn):
return
print("Now manually add %s to your $PATH"%bin_dir)
ifwindows:
print("Windows Cmd Prompt Example:")
print(" setx path %%path;\"%s\""%bin_dir)
print()
else:
print("Bash Example:")
print(" echo 'export PATH=\"$PATH:%s\"' >> $HOME/.bashrc"%bin_dir)
print(" source $HOME/.bashrc")
print()
print("Zsh Example:")
print(" echo 'export PATH=\"$PATH:%s\"' >> $HOME/.zshrc"%bin_dir)
print(" source $HOME/.zshrc")
print()
# Returns True if install instructions are not needed
defmaybe_symlink(exe_fn):
sym_dir=nsc_symlink_dir()
ifnotsym_dir:
returnFalse
link_path=os.path.join(sym_dir, os.path.basename(exe_fn))
ifos.path.exists(link_path):
ifos.path.islink(link_path):
try:
os.unlink(link_path)
exceptException:
returnFalse
else:
print("Not touching non-symlink: "+link_path)
returnFalse
try:
os.symlink(exe_fn, link_path)
print("NSC: "+link_path)
ifdir_in_PATH(sym_dir):
returnTrue
returnFalse
exceptException:
# Python2 does not support symlinks on Windows, amongst other
# reasons this might have failed.
returnFalse
defdir_in_PATH(dirname):
try:
envPath=os.environ['PATH']
exceptException:
returnFalse
returndirnameinenvPath.split(os.pathsep)
defmkdir(d):
ifnotos.path.exists(d):
print("mkdir", d)
os.mkdir(d)
defnsc_bin_dir():
home=os.path.expanduser("~")
nsccli=os.path.join(home, ".nsccli")
mkdir(nsccli)
bin_dir=os.path.join(nsccli, "bin")
mkdir(bin_dir)
returnbin_dir
defnsc_symlink_dir():
home=os.path.expanduser("~")
sym_dir=os.path.join(home, "bin")
ifos.path.exists(sym_dir):
returnsym_dir
returnNone
if__name__=='__main__':
main()