1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | """
|
22 | Usage: neoget.py <cmd> [arg]
|
23 | -v neo4j-version: download this specific neo4j enterprise version
|
24 | -n neo4j-version: download this specific neo4j enterprise nightly version from teamcity with basic access auth
|
25 | -l download-url : download neo4j provided by this url
|
26 | -h : show this help message
|
27 |
|
28 | Example: neoget.py -v 2.3.1
|
29 | neoget.py -n 3.0
|
30 | neoget.py -h
|
31 | """
|
32 | from __future__ import print_function
|
33 | from sys import argv, stdout, exit, stderr
|
34 | import getopt
|
35 | from os import path, name, makedirs, getenv
|
36 | from zipfile import ZipFile
|
37 | from tarfile import TarFile
|
38 | from re import match, sub
|
39 | from base64 import b64encode
|
40 |
|
41 | try:
|
42 |
|
43 | from urllib.parse import urlparse
|
44 | except ImportError:
|
45 | from urlparse import urlparse
|
46 |
|
47 | try:
|
48 | from urllib.request import urlopen, Request
|
49 | except ImportError:
|
50 | from urllib2 import urlopen, Request
|
51 |
|
52 | DIST = "http://dist.neo4j.org"
|
53 | DEFAULT_UNIX_URL = DIST + "/neo4j-enterprise-3.0.2-unix.tar.gz"
|
54 | DEFAULT_WIN_URL = DIST + "/neo4j-enterprise-3.0.2-windows.zip"
|
55 |
|
56 | is_windows = (name == 'nt')
|
57 |
|
58 |
|
59 | def main():
|
60 | try:
|
61 | opts, args = getopt.getopt(argv[1:], "hv:n:l:")
|
62 | except getopt.GetoptError as err:
|
63 | print(str(err))
|
64 | print_help()
|
65 | exit()
|
66 |
|
67 | archive_url, archive_name, require_basic_auth = neo4j_default_archive()
|
68 |
|
69 | for opt, arg in opts:
|
70 | if opt == '-h':
|
71 | print_help()
|
72 | exit()
|
73 | elif opt in ('-v', '-n', '-l'):
|
74 | archive_url, archive_name, require_basic_auth = neo4j_archive(opt, arg)
|
75 |
|
76 |
|
77 | download(archive_url, archive_name, require_basic_auth=require_basic_auth)
|
78 |
|
79 | ret = 0 if path.exists(archive_name) else 1
|
80 | exit(ret)
|
81 |
|
82 |
|
83 | def neo4j_default_archive():
|
84 | archive_url = DEFAULT_WIN_URL if is_windows else DEFAULT_UNIX_URL
|
85 | archive_name = path.split(urlparse(archive_url).path)[-1]
|
86 | require_basic_auth = False
|
87 | return archive_url, archive_name, require_basic_auth
|
88 |
|
89 |
|
90 | def neo4j_archive(opt, arg):
|
91 | archive_url, archive_name = '', ''
|
92 | require_basic_auth = False
|
93 |
|
94 | if opt == '-v':
|
95 | if is_windows:
|
96 | archive_name = "neo4j-enterprise-%s-windows.zip" % arg
|
97 | else:
|
98 | archive_name = "neo4j-enterprise-%s-unix.tar.gz" % arg
|
99 | archive_url = "%s/%s" % (DIST, archive_name)
|
100 | elif opt == '-n':
|
101 | if is_windows:
|
102 | archive_name = "neo4j-enterprise-%s-NIGHTLY-windows.zip" % arg
|
103 | url_env_var_name = "TEAMCITY_NEO4J_%sNIGHTLY_WIN" % sub('\.', '', arg)
|
104 | else:
|
105 | archive_name = "neo4j-enterprise-%s-NIGHTLY-unix.tar.gz" % arg
|
106 | url_env_var_name = "TEAMCITY_NEO4J_%sNIGHTLY" % sub('\.', '', arg)
|
107 |
|
108 | archive_url = getenv(url_env_var_name)
|
109 | require_basic_auth = True
|
110 | if not archive_url:
|
111 | stderr.write("Failed to load archive url for `%s` due to missing env variable `%s`.\n" % (archive_name, url_env_var_name))
|
112 | exit(1)
|
113 |
|
114 | elif opt == '-l':
|
115 | archive_url = arg
|
116 | archive_name = path.split(urlparse(archive_url).path)[-1]
|
117 | return archive_url, archive_name, require_basic_auth
|
118 |
|
119 |
|
120 | def teamcityurlopen(archive_url):
|
121 |
|
122 |
|
123 | user = getenv("TEAMCITY_USER")
|
124 | password = getenv("TEAMCITY_PASSWORD")
|
125 |
|
126 |
|
127 | if not user or not password:
|
128 | matchResult = match("^(.*):\/\/(.*):(.*)@(.*)$", archive_url)
|
129 | if not matchResult:
|
130 | stderr.write("Please either provide `TEAMCITY_USER`, `TEAMCITY_PASSWORD` env variables, "
|
131 | "or prepend `username:password@` to the hostname in the url to authenticate to teamcity.\n")
|
132 | exit(1)
|
133 |
|
134 | user = matchResult.group(2)
|
135 | password = matchResult.group(3)
|
136 | archive_url = matchResult.group(1) + "://" + matchResult.group(4)
|
137 |
|
138 |
|
139 | headers = {"Authorization": "Basic " + b64encode((user + ":" + password).encode("utf-8")).decode("ascii")}
|
140 |
|
141 | request = Request(archive_url, headers=headers)
|
142 | return urlopen(request)
|
143 |
|
144 |
|
145 | def download(archive_url, archive_name, extract_to_path='.', require_basic_auth=False):
|
146 |
|
147 | if not path.exists(extract_to_path):
|
148 | makedirs(extract_to_path)
|
149 |
|
150 | archive_path = path.join(extract_to_path, archive_name)
|
151 | stdout.write("Downloading '%s' to '%s'...\n" % (archive_url, archive_path))
|
152 |
|
153 | source = urlopen(archive_url) if not require_basic_auth else teamcityurlopen(archive_url)
|
154 |
|
155 | with open(archive_path, "wb") as destination:
|
156 | more = True
|
157 | while more:
|
158 | data = source.read(8192)
|
159 | if data:
|
160 | destination.write(data)
|
161 | else:
|
162 | more = False
|
163 |
|
164 | if archive_name.endswith('.zip'):
|
165 | stdout.write("Unzipping '%s' to '%s'...\n" % (archive_path, extract_to_path))
|
166 | zip_ref = ZipFile(archive_path, 'r')
|
167 | zip_ref.extractall(extract_to_path)
|
168 | unzip_folder = zip_ref.namelist()[0]
|
169 | zip_ref.close()
|
170 | return unzip_folder
|
171 | elif archive_name.endswith('.tar.gz'):
|
172 | stdout.write("Unarchiving '%s' to '%s'...\n" % (archive_path, extract_to_path))
|
173 | tar_ref = TarFile.open(archive_path)
|
174 | tar_ref.extractall(extract_to_path)
|
175 | untar_folder=tar_ref.getnames()[0]
|
176 | tar_ref.close()
|
177 | return untar_folder
|
178 |
|
179 |
|
180 | def print_help():
|
181 | print(__doc__)
|
182 |
|
183 |
|
184 | if __name__ == "__main__":
|
185 | main()
|