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/cache.py | 2 +- apt/cdrom.py | 7 ++++--- apt/debfile.py | 2 +- apt/package.py | 12 +++++------- 4 files changed, 11 insertions(+), 12 deletions(-) (limited to 'apt') diff --git a/apt/cache.py b/apt/cache.py index bfa41edc..3bbae923 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -107,7 +107,7 @@ class Cache(object): os.makedirs(rootdir + d) for f in files: if not os.path.exists(rootdir + f): - open(rootdir + f, "w") + open(rootdir + f, "w").close() def _run_callbacks(self, name): """ internal helper to run a callback """ diff --git a/apt/cdrom.py b/apt/cdrom.py index 01caa12f..9688de9e 100644 --- a/apt/cdrom.py +++ b/apt/cdrom.py @@ -79,9 +79,10 @@ class Cdrom(apt_pkg.Cdrom): src.append(apt_pkg.config.find_file("Dir::Etc::sourcelist")) # Check each file for fname in src: - for line in open(fname): - if not line.lstrip().startswith("#") and cd_id in line: - return True + with open(fname) as fobj: + for line in fobj: + if not line.lstrip().startswith("#") and cd_id in line: + return True return False if apt_pkg._COMPAT_0_7: diff --git a/apt/debfile.py b/apt/debfile.py index fb4312a1..d0f41def 100644 --- a/apt/debfile.py +++ b/apt/debfile.py @@ -64,7 +64,7 @@ class DebPackage(object): self._installed_conflicts = set() self._failure_string = "" self.filename = filename - self._debfile = apt_inst.DebFile(open(self.filename)) + self._debfile = apt_inst.DebFile(self.filename) control = self._debfile.control.extractdata("control") self._sections = apt_pkg.TagSection(control) self.pkgname = self._sections["Package"] 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 30bde83a45ebe2b8c401ef72f8bac3f4058614ab Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 11 Apr 2011 11:54:15 +0200 Subject: apt.progress: Use long for ETA, natural type for size (LP: #377375) --- apt/progress/old.py | 5 +++-- apt/progress/text.py | 5 ++--- debian/changelog | 1 + 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'apt') diff --git a/apt/progress/old.py b/apt/progress/old.py index 4bd79f2e..364cd2ce 100644 --- a/apt/progress/old.py +++ b/apt/progress/old.py @@ -149,10 +149,11 @@ class TextFetchProgress(FetchProgress): Return True to continue or False to cancel. """ FetchProgress.pulse(self) + if self.currentCPS > 0: s = "[%2.f%%] %sB/s %s" % (self.percent, - apt_pkg.size_to_str(int(self.currentCPS)), - apt_pkg.time_to_str(int(self.eta))) + apt_pkg.size_to_str(self.currentCPS), + apt_pkg.time_to_str(long(self.eta))) else: s = "%2.f%% [Working]" % (self.percent) print "\r%s" % (s), diff --git a/apt/progress/text.py b/apt/progress/text.py index d777837c..c5eec092 100644 --- a/apt/progress/text.py +++ b/apt/progress/text.py @@ -157,11 +157,10 @@ class AcquireProgress(base.AcquireProgress, TextProgress): shown = False tval = '%i%%' % percent - end = "" if self.current_cps: - eta = int(float(self.total_bytes - self.current_bytes) / - self.current_cps) + eta = long(float(self.total_bytes - self.current_bytes) / + self.current_cps) end = " %sB/s %s" % (apt_pkg.size_to_str(self.current_cps), apt_pkg.time_to_str(eta)) diff --git a/debian/changelog b/debian/changelog index 2272462a..4d5c6225 100644 --- a/debian/changelog +++ b/debian/changelog @@ -8,6 +8,7 @@ python-apt (0.8.0~exp2) UNRELEASED; urgency=low * apt_pkg: Raise error when parse_commandline gets empty argv (LP: #707416) * apt_pkg: Fix time_to_str, time_rfc1123 to accept more correct values (time_to_str accepts unsigned long, time_rfc1123 long long, y2k31-correct). + * apt.progress: Use long for ETA, natural type for size (LP: #377375) -- Julian Andres Klode Wed, 06 Apr 2011 09:46:52 +0200 -- cgit v1.2.3 From 287e27e498516dd00aba1b78aea92a12ec079886 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 12 Apr 2011 14:33:39 +0200 Subject: apt.cache: Document that update() may need an open() (Closes: #622342) --- apt/cache.py | 4 ++++ debian/changelog | 1 + 2 files changed, 5 insertions(+) (limited to 'apt') diff --git a/apt/cache.py b/apt/cache.py index 3bbae923..373bb91d 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -339,6 +339,10 @@ class Cache(object): raise_on_error=True, sources_list=None): """Run the equivalent of apt-get update. + You probably want to call open() afterwards, in order to utilise the + new cache. Otherwise, the old cache will be used which can lead to + strange bugs. + The first parameter *fetch_progress* may be set to an instance of apt.progress.FetchProgress, the default is apt.progress.FetchProgress() . diff --git a/debian/changelog b/debian/changelog index 1460acce..f89de6de 100644 --- a/debian/changelog +++ b/debian/changelog @@ -12,6 +12,7 @@ python-apt (0.8.0~exp2) UNRELEASED; urgency=low * aptsources/sourceslist.py: s/aptsource.py/sourceslist.py/ (LP: #309603) * doc/examples: Add example on how to get architecture names (LP: #194374) * apt_pkg: Fix unsigned/long-vs-int issues (LP: #610820) + * apt.cache: Document that update() may need an open() (Closes: #622342) -- Julian Andres Klode Wed, 06 Apr 2011 09:46:52 +0200 -- cgit v1.2.3 From d206a03cc50d3c0fee56b8dbd6a915eadc7038cf Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 12 Apr 2011 14:51:08 +0200 Subject: apt.cache: Add a fetch_archives() method (Closes: #622347) --- apt/cache.py | 22 ++++++++++++++++++++++ debian/changelog | 1 + 2 files changed, 23 insertions(+) (limited to 'apt') diff --git a/apt/cache.py b/apt/cache.py index 373bb91d..339de763 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -288,6 +288,28 @@ class Cache(object): finally: os.close(lock) + def fetch_archives(self, progress=None, fetcher=None): + """Fetch the archives for all packages marked for install/upgrade. + + You can specify either an :class:`apt.progress.base.AcquireProgress()` + object for the parameter *progress*, or specify an already + existing :class:`apt_pkg.Acquire` object for the parameter *fetcher*. + + The return value of the function is undefined. If an error occured, + an exception of type :class:`FetchFailedException` or + :class:`FetchCancelledException` is raised. + """ + if progress is not None and fetcher is not None: + raise ValueError("Takes a progress or a an Acquire object") + if progress is None: + progress = apt.progress.text.AcquireProgress() + if fetcher is None: + fetcher = apt_pkg.Acquire(progress) + + + return self._fetch_archives(fetcher, + apt_pkg.PackageManager(self._depcache)) + def is_virtual_package(self, pkgname): """Return whether the package is a virtual package.""" try: diff --git a/debian/changelog b/debian/changelog index f89de6de..bd87c411 100644 --- a/debian/changelog +++ b/debian/changelog @@ -13,6 +13,7 @@ python-apt (0.8.0~exp2) UNRELEASED; urgency=low * doc/examples: Add example on how to get architecture names (LP: #194374) * apt_pkg: Fix unsigned/long-vs-int issues (LP: #610820) * apt.cache: Document that update() may need an open() (Closes: #622342) + * apt.cache: Add a fetch_archives() method (Closes: #622347) -- Julian Andres Klode Wed, 06 Apr 2011 09:46:52 +0200 -- cgit v1.2.3 From da11ffd96bbd58f51c54857dc72e18f3c2d6b998 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 12 Apr 2011 15:02:22 +0200 Subject: apt/cache.py: Add versionadded --- apt/cache.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'apt') diff --git a/apt/cache.py b/apt/cache.py index 339de763..3bbf5a51 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -298,6 +298,8 @@ class Cache(object): The return value of the function is undefined. If an error occured, an exception of type :class:`FetchFailedException` or :class:`FetchCancelledException` is raised. + + .. versionadded:: 0.8.0 """ if progress is not None and fetcher is not None: raise ValueError("Takes a progress or a an Acquire object") -- 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') 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 8c23a5f32378cc25f6a4480b4b58c6bb71d6862c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 29 Apr 2011 11:04:34 +0200 Subject: apt.cache: Emit change signals in ProblemResolver --- apt/cache.py | 5 +++++ debian/changelog | 1 + 2 files changed, 6 insertions(+) (limited to 'apt') diff --git a/apt/cache.py b/apt/cache.py index 3bbf5a51..920a3023 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -576,6 +576,7 @@ class ProblemResolver(object): def __init__(self, cache): self._resolver = apt_pkg.ProblemResolver(cache._depcache) + self._cache = cache def clear(self, package): """Reset the package to the default state.""" @@ -595,11 +596,15 @@ class ProblemResolver(object): def resolve(self): """Resolve dependencies, try to remove packages where needed.""" + self._cache.cache_pre_change() self._resolver.resolve() + self._cache.cache_post_change() def resolve_by_keep(self): """Resolve dependencies, do not try to remove packages.""" + self._cache.cache_pre_change() self._resolver.resolve_by_keep() + self._cache.cache_post_change() # ----------------------------- experimental interface diff --git a/debian/changelog b/debian/changelog index ab1839d2..1d9391a1 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,7 @@ python-apt (0.8.0~exp4) UNRELEASED; urgency=low * apt_pkg: Add OrderList, wanted for mancoosi (Closes: #623485) + * apt.cache: Emit change signals in ProblemResolver -- Julian Andres Klode Wed, 27 Apr 2011 12:55:59 +0200 -- cgit v1.2.3 From 4775b948dcda187f03b765d7da2c00ea27834e9c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 29 Apr 2011 11:05:40 +0200 Subject: apt.Cache: Add a _changes_count member for later use --- apt/cache.py | 9 +++++++++ debian/changelog | 1 + 2 files changed, 10 insertions(+) (limited to 'apt') diff --git a/apt/cache.py b/apt/cache.py index 920a3023..be137b76 100644 --- a/apt/cache.py +++ b/apt/cache.py @@ -66,7 +66,11 @@ class Cache(object): self._weakref = weakref.WeakValueDictionary() self._set = set() self._fullnameset = set() + self._changes_count = -1 self._sorted_set = None + + self.connect("cache_post_open", self._inc_changes_count) + self.connect("cache_post_change", self._inc_changes_count) if memonly: # force apt to build its caches in memory apt_pkg.config.set("Dir::Cache::pkgcache", "") @@ -87,6 +91,11 @@ class Cache(object): # recognized (LP: #320665) apt_pkg.init_system() self.open(progress) + + + def _inc_changes_count(self): + """Increase the number of changes""" + self._changes_count += 1 def _check_and_create_required_dirs(self, rootdir): """ diff --git a/debian/changelog b/debian/changelog index 1d9391a1..ff07fc21 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,6 +2,7 @@ python-apt (0.8.0~exp4) UNRELEASED; urgency=low * apt_pkg: Add OrderList, wanted for mancoosi (Closes: #623485) * apt.cache: Emit change signals in ProblemResolver + * apt.Cache: Add a _changes_count member for later use -- Julian Andres Klode Wed, 27 Apr 2011 12:55:59 +0200 -- 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') 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') 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