This repository has been archived by the owner on Mar 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
neoget.py
185 lines (148 loc) · 6.15 KB
/
neoget.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (c) 2002-2016 "Neo Technology,"
# Network Engine for Objects in Lund AB [http://neotechnology.com]
#
# This file is part of Neo4j.
#
# 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.
"""
Usage: neoget.py <cmd> [arg]
-v neo4j-version: download this specific neo4j enterprise version
-n neo4j-version: download this specific neo4j enterprise nightly version from teamcity with basic access auth
-l download-url : download neo4j provided by this url
-h : show this help message
Example: neoget.py -v 2.3.1
neoget.py -n 3.0
neoget.py -h
"""
from __future__ import print_function
from sys import argv, stdout, exit, stderr
import getopt
from os import path, name, makedirs, getenv
from zipfile import ZipFile
from tarfile import TarFile
from re import match, sub
from base64 import b64encode
try:
# py v3
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
DIST = "http://dist.neo4j.org"
DEFAULT_UNIX_URL = DIST + "/neo4j-enterprise-3.1.2-unix.tar.gz"
DEFAULT_WIN_URL = DIST + "/neo4j-enterprise-3.1.2-windows.zip"
is_windows = (name == 'nt')
def main():
try:
opts, args = getopt.getopt(argv[1:], "hv:n:l:")
except getopt.GetoptError as err:
print(str(err))
print_help()
exit()
archive_url, archive_name, require_basic_auth = neo4j_default_archive()
for opt, arg in opts:
if opt == '-h':
print_help()
exit()
elif opt in ('-v', '-n', '-l'):
archive_url, archive_name, require_basic_auth = neo4j_archive(opt, arg)
# download to the current dir
download(archive_url, archive_name, require_basic_auth=require_basic_auth)
ret = 0 if path.exists(archive_name) else 1
exit(ret)
def neo4j_default_archive():
archive_url = DEFAULT_WIN_URL if is_windows else DEFAULT_UNIX_URL
archive_name = path.split(urlparse(archive_url).path)[-1]
require_basic_auth = False
return archive_url, archive_name, require_basic_auth
def neo4j_archive(opt, arg):
archive_url, archive_name = '', ''
require_basic_auth = False
if opt == '-v':
if is_windows:
archive_name = "neo4j-enterprise-%s-windows.zip" % arg
else:
archive_name = "neo4j-enterprise-%s-unix.tar.gz" % arg
archive_url = "%s/%s" % (DIST, archive_name)
elif opt == '-n':
if is_windows:
archive_name = "neo4j-enterprise-%s-NIGHTLY-windows.zip" % arg
url_env_var_name = "TEAMCITY_NEO4J_%sNIGHTLY_WIN" % sub('\.', '', arg)
else:
archive_name = "neo4j-enterprise-%s-NIGHTLY-unix.tar.gz" % arg
url_env_var_name = "TEAMCITY_NEO4J_%sNIGHTLY" % sub('\.', '', arg)
archive_url = getenv(url_env_var_name)
require_basic_auth = True
if not archive_url:
stderr.write("Failed to load archive url for `%s` due to missing env variable `%s`.\n" % (archive_name, url_env_var_name))
exit(1)
elif opt == '-l':
archive_url = arg
archive_name = path.split(urlparse(archive_url).path)[-1]
return archive_url, archive_name, require_basic_auth
def teamcityurlopen(archive_url):
# first try to get the user and password from environment var
user = getenv("TEAMCITY_USER")
password = getenv("TEAMCITY_PASSWORD")
# if not found, try to get it from url directly
if not user or not password:
matchResult = match("^(.*):\/\/(.*):(.*)@(.*)$", archive_url)
if not matchResult:
stderr.write("Please either provide `TEAMCITY_USER`, `TEAMCITY_PASSWORD` env variables, "
"or prepend `username:password@` to the hostname in the url to authenticate to teamcity.\n")
exit(1)
user = matchResult.group(2)
password = matchResult.group(3)
archive_url = matchResult.group(1) + "://" + matchResult.group(4)
# Create basic access authentication token using username and password found
headers = {"Authorization": "Basic " + b64encode((user + ":" + password).encode("utf-8")).decode("ascii")}
request = Request(archive_url, headers=headers)
return urlopen(request)
def download(archive_url, archive_name, extract_to_path='.', require_basic_auth=False):
# download the file to extract_to_path
if not path.exists(extract_to_path):
makedirs(extract_to_path)
archive_path = path.join(extract_to_path, archive_name)
stdout.write("Downloading '%s' to '%s'...\n" % (archive_url, archive_path))
source = urlopen(archive_url) if not require_basic_auth else teamcityurlopen(archive_url)
with open(archive_path, "wb") as destination:
more = True
while more:
data = source.read(8192)
if data:
destination.write(data)
else:
more = False
if archive_name.endswith('.zip'):
stdout.write("Unzipping '%s' to '%s'...\n" % (archive_path, extract_to_path))
zip_ref = ZipFile(archive_path, 'r')
zip_ref.extractall(extract_to_path)
unzip_folder = zip_ref.namelist()[0]
zip_ref.close()
return unzip_folder
elif archive_name.endswith('.tar.gz'):
stdout.write("Unarchiving '%s' to '%s'...\n" % (archive_path, extract_to_path))
tar_ref = TarFile.open(archive_path)
tar_ref.extractall(extract_to_path)
untar_folder=tar_ref.getnames()[0]
tar_ref.close()
return untar_folder
def print_help():
print(__doc__)
if __name__ == "__main__":
main()