Esempio n. 1
0
    def check(self):
        """Check if the package is installable."""
        self._dbg(3, "check")

        self._check_was_run = True

        # check arch
        if not "Architecture" in self._sections:
            self._dbg(1, "ERROR: no architecture field")
            self._failure_string = _("No Architecture field in the package")
            return False
        arch = self._sections["Architecture"]
        if  arch != "all" and arch != apt_pkg.config.find("APT::Architecture"):
            if arch in apt_pkg.get_architectures():
                self._multiarch = arch
                self.pkgname = "%s:%s" % (self.pkgname, self._multiarch)
                self._dbg(1, "Found multiarch arch: '%s'" % arch)
            else:
                self._dbg(1, "ERROR: Wrong architecture dude!")
                self._failure_string = _("Wrong architecture '%s'") % arch
                return False

        self._failure_string = ""

        if self._cache._depcache.broken_count > 0:
            self._failure_string = _("Failed to satisfy all dependencies "
                                     "(broken cache)")
            # clean the cache again
            self._cache.clear()
            return False
        return True
Esempio n. 2
0
 def __init__(self, repository):
     self.__repository = repository
     self.__foreign = frozenset(apt_pkg.get_architectures()[1:])
     self.__packages = Dict()
     self.__versions = Dict()
     self.__resolve = {
         'Conflicts': self.__resolve_conflicts,
         'Depends': self.__resolve_depends,
         'Breaks': self.__resolve_conflicts,
         'Enhances': self.__resolve_ignore, # XXX
         'Obsoletes': self.__resolve_ignore, # XXX
         'PreDepends': self.__resolve_depends,
         'Recommends': self.__resolve_depends,
         'Replaces': self.__resolve_replaces,
         'Suggests': self.__resolve_ignore,
         }
     self.__ignore_forward = { 'Enhances', }
     self.__ignore_backward = { 'Depends', 'Recommends', 'Suggests', 'PreDepends', }
     self.__rank = 0
     # Initialize the candidate version for each package. The
     # information is required in __find_base_versions, so it makes
     # no sense to do it lazily.
     for p in map(self.wrapped_package, self.__repository.find_packages()):
         if p.has_versions:
             v = self.wrapped_version(self.__repository.find_candidate_version(p.underlying))
             p.candidate_version = v
             v.is_candidate_version = True
     # Automatically rank all base packages.
     self.rank(self.__find_base_versions(), 'D')
Esempio n. 3
0
    def check(self, allow_downgrade=False):
        """Check if the package is installable."""
        self._dbg(3, "check")

        self._check_was_run = True

        # check arch
        if "Architecture" not in self._sections:
            self._dbg(1, "ERROR: no architecture field")
            self._failure_string = _("No Architecture field in the package")
            return False
        arch = self._sections["Architecture"]
        if arch != "all" and arch != apt_pkg.config.find("APT::Architecture"):
            if arch in apt_pkg.get_architectures():
                self._multiarch = arch
                self.pkgname = "%s:%s" % (self.pkgname, self._multiarch)
                self._dbg(1, "Found multiarch arch: '%s'" % arch)
            else:
                self._dbg(1, "ERROR: Wrong architecture dude!")
                self._failure_string = _("Wrong architecture '%s'") % arch
                return False

        # check version
        if (not allow_downgrade and
            self.compare_to_version_in_cache() == self.VERSION_OUTDATED):
            if self._cache[self.pkgname].installed:
                # the deb is older than the installed
                self._failure_string = _(
                    "A later version is already installed")
                return False

        # FIXME: this sort of error handling sux
        self._failure_string = ""

        # check conflicts
        if not self.check_conflicts():
            return False

        # check if installing it would break anything on the
        # current system
        if not self.check_breaks_existing_packages():
            return False

        # try to satisfy the dependencies
        if not self._satisfy_depends(self.depends):
            return False

        # check for conflicts again (this time with the packages that are
        # makeed for install)
        if not self.check_conflicts():
            return False

        if self._cache._depcache.broken_count > 0:
            self._failure_string = _("Failed to satisfy all dependencies "
                                     "(broken cache)")
            # clean the cache again
            self._cache.clear()
            return False
        return True
