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 @staticmethod
23 - def name():
24 return "binhost"
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 if errors: 90 return (False, errors) 91 return (True, None)
92
93 - def fix(self, **kwargs):
94 onProgress = kwargs.get('onProgress', None) 95 bintree = self._bintree 96 _instance_key = bintree.dbapi._instance_key 97 cpv_all = self._bintree.dbapi.cpv_all() 98 cpv_all.sort() 99 missing = [] 100 maxval = 0 101 if onProgress: 102 onProgress(maxval, 0) 103 pkgindex = self._pkgindex 104 missing = [] 105 stale = [] 106 metadata = {} 107 for d in pkgindex.packages: 108 cpv = _pkg_str(d["CPV"], metadata=d, 109 settings=bintree.settings) 110 d["CPV"] = cpv 111 metadata[_instance_key(cpv)] = d 112 if not bintree.dbapi.cpv_exists(cpv): 113 stale.append(cpv) 114 115 for cpv in cpv_all: 116 d = metadata.get(_instance_key(cpv)) 117 if not d or self._need_update(cpv, d): 118 missing.append(cpv) 119 120 if missing or stale: 121 from portage import locks 122 pkgindex_lock = locks.lockfile( 123 self._pkgindex_file, wantnewlockfile=1) 124 try: 125 # Repopulate with lock held. 126 bintree._populate() 127 cpv_all = self._bintree.dbapi.cpv_all() 128 cpv_all.sort() 129 130 pkgindex = bintree._load_pkgindex() 131 self._pkgindex = pkgindex 132 133 # Recount stale/missing packages, with lock held. 134 missing = [] 135 stale = [] 136 metadata = {} 137 for d in pkgindex.packages: 138 cpv = _pkg_str(d["CPV"], metadata=d, 139 settings=bintree.settings) 140 d["CPV"] = cpv 141 metadata[_instance_key(cpv)] = d 142 if not bintree.dbapi.cpv_exists(cpv): 143 stale.append(cpv) 144 145 for cpv in cpv_all: 146 d = metadata.get(_instance_key(cpv)) 147 if not d or self._need_update(cpv, d): 148 missing.append(cpv) 149 150 maxval = len(missing) 151 for i, cpv in enumerate(missing): 152 d = bintree._pkgindex_entry(cpv) 153 try: 154 bintree._eval_use_flags(cpv, d) 155 except portage.exception.InvalidDependString: 156 writemsg("!!! Invalid binary package: '%s'\n" % \ 157 bintree.getname(cpv), noiselevel=-1) 158 else: 159 metadata[_instance_key(cpv)] = d 160 161 if onProgress: 162 onProgress(maxval, i+1) 163 164 for cpv in stale: 165 del metadata[_instance_key(cpv)] 166 167 # We've updated the pkgindex, so set it to 168 # repopulate when necessary. 169 bintree.populated = False 170 171 del pkgindex.packages[:] 172 pkgindex.packages.extend(metadata.values()) 173 bintree._pkgindex_write(self._pkgindex) 174 175 finally: 176 locks.unlockfile(pkgindex_lock) 177 178 if onProgress: 179 if maxval == 0: 180 maxval = 1 181 onProgress(maxval, maxval) 182 return (True, None)
183