]> git.ipfire.org Git - ipfire-3.x.git/blame - tools/downloader
Added new package: dosfstools.
[ipfire-3.x.git] / tools / downloader
CommitLineData
fa0dac0f 1#!/usr/bin/python
bdb1a79b
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
fa0dac0f 5# Copyright (C) 2008 Michael Tremer & Christian Schmidt #
bdb1a79b
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21# #
22# This is a very simple script for downloading our files from the server. #
23# It is keep that simple because we plan to rewrite this anytime or move #
24# away from wget. #
25# #
26###############################################################################
27
fa0dac0f
MT
28import os
29import sys
30
6e043462
MT
31hashlib = 0
32try:
33 import sha
34except ImportError:
35 from hashlib import sha1
36 hashlib = 1
37
fa0dac0f
MT
38import shutil
39
40from urlgrabber.grabber import URLGrabber, URLGrabError
41import urlgrabber.progress as progress
42
43###############################################################################
44# Sample header #
45###############################################################################
46#X-Object: FileObject
47#X-MD5: d79f553e7916ea21c556329eacfeaa16
48#X-SHA1: bb20efc7750fe0d6172c5945572bf036fe59d3dd
49
50###############################################################################
51# Environment #
52###############################################################################
53
54BSIZE = 1024 #1k
55gobj = None
56
57try:
58 DIR_DL = os.environ["DIR_DL"] or os.getcwd()
59 DIR_PATCHES = os.environ["DIR_PATCHES"] or os.getcwd()
60 DIR_TMP = os.environ["DIR_TMP"] or None
61except KeyError:
62 sys.stderr.write("DIR_TMP, DIR_DL and DIR_PATCHES have to be set "
63 "in environment.\n")
64 sys.exit(1)
65
66url = sys.argv[1]
67filename = os.path.basename(url)
68
69#if os.access(os.path.join(DIR_PATCHES), filename), R_OK) or
70# os.access(os.path.join(DIR_DL), filename), R_OK):
71# sys.stdout.write("Already downloaded: %s\n" % (filename,))
72# sys.exit(0)
73
74#sys.stdout.write("Downloading %s\n" % (url,))
75
76###############################################################################
77# Create urlgrabber instance #
78###############################################################################
79g = URLGrabber( user_agent = "IPFireSourceGrabber/3.x",
80 progress_obj = progress.TextMeter(fo=sys.stdout) )
81try:
82 gobj = g.urlopen(url)
83except URLGrabError, e:
da1befdd
MT
84 sys.stdout.write("%s: %s" % (filename, e))
85 sys.stderr.write("Error: %s\n" % e)
86 sys.exit(1)
fa0dac0f
MT
87
88if not gobj:
89 sys.stderr.write("Unknown happended: %s\n" % (url,))
90 sys.exit(1)
91
92# Init hash
6e043462
MT
93if hashlib:
94 hobj = sha1()
95else:
96 hobj = sha.new()
fa0dac0f
MT
97
98###############################################################################
99# Parse header #
100###############################################################################
101try:
102 object_type = gobj.hdr["X-Object"]
103
104 #hash_md5 = gobj.hdr["X-MD5"]
105 hash_sha1 = gobj.hdr["X-SHA1"]
106
107 hash = hash_sha1
108except KeyError:
109 sys.stderr.write("No header data found. Can't check file.\n")
110 #for line in gobj.hdr.items(): sys.stderr.write(" %s\n" % (line,))
111 sys.exit(1)
112
113###############################################################################
114# Setup fileobjects #
115###############################################################################
116if object_type == "FileObject":
117 dest_file = os.path.join(DIR_DL,filename)
118elif object_type == "PatchObject":
119 dest_file = os.path.join(DIR_PATCHES,filename)
120
121if DIR_TMP:
122 temp_file = os.path.join(DIR_TMP, filename)
123else:
124 temp_file = dest_file
125
126fobj = open(temp_file, "wb")
127
128###############################################################################
129# Download #
130###############################################################################
131buf = gobj.read(BSIZE)
132while len(buf) > 0:
133 hobj.update(buf)
134 fobj.write(buf)
135 buf = gobj.read(BSIZE)
136
137gobj.close() # Close connection to server.
138fobj.close() # Also close local file.
139
140###############################################################################
141# Check hash #
142###############################################################################
143if hash == hobj.hexdigest():
144 shutil.move(temp_file, dest_file)
145else:
146 os.unlink(temp_file)
147 sys.stderr.write("Hash mismatch: %s\n" % (filename,))
148 sys.exit(1)
149
150sys.exit(0)
151
152###############################################################################