]>
git.ipfire.org Git - ipfire-3.x.git/blob - tools/downloader
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
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 #
26 ###############################################################################
35 from hashlib
import sha1
40 from urlgrabber
.grabber
import URLGrabber
, URLGrabError
41 import urlgrabber
.progress
as progress
43 ###############################################################################
45 ###############################################################################
47 #X-MD5: d79f553e7916ea21c556329eacfeaa16
48 #X-SHA1: bb20efc7750fe0d6172c5945572bf036fe59d3dd
50 ###############################################################################
52 ###############################################################################
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
62 sys
.stderr
.write("DIR_TMP, DIR_DL and DIR_PATCHES have to be set "
67 filename
= os
.path
.basename(url
)
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,))
74 #sys.stdout.write("Downloading %s\n" % (url,))
76 ###############################################################################
77 # Create urlgrabber instance #
78 ###############################################################################
79 g
= URLGrabber( user_agent
= "IPFireSourceGrabber/3.x",
80 progress_obj
= progress
.TextMeter(fo
=sys
.stdout
) )
83 except URLGrabError
, e
:
84 sys
.stdout
.write("%s: %s" % (filename
, e
))
85 sys
.stderr
.write("Error: %s\n" % e
)
89 sys
.stderr
.write("Unknown happended: %s\n" % (url
,))
98 ###############################################################################
100 ###############################################################################
102 object_type
= gobj
.hdr
["X-Object"]
104 #hash_md5 = gobj.hdr["X-MD5"]
105 hash_sha1
= gobj
.hdr
["X-SHA1"]
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,))
113 ###############################################################################
114 # Setup fileobjects #
115 ###############################################################################
116 if object_type
== "FileObject":
117 dest_file
= os
.path
.join(DIR_DL
,filename
)
118 elif object_type
== "PatchObject":
119 dest_file
= os
.path
.join(DIR_PATCHES
,filename
)
122 temp_file
= os
.path
.join(DIR_TMP
, filename
)
124 temp_file
= dest_file
126 fobj
= open(temp_file
, "wb")
128 ###############################################################################
130 ###############################################################################
131 buf
= gobj
.read(BSIZE
)
135 buf
= gobj
.read(BSIZE
)
137 gobj
.close() # Close connection to server.
138 fobj
.close() # Also close local file.
140 ###############################################################################
142 ###############################################################################
143 if hash == hobj
.hexdigest():
144 shutil
.move(temp_file
, dest_file
)
147 sys
.stderr
.write("Hash mismatch: %s\n" % (filename
,))
152 ###############################################################################