Esempio n. 4
0
    def check(self, allow_downgrade=False):
        """Check if the package is installable."""
        self._dbg(3, "check")

        self._check_was_run = True

        # check arch
        if "Architecture" not in self._sections:
            self._dbg(1, "ERROR: no architecture field")
            self._failure_string = _("No Architecture field in the package")
            return False
        arch = self._sections["Architecture"]
        if arch != "all" and arch != apt_pkg.config.find("APT::Architecture"):
            if arch in apt_pkg.get_architectures():
                self._multiarch = arch
                self.pkgname = "%s:%s" % (self.pkgname, self._multiarch)
                self._dbg(1, "Found multiarch arch: '%s'" % arch)
            else:
                self._dbg(1, "ERROR: Wrong architecture dude!")
                self._failure_string = _("Wrong architecture '%s'") % arch
                return False

        # check version
        if (not allow_downgrade and
            self.compare_to_version_in_cache() == self.VERSION_OUTDATED):
            if self._cache[self.pkgname].installed:
                # the deb is older than the installed
                self._failure_string = _(
                    "A later version is already installed")
                return False

        # FIXME: this sort of error handling sux
        self._failure_string = ""

        # check conflicts
        if not self.check_conflicts():
            return False

        # check if installing it would break anything on the
        # current system
        if not self.check_breaks_existing_packages():
            return False

        # try to satisfy the dependencies
        if not self._satisfy_depends(self.depends):
            return False

        # check for conflicts again (this time with the packages that are
        # makeed for install)
        if not self.check_conflicts():
            return False

        if self._cache._depcache.broken_count > 0:
            self._failure_string = _("Failed to satisfy all dependencies "
                                     "(broken cache)")
            # clean the cache again
            self._cache.clear()
            return False
        return True
Esempio n. 5
0
 def test_multiarch_deb_check(self):
     if apt_pkg.get_architectures() != ["amd64", "i386"]:
         logging.warn("skipping test because running on a non-multiarch system")
         return
     deb = apt.debfile.DebPackage(
         "./data/test_debs/multiarch-test1_i386.deb")
     missing = deb.missing_deps
     #print missing
     self.assertFalse("dpkg:i386" in missing)
Esempio n. 6
0
def get_architecture():
    """Get the Debian architecture of the running system."""
    arch = os.getenv("SNAP_ARCH")
    if not arch:
        # assume it's a deb environment
        import apt_pkg

        apt_pkg.init()
        arch = apt_pkg.get_architectures()[0]
    return arch
Esempio n. 7
0
    def open(self, progress=None):
        """ Open the package cache, after that it can be used like
            a dictionary
        """
        if progress is None:
            progress = apt.progress.base.OpProgress()
        # close old cache on (re)open
        self.close()
        self.op_progress = progress
        self._run_callbacks("cache_pre_open")

        self._cache = apt_pkg.Cache(progress)
        self._depcache = apt_pkg.DepCache(self._cache)
        self._records = apt_pkg.PackageRecords(self._cache)
        self._list = apt_pkg.SourceList()
        self._list.read_main_list()
        self._set.clear()
        self._fullnameset.clear()
        self._sorted_set = None
        self._weakref.clear()

        self._have_multi_arch = len(apt_pkg.get_architectures()) > 1

        progress.op = _("Building data structures")
        i = last = 0
        size = len(self._cache.packages)
        for pkg in self._cache.packages:
            if progress is not None and last + 100 < i:
                progress.update(i / float(size) * 100)
                last = i
            # drop stuff with no versions (cruft)
            if pkg.has_versions:
                self._set.add(pkg.get_fullname(pretty=True))
                if self._have_multi_arch:
                    self._fullnameset.add(pkg.get_fullname(pretty=False))

            i += 1

        progress.done()
        self._run_callbacks("cache_post_open")
