From f69af0110f837371b45f2939a5e5c991357d9b2d Mon Sep 17 00:00:00 2001 From: Greg Studer Date: Tue, 5 Mar 2013 10:39:10 -0500 Subject: improve multiversion install script, now works with python 2.6 --- buildscripts/setup_multiversion_mongodb.py | 132 +++++++++++++++-------------- 1 file changed, 69 insertions(+), 63 deletions(-) diff --git a/buildscripts/setup_multiversion_mongodb.py b/buildscripts/setup_multiversion_mongodb.py index d476e81d1a5..a19f560bf20 100644 --- a/buildscripts/setup_multiversion_mongodb.py +++ b/buildscripts/setup_multiversion_mongodb.py @@ -9,6 +9,8 @@ import subprocess import tarfile import shutil import errno +# To ensure it exists on the system +import gzip # # Useful script for installing multiple versions of MongoDB on a machine @@ -23,30 +25,29 @@ class MultiVersionDownloader : match = re.compile("(.*)\/(.*)").match(platform) self.platform = match.group(1) self.arch = match.group(2) - self.links = self.download_links() + self.links = self.download_links() def download_links(self): href = "http://dl.mongodb.org/dl/%s/%s" \ % (self.platform.lower(), self.arch) - html = urllib2.urlopen(href).read() + html = urllib2.urlopen(href).read() - links = {} + links = {} for line in html.split(): - match = re.compile("http:\/\/downloads\.mongodb\.org\/%s/mongodb-%s-%s-(.*)\.tgz" \ - % (self.platform.lower(), self.platform.lower(), self.arch)).search(line) + match = re.compile("http:\/\/downloads\.mongodb\.org\/%s/mongodb-%s-%s-([^\"]*)\.tgz" \ + % (self.platform.lower(), self.platform.lower(), self.arch)).search(line) if match == None: continue - link = match.group(0) - version = match.group(1) + link = match.group(0) + version = match.group(1) + links[version] = link - links[version] = link + return links - return links - def download_version(self, version): - + try: os.makedirs(self.install_dir) except OSError as exc: @@ -54,52 +55,58 @@ class MultiVersionDownloader : pass else: raise - urls = [] - for link_version, link_url in self.links.iteritems(): - if link_version.startswith(version): - + urls = [] + for link_version, link_url in self.links.iteritems(): + if link_version.startswith(version): # If we have a "-" in our version, exact match only if version.find("-") >= 0: if link_version != version: continue elif link_version.find("-") >= 0: continue - urls.append((link_version, link_url)) - - if len(urls) == 0: - raise Exception("Cannot find a link for version %s, versions %s found." \ - % (version, self.links)) - + urls.append((link_version, link_url)) + + if len(urls) == 0: + raise Exception("Cannot find a link for version %s, versions %s found." \ + % (version, self.links)) + urls.sort() full_version = urls[-1][0] - url = urls[-1][1] - + url = urls[-1][1] + temp_dir = tempfile.mkdtemp() - temp_file = tempfile.mktemp(suffix=".tgz") - - data = urllib2.urlopen(url) - - print "Downloading data for version %s (%s)..." % (version, full_version) - - with open(temp_file, 'wb') as f: - f.write(data.read()) - - print "Uncompressing data for version %s (%s)..." % (version, full_version) - - with tarfile.open(temp_file, 'r') as tf: - tf.extractall(path=temp_dir) - - extract_dir = os.listdir(temp_dir)[0] - temp_install_dir = os.path.join(temp_dir, extract_dir) - + temp_file = tempfile.mktemp(suffix=".tgz") + + data = urllib2.urlopen(url) + + print "Downloading data for version %s (%s)..." % (version, full_version) + + with open(temp_file, 'wb') as f: + f.write(data.read()) + print "Uncompressing data for version %s (%s)..." % (version, full_version) + + # Can't use cool with syntax b/c of python 2.6 + tf = tarfile.open(temp_file, 'r:gz') + + try: + tf.extractall(path=temp_dir) + except: + tf.close() + raise + + tf.close() + + extract_dir = os.listdir(temp_dir)[0] + temp_install_dir = os.path.join(temp_dir, extract_dir) + shutil.move(temp_install_dir, self.install_dir) - + shutil.rmtree(temp_dir) os.remove(temp_file) - self.symlink_version(version, os.path.join(self.install_dir, extract_dir)) + self.symlink_version(version, os.path.abspath(os.path.join(self.install_dir, extract_dir))) + - def symlink_version(self, version, installed_dir): try: @@ -108,14 +115,13 @@ class MultiVersionDownloader : if exc.errno == errno.EEXIST and os.path.isdir(self.link_dir): pass else: raise - - for executable in os.listdir(os.path.join(installed_dir, "bin")): - + + for executable in os.listdir(os.path.join(installed_dir, "bin")): + link_name = "%s-%s" % (executable, version) - + os.symlink(os.path.join(installed_dir, "bin", executable),\ os.path.join(self.link_dir, link_name)) - CL_HELP_MESSAGE = \ @@ -132,44 +138,44 @@ version compatible with the version specified. """ def parse_cl_args(args): - + def raise_exception(msg): print CL_HELP_MESSAGE raise Exception(msg) - + if len(args) == 0: raise_exception("Missing INSTALL_DIR") - + install_dir = args[0] - + args = args[1:] if len(args) == 0: raise_exception("Missing LINK_DIR") - + link_dir = args[0] - + args = args[1:] if len(args) == 0: raise_exception("Missing PLATFORM_AND_ARCH") - + platform = args[0] - + args = args[1:] if re.compile(".*\/.*").match(platform) == None: raise_exception("PLATFORM_AND_ARCH isn't of the correct format") - + if len(args) == 0: raise_exception("Missing VERSION1") versions = args - + return (MultiVersionDownloader(install_dir, link_dir, platform), versions) def main(): - + downloader, versions = parse_cl_args(sys.argv[1:]) - + for version in versions: downloader.download_version(version) - - + + if __name__ == '__main__': main() - + -- cgit v1.2.1