Package portage :: Package emaint :: Package modules :: Package binhost :: Module binhost
[hide private]

Source Code for Module portage.emaint.modules.binhost.binhost

  1  # Copyright 2005-2014 Gentoo Foundation 
  2  # Distributed under the terms of the GNU General Public License v2 
  3   
  4  import errno 
  5  import stat 
  6   
  7  import portage 
  8  from portage import os 
  9  from portage.util import writemsg 
 10  from portage.versions import _pkg_str 
 11   
 12  import sys 
 13   
 14  if sys.hexversion >= 0x3000000: 
 15          # pylint: disable=W0622 
 16          long = int 
 17   
18 -class BinhostHandler(object):
19 20 short_desc = "Generate a metadata index for binary packages" 21
22 - def name():
23 return "binhost"
24 name = staticmethod(name) 25
26 - def __init__(self):
27 eroot = portage.settings['EROOT'] 28 self._bintree = portage.db[eroot]["bintree"] 29 self._bintree.populate() 30 self._pkgindex_file = self._bintree._pkgindex_file 31 self._pkgindex = self._bintree._load_pkgindex()
32
33 - def _need_update(self, cpv, data):
34 35 if "MD5" not in data: 36 return True 37 38 size = data.get("SIZE") 39 if size is None: 40 return True 41 42 mtime = data.get("_mtime_") 43 if mtime is None: 44 return True 45 46 pkg_path = self._bintree.getname(cpv) 47 try: 48 s = os.lstat(pkg_path) 49 except OSError as e: 50 if e.errno not in (errno.ENOENT, errno.ESTALE): 51 raise 52 # We can't update the index for this one because 53 # it disappeared. 54 return False 55 56 try: 57 if long(mtime) != s[stat.ST_MTIME]: 58 return True 59 if long(size) != long(s.st_size): 60 return True 61 except ValueError: 62 return True 63 64 return False
65
66 - def check(self, **kwargs):
67 onProgress = kwargs.get('onProgress', None) 68 missing = [] 69 cpv_all = self._bintree.dbapi.cpv_all() 70 cpv_all.sort() 71 maxval = len(cpv_all) 72 if onProgress: 73 onProgress(maxval, 0) 74 pkgindex = self._pkgindex 75 missing = [] 76 metadata = {} 77 for d in pkgindex.packages: 78 metadata[d["CPV"]] = d 79 for i, cpv in enumerate(cpv_all): 80 d = metadata.get(cpv) 81 if not d or self._need_update(cpv, d): 82 missing.append(cpv) 83 if onProgress: 84 onProgress(maxval, i+1) 85 errors = ["'%s' is not in Packages" % cpv for cpv in missing] 86 stale = set(metadata).difference(cpv_all) 87 for cpv in stale: 88 errors.append("'%s' is not in the repository" % cpv) 89 return errors
90
91 - def fix(self, **kwargs):
92 onProgress = kwargs.get('onProgress', None) 93 bintree = self._bintree 94 _instance_key = bintree.dbapi._instance_key 95 cpv_all = self._bintree.dbapi.cpv_all() 96 cpv_all.sort() 97 missing = [] 98 maxval = 0 99 if onProgress: 100 onProgress(maxval, 0) 101 pkgindex = self._pkgindex 102 missing = [] 103 stale = [] 104 metadata = {} 105 for d in pkgindex.packages: 106 cpv = _pkg_str(d["CPV"], metadata=d, 107 settings=bintree.settings) 108 d["CPV"] = cpv 109 metadata[_instance_key(cpv)] = d 110 if not bintree.dbapi.cpv_exists(cpv): 111 stale.append(cpv) 112 113 for cpv in cpv_all: 114 d = metadata.get(_instance_key(cpv)) 115 if not d or self._need_update(cpv, d): 116 missing.append(cpv) 117 118 if missing or stale: 119 from portage import locks 120 pkgindex_lock = locks.lockfile( 121 self._pkgindex_file, wantnewlockfile=1) 122 try: 123 # Repopulate with lock held. 124 bintree._populate() 125 cpv_all = self._bintree.dbapi.cpv_all() 126 cpv_all.sort() 127 128 pkgindex = bintree._load_pkgindex() 129 self._pkgindex = pkgindex 130 131 # Recount stale/missing packages, with lock held. 132 missing = [] 133 stale = [] 134 metadata = {} 135 for d in pkgindex.packages: 136 cpv = _pkg_str(d["CPV"], metadata=d, 137 settings=bintree.settings) 138 d["CPV"] = cpv 139 metadata[_instance_key(cpv)] = d 140 if not bintree.dbapi.cpv_exists(cpv): 141 stale.append(cpv) 142 143 for cpv in cpv_all: 144 d = metadata.get(_instance_key(cpv)) 145 if not d or self._need_update(cpv, d): 146 missing.append(cpv) 147 148 maxval = len(missing) 149 for i, cpv in enumerate(missing): 150 d = bintree._pkgindex_entry(cpv) 151 try: 152 bintree._eval_use_flags(cpv, d) 153 except portage.exception.InvalidDependString: 154 writemsg("!!! Invalid binary package: '%s'\n" % \ 155 bintree.getname(cpv), noiselevel=-1) 156 else: 157 metadata[_instance_key(cpv)] = d 158 159 if onProgress: 160 onProgress(maxval, i+1) 161 162 for cpv in stale: 163 del metadata[_instance_key(cpv)] 164 165 # We've updated the pkgindex, so set it to 166 # repopulate when necessary. 167 bintree.populated = False 168 169 del pkgindex.packages[:] 170 pkgindex.packages.extend(metadata.values()) 171 bintree._pkgindex_write(self._pkgindex) 172 173 finally: 174 locks.unlockfile(pkgindex_lock) 175 176 if onProgress: 177 if maxval == 0: 178 maxval = 1 179 onProgress(maxval, maxval) 180 return None
181