Esempio n. 8
0
    def open(self, progress=None):
        """ Open the package cache, after that it can be used like
            a dictionary
        """
        if progress is None:
            progress = apt.progress.base.OpProgress()
        # close old cache on (re)open
        self.close()
        self.op_progress = progress
        self._run_callbacks("cache_pre_open")

        self._cache = apt_pkg.Cache(progress)
        self._depcache = apt_pkg.DepCache(self._cache)
        self._records = apt_pkg.PackageRecords(self._cache)
        self._list = apt_pkg.SourceList()
        self._list.read_main_list()
        self._set.clear()
        self._fullnameset.clear()
        self._sorted_set = None
        self._weakref.clear()

        self._have_multi_arch = len(apt_pkg.get_architectures()) > 1

        progress.op = _("Building data structures")
        i = last = 0
        size = len(self._cache.packages)
        for pkg in self._cache.packages:
            if progress is not None and last + 100 < i:
                progress.update(i / float(size) * 100)
                last = i
            # drop stuff with no versions (cruft)
            if pkg.has_versions:
                self._set.add(pkg.get_fullname(pretty=True))
                if self._have_multi_arch:
                    self._fullnameset.add(pkg.get_fullname(pretty=False))

            i += 1

        progress.done()
        self._run_callbacks("cache_post_open")
Esempio n. 9
0
 def find_versions_by_priority(self, priority_name, origins=None, components=None):
     priority = {
         'required':apt_pkg.PRI_REQUIRED,
         'important':apt_pkg.PRI_IMPORTANT,
         'standard':apt_pkg.PRI_STANDARD,
         'optional':apt_pkg.PRI_OPTIONAL,
         'extra':apt_pkg.PRI_EXTRA,
     }[priority_name]
     foreign = frozenset(apt_pkg.get_architectures()[1:])
     result = []
     for p in self.__impl.repository.find_packages():
         if p.has_versions:
             v = self.__impl.repository.find_candidate_version(p)
             if v is None: continue
             if v.priority != priority: continue
             if v.arch in foreign: continue
             pf = v.file_list[0][0]
             if origins and pf.origin not in origins: continue
             if components and pf.component not in components: continue
             result.append(v)
     # sort the versions by name to make the result deterministic
     return map(self.__impl.version, sorted(result, key=lambda v: v.parent_pkg.name))
Esempio n. 10
0
    def check(self):
        """Check if the package is installable."""

        self.check_was_run = True

        if not "Architecture" in self.sections:
            print("eDeb Package Error: No Architecture field in the package")
            return "Package was created poorly. No Architecture field in the package"
        arch = self.sections["Architecture"]
        if  arch != "all" and arch != apt_pkg.config.find("APT::Architecture"):
            if arch in apt_pkg.get_architectures():
                self.multiarch = arch
                self.pkgname = "%s:%s" % (self.pkgname, self.multiarch)
            else:
                print("eDeb Package Error: Wrong architecture, %s" %arch)
                return "Package is not eligible for installation because of wrong architecture: <b>%s</b>" %arch

        if self.cache._depcache.broken_count > 0:
            print("eDeb Package Error: Failed to satisfy dependencies. Broken cache. Now clearing..")
            self.cache.clear()
            print("eDeb Notification: Cache cleared.")
            return "Broken dependencies from previous installation (broken cache). Cache has been cleared.<ps><ps>If the issue persists, please select Fix to attempt to complete the broken installation."
        return True
    def open(self, progress=None):
        """ Open the package cache, after that it can be used like
            a dictionary
        """
        if progress is None:
            progress = apt.progress.base.OpProgress()
        # close old cache on (re)open
        self.close()
        self.op_progress = progress
        self._run_callbacks("cache_pre_open")

        self._cache = apt_pkg.Cache(progress)
        self._depcache = apt_pkg.DepCache(self._cache)
        self._records = apt_pkg.PackageRecords(self._cache)
        self._list = apt_pkg.SourceList()
        self._list.read_main_list()
        self._sorted_set = None
        self.__remap()

        self._have_multi_arch = len(apt_pkg.get_architectures()) > 1

        progress.done()
        self._run_callbacks("cache_post_open")
