diff options
| author | Julian Andres Klode <jak@debian.org> | 2009-04-01 15:34:44 +0200 |
|---|---|---|
| committer | Julian Andres Klode <jak@debian.org> | 2009-04-01 15:34:44 +0200 |
| commit | 3fbbced1f6d22d6ce3a7188032b0d19251159fbd (patch) | |
| tree | 6ef1b1113c046fbde05694fd967bc9d39f2d7eed | |
| parent | bea068087a98e03bbdaae2ac46287ed0d69e509a (diff) | |
| download | python-apt-3fbbced1f6d22d6ce3a7188032b0d19251159fbd.tar.gz | |
* apt/package.py: Introduce Version.{uri,uris,fetch_binary()}
| -rw-r--r-- | apt/package.py | 116 | ||||
| -rw-r--r-- | debian/changelog | 2 |
2 files changed, 109 insertions, 9 deletions
diff --git a/apt/package.py b/apt/package.py index 37cd33fb..595cf8ec 100644 --- a/apt/package.py +++ b/apt/package.py @@ -41,6 +41,17 @@ def _(string): return gettext.dgettext("python-apt", string) +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 + + +class FetchError(Exception): + """Raised when a file could not be fetched.""" + + class BaseDependency(object): """A single dependency. @@ -338,10 +349,87 @@ class Version(object): origins.append(Origin(self.package, verFileIter)) return origins - def fetch_source(self): - """Get the source code of a package""" + @property + def filename(self): + """Return the path to the file inside the archive.""" + return self._records.FileName + + @property + def md5(self): + """Return the md5sum of the binary.""" + return self._records.MD5Hash + + @property + def sha1(self): + """Return the sha1sum of the binary.""" + return self._records.SHA1Hash + + @property + def sha256(self): + """Return the sha1sum of the binary.""" + return self._records.SHA256Hash + + def _uris(self): + """Return an iterator over all available urls.""" + for (packagefile, index) in self._cand.FileList: + indexfile = self.package._pcache._list.FindIndex(packagefile) + if indexfile: + yield indexfile.ArchiveURI(self._records.FileName) + + @property + def uris(self): + """Return a list of all available uris for the binary.""" + return list(self._uris()) + + @property + def uri(self): + """Return a single URI for the binary.""" + return self._uris().next() + + def fetch_binary(self, destdir='', progress=None): + """Fetch the binary version of the package. + + The parameter 'destdir' specifies the directory where the package will + be fetched to. + + The parameter 'progress' may refer to an apt.progress.FetchProgress() + object. If not specified or None, apt.progress.TextFetchProgress() is + used. + """ + base = os.path.basename(self._records.FileName) + destfile = os.path.join(destdir, base) + if _file_is_same(destfile, self.size, self._records.MD5Hash): + print 'Ignoring already existing file:', destfile + return + acq = apt_pkg.GetAcquire(progress or apt.progress.TextFetchProgress()) + apt_pkg.GetPkgAcqFile(acq, self.uri, self._records.MD5Hash, self.size, + base, destFile=destfile) + acq.Run() + for item in acq.Items: + if item.Status != item.StatDone: + raise FetchError("The item %r could not be fetched: %s" % + (item.DestFile, item.ErrorText)) + return os.path.abspath(destfile) + + def fetch_source(self, destdir="", progress=None, unpack=True): + """Get the source code of a package. + + The parameter 'destdir' specifies the directory where the source will + be fetched to. + + The parameter 'progress' may refer to an apt.progress.FetchProgress() + object. If not specified or None, apt.progress.TextFetchProgress() is + used. + + The parameter 'unpack' describes whether the source should be unpacked + (True) or not (False). By default, it is unpacked. + + If 'unpack' is True, the path to the extracted directory is returned. + Otherwise, the path to the .dsc file is returned. + """ src = apt_pkg.GetPkgSrcRecords() - acq = apt_pkg.GetAcquire(apt.progress.TextFetchProgress()) + acq = apt_pkg.GetAcquire(progress or apt.progress.TextFetchProgress()) + dsc = None src.Lookup(self.package.name) try: @@ -351,23 +439,33 @@ class Version(object): raise ValueError("No source for %r" % self) for md5, size, path, type in src.Files: base = os.path.basename(path) + destfile = os.path.join(destdir, base) if type == 'dsc': - dsc = base + dsc = destfile if os.path.exists(base) and os.path.getsize(base) == size: fobj = open(base) try: if apt_pkg.md5sum(fobj) == md5: - print 'Ignoring already existing file', base + print 'Ignoring already existing file:', destfile continue finally: fobj.close() apt_pkg.GetPkgAcqFile(acq, src.Index.ArchiveURI(path), md5, size, - base) + base, destFile=destfile) acq.Run() - outdir = src.Package + '-' + apt_pkg.UpstreamVersion(src.Version) - subprocess.check_call(["dpkg-source", "-x", dsc, outdir]) - return os.path.abspath(outdir) + for item in acq.Items: + if item.Status != item.StatDone: + raise FetchError("The item %r could not be fetched: %s" % + (item.DestFile, item.ErrorText)) + + if unpack: + outdir = src.Package + '-' + apt_pkg.UpstreamVersion(src.Version) + outdir = os.path.join(destdir, outdir) + subprocess.check_call(["dpkg-source", "-x", dsc, outdir]) + return os.path.abspath(outdir) + else: + return os.path.abspath(dsc) class Package(object): diff --git a/debian/changelog b/debian/changelog index 3244d439..0015319c 100644 --- a/debian/changelog +++ b/debian/changelog @@ -2,6 +2,8 @@ python-apt (0.7.10~exp1) experimental; urgency=low * Build-Depend on python-debian, use it to get version number from changelog * Depend on libjs-jquery, and remove internal copy + * apt/package.py: + - Introduce Version.{uri,uris,fetch_binary()} -- Julian Andres Klode <jak@debian.org> Wed, 01 Apr 2009 15:24:29 +0200 |
