From: Michael Tremer Date: Mon, 16 Nov 2009 20:57:43 +0000 (+0100) Subject: naoki: Rewrite of downloader. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=889b82e7350bc4ac089a3e222b77c5de513cf4a1;p=ipfire-3.x.git naoki: Rewrite of downloader. This is just to handle more than one file at once. Has to be reworked soon. --- diff --git a/tools/downloader b/tools/downloader index 866f7a363..5f29230e3 100755 --- a/tools/downloader +++ b/tools/downloader @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # @@ -25,128 +25,33 @@ # # ############################################################################### -import os -import sys - -hashlib = 0 -try: - import sha -except ImportError: - from hashlib import sha1 - hashlib = 1 - -import shutil - -from urlgrabber.grabber import URLGrabber, URLGrabError -import urlgrabber.progress as progress - -############################################################################### -# Sample header # -############################################################################### -#X-Object: FileObject -#X-MD5: d79f553e7916ea21c556329eacfeaa16 -#X-SHA1: bb20efc7750fe0d6172c5945572bf036fe59d3dd - -############################################################################### -# Environment # -############################################################################### - -BSIZE = 1024 #1k -gobj = None - -try: - DIR_DL = os.environ["DIR_DL"] or os.getcwd() - DIR_PATCHES = os.environ["DIR_PATCHES"] or os.getcwd() - DIR_TMP = os.environ["DIR_TMP"] or None -except KeyError: - sys.stderr.write("DIR_TMP, DIR_DL and DIR_PATCHES have to be set " - "in environment.\n") - sys.exit(1) - -url = sys.argv[1] -filename = os.path.basename(url) - -#if os.access(os.path.join(DIR_PATCHES), filename), R_OK) or -# os.access(os.path.join(DIR_DL), filename), R_OK): -# sys.stdout.write("Already downloaded: %s\n" % (filename,)) -# sys.exit(0) - -#sys.stdout.write("Downloading %s\n" % (url,)) - -############################################################################### -# Create urlgrabber instance # -############################################################################### -g = URLGrabber( user_agent = "IPFireSourceGrabber/3.x", - progress_obj = progress.TextMeter(fo=sys.stdout) ) -try: - gobj = g.urlopen(url) -except URLGrabError, e: - sys.stdout.write("%s: %s" % (filename, e)) - sys.stderr.write("Error: %s\n" % e) - sys.exit(1) - -if not gobj: - sys.stderr.write("Unknown happended: %s\n" % (url,)) - sys.exit(1) - -# Init hash -if hashlib: - hobj = sha1() -else: - hobj = sha.new() - -############################################################################### -# Parse header # -############################################################################### -try: - object_type = gobj.hdr["X-Object"] - - #hash_md5 = gobj.hdr["X-MD5"] - hash_sha1 = gobj.hdr["X-SHA1"] - - hash = hash_sha1 -except KeyError: - sys.stderr.write("No header data found. Can't check file.\n") - #for line in gobj.hdr.items(): sys.stderr.write(" %s\n" % (line,)) - sys.exit(1) - -############################################################################### -# Setup fileobjects # -############################################################################### -if object_type == "FileObject": - dest_file = os.path.join(DIR_DL,filename) -elif object_type == "PatchObject": - dest_file = os.path.join(DIR_PATCHES,filename) - -if DIR_TMP: - temp_file = os.path.join(DIR_TMP, filename) -else: - temp_file = dest_file - -fobj = open(temp_file, "wb") - -############################################################################### -# Download # -############################################################################### -buf = gobj.read(BSIZE) -while len(buf) > 0: - hobj.update(buf) - fobj.write(buf) - buf = gobj.read(BSIZE) - -gobj.close() # Close connection to server. -fobj.close() # Also close local file. - -############################################################################### -# Check hash # -############################################################################### -if hash == hobj.hexdigest(): - shutil.move(temp_file, dest_file) -else: - os.unlink(temp_file) - sys.stderr.write("Hash mismatch: %s\n" % (filename,)) - sys.exit(1) - -sys.exit(0) - -############################################################################### +URL="http://source.ipfire.org/source-3.x" + +objects= + +while [ $# -gt 0 ]; do + case "${1}" in + --tarballs=*) + DIR_TARBALLS=${1#--tarballs=} + ;; + --patches=*) + DIR_PATCHES=${1#--patches=} + ;; + *) + objects="${objects} ${1}" + ;; + esac + shift +done + +for object in ${objects}; do + echo object: $object + target= + if [[ ${object} =~ .patch$ ]]; then + target="${DIR_PATCHES}/${object}" + else + target="${DIR_TARBALLS}/${object}" + fi + + wget ${URL}/${object} -O ${target} +done