UNPKG

6.3 kBPlain TextView Raw
1#!/usr/bin/env python
2# -*- encoding: utf-8 -*-
3
4# Copyright (c) 2002-2016 "Neo Technology,"
5# Network Engine for Objects in Lund AB [http://neotechnology.com]
6#
7# This file is part of Neo4j.
8#
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20
21"""
22Usage: 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
28Example: neoget.py -v 2.3.1
29 neoget.py -n 3.0
30 neoget.py -h
31"""
32from __future__ import print_function
33from sys import argv, stdout, exit, stderr
34import getopt
35from os import path, name, makedirs, getenv
36from zipfile import ZipFile
37from tarfile import TarFile
38from re import match, sub
39from base64 import b64encode
40
41try:
42 # py v3
43 from urllib.parse import urlparse
44except ImportError:
45 from urlparse import urlparse
46
47try:
48 from urllib.request import urlopen, Request
49except ImportError:
50 from urllib2 import urlopen, Request
51
52DIST = "http://dist.neo4j.org"
53DEFAULT_UNIX_URL = DIST + "/neo4j-enterprise-3.0.2-unix.tar.gz"
54DEFAULT_WIN_URL = DIST + "/neo4j-enterprise-3.0.2-windows.zip"
55
56is_windows = (name == 'nt')
57
58
59def 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 # download to the current dir
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
83def 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
90def 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
120def teamcityurlopen(archive_url):
121
122 # first try to get the user and password from environment var
123 user = getenv("TEAMCITY_USER")
124 password = getenv("TEAMCITY_PASSWORD")
125
126 # if not found, try to get it from url directly
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 # Create basic access authentication token using username and password found
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
145def download(archive_url, archive_name, extract_to_path='.', require_basic_auth=False):
146 # download the file to extract_to_path
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
180def print_help():
181 print(__doc__)
182
183
184if __name__ == "__main__":
185 main()