1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import sys
18
19 from spacewalk.common.usix import ListType
20
21 from spacewalk.common import rhnFlags
22 from spacewalk.common.rhnLog import log_debug, log_error
23 from spacewalk.common.rhnConfig import CFG
24 from spacewalk.common.rhnException import rhnFault
25 from spacewalk.common.rhnTranslate import _
26 from spacewalk.server import rhnSQL
27 from rhnLib import parseRPMFilename
28
29
30
31
32
33
34
35
37 log_debug(3, server_id, pkg_spec, channel)
38 if isinstance(pkg_spec, ListType):
39 pkg = pkg_spec[:4]
40
41 pkg.insert(1, None)
42 else:
43 pkg = parseRPMFilename(pkg_spec)
44 if pkg is None:
45 log_debug(4, "Error", "Requested weird package", pkg_spec)
46 raise rhnFault(17, _("Invalid RPM package %s requested") % pkg_spec)
47
48 statement = """
49 select p.id, p.path path, pe.epoch epoch
50 from
51 rhnPackageArch pa,
52 rhnChannelPackage cp,
53 rhnPackage p,
54 rhnPackageEVR pe,
55 rhnServerChannel sc,
56 rhnPackageName pn,
57 rhnChannel c
58 where 1=1
59 and c.label = :channel
60 and pn.name = :name
61 and sc.server_id = :server_id
62 and pe.version = :ver
63 and pe.release = :rel
64 and c.id = sc.channel_id
65 and c.id = cp.channel_id
66 and pa.label = :arch
67 and pn.id = p.name_id
68 and p.id = cp.package_id
69 and p.evr_id = pe.id
70 and sc.channel_id = cp.channel_id
71 and p.package_arch_id = pa.id
72 """
73 h = rhnSQL.prepare(statement)
74 pkg = list(map(str, pkg))
75 h.execute(name=pkg[0], ver=pkg[2], rel=pkg[3], arch=pkg[4],
76 channel=channel, server_id=server_id)
77 rs = h.fetchall_dict()
78 if not rs:
79 log_debug(4, "Error", "Non-existant package requested", server_id,
80 pkg_spec, channel)
81 raise rhnFault(17, _("Invalid RPM package %s requested") % pkg_spec)
82
83
84
85
86
87 max_row = rs[0]
88 for each in rs[1:]:
89
90 if _none2emptyString(each['epoch']) > _none2emptyString(max_row['epoch']):
91 max_row = each
92
93
94 rhnFlags.set("Download-Accelerator-Path", max_row['path'])
95 return check_package_file(max_row['path'], max_row['id'], pkg_spec), max_row['id']
96
97
99 if rel_path is None:
100 log_error("Package path null for package id", logpkg)
101 raise rhnFault(17, _("Invalid RPM package %s requested") % raisepkg)
102 filePath = "%s/%s" % (CFG.MOUNT_POINT, rel_path)
103 if not os.access(filePath, os.R_OK):
104
105 log_error("Package not found", filePath)
106 raise rhnFault(17, _("Package not found"))
107
108 return filePath
109
110
112 try:
113 os.unlink(path)
114 except OSError:
115 log_debug(1, "Error unlinking %s;" % path)
116 dirname = os.path.dirname(path)
117 base_dirs = (CFG.MOUNT_POINT + '/' + CFG.PREPENDED_DIR, CFG.MOUNT_POINT)
118 while dirname not in base_dirs:
119 try:
120 os.rmdir(dirname)
121 except OSError:
122 e = sys.exc_info()[1]
123 if e.errno == 39:
124 break
125 else:
126 raise
127 dirname = os.path.dirname(dirname)
128
129
131 """
132 return the remote path if available and localpath
133 for the requested package with respect to package id
134 """
135 log_debug(3, server_id, pkg_spec, channel)
136 remotepath = None
137
138 localpath, pkg_id = get_package_path(server_id, pkg_spec, channel)
139
140 return remotepath, localpath
141
142
143
144
145
147 log_debug(3, server_id, pkgFilename, channel)
148 rs = __query_source_package_path_by_name(server_id, pkgFilename, channel)
149 if rs is None:
150 log_debug(4, "Error", "Non-existant package requested", server_id,
151 pkgFilename, channel)
152 raise rhnFault(17, _("Invalid RPM package %s requested") % pkgFilename)
153
154
155 rhnFlags.set("Download-Accelerator-Path", rs['path'])
156 return check_package_file(rs['path'], pkgFilename, pkgFilename)
157
158
159
166
167
168
170 statement = """
171 select
172 unique ps.path
173 from
174 rhnSourceRPM sr,
175 rhnPackageSource ps,
176 rhnPackage p,
177 rhnChannelPackage cp,
178 rhnChannel c,
179 rhnServerChannel sc
180 where
181 sc.server_id = :server_id
182 and sc.channel_id = cp.channel_id
183 and cp.channel_id = c.id
184 and c.label = :channel
185 and cp.package_id = p.id
186 and p.source_rpm_id = sr.id
187 and sr.name = :name
188 and p.source_rpm_id = ps.source_rpm_id
189 and ((p.org_id is null and ps.org_id is null)
190 or p.org_id = ps.org_id)
191 """
192 h = rhnSQL.prepare(statement)
193 h.execute(name=pkgFilename, channel=channel, server_id=server_id)
194 return h.fetchone_dict()
195
196
198 log_debug(3, pkg)
199 pkg = list(map(str, pkg))
200 params = {'name': pkg[0],
201 'ver': pkg[1],
202 'rel': pkg[2],
203 'epoch': pkg[3],
204 'arch': pkg[4],
205 'channel_id': channel_id,
206 'org_id': org_id}
207
208 if pkg[3] == '0' or pkg[3] == '':
209 epochStatement = "(epoch is null or epoch = :epoch)"
210 else:
211 epochStatement = "epoch = :epoch"
212 if params['org_id']:
213 orgStatement = "org_id = :org_id"
214 else:
215 orgStatement = "org_id is null"
216
217 statement = """
218 select p.path, cp.channel_id,
219 cv.checksum_type, cv.checksum, pe.epoch
220 from rhnPackage p
221 join rhnPackageName pn
222 on p.name_id = pn.id
223 join rhnPackageEVR pe
224 on p.evr_id = pe.id
225 join rhnPackageArch pa
226 on p.package_arch_id = pa.id
227 left join rhnChannelPackage cp
228 on p.id = cp.package_id
229 and cp.channel_id = :channel_id
230 join rhnChecksumView cv
231 on p.checksum_id = cv.id
232 where pn.name = :name
233 and pe.version = :ver
234 and pe.release = :rel
235 and %s
236 and pa.label = :arch
237 and %s
238 order by cp.channel_id nulls last,
239 p.id desc
240 """ % (epochStatement, orgStatement)
241
242 h = rhnSQL.prepare(statement)
243 h.execute(**params)
244
245 ret = h.fetchone_dict()
246 return ret
247
248
250 if foo is None:
251 return ""
252 return str(foo)
253
254 if __name__ == '__main__':
255 """Test code.
256 """
257 from spacewalk.common.rhnLog import initLOG
258 initLOG("stdout", 1)
259 rhnSQL.initDB()
260 print("")
261
262 print(get_package_path(1000463284, 'kernel-2.4.2-2.i686.rpm', 'redhat-linux-i386-7.1'))
263 print(get_source_package_path(1000463284, 'kernel-2.4.2-2.i686.rpm', 'redhat-linux-i386-7.1'))
264