Esempio n. 12
0
    def open(self, progress=None):
        """ Open the package cache, after that it can be used like
            a dictionary
        """
        if progress is None:
            progress = apt.progress.base.OpProgress()
        # close old cache on (re)open
        self.close()
        self.op_progress = progress
        self._run_callbacks("cache_pre_open")

        self._cache = apt_pkg.Cache(progress)
        self._depcache = apt_pkg.DepCache(self._cache)
        self._records = apt_pkg.PackageRecords(self._cache)
        self._list = apt_pkg.SourceList()
        self._list.read_main_list()
        self._sorted_set = None
        self._weakref.clear()

        self._have_multi_arch = len(apt_pkg.get_architectures()) > 1

        progress.done()
        self._run_callbacks("cache_post_open")
 def test_get_architectures(self):
     main_arch = apt.apt_pkg.config.get("APT::Architecture")
     arches = apt_pkg.get_architectures()
     self.assertTrue(main_arch in arches)
Esempio n. 14
0
 def test_get_architectures(self):
     main_arch = apt.apt_pkg.config.get("APT::Architecture")
     arches = apt_pkg.get_architectures()
     self.assertTrue(main_arch in arches)
Esempio n. 15
0
def install_deb(m, debs, cache, force, install_recommends, allow_unauthenticated, dpkg_options):
    changed = False
    deps_to_install = []
    pkgs_to_install = []
    for deb_file in debs.split(','):
        try:
            pkg = apt.debfile.DebPackage(deb_file)
            pkg_name = get_field_of_deb(m, deb_file, "Package")
            pkg_version = get_field_of_deb(m, deb_file, "Version")
            if len(apt_pkg.get_architectures()) > 1:
                pkg_arch = get_field_of_deb(m, deb_file, "Architecture")
                pkg_key = "%s:%s" % (pkg_name, pkg_arch)
            else:
                pkg_key = pkg_name
            try:
                installed_pkg = apt.Cache()[pkg_key]
                installed_version = installed_pkg.installed.version
                if package_version_compare(pkg_version, installed_version) == 0:
                    # Does not need to down-/upgrade, move on to next package
                    continue
            except Exception:
                # Must not be installed, continue with installation
                pass
            # Check if package is installable
            if not pkg.check() and not force:
                m.fail_json(msg=pkg._failure_string)

            # add any missing deps to the list of deps we need
            # to install so they're all done in one shot
            deps_to_install.extend(pkg.missing_deps)

        except Exception:
            e = get_exception()
            m.fail_json(msg="Unable to install package: %s" % str(e))

        # and add this deb to the list of packages to install
        pkgs_to_install.append(deb_file)

    # install the deps through apt
    retvals = {}
    if deps_to_install:
        (success, retvals) = install(m=m, pkgspec=deps_to_install, cache=cache,
                                     install_recommends=install_recommends,
                                     dpkg_options=expand_dpkg_options(dpkg_options))
        if not success:
            m.fail_json(**retvals)
        changed = retvals.get('changed', False)

    if pkgs_to_install:
        options = ' '.join(["--%s"% x for x in dpkg_options.split(",")])
        if m.check_mode:
            options += " --simulate"
        if force:
            options += " --force-all"

        cmd = "dpkg %s -i %s" % (options, " ".join(pkgs_to_install))
        rc, out, err = m.run_command(cmd)
        if "stdout" in retvals:
            stdout = retvals["stdout"] + out
        else:
            stdout = out
        if "diff" in retvals:
            diff = retvals["diff"]
            if 'prepared' in diff:
                diff['prepared'] += '\n\n' + out
        else:
            diff = parse_diff(out)
        if "stderr" in retvals:
            stderr = retvals["stderr"] + err
        else:
            stderr = err

        if rc == 0:
            m.exit_json(changed=True, stdout=stdout, stderr=stderr, diff=diff)
        else:
            m.fail_json(msg="%s failed" % cmd, stdout=stdout, stderr=stderr)
    else:
        m.exit_json(changed=changed, stdout=retvals.get('stdout', ''), stderr=retvals.get('stderr', ''), diff=retvals.get('diff', ''))
