From 5ad927a38cad08a2d79f327e7bb3cc46316fa6a4 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 6 Apr 2011 11:15:47 +0200 Subject: all: Fix all instances of ResourceWarning about unclosed files --- apt/package.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index d7d5d167..54ef1c01 100644 --- a/apt/package.py +++ b/apt/package.py @@ -50,9 +50,9 @@ __all__ = ('BaseDependency', 'Dependency', 'Origin', 'Package', 'Record', def _file_is_same(path, size, md5): """Return ``True`` if the file is the same.""" - if (os.path.exists(path) and os.path.getsize(path) == size and - apt_pkg.md5sum(open(path)) == md5): - return True + if os.path.exists(path) and os.path.getsize(path) == size: + with open(path) as fobj: + return apt_pkg.md5sum(fobj) == md5 class FetchError(Exception): @@ -994,11 +994,8 @@ class Package(object): """ path = "/var/lib/dpkg/info/%s.list" % self.name try: - file_list = open(path, "rb") - try: + with open(path, "rb") as file_list: return file_list.read().decode("utf-8").split(u"\n") - finally: - file_list.close() except EnvironmentError: return [] @@ -1104,6 +1101,7 @@ class Package(object): # Check if the download was canceled if cancel_lock and cancel_lock.isSet(): return u"" + # FIXME: python3.2: Should be closed manually changelog_file = urllib2.urlopen(uri) # do only get the lines that are new changelog = u"" -- cgit v1.2.3 From 16955bcd8717b3e2c10496968765c3a4ea252b1f Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 12 Apr 2011 15:23:17 +0200 Subject: apt.package: Add 'tasks' to Version, improve doc (Closes: #619574) --- apt/package.py | 38 ++++++++++++++++++++++++++++++++++---- debian/changelog | 1 + doc/source/library/apt.package.rst | 29 +++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index 54ef1c01..14e80594 100644 --- a/apt/package.py +++ b/apt/package.py @@ -162,10 +162,23 @@ class Origin(object): class Record(Mapping): - """Represent a pkgRecord. + """Record in a Packages file + + Represent a record as stored in a Packages file. You can use this like + a dictionary mapping the field names of the record to their values:: + + >>> record = Record("Package: python-apt\\nVersion: 0.8.0\\n\\n") + >>> record["Package"] + 'python-apt' + >>> record["Version"] + '0.8.0' + + For example, to get the tasks of a package from a cache, you could do:: + + package.candidate.record["Tasks"].split() + + Of course, you can also use the :attr:`Version.tasks` property. - It can be accessed like a dictionary and can also give the original package - record if accessed as a string. """ def __init__(self, record_str): @@ -209,6 +222,9 @@ class Record(Mapping): class Version(object): """Representation of a package version. + The Version class contains all information related to a + specific package version. + .. versionadded:: 0.7.9 """ @@ -393,7 +409,11 @@ class Version(object): @property def record(self): - """Return a Record() object for this version.""" + """Return a Record() object for this version. + + Return a Record() object for this version which provides access + to the raw attributes of the candidate version + """ return Record(self._records.record) def get_dependencies(self, *types): @@ -474,6 +494,16 @@ class Version(object): """ return self._records.sha256_hash + @property + def tasks(self): + """Get the tasks of the package. + + A set of the names of the tasks this package belongs to. + + .. versionadded:: 0.8.0 + """ + return set(self.record["Task"].split()) + def _uris(self): """Return an iterator over all available urls. diff --git a/debian/changelog b/debian/changelog index ebe2de3f..c8b47acc 100644 --- a/debian/changelog +++ b/debian/changelog @@ -15,6 +15,7 @@ python-apt (0.8.0~exp2) UNRELEASED; urgency=low * apt.cache: Document that update() may need an open() (Closes: #622342) * apt.cache: Add a fetch_archives() method (Closes: #622347) * doc: Fix a minor formatting error, patch by Jakub Wilk (Closes: #608914) + * apt.package: Add 'tasks' to Version, improve doc (Closes: #619574) -- Julian Andres Klode Wed, 06 Apr 2011 09:46:52 +0200 diff --git a/doc/source/library/apt.package.rst b/doc/source/library/apt.package.rst index 4b143b8a..d8731ea8 100644 --- a/doc/source/library/apt.package.rst +++ b/doc/source/library/apt.package.rst @@ -90,6 +90,35 @@ Origin Information it provides a GPG-signed Release file and the GPG-key used is in the keyring used by apt (see apt-key). + + +The Record class +----------------- +.. autoclass:: Record + :members: + + .. note:: + .. versionchanged:: 0.7.100 + This class is a subclass of :class:`collections.Mapping` when used + in Python 2.6 or newer. + + .. describe:: record[name] + + Return the value of the field with the name *name*. + + .. describe:: name in record + + Return whether a field *name* exists in record. + + .. describe:: len(record) + + The number of fields in the record + + .. describe:: str(record) + + Display the record as a string + + Examples --------- .. code-block:: python -- cgit v1.2.3 From 48d0403a804e8185c5ca6e77660a80725404e7d8 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 27 May 2011 15:44:45 +0200 Subject: Fix get_changelog in Python 3 (Closes: #626532) --- apt/package.py | 2 +- debian/changelog | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index 14e80594..00470ced 100644 --- a/apt/package.py +++ b/apt/package.py @@ -1142,7 +1142,7 @@ class Package(object): return u"" # Read changelog line by line line_raw = changelog_file.readline() - if line_raw == "": + if not line_raw: break # The changelog is encoded in utf-8, but since there isn't # any http header, urllib2 seems to treat it as ascii diff --git a/debian/changelog b/debian/changelog index f4038caa..f27bb797 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,9 +1,13 @@ python-apt (0.8.0~exp5) UNRELEASED; urgency=low + [ Julian Andres Klode ] * Increase Breaks for update-notifier to 0.99.3debian9 * utils/get_debian_mirrors.py: Adjust for new Alioth SCM urls * debian/control: Standards-Version 3.9.2 + [ Tshepang Lekhonkhobe ] + * Fix get_changelog in Python 3 (Closes: #626532) + -- Julian Andres Klode Thu, 26 May 2011 18:01:57 +0200 python-apt (0.8.0~exp4) experimental; urgency=low -- cgit v1.2.3 From ba4ba3e73303860b032ae1a1ae5c27c98e629cd6 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 27 May 2011 15:46:49 +0200 Subject: apt/package.py: fix a few typos [formated->formatted] (Closes: #597054) --- apt/package.py | 8 ++++---- debian/changelog | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'apt/package.py') diff --git a/apt/package.py b/apt/package.py index 00470ced..9223ed22 100644 --- a/apt/package.py +++ b/apt/package.py @@ -343,7 +343,7 @@ class Version(object): def description(self): """Return the formatted long description. - Return the formated long description according to the Debian policy + Return the formatted long description according to the Debian policy (Chapter 5.6.13). See http://www.debian.org/doc/debian-policy/ch-controlfields.html for more information. @@ -892,7 +892,7 @@ class Package(object): def description(self): """Return the formatted long description. - Return the formated long description according to the Debian policy + Return the formatted long description according to the Debian policy (Chapter 5.6.13). See http://www.debian.org/doc/debian-policy/ch-controlfields.html for more information. @@ -1360,8 +1360,8 @@ def _test(): print "SourcePkg: %s " % pkg.candidate.source_name print "Section: %s " % pkg.section print "Summary: %s" % pkg.candidate.summary - print "Description (formated) :\n%s" % pkg.candidate.description - print "Description (unformated):\n%s" % pkg.candidate.raw_description + print "Description (formatted) :\n%s" % pkg.candidate.description + print "Description (unformatted):\n%s" % pkg.candidate.raw_description print "InstalledSize: %s " % pkg.candidate.installed_size print "PackageSize: %s " % pkg.candidate.size print "Dependencies: %s" % pkg.installed.dependencies diff --git a/debian/changelog b/debian/changelog index f27bb797..6a906324 100644 --- a/debian/changelog +++ b/debian/changelog @@ -7,6 +7,7 @@ python-apt (0.8.0~exp5) UNRELEASED; urgency=low [ Tshepang Lekhonkhobe ] * Fix get_changelog in Python 3 (Closes: #626532) + * apt/package.py: fix a few typos [formated->formatted] (Closes: #597054) -- Julian Andres Klode Thu, 26 May 2011 18:01:57 +0200 -- cgit v1.2.3