fetch/__init__: Clean up minor mirror code duplication
PATCH 1/6 fetch/__init__: Clean up minor mirror code duplication
The mirror_from_string calls can be in try_mirror along with the debug logging.
--- lib/bb/fetch2/__init__.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-)
lib/bb/fetch2/__init__.py +11 -13
@@ -1183,15 +1183,18 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): if ud.lockfile and ud.lockfile != origud.lockfile: bb.utils.unlockfile(lf) -def try_mirrors(fetch, d, origud, mirrors, check = False): +def try_mirrors(fetch, d, origud, mirrorvar, check = False): """ Try to use a mirrored version of the sources. This method will be automatically called before the fetchers go. d Is a bb.data instance uri is the original uri we're trying to download - mirrors is the list of mirrors we're going to try + mirrorvar is variable name that contains the list of mirrors we're going to try """ + logger.debug("Trying mirrors in %s" % mirrorvar) + mirrors = mirror_from_string(d.getVar(mirrorvar)) + ld = d.createCopy() uris, uds = build_mirroruris(origud, mirrors, ld)
@@ -1716,11 +1719,11 @@ class FetchMethod(object): """ return True - def try_mirrors(self, fetch, urldata, d, mirrors, check=False): + def try_mirrors(self, fetch, urldata, d, mirrorvar, check=False): """ Try to use a mirror """ - return bool(try_mirrors(fetch, d, urldata, mirrors, check)) + return bool(try_mirrors(fetch, d, urldata, mirrorvar, check)) def checkstatus(self, fetch, urldata, d): """
@@ -1921,9 +1924,7 @@ class Fetch(object): if m.verify_donestamp(ud, self.d) and not m.need_update(ud, self.d): done = True elif m.try_premirror(ud, self.d): - logger.debug("Trying PREMIRRORS") - mirrors = mirror_from_string(self.d.getVar('PREMIRRORS')) - done = m.try_mirrors(self, ud, self.d, mirrors) + done = m.try_mirrors(self, ud, self.d, 'PREMIRRORS') if done: try: # early checksum verification so that if the checksum of the premirror
@@ -1975,9 +1976,7 @@ class Fetch(object): # Remove any incomplete fetch if not verified_stamp and m.cleanup_upon_failure(): m.clean(ud, d) - logger.debug("Trying MIRRORS") - mirrors = mirror_from_string(d.getVar('MIRRORS')) - done = m.try_mirrors(self, ud, d, mirrors) + done = m.try_mirrors(self, ud, d, 'MIRRORS') if not done or not m.done(ud, d): if firsterr:
@@ -2027,14 +2026,13 @@ class Fetch(object): logger.debug("Testing URL %s", u) # First try checking uri, u, from PREMIRRORS mirrors = mirror_from_string(self.d.getVar('PREMIRRORS')) - ret = m.try_mirrors(self, ud, self.d, mirrors, True) + ret = m.try_mirrors(self, ud, self.d, 'PREMIRRORS', True) if not ret: # Next try checking from the original uri, u ret = m.checkstatus(self, ud, self.d) if not ret: # Finally, try checking uri, u, from MIRRORS - mirrors = mirror_from_string(self.d.getVar('MIRRORS')) - ret = m.try_mirrors(self, ud, self.d, mirrors, True) + ret = m.try_mirrors(self, ud, self.d, 'MIRRORS', True) if not ret: raise FetchError("URL doesn't work", u)
PATCH 2/6 fetch2: Drop unneeded checksum check
This was added in 022adb30dbb0df764c9fb515918cb9a88e4f8d6f however the checksum exception is now correctly handled in try_mirror_url so this code block is just duplication and not needed anymore.
--- lib/bb/fetch2/__init__.py | 9 --------- 1 file changed, 9 deletions(-)
lib/bb/fetch2/__init__.py +0 -9
@@ -1925,15 +1925,6 @@ class Fetch(object): done = True elif m.try_premirror(ud, self.d): done = m.try_mirrors(self, ud, self.d, 'PREMIRRORS') - if done: - try: - # early checksum verification so that if the checksum of the premirror - # contents mismatch the fetcher can still try upstream and mirrors - m.update_donestamp(ud, self.d) - except ChecksumError as e: - logger.warning("Checksum failure encountered with premirror download of %s - will attempt other sources." % u) - logger.debug(str(e)) - done = False d = self.d if premirroronly:
PATCH 3/6 fetch2: Drop ESTALE error handling
In reply to Richard PurdieThis is old code and nobody I've talked to using NFS has seen this. Translating this into a checksum error probably isn't correct, even if we did want to silently handle it.
I propose we remove this code and see if anyone des really need something like this, if so, writing it correctly without the ChecksumError usage.
--- lib/bb/fetch2/__init__.py | 12 ------------ 1 file changed, 12 deletions(-)
lib/bb/fetch2/__init__.py +0 -12
@@ -1156,12 +1156,6 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): except bb.fetch2.NetworkAccess: raise - except IOError as e: - if e.errno in [errno.ESTALE]: - logger.warning("Stale Error Observed %s." % ud.url) - return False - raise - except bb.fetch2.BBFetchException as e: if isinstance(e, ChecksumError): logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url))
@@ -1976,12 +1970,6 @@ class Fetch(object): m.update_donestamp(ud, d) - except IOError as e: - if e.errno in [errno.ESTALE]: - logger.error("Stale Error Observed %s." % u) - raise ChecksumError("Stale Error Detected") - raise - except BBFetchException as e: if isinstance(e, NoChecksumError): (message, _) = e.args
PATCH 4/6 utils: Allow lockfile/unlockfile to take empty names
In reply to Richard PurdieI've been torn on whether to allow this for a long time. The context manager does allow it and it does allow code simplification if it can just accept no locks are present so on balance, it is probably slightly neater.
--- lib/bb/utils.py | 6 ++++++ 1 file changed, 6 insertions(+)
lib/bb/utils.py +6 -0
@@ -529,6 +529,9 @@ def lockfile(name, shared=False, retry=True, block=False): Returns the locked file descriptor in case of success, ``None`` otherwise. """ + if not name: + return None + basename = os.path.basename(name) if len(basename) > 255: root, ext = os.path.splitext(basename)
@@ -594,6 +597,9 @@ def unlockfile(lf): No return value. """ + if not lf: + return + try: # If we had a shared lock, we need to promote to exclusive before # removing the lockfile. Attempt this, ignore failures.
PATCH 5/6 utils: Add lockfile_to_exclusive
In reply to Richard PurdieAdd a function which turns a shared lock into an exclusive lock. This is useful for planned changes to the fetcher code.
--- lib/bb/utils.py | 7 +++++++ 1 file changed, 7 insertions(+)
lib/bb/utils.py +7 -0
@@ -587,6 +587,13 @@ def lockfile(name, shared=False, retry=True, block=False): if not retry: return None +def lockfile_to_exclusive(lf): + if not lf: + return + fileno = lf.fileno() + fcntl.flock(fileno, fcntl.LOCK_EX) + return + def unlockfile(lf): """ Unlock a file locked using ``bb.utils.lockfile()``.
PATCH 6/6 fetch2: Switch to shared locking for read accesses
In reply to Richard PurdieInstead of always using an exclusive lock, use a shared lock for unpack and the initial checks of download. This should mean that if we don't need to make any writes to the download directory, we only ever need the shared lock. As soon as any writes are possible, switch to the full lock.
Also take advanatge of being able to pass None to the lock functions.
--- lib/bb/fetch2/__init__.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-)
lib/bb/fetch2/__init__.py +13 -16
@@ -1097,7 +1097,8 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): # False means try another url try: - if ud.lockfile and ud.lockfile != origud.lockfile: + lf = None + if ud.lockfile != origud.lockfile: lf = bb.utils.lockfile(ud.lockfile) if check:
@@ -1174,7 +1175,7 @@ def try_mirror_url(fetch, origud, ud, ld, check = False): pass return False finally: - if ud.lockfile and ud.lockfile != origud.lockfile: + if ud.lockfile != origud.lockfile: bb.utils.unlockfile(lf) def try_mirrors(fetch, d, origud, mirrorvar, check = False):
@@ -1911,14 +1912,15 @@ class Fetch(object): done = False try: - if ud.lockfile: - lf = bb.utils.lockfile(ud.lockfile) + lf = bb.utils.lockfile(ud.lockfile, shared=True) self.d.setVar("BB_NO_NETWORK", network) if m.verify_donestamp(ud, self.d) and not m.need_update(ud, self.d): done = True - elif m.try_premirror(ud, self.d): - done = m.try_mirrors(self, ud, self.d, 'PREMIRRORS') + if not done: + bb.utils.lockfile_to_exclusive(lf) + if m.try_premirror(ud, self.d): + done = m.try_mirrors(self, ud, self.d, 'PREMIRRORS') d = self.d if premirroronly:
@@ -1980,8 +1982,7 @@ class Fetch(object): raise finally: - if ud.lockfile: - bb.utils.unlockfile(lf) + bb.utils.unlockfile(lf) if checksum_missing_messages: logger.error("Missing SRC_URI checksum, please add those to the recipe: \n%s", "\n".join(checksum_missing_messages)) raise BBFetchException("There was some missing checksums in the recipe")
@@ -2031,8 +2032,7 @@ class Fetch(object): ud = self.ud[u] ud.setup_localpath(self.d) - if ud.lockfile: - lf = bb.utils.lockfile(ud.lockfile) + lf = bb.utils.lockfile(ud.lockfile, shared=True) unpack_tracer.start_url(u) if update:
@@ -2042,8 +2042,7 @@ class Fetch(object): unpack_tracer.finish_url(u) finally: - if ud.lockfile: - bb.utils.unlockfile(lf) + bb.utils.unlockfile(lf) unpack_tracer.complete()
@@ -2068,16 +2067,14 @@ class Fetch(object): if not ud.localfile and ud.localpath is None: continue - if ud.lockfile: - lf = bb.utils.lockfile(ud.lockfile) + lf = bb.utils.lockfile(ud.lockfile) ud.method.clean(ud, self.d) if ud.donestamp: bb.utils.remove(ud.donestamp) finally: - if ud.lockfile: - bb.utils.unlockfile(lf) + bb.utils.unlockfile(lf) def expanded_urldata(self, urls=None): """