Esempio n. 16
0
def install_deb(m, debs, cache, force, install_recommends,
                allow_unauthenticated, dpkg_options):
    changed = False
    deps_to_install = []
    pkgs_to_install = []
    for deb_file in debs.split(','):
        try:
            pkg = apt.debfile.DebPackage(deb_file)
            pkg_name = get_field_of_deb(m, deb_file, "Package")
            pkg_version = get_field_of_deb(m, deb_file, "Version")
            if len(apt_pkg.get_architectures()) > 1:
                pkg_arch = get_field_of_deb(m, deb_file, "Architecture")
                pkg_key = "%s:%s" % (pkg_name, pkg_arch)
            else:
                pkg_key = pkg_name
            try:
                installed_pkg = apt.Cache()[pkg_key]
                installed_version = installed_pkg.installed.version
                if package_version_compare(pkg_version,
                                           installed_version) == 0:
                    # Does not need to down-/upgrade, move on to next package
                    continue
            except Exception:
                # Must not be installed, continue with installation
                pass
            # Check if package is installable
            if not pkg.check() and not force:
                m.fail_json(msg=pkg._failure_string)

            # add any missing deps to the list of deps we need
            # to install so they're all done in one shot
            deps_to_install.extend(pkg.missing_deps)

        except Exception:
            e = get_exception()
            m.fail_json(msg="Unable to install package: %s" % str(e))

        # and add this deb to the list of packages to install
        pkgs_to_install.append(deb_file)

    # install the deps through apt
    retvals = {}
    if len(deps_to_install) > 0:
        (success,
         retvals) = install(m=m,
                            pkgspec=deps_to_install,
                            cache=cache,
                            install_recommends=install_recommends,
                            dpkg_options=expand_dpkg_options(dpkg_options))
        if not success:
            m.fail_json(**retvals)
        changed = retvals.get('changed', False)

    if len(pkgs_to_install) > 0:
        options = ' '.join(["--%s" % x for x in dpkg_options.split(",")])
        if m.check_mode:
            options += " --simulate"
        if force:
            options += " --force-all"

        cmd = "dpkg %s -i %s" % (options, " ".join(pkgs_to_install))
        rc, out, err = m.run_command(cmd)
        if "stdout" in retvals:
            stdout = retvals["stdout"] + out
        else:
            stdout = out
        if "diff" in retvals:
            diff = retvals["diff"]
            if 'prepared' in diff:
                diff['prepared'] += '\n\n' + out
        else:
            diff = parse_diff(out)
        if "stderr" in retvals:
            stderr = retvals["stderr"] + err
        else:
            stderr = err

        if rc == 0:
            m.exit_json(changed=True, stdout=stdout, stderr=stderr, diff=diff)
        else:
            m.fail_json(msg="%s failed" % cmd, stdout=stdout, stderr=stderr)
    else:
        m.exit_json(changed=changed,
                    stdout=retvals.get('stdout', ''),
                    stderr=retvals.get('stderr', ''),
                    diff=retvals.get('diff', ''))
Esempio n. 17
0
 def __init__(self, files):
     self.files = files
     self.primary_arch = apt_pkg.get_architectures()[0]
     self.stats = {"total": 0, "total_time": time.time()}