Exemple #1
0
 def test_acquire(self):
     apt_pkg.AcquireFile(apt_pkg.Acquire(), "http://example.com",
                         destdir=self.file_bytes, destfile=self.file_bytes)
     apt_pkg.AcquireFile(apt_pkg.Acquire(),
                         "http://example.com",
                         destdir=self.file_unicode,
                         destfile=self.file_unicode)
Exemple #2
0
    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_pkg.AcquireProgress()
        object. If not specified or None, apt.progress.text.AcquireProgress()
        is used.

        .. versionadded:: 0.7.10
        """
        base = os.path.basename(self._records.filename)
        destfile = os.path.join(destdir, base)
        if _file_is_same(destfile, self.size, self._records.md5_hash):
            print('Ignoring already existing file: %s' % destfile)
            return os.path.abspath(destfile)
        acq = apt_pkg.Acquire(progress or apt.progress.text.AcquireProgress())
        acqfile = apt_pkg.AcquireFile(acq,
                                      self.uri,
                                      self._records.md5_hash,
                                      self.size,
                                      base,
                                      destfile=destfile)
        acq.run()

        if acqfile.status != acqfile.STAT_DONE:
            raise FetchError("The item %r could not be fetched: %s" %
                             (acqfile.destfile, acqfile.error_text))

        return os.path.abspath(destfile)
Exemple #3
0
    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_pkg.AcquireProgress()
        object. If not specified or None, apt.progress.text.AcquireProgress()
        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.SourceRecords()
        acq = apt_pkg.Acquire(progress or apt.progress.text.AcquireProgress())

        dsc = None
        record = self._records
        source_name = record.source_pkg or self.package.shortname
        source_version = record.source_ver or self._cand.ver_str
        source_lookup = src.lookup(source_name)

        while source_lookup and source_version != src.version:
            source_lookup = src.lookup(source_name)
        if not source_lookup:
            raise ValueError("No source for %r" % self)
        files = list()
        for md5, size, path, type_ in src.files:
            base = os.path.basename(path)
            destfile = os.path.join(destdir, base)
            if type_ == 'dsc':
                dsc = destfile
            if _file_is_same(destfile, size, md5):
                print('Ignoring already existing file: %s' % destfile)
                continue
            files.append(
                apt_pkg.AcquireFile(acq,
                                    src.index.archive_uri(path),
                                    md5,
                                    size,
                                    base,
                                    destfile=destfile))
        acq.run()

        for item in acq.items:
            if item.status != item.STAT_DONE:
                raise FetchError("The item %r could not be fetched: %s" %
                                 (item.destfile, item.error_text))

        if unpack:
            outdir = src.package + '-' + apt_pkg.upstream_version(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)
Exemple #4
0
def f_getpkg(pkg):
    if not path.isdir(aptdir):
        mkdir(aptdir)
    p = apt.progress.text.AcquireProgress()
    c = apt.Cache()
    u = c[pkg].candidate.uri
    a = apt_pkg.Acquire(p)
    acq = apt_pkg.AcquireFile(a, uri=u, destdir=aptdir)
    a.run()
Exemple #5
0
def get_file(fetcher, uri, destfile):
    # get the file
    af = apt_pkg.AcquireFile(fetcher, uri=uri, descr="sample descr",
                               destfile=destfile)
    print "desc_uri: %s -> %s" % (af.desc_uri, af.destfile)
    res = fetcher.run()
    if res != fetcher.RESULT_CONTINUE:
        return False
    return True
Exemple #6
0
 def test_acquire_hashes(self):
     hs = apt_pkg.HashString("d41d8cd98f00b204e9800998ecf8427e")
     hsl = apt_pkg.HashStringList()
     hsl.append(hs)
     apt_pkg.AcquireFile(apt_pkg.Acquire(),
                         "http://example.com",
                         hash=hsl,
                         destdir=self.file_unicode,
                         destfile=self.file_unicode)
     apt_pkg.AcquireFile(apt_pkg.Acquire(),
                         "http://example.com",
                         hash=str(hs),
                         destdir=self.file_unicode,
                         destfile=self.file_unicode)
     self.assertRaises(TypeError, apt_pkg.AcquireFile, apt_pkg.Acquire(),
                         "http://example.com",
                         hash=hs,
                         destdir=self.file_unicode,
                         destfile=self.file_unicode)
Exemple #7
0
def get_file(fetcher, uri, destfile):
    # get the file
    af = apt_pkg.AcquireFile(fetcher,
                             uri=uri,
                             descr="sample descr",
                             destfile=destfile)
    res = fetcher.Run()
    if res != fetcher.ResultContinue:
        return False
    return True
Exemple #8
0
 def fetchDistUpgrader(self):
     " download the tarball with the upgrade script "
     tmpdir = tempfile.mkdtemp(prefix="ubuntu-release-upgrader-")
     self.tmpdir = tmpdir
     os.chdir(tmpdir)
     logging.debug("using tmpdir: '%s'" % tmpdir)
     # turn debugging on here (if required)
     if self.DEBUG > 0:
         apt_pkg.config.set("Debug::Acquire::http", "1")
         apt_pkg.config.set("Debug::Acquire::ftp", "1")
     #os.listdir(tmpdir)
     fetcher = apt_pkg.Acquire(self._progress)
     if self.new_dist.upgradeToolSig is not None:
         uri = self._expandUri(self.new_dist.upgradeToolSig)
         af1 = apt_pkg.AcquireFile(fetcher,
                                   uri,
                                   descr=_("Upgrade tool signature"))
         # reference it here to shut pyflakes up
         af1
     if self.new_dist.upgradeTool is not None:
         self.uri = self._expandUri(self.new_dist.upgradeTool)
         af2 = apt_pkg.AcquireFile(fetcher,
                                   self.uri,
                                   descr=_("Upgrade tool"))
         # reference it here to shut pyflakes up
         af2
         result = fetcher.run()
         if result != fetcher.RESULT_CONTINUE:
             logging.warn("fetch result != continue (%s)" % result)
             return False
         # check that both files are really there and non-null
         for f in [
                 os.path.basename(self.new_dist.upgradeToolSig),
                 os.path.basename(self.new_dist.upgradeTool)
         ]:
             if not (os.path.exists(f) and os.path.getsize(f) > 0):
                 logging.warn("file '%s' missing" % f)
                 return False
         return True
     return False
    def test_acquire_file_md5_keyword_backward_compat(self):
        """
        Ensure that both "md5" and "hash" is supported as keyword for
        AcquireFile
        """
        with warnings.catch_warnings(record=True) as caught_warnings:
            warnings.simplefilter("always")
            apt_pkg.AcquireFile(apt_pkg.Acquire(),
                                "http://example.com",
                                destfile=self.file_bytes,
                                md5="abcdef")

        self.assertEqual(len(caught_warnings), 1)
        self.assertTrue(
            issubclass(caught_warnings[0].category, DeprecationWarning))
        self.assertIn("md5", str(caught_warnings[0].message))
        self.assertIn("hash", str(caught_warnings[0].message))

        apt_pkg.AcquireFile(
            apt_pkg.Acquire(),
            "http://example.com",
            destfile=self.file_bytes,
            hash="sha1:41050ed528554fdd6c6c9a086ddd6bdba5857b21")
Exemple #10
0
def fetch_binary(version, destdir='', progress=None):
    # type: (str, AcquireProgress) -> str
    """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_pkg.AcquireProgress()
    object. If not specified or None, apt.progress.text.AcquireProgress()
    is used.

    taken from python-apt-1.8.4
    https://salsa.debian.org/apt-team/python-apt/-/blob/1.8.4/apt/package.py

    ---------------------------------------------------------
    Copyright (c) 2005-2009 Canonical

    Author: Michael Vogt <*****@*****.**>
    ---------------------------------------------------------

    Then fixed up to use sha256 and pass pycodestyle.
    """
    # pylint: disable=protected-access
    base = os.path.basename(version._records.filename)
    destfile = os.path.join(destdir, base)
    # pylint: disable=protected-access
    if _file_is_same(destfile, version.size, version._records.sha256_hash):
        logging.debug('Ignoring already existing file: %s', destfile)
        return os.path.abspath(destfile)
    acq = apt_pkg.Acquire(progress or apt.progress.text.AcquireProgress())
    # pylint: disable=protected-access
    acqfile = apt_pkg.AcquireFile(acq,
                                  version.uri,
                                  "SHA256:" + version._records.sha256_hash,
                                  version.size,
                                  base,
                                  destfile=destfile)
    acq.run()

    if acqfile.status != acqfile.STAT_DONE:
        raise FetchError("The item %r could not be fetched: %s" %
                         (acqfile.destfile, acqfile.error_text))

    return os.path.abspath(destfile)
    def mark_pkg_download(self, pkgname):
        pkg = self.cache[pkgname]
        c = self.depcache.get_candidate_ver(pkg)

        r = apt_pkg.PackageRecords(self.cache)
        r.lookup(c.file_list[0])

        x = self.source.find_index(c.file_list[0][0])
        uri = x.archive_uri(r.filename)
        hashval = str(r.hashes.find('SHA256'))

        acq = apt_pkg.AcquireFile(self.acquire,
                                  uri,
                                  hash=hashval,
                                  size=c.size,
                                  descr=r.long_desc,
                                  short_descr=r.short_desc,
                                  destdir=self.basefs.fname('/cache/archives'))
        self.downloads[pkgname] = acq
Exemple #12
0
    def fetch_packages(self, packages, download_content=True):
        """Fetch the files for the given list of package names.

        The files, and all their dependencies are download, and the metadata
        and content returned as FetchedPackage objects.

        If download_content is False then only the metadata is returned
        (i.e. the FetchedPackages will have None for their content
         attribute), and only information about the specified packages
        will be returned, no dependencies.

        No packages that have been ignored, or are recursive dependencies
        of ignored packages will be returned.

        :param packages: a list of package names to install
        :type packages: an iterable of str
        :param download_content: whether to download the content of the
            packages. Default is to do so.
        :type download_content: bool
        :return: a list of the packages that were fetched, with relevant
            metdata and the contents of the files available.
        :rtype: an iterable of FetchedPackages.
        :raises KeyError: if any of the package names in the list couldn't
            be found.
        """
        fetched = {}
        for package in packages:
            candidate = self.cache.cache[package].candidate
            base = os.path.basename(candidate.filename)
            result_package = FetchedPackage.from_apt(candidate, base)
            fetched[package] = result_package

        def check_no_broken_packages():
            if self.cache.cache.broken_count:
                raise DependencyNotSatisfied(
                    "Unable to satisfy dependencies of %s" % ", ".join(
                        [p.name
                         for p in self.cache.cache if p.is_inst_broken]))

        for package in packages:
            try:
                self.cache.cache[package].mark_install(auto_fix=True)
            except SystemError:
                # Either we raise a DependencyNotSatisfied error
                # if some packages are broken, or we raise the original
                # error if there was another cause
                check_no_broken_packages()
                raise
            # Check that nothing was broken, even if mark_install didn't
            # raise SystemError, just to make sure.
            check_no_broken_packages()
        self._filter_ignored(fetched)
        if not download_content:
            self.cache.cache.clear()
            return fetched.values()
        acq = apt_pkg.Acquire(DummyProgress())
        acqfiles = []
        # re to remove the repo private key
        deb_url_auth_re = re.compile(
            r"(?P<transport>.*://)(?P<user>.*):.*@(?P<path>.*$)")
        for package in self.cache.cache.get_changes():
            if (package.marked_delete or package.marked_keep):
                continue
            logger.debug("Fetching %s ..." % package)
            candidate = package.candidate
            base = os.path.basename(candidate.filename)
            if package.name not in fetched:
                result_package = FetchedPackage.from_apt(candidate, base)
                fetched[package.name] = result_package
            result_package = fetched[package.name]
            destfile = os.path.join(self.cache.tempdir, base)
            acqfile = apt_pkg.AcquireFile(acq,
                                          candidate.uri,
                                          candidate.md5,
                                          candidate.size,
                                          base,
                                          destfile=destfile)
            acqfiles.append((acqfile, result_package, destfile))
            # check if we have a private key in the pkg url
            deb_url_auth = deb_url_auth_re.match(acqfile.desc_uri)
            if deb_url_auth:
                logger.debug(" ... from %s%s:***@%s" % deb_url_auth.groups())
            else:
                logger.debug(" ... from %s" % acqfile.desc_uri)
        self.cache.cache.clear()
        acq.run()
        for acqfile, result_package, destfile in acqfiles:
            if acqfile.status != acqfile.STAT_DONE:
                raise FetchError("The item %r could not be fetched: %s" %
                                 (acqfile.destfile, acqfile.error_text))
            result_package.content = open(destfile)
            result_package._file_path = destfile
        return fetched.values()