summaryrefslogtreecommitdiffstats
path: root/urlinstall.py
blob: a3cfa57c9e8e9ecd46e0995a598c9513bb62d065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#
# urlinstall.py - URL based install source method
#
# Erik Troan <ewt@redhat.com>
#
# Copyright 1999-2002 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

from comps import ComponentSet, HeaderList
from installmethod import InstallMethod, FileCopyException
import os
import rpm
import time
import urllib2
import string
import struct
import socket

# we import these explicitly because urllib loads them dynamically, which
# stinks -- and we need to have them imported for the --traceonly option
import ftplib
import httplib
import StringIO

from rhpl.log import log

FILENAME = 1000000
DISCNUM  = 1000002

def urlretrieve(location, file):
    """Downloads from location and saves to file."""

    try:
        url = urllib2.urlopen(location)
    except urllib2.HTTPError, e:
        raise IOError(e.code, e.msg)
    except urllib2.URLError, e:
	raise IOError(-1, e.reason)
    else:
	# sanity check result - sometimes FTP doesnt
	# catch a file is missing
	try:
	    clen = url.info()['content-length']
	except Exception, e:
            log("exception %s getting content-length" %(e,))
	    clen = 0

	if clen < 1:
	    raise IOError(-1, "File not found")
	
    f = open(file, 'w+')
    f.write(url.read())
    f.close()
    url.close()
    

class UrlInstallMethod(InstallMethod):
    def readCompsViaMethod(self, hdlist):
	fname = self.findBestFileMatch(None, 'comps.xml')
	# if not local then assume its on host
	if fname is None:
	    fname = self.baseUrl + '/RedHat/base/comps.xml'
	    log("Comps not in update dirs, using %s",fname)
	return ComponentSet(fname, hdlist)

    def getFilename(self, h, timer):
        tmppath = self.getTempPath()
        
	# h doubles as a filename -- gross
	if type("/") == type(h):
	    fullPath = self.baseUrl + "/" + h
	else:
	    if self.multiDiscs:
		base = "%s/disc%d" % (self.pkgUrl, h[DISCNUM])
	    else:
		base = self.pkgUrl

	    fullPath = base + "/RedHat/RPMS/" + h[FILENAME]

	file = tmppath + os.path.basename(fullPath)

        tries = 0
        while tries < 5:
            try:
                urlretrieve(fullPath, file)
            except IOError, (errnum, msg):
		log("IOError %s occurred getting %s: %s"
                    %(errnum, fullPath, str(msg)))
                time.sleep(5)
            else:
                break
            tries = tries + 1

        if tries >= 5:
            raise FileCopyException
                
	return file

    def copyFileToTemp(self, filename):
        tmppath = self.getTempPath()

        if self.multiDiscs:
            base = "%s/disc1" % (self.pkgUrl,)
        else:
            base = self.pkgUrl
	    
        fullPath = base + "/" + filename

        file = tmppath + "/" + os.path.basename(fullPath)

        connected = 0
        while not connected:
            try:
                urlretrieve(fullPath, file)
            except IOError, (errnum, msg):
		log("IOError %s occurred getting %s: %s",
			errnum, fullPath, str(msg))
                time.sleep(5)
            else:
                connected = 1

	return file

    def unlinkFilename(self, fullName):
	os.remove(fullName)

    def readHeaders(self):
        tries = 0

        while tries < 5:
	    hdurl = self.baseUrl + "/RedHat/base/hdlist"
            try:
                url = urllib2.urlopen(hdurl)
	    except urllib2.HTTPError, e:
		log("HTTPError: %s occurred getting %s", hdurl, e)
	    except urllib2.URLError, e:
		log("URLError: %s occurred getting %s", hdurl, e)
            except IOError, (errnum, msg):
		log("IOError %s occurred getting %s: %s",
			errnum, hdurl, msg)
	    else:
		# sanity check result - sometimes FTP doesnt
		# catch a file is missing
		try:
		    clen = url.info()['content-length']
		except Exception, e:
		    log("readHeaders(): exception %s getting content-length" %(e,))
		    clen = 0

		if clen < 1:
		    log("File %s not found.", hdurl)
		else:
		    break

	    time.sleep(5)
            tries = tries + 1

        if tries >= 5:
            raise FileCopyException
                
	raw = url.read(16)
	if raw is None or len(raw) < 1:
	    raise TypeError, "header list is empty!"
	
	hl = []
	while (raw and len(raw)>0):
	    info = struct.unpack("iiii", raw)
	    magic1 = socket.ntohl(info[0]) & 0xffffffff
	    if (magic1 != 0x8eade801 or info[1]):
		raise TypeError, "bad magic in header"

	    il = socket.ntohl(info[2])
	    dl = socket.ntohl(info[3])
	    totalSize = il * 16 + dl;
	    hdrString = raw[8:] + url.read(totalSize)
	    hdr = rpm.headerLoad(hdrString)
	    hl.append(hdr)

	    raw = url.read(16)

	return HeaderList(hl)

    def mergeFullHeaders(self, hdlist):
	fn = self.getFilename("RedHat/base/hdlist2", None)
	hdlist.mergeFullHeaders(fn)
	os.unlink(fn)

    def __init__(self, url, rootPath):
	InstallMethod.__init__(self, rootPath)

	i = string.index(url, '://') + 2
	self.baseUrl = url[0:i]
	rem = url[i:]
	new = string.replace(rem, "//", "/")
	while (new != rem):
	    rem = new
	    new = string.replace(rem, "//", "/")
	rem = new
        if rem and rem[-1] == "/":
            rem = rem[:-1]
	self.baseUrl = self.baseUrl + rem

	# self.baseUrl points at the path which contains the 'RedHat'
	# directory with the hdlist.

	if self.baseUrl[-6:] == "/disc1":
	    self.multiDiscs = 1
	    self.pkgUrl = self.baseUrl[:-6]
	else:
	    self.multiDiscs = 0
	    self.pkgUrl = self.baseUrl