/usr/lib/python2.7/dist-packages/dput/changes.py is in python-dput 1.17.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | # -*- coding: utf-8 -*-
#
# changes.py — .changes file handling class
#
# This file was originally part of debexpo
# https://alioth.debian.org/projects/debexpo/
#
# Copyright © 2008 Jonny Lamb <jonny@debian.org>
# Copyright © 2010 Jan Dittberner <jandd@debian.org>
# Copyright © 2012 Arno Töll <arno@debian.org>
# Copyright © 2012 Paul Tagliamonte <paultag@debian.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""
This code deals with the reading and processing of Debian .changes files. This
code is copyright (c) Jonny Lamb, and is used by dput, rather then created as
a result of it. Thank you Jonny.
"""
__author__ = 'Jonny Lamb'
__copyright__ = 'Copyright © 2008 Jonny Lamb, Copyright © 2010 Jan Dittberner'
__license__ = 'MIT'
import sys
import os.path
import hashlib
from debian import deb822
from dput.core import logger
from dput.util import run_command
from dput.exceptions import ChangesFileException
class Changes(object):
"""
Changes object to help process and store information regarding Debian
.changes files, used in the upload process.
"""
def __init__(self, filename=None, string=None):
"""
Object constructor. The object allows the user to specify **either**:
#. a path to a *changes* file to parse
#. a string with the *changes* file contents.
::
a = Changes(filename='/tmp/packagename_version.changes')
b = Changes(string='Source: packagename\\nMaintainer: ...')
``filename``
Path to *changes* file to parse.
``string``
*changes* file in a string to parse.
"""
if (filename and string) or (not filename and not string):
raise TypeError
if filename:
self._absfile = os.path.abspath(filename)
self._data = deb822.Changes(open(filename))
else:
self._data = deb822.Changes(string)
if len(self._data) == 0:
raise ChangesFileException('Changes file could not be parsed.')
if filename:
self.basename = os.path.basename(filename)
else:
self.basename = None
self._directory = ""
self.is_python3 = False
if sys.version_info[0] >= 3:
self.is_python3 = True
def get_filename(self):
"""
Returns the filename from which the changes file was generated from.
Please do note this is just the basename, not the entire full path, or
even a relative path. For the absolute path to the changes file, please
see :meth:`get_changes_file`.
"""
return self.basename
def get_changes_file(self):
"""
Return the full, absolute path to the changes file. For just the
filename, please see :meth:`get_filename`.
"""
return os.path.join(self._directory, self.get_filename())
def get_files(self):
"""
Returns a list of files referenced in the changes file, such as
the .dsc, .deb(s), .orig.tar.gz, and .diff.gz or .debian.tar.gz.
All strings in the array will be absolute paths to the files.
"""
return [os.path.join(self._directory, z['name'])
for z in self._data['Files']]
def __getitem__(self, key):
"""
Returns the value of the rfc822 key specified.
``key``
Key of data to request.
"""
return self._data[key]
def __contains__(self, key):
"""
Returns whether the specified RFC822 key exists.
``key``
Key of data to check for existence.
"""
return key in self._data
def get(self, key, default=None):
"""
Returns the value of the rfc822 key specified, but defaults
to a specific value if not found in the rfc822 file.
``key``
Key of data to request.
``default``
Default return value if ``key`` does not exist.
"""
return self._data.get(key, default)
def get_component(self):
"""
Returns the component of the package.
"""
return self._parse_section(self._data['Files'][0]['section'])[0]
def get_priority(self):
"""
Returns the priority of the package.
"""
return self._parse_section(self._data['Files'][0]['priority'])[1]
def get_section(self):
"""
Returns the section of the package.
"""
return self._parse_section(self._data['Files'][0]['section'])[1]
def get_dsc(self):
"""
Returns the name of the .dsc file.
"""
for item in self.get_files():
if item.endswith('.dsc'):
return item
def get_diff(self):
"""
Returns the name of the .diff.gz file if there is one, otherwise None.
"""
for item in self.get_files():
if item.endswith('.diff.gz') or item.endswith('.debian.tar.gz'):
return item
return None
def get_pool_path(self):
"""
Returns the path the changes file would be
"""
return self._data.get_pool_path()
def get_package_name(self):
"""
Returns the source package name
"""
return self.get("Source")
def _parse_section(self, section):
"""
Works out the component and section from the "Section" field.
Sections like `python` or `libdevel` are in main.
Sections with a prefix, separated with a forward-slash also show the
component.
It returns a list of strings in the form [component, section].
For example, `non-free/python` has component `non-free` and section
`python`.
``section``
Section name to parse.
"""
if '/' in section:
return section.split('/')
else:
return ['main', section]
def set_directory(self, directory):
if directory:
self._directory = directory
else:
self._directory = ""
def validate(self, check_hash="sha1", check_signature=True):
"""
See :meth:`validate_checksums` for ``check_hash``, and
:meth:`validate_signature` if ``check_signature`` is True.
"""
self.validate_checksums(check_hash)
if check_signature:
self.validate_signature(check_signature)
else:
logger.info("Not checking signature")
def validate_signature(self, check_signature=True):
"""
Validate the GPG signature of a .changes file.
Throws a :class:`dput.exceptions.ChangesFileException` if there's
an issue with the GPG signature. Returns the GPG key ID.
"""
gpg_path = "gpg"
(gpg_output, gpg_output_stderr, exit_status) = run_command([
gpg_path, "--status-fd", "1", "--verify",
"--batch", self.get_changes_file()
])
if exit_status == -1:
raise ChangesFileException(
"Unknown problem while verifying signature")
# contains verbose human readable GPG information
if self.is_python3:
gpg_output_stderr = str(gpg_output_stderr, encoding='utf8')
if self.is_python3:
gpg_output = gpg_output.decode(encoding='UTF-8')
if gpg_output.count('[GNUPG:] GOODSIG'):
pass
elif gpg_output.count('[GNUPG:] BADSIG'):
raise ChangesFileException("Bad signature", gpg_output_stderr)
elif gpg_output.count('[GNUPG:] ERRSIG'):
raise ChangesFileException("Error verifying signature",
gpg_output_stderr)
elif gpg_output.count('[GNUPG:] NODATA'):
raise ChangesFileException("No signature on", gpg_output_stderr)
else:
raise ChangesFileException(
"Unknown problem while verifying signature", gpg_output_stderr
)
key = None
for line in gpg_output.split("\n"):
if line.startswith('[GNUPG:] VALIDSIG'):
key = line.split()[2]
return key
def validate_checksums(self, check_hash="sha1"):
"""
Validate checksums for a package, using ``check_hack``'s type
to validate the package.
Valid ``check_hash`` types:
* sha1
* sha256
* md5
* md5sum
"""
logger.debug("validating %s checksums" % (check_hash))
for filename in self.get_files():
if check_hash == "sha1":
hash_type = hashlib.sha1()
checksums = self.get("Checksums-Sha1")
field_name = "sha1"
elif check_hash == "sha256":
hash_type = hashlib.sha256()
checksums = self.get("Checksums-Sha256")
field_name = "sha256"
elif check_hash == "md5":
hash_type = hashlib.md5()
checksums = self.get("Files")
field_name = "md5sum"
for changed_files in checksums:
if changed_files['name'] == os.path.basename(filename):
break
else:
assert(
"get_files() returns different files than Files: knows?!")
with open(filename, "rb") as fc:
while True:
chunk = fc.read(131072)
if not chunk:
break
hash_type.update(chunk)
fc.close()
if not hash_type.hexdigest() == changed_files[field_name]:
raise ChangesFileException(
"Checksum mismatch for file %s: %s != %s" % (
filename,
hash_type.hexdigest(),
changed_files[field_name]
))
else:
logger.trace("%s Checksum for file %s matches" % (
field_name, filename
))
def parse_changes_file(filename, directory=None):
"""
Parse a .changes file and return a dput.changes.Change instance with
parsed changes file data. The optional directory argument refers to the
base directory where the referred files from the changes file are expected
to be located.
"""
_c = Changes(filename=filename)
_c.set_directory(directory)
return(_c)
|