fetch2/git: Check fullshallow tarball and clonedir before access
cover fetch2/git: Check fullshallow tarball and clonedir before access
Add checks if a source exists before unpacking it. Throw an understandable error when the source does not exist.
This is a follow up error that happens in some cases with mirror rewrite rules. We only fix the follow up error with this patches. The fix for the root of the error will follow.
Urs Fässler (7):
fetch2/git: prevent access to non-existing fullshallow tarball fetch2/git: improve readability of method need_update fetch2/git: use correct check to decide if the shallow tarball should
be unpacked
tests/data: extract LogRecord into own file
fetch2/git: prevent access to non-existing clonedir fetch2/git: explicitly show the decision logic to select the source in
code
fetch2/git: provide information about missing sources
lib/bb/fetch2/git.py | 39 +++++++++++++++++++++++------- lib/bb/tests/data.py | 25 +------------------ lib/bb/tests/fetch.py | 29 ++++++++++++++++++++++ lib/bb/tests/logrecord.py | 51 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 33 deletions(-) create mode 100644 lib/bb/tests/logrecord.py
Signature · 1 line
2.19.0
PATCH 1/7 fetch2/git: prevent access to non-existing fullshallow tarball
--- lib/bb/fetch2/git.py | 2 +- lib/bb/tests/fetch.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
lib/bb/fetch2/git.py +1 -1
@@ -471,7 +471,7 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - if ud.shallow and self.need_update(ud, d): + if ud.shallow and os.path.exists(ud.fullshallow) and self.need_update(ud, d): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) else:
lib/bb/tests/fetch.py +14 -0
@@ -1580,3 +1580,17 @@ class GitShallowTest(FetcherTest): self.assertNotEqual(orig_revs, revs) self.assertRefs(['master', 'origin/master']) self.assertRevCount(orig_revs - 1758) + + @skipIfNoNetwork() + def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self): + self.d.setVar('SRCREV', 'e5939ff608b95cdd4d0ab0e1935781ab9a276ac0') + self.d.setVar('BB_GIT_SHALLOW', '1') + self.d.setVar('BB_GENERATE_SHALLOW_TARBALLS', '1') + fetcher = bb.fetch.Fetch(["git://git.yoctoproject.org/fstests"], self.d) + fetcher.download() + + bb.utils.remove(self.dldir + "/*.tar.gz") + fetcher.unpack(self.unpackdir) + + dir = os.listdir(self.unpackdir + "/git/") + self.assertIn("fstests.doap", dir)
Signature · 1 line
2.19.0
PATCH 2/7 fetch2/git: improve readability of method need_update
In reply to Urs FässlerTo improve the readability we extract the different scenarios of why the clonedir needs an update.
--- lib/bb/fetch2/git.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
lib/bb/fetch2/git.py +9 -4
@@ -299,17 +299,22 @@ class Git(FetchMethod): return ud.clonedir def need_update(self, ud, d): + return self.clonedir_need_update(ud, d) or self.shallow_tarball_need_update(ud) or self.tarball_need_update(ud) + + def clonedir_need_update(self, ud, d): if not os.path.exists(ud.clonedir): return True for name in ud.names: if not self._contains_ref(ud, d, name, ud.clonedir): return True - if ud.shallow and ud.write_shallow_tarballs and not os.path.exists(ud.fullshallow): - return True - if ud.write_tarballs and not os.path.exists(ud.fullmirror): - return True return False + def shallow_tarball_need_update(self, ud): + return ud.shallow and ud.write_shallow_tarballs and not os.path.exists(ud.fullshallow) + + def tarball_need_update(self, ud): + return ud.write_tarballs and not os.path.exists(ud.fullmirror) + def try_premirror(self, ud, d): # If we don't do this, updating an existing checkout with only premirrors # is not possible
Signature · 1 line
2.19.0
PATCH 3/7 fetch2/git: use correct check to decide if the shallow tarball should be unpacked
In reply to Urs FässlerThe shallow_tarball check is never true due a check on the caller side. The tarball check is not related to the code on the caller side.
--- lib/bb/fetch2/git.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bb/fetch2/git.py +1 -1
@@ -476,7 +476,7 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - if ud.shallow and os.path.exists(ud.fullshallow) and self.need_update(ud, d): + if ud.shallow and os.path.exists(ud.fullshallow) and self.clonedir_need_update(ud, d): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) else:
Signature · 1 line
2.19.0
PATCH 4/7 tests/data: extract LogRecord into own file
In reply to Urs FässlerLogRecord is a mechanism to capture the log output. It is used in the data tests. It is moved into its own file to be used by other tests.
--- lib/bb/tests/data.py | 25 +------------------ lib/bb/tests/logrecord.py | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 lib/bb/tests/logrecord.py
lib/bb/tests/data.py +1 -24
@@ -24,30 +24,7 @@ import unittest import bb import bb.data import bb.parse -import logging - -class LogRecord(): - def __enter__(self): - logs = [] - class LogHandler(logging.Handler): - def emit(self, record): - logs.append(record) - logger = logging.getLogger("BitBake") - handler = LogHandler() - self.handler = handler - logger.addHandler(handler) - return logs - def __exit__(self, type, value, traceback): - logger = logging.getLogger("BitBake") - logger.removeHandler(self.handler) - return - -def logContains(item, logs): - for l in logs: - m = l.getMessage() - if item in m: - return True - return False +from bb.tests.logrecord import LogRecord, logContains class DataExpansions(unittest.TestCase): def setUp(self):
lib/bb/tests/logrecord.py new file +51 -0
51 lines not on this page · raw message
Signature · 1 line
2.19.0
PATCH 5/7 fetch2/git: prevent access to non-existing clonedir
In reply to Urs FässlerA user friendly error is throw when neither the clonedir nor fullshallow exist. Without the check, a difficult to interpret error is throw from within the fetch command.
--- lib/bb/fetch2/git.py | 4 +++- lib/bb/tests/fetch.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
lib/bb/fetch2/git.py +3 -1
@@ -479,8 +479,10 @@ class Git(FetchMethod): if ud.shallow and os.path.exists(ud.fullshallow) and self.clonedir_need_update(ud, d): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - else: + elif not self.clonedir_need_update(ud, d): runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + else: + bb.fatal("no up to date source found") repourl = self._get_repo_url(ud) runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, repourl), d, workdir=destdir)
lib/bb/tests/fetch.py +13 -0
@@ -28,6 +28,7 @@ import os from bb.fetch2 import URI from bb.fetch2 import FetchMethod import bb +from bb.tests.logrecord import LogRecord, logContains def skipIfNoNetwork(): if os.environ.get("BB_SKIP_NETTESTS") == "yes":
@@ -1581,6 +1582,18 @@ class GitShallowTest(FetcherTest): self.assertRefs(['master', 'origin/master']) self.assertRevCount(orig_revs - 1758) + def test_that_unpack_throws_an_error_when_the_git_clone_nor_shallow_tarball_exist(self): + self.add_empty_file('a') + fetcher, ud = self.fetch() + bb.utils.remove(self.gitdir, recurse=True) + bb.utils.remove(self.dldir, recurse=True) + + with LogRecord() as logs: + with self.assertRaises(Exception) as context: + fetcher.unpack(self.d.getVar('WORKDIR')) + + self.assertTrue(logContains("no up to date source found", logs)) + @skipIfNoNetwork() def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self): self.d.setVar('SRCREV', 'e5939ff608b95cdd4d0ab0e1935781ab9a276ac0')
Signature · 1 line
2.19.0
Re: [PATCH 5/7] fetch2/git: prevent access to non-existing clonedir
Quoted 16 lines · Urs Fässler
A user friendly error is throw when neither the clonedir nor fullshallow exist. Without the check, a difficult to interpret error is throw from within the fetch command.
Signed-off-by Urs Fässler <[email protected]>Signed-off-by Pascal Bach <[email protected]>--- lib/bb/fetch2/git.py | 4 +++- lib/bb/tests/fetch.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)lib/bb/fetch2/git.py +0
-0
lib/bb/fetch2/git.py class Git(FetchMethod): · line 485
Quoted 8 lines
lib/bb/fetch2/git.py +3
-1@@ -479,8 +479,10 @@ class Git(FetchMethod): if ud.shallow and os.path.exists(ud.fullshallow) and self.clonedir_need_update(ud, d): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - else: + elif not self.clonedir_need_update(ud, d): runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + else: + bb.fatal("no up to date source found")
I appreciate there are other places the fetcher code calls bb.fatal() but this is problematic.
The intent is to make the fetcher code a library which other code call call into and this is already how OE treats it. Python libraries should raise exceptions on errors and allow the parent code to handle it, not raise SystemExit() exceptions.
Is there some other way we can raise a decent error even if means adding a new exception (which can subclass one of the other fetch exceptions)?
Cheers,
Richard
Re: [PATCH 5/7] fetch2/git: prevent access to non-existing clonedir
Quoted 41 lines · Richard Purdie
Quoted 26 lines · Urs Fässler
A user friendly error is throw when neither the clonedir nor fullshallow exist. Without the check, a difficult to interpret error
is throw from within the fetch command.Signed-off-by Urs Fässler <[email protected]>Signed-off-by Pascal Bach <[email protected]>--- lib/bb/fetch2/git.py | 4 +++- lib/bb/tests/fetch.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)lib/bb/fetch2/git.py +0
-0@@ -479,8 +479,10 @@ class Git(FetchMethod): if ud.shallow and os.path.exists(ud.fullshallow) and
self.clonedir_need_update(ud, d):
bb.utils.mkdirhier(destdir)runfetchcmd("tar -xzf %s" % ud.fullshallow, d,
workdir=destdir)lib/bb/fetch2/git.py +1
-1- else: + elif not self.clonedir_need_update(ud, d): runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd,ud.cloneflags, ud.clonedir, destdir), d)
lib/bb/fetch2/git.py class Git(FetchMethod): · line 485
Quoted 2 lines
lib/bb/fetch2/git.py +2
-0+ else: + bb.fatal("no up to date source found")I appreciate there are other places the fetcher code calls
bb.fatal()but this is problematic.The intent is to make the fetcher code a library which other code call
call into and this is already how OE treats it. Python libraries should
raise exceptions on errors and allow the parent code to handle it, not
raiseSystemExit()exceptions.
Quoted 3 lines
Is there some other way we can raise a decent error even if means adding a new exception (which can subclass one of the other fetch exceptions)?
There is the UnpackError exception for "General fetcher exception when something happens incorrectly when unpacking" I can use.
When I use exceptions, I don't need the LogRecord class I extracted in patch "[PATCH 4/7] tests/data: extract LogRecord into own file" anymore.
I am not sure if I should remove that from the patch-set since it is unrelated. But it imho improves the code base a bit.
Regards
Urs
PATCH 6/7 fetch2/git: explicitly show the decision logic to select the source in code
In reply to Urs Fässler--- lib/bb/fetch2/git.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/bb/fetch2/git.py +4 -3
@@ -476,11 +476,12 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - if ud.shallow and os.path.exists(ud.fullshallow) and self.clonedir_need_update(ud, d): + clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) + if clonedir_is_up_to_date: + runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + elif ud.shallow and os.path.exists(ud.fullshallow): bb.utils.mkdirhier(destdir) runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - elif not self.clonedir_need_update(ud, d): - runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) else: bb.fatal("no up to date source found")
Signature · 1 line
2.19.0
PATCH 7/7 fetch2/git: provide information about missing sources
In reply to Urs FässlerProvide more information in the case the sources are not found in the unpack step.
--- lib/bb/fetch2/git.py | 29 +++++++++++++++++++++-------- lib/bb/tests/fetch.py | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-)
lib/bb/fetch2/git.py +21 -8
@@ -476,14 +476,27 @@ class Git(FetchMethod): if os.path.exists(destdir): bb.utils.prunedir(destdir) - clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) - if clonedir_is_up_to_date: - runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) - elif ud.shallow and os.path.exists(ud.fullshallow): - bb.utils.mkdirhier(destdir) - runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) - else: - bb.fatal("no up to date source found") + source_found = False + source_error = [] + + if not source_found: + clonedir_is_up_to_date = not self.clonedir_need_update(ud, d) + if clonedir_is_up_to_date: + runfetchcmd("%s clone %s %s/ %s" % (ud.basecmd, ud.cloneflags, ud.clonedir, destdir), d) + source_found = True + else: + source_error.append("clone directory not available or not up to date: " + ud.clonedir) + + if not source_found: + if ud.shallow and os.path.exists(ud.fullshallow): + bb.utils.mkdirhier(destdir) + runfetchcmd("tar -xzf %s" % ud.fullshallow, d, workdir=destdir) + source_found = True + else: + source_error.append("shallow clone not enabled or not available: " + ud.fullshallow) + + if not source_found: + bb.fatal("no up to date source found: " + "; ".join(source_error)) repourl = self._get_repo_url(ud) runfetchcmd("%s remote set-url origin %s" % (ud.basecmd, repourl), d, workdir=destdir)
lib/bb/tests/fetch.py +2 -0
@@ -1593,6 +1593,8 @@ class GitShallowTest(FetcherTest): fetcher.unpack(self.d.getVar('WORKDIR')) self.assertTrue(logContains("no up to date source found", logs)) + self.assertTrue(logContains("clone directory not available or not up to date", logs)) + self.assertTrue(logContains("shallow clone not enabled or not available", logs)) @skipIfNoNetwork() def test_that_unpack_does_work_when_using_git_shallow_tarball_but_tarball_is_not_available(self):
Signature · 1 line
2.19.0