Package backend :: Package server :: Module rhnPackage
[hide private]
[frames] | no frames]

Source Code for Module backend.server.rhnPackage

  1  # 
  2  # Copyright (c) 2008--2016 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU General Public License, 
  5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
  6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
  7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
  8  # along with this software; if not, see 
  9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
 10  # 
 11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
 12  # granted to use or replicate Red Hat trademarks that are incorporated 
 13  # in this software or its documentation. 
 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  # Functions that deal with the database 
 32  # 
 33   
 34  # New client 
 35  # Returns a package path, given a server_id, package filename and channel label 
36 -def get_package_path(server_id, pkg_spec, channel):
37 log_debug(3, server_id, pkg_spec, channel) 38 if isinstance(pkg_spec, ListType): 39 pkg = pkg_spec[:4] 40 # Insert EPOCH 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 # It is unlikely for this query to return more than one row, 83 # but it is possible 84 # (having two packages with the same n, v, r, a and different epoch in 85 # the same channel is prohibited by the RPM naming scheme; but extra 86 # care won't hurt) 87 max_row = rs[0] 88 for each in rs[1:]: 89 # Compare the epoch as string 90 if _none2emptyString(each['epoch']) > _none2emptyString(max_row['epoch']): 91 max_row = each 92 93 # Set the flag for the proxy download accelerator 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
98 -def check_package_file(rel_path, logpkg, raisepkg):
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 # Package not found on the filesystem 105 log_error("Package not found", filePath) 106 raise rhnFault(17, _("Package not found")) 107 108 return filePath
109 110 128 129
130 -def get_all_package_paths(server_id, pkg_spec, channel):
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 # get the path and package 138 localpath, pkg_id = get_package_path(server_id, pkg_spec, channel) 139 140 return remotepath, localpath
141 142 # New client 143 # Returns the path to a source rpm 144 145
146 -def get_source_package_path(server_id, pkgFilename, channel):
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 # Set the flag for the proxy download accelerator 155 rhnFlags.set("Download-Accelerator-Path", rs['path']) 156 return check_package_file(rs['path'], pkgFilename, pkgFilename)
157 158 159 # 0 or 1: is this source in this channel?
160 -def package_source_in_channel(server_id, pkgFilename, channel):
161 log_debug(3, server_id, pkgFilename, channel) 162 rs = __query_source_package_path_by_name(server_id, pkgFilename, channel) 163 if rs is None: 164 return 0 165 return 1
166 167 168 # The query used both in get_source_package_path and package_source_in_channel
169 -def __query_source_package_path_by_name(server_id, pkgFilename, channel):
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
197 -def get_info_for_package(pkg, channel_id, org_id):
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 # yum repo has epoch="0" not only when epoch is "0" but also if it's NULL 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
249 -def _none2emptyString(foo):
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 # new client 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