Skip to content
Snippets Groups Projects
Commit 5f8952ae authored by Chris Lamb's avatar Chris Lamb :eyes:
Browse files

Use a BuildinfoFile (etc.) regardless of whether the associated files such as...

Use a BuildinfoFile (etc.) regardless of whether the associated files such as the orig.tar.gz and the .deb are present. (Re: #122)
parent ca8861d5
No related branches found
No related tags found
No related merge requests found
......@@ -70,6 +70,31 @@ class DebControlContainer(Container):
super().__init__(*args, **kwargs)
self._version_re = DebControlContainer.get_version_trimming_re(self)
def recognizes(self):
if "Checksums-Sha256" not in self.source._deb822:
return False
for x in self.source._deb822.get("Checksums-Sha256"):
sha256 = hashlib.sha256()
# This will not work in nested containers
dsc_in_same_dir = os.path.join(
os.path.dirname(self.source.path), x["Name"]
)
if not os.path.exists(dsc_in_same_dir):
return False
# Validate whether the checksum matches
with open(dsc_in_same_dir, "rb") as f:
for buf in iter(functools.partial(f.read, 32768), b""):
sha256.update(buf)
if sha256.hexdigest() != x["sha256"]:
return False
return True
@staticmethod
def get_version_trimming_re(dcc):
version = dcc.source._deb822.get('Version')
......@@ -185,15 +210,13 @@ class DotChangesFile(DebControlFile):
if not super().recognizes(file):
return False
changes = Changes(filename=file.path)
file._deb822 = Changes(filename=file.path)
try:
changes.validate("sha256", check_signature=False)
file._deb822.validate("sha256", check_signature=False)
except FileNotFoundError:
return False
file._deb822 = changes
return True
def compare(self, other, *args, **kwargs):
......@@ -231,25 +254,7 @@ class DotDscFile(DebControlFile):
return False
with open(file.path, 'rb') as f:
dsc = Dsc(f)
for d in dsc.get('Files'):
md5 = hashlib.md5()
# XXX: this will not work for containers
dsc_in_same_dir = os.path.join(
os.path.dirname(file.path), d['Name']
)
if not os.path.exists(dsc_in_same_dir):
return False
with open(dsc_in_same_dir, 'rb') as f:
for buf in iter(functools.partial(f.read, 32768), b''):
md5.update(buf)
if md5.hexdigest() != d['md5sum']:
return False
file._deb822 = dsc
file._deb822 = Dsc(f)
return True
......@@ -284,29 +289,8 @@ class DotBuildinfoFile(DebControlFile):
if not super().recognizes(file):
return False
with open(file.path, 'rb') as f:
# We can parse .buildinfo files just like .dsc
buildinfo = Dsc(f)
if 'Checksums-Sha256' not in buildinfo:
return False
for d in buildinfo.get('Checksums-Sha256'):
sha256 = hashlib.sha256()
# XXX: this will not work for containers
dsc_in_same_dir = os.path.join(
os.path.dirname(file.path), d['Name']
)
if not os.path.exists(dsc_in_same_dir):
return False
with open(dsc_in_same_dir, 'rb') as f:
for buf in iter(functools.partial(f.read, 32768), b''):
sha256.update(buf)
if sha256.hexdigest() != d['sha256']:
return False
file._deb822 = buildinfo
# Parse .buildinfo files like .dsc files
with open(file.path, "rb") as f:
file._deb822 = Dsc(f)
return True
......@@ -70,6 +70,9 @@ class Container(metaclass=abc.ABCMeta):
def get_member(self, member_name):
raise NotImplementedError()
def recognizes(self):
return True
def get_path_name(self, dest_dir):
return os.path.join(dest_dir, str(uuid.uuid4()))
......
......@@ -252,7 +252,18 @@ class File(metaclass=abc.ABCMeta):
'Instantiating a %s for %s', type_name(klass), self.name,
)
try:
self._as_container = klass(self)
container = klass(self)
if not container.recognizes():
logger.debug(
"Instantiated a %s for %s, but cannot use it as a container",
type_name(klass),
self.name,
)
continue
self._as_container = container
logger.debug(
"Returning a %s for %s", type_name(klass), self.name,
)
......
......@@ -230,13 +230,13 @@ def test_dot_dsc_identification(dot_dsc1):
@skip_unless_module_exists('debian.deb822')
def test_dot_dsc_invalid(tmpdir, dot_dsc2):
def test_dot_dsc_no_associated_tar_gz(tmpdir, dot_dsc2):
tmpdir.mkdir('a')
dot_dsc_path = str(tmpdir.join('a/test_1.dsc'))
shutil.copy(TEST_DOT_CHANGES_FILE1_PATH, dot_dsc_path)
# we don't copy the referenced .tar.gz
identified = specialize(FilesystemFile(dot_dsc_path))
assert not isinstance(identified, DotDscFile)
assert isinstance(identified, DotDscFile)
def test_dot_dsc_no_differences(dot_dsc1):
......@@ -288,13 +288,13 @@ def test_dot_buildinfo_identification(dot_buildinfo1):
@skip_unless_module_exists('debian.deb822')
def test_dot_buildinfo_invalid(tmpdir):
def test_dot_buildinfo_no_deb(tmpdir):
tmpdir.mkdir('a')
dot_buildinfo_path = str(tmpdir.join('a/test_1.buildinfo'))
shutil.copy(TEST_DOT_BUILDINFO_FILE1_PATH, dot_buildinfo_path)
# we don't copy the referenced .deb
identified = specialize(FilesystemFile(dot_buildinfo_path))
assert not isinstance(identified, DotBuildinfoFile)
assert isinstance(identified, DotBuildinfoFile)
def test_dot_buildinfo_no_differences(dot_buildinfo1):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment