Ejemplo n.º 1
0
 def __sources():
     '''
         Settings to use standard directory layout
     '''
     src = apt_pkg.SourceList()
     src.read_main_list()
     return src
Ejemplo n.º 2
0
    def test_index_file(self):
        apt_pkg.config["dir"] = "data/test_debs"
        slist = apt_pkg.SourceList()
        slist.read_main_list()

        for meta in slist.list:
            for index in meta.index_files:
                index.archive_uri(self.file_bytes)
                index.archive_uri(self.file_unicode)
Ejemplo n.º 3
0
 def downloadArchives(self):
     pkgAcquire, pkgSourceList = apt_pkg.Acquire(), apt_pkg.SourceList()
     self.logger.verbose("Reading main source lists...")
     pkgSourceList.read_main_list()
     self.logger.verbose("Downloading archives...")
     self.pkgManager = apt_pkg.PackageManager(self.depCache)
     self.pkgManager.get_archives(pkgAcquire, pkgSourceList,
                                  apt_pkg.PackageRecords(self.pkgCache))
     pkgAcquire.run()
     [package.update(install=False) for package in self.pkgList.values()]
Ejemplo n.º 4
0
    def update(self,
               fetch_progress=None,
               pulse_interval=0,
               raise_on_error=True,
               sources_list=None):
        # FIXME: type: (AcquireProgress, int, bool, str) -> int
        """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()
        .
        sources_list -- Update a alternative sources.list than the default.
        Note that the sources.list.d directory is ignored in this case
        """
        lockfile = apt_pkg.config.find_dir("Dir::State::Lists") + "lock"
        lock = apt_pkg.get_lock(lockfile)

        if lock < 0:
            raise LockFailedException("Failed to lock %s" % lockfile)

        if sources_list:
            old_sources_list = apt_pkg.config.find("Dir::Etc::sourcelist")
            old_sources_list_d = apt_pkg.config.find("Dir::Etc::sourceparts")
            old_cleanup = apt_pkg.config.find("APT::List-Cleanup")
            apt_pkg.config.set("Dir::Etc::sourcelist",
                               os.path.abspath(sources_list))
            apt_pkg.config.set("Dir::Etc::sourceparts", "xxx")
            apt_pkg.config.set("APT::List-Cleanup", "0")
            slist = apt_pkg.SourceList()
            slist.read_main_list()
        else:
            slist = self._list

        try:
            if fetch_progress is None:
                fetch_progress = apt.progress.base.AcquireProgress()
            try:
                res = self._cache.update(fetch_progress, slist, pulse_interval)
            except SystemError as e:
                raise FetchFailedException(e)
            if not res and raise_on_error:
                raise FetchFailedException()
            else:
                return res
        finally:
            os.close(lock)
            if sources_list:
                apt_pkg.config.set("Dir::Etc::sourcelist", old_sources_list)
                apt_pkg.config.set("Dir::Etc::sourceparts", old_sources_list_d)
                apt_pkg.config.set("APT::List-Cleanup", old_cleanup)
Ejemplo n.º 5
0
def main():
    apt_pkg.init_config()
    apt_pkg.init_system()
    acquire = apt_pkg.Acquire()
    slist = apt_pkg.SourceList()
    # Read the list
    slist.read_main_list()
    # Add all indexes to the fetcher.
    slist.get_indexes(acquire, True)
    # Now print the URI of every item.
    for item in acquire.items:
        print(item.desc_uri)
Ejemplo n.º 6
0
    def __init__(self, arch, suite, sources, prefs, keylist=None, noauth=False):

        # pylint: disable=too-many-arguments

        self.projectpath = mkdtemp()
        self.initialize_dirs()

        self.create_apt_sources_list(sources)
        self.create_apt_prefs(prefs)
        self.setup_gpg()
        if keylist:
            for k in keylist:
                self.add_pubkey_url(k)

        apt_pkg.config.set("APT::Architecture", arch)
        apt_pkg.config.set("APT::Architectures", arch)
        apt_pkg.config.set("Acquire::http::Proxy::127.0.0.1", "DIRECT")
        apt_pkg.config.set("APT::Install-Recommends", "0")
        apt_pkg.config.set("Dir::Etc", self.projectpath)
        apt_pkg.config.set("APT::Cache-Limit", "0")
        apt_pkg.config.set("APT::Cache-Start", "32505856")
        apt_pkg.config.set("APT::Cache-Grow", "2097152")
        apt_pkg.config.set("Dir::State", os.path.join(self.projectpath, "state"))
        apt_pkg.config.set("Dir::State::status", os.path.join(self.projectpath, "state/status"))
        apt_pkg.config.set("Dir::Cache", os.path.join(self.projectpath, "cache"))
        apt_pkg.config.set("Dir::Cache::archives", os.path.join(self.projectpath, "cache/archives"))
        apt_pkg.config.set("Dir::Etc", os.path.join(self.projectpath, "etc/apt"))
        apt_pkg.config.set("Dir::Log", os.path.join(self.projectpath, "log"))
        if noauth:
            apt_pkg.config.set("APT::Get::AllowUnauthenticated", "1")
            apt_pkg.config.set("Acquire::AllowInsecureRepositories", "1")
        else:
            apt_pkg.config.set("APT::Get::AllowUnauthenticated", "0")
            apt_pkg.config.set("Acquire::AllowInsecureRepositories", "0")

        apt_pkg.init_system()

        self.source = apt_pkg.SourceList()
        self.source.read_main_list()
        self.cache = apt_pkg.Cache()
        try:
            self.cache.update(self, self.source)
        except BaseException as e:
            print(e)

        apt_pkg.config.set("APT::Default-Release", suite)

        self.cache = apt_pkg.Cache()
        try:
            self.cache.update(self, self.source)
        except BaseException as e:
            print(e)
Ejemplo n.º 7
0
def packageList():
    import apt_pkg
    apt_pkg.init_config()
    apt_pkg.init_system()
    acquire = apt_pkg.Acquire()
    slist = apt_pkg.SourceList()
    slist.read_main_list()
    slist.get_indexes(acquire, True)

    output = {"packages": []}
    # Now print the URI of every item.
    for item in acquire.items:
        output["packages"].append(item.desc_uri)

    pipLog.sharedInstance.debug("APT:" + json.dumps(output))

    return sendMessage(json.dumps(output))
Ejemplo n.º 8
0
    def __init__(self, config_dir, state_dir):
        super().__init__(None)

        config = apt_pkg.config
        config["Dir::Etc"] = os.path.realpath(config_dir)
        config["Dir::State"] = os.path.join(os.path.realpath(state_dir), "state")
        config["Dir::Cache"] = os.path.join(os.path.realpath(state_dir), "cache")
        apt_pkg.init_config()
        apt_pkg.init_system()

        lists = apt_pkg.config.find_dir("Dir::State::Lists")
        os.makedirs(lists, exist_ok=True)
        os.makedirs(config["Dir::Cache"], exist_ok=True)

        self.cache = apt_pkg.Cache(None)
        self.depcache = apt_pkg.DepCache(self.cache)
        self.source_list = apt_pkg.SourceList()
        self.source_list.read_main_list()
Ejemplo n.º 9
0
    def __init__(self, name, arch, suite, sources, prefs, keylist=[]):

        self.projectpath = mkdtemp()
        self.initialize_dirs()

        self.create_apt_sources_list(sources)
        self.create_apt_prefs(prefs)
        self.setup_gpg()
        for k in keylist:
            self.add_pubkey_url(k)

        apt_pkg.config.set("APT::Architecture", arch)
        apt_pkg.config.set("Acquire::http::Proxy::127.0.0.1", "DIRECT")
        apt_pkg.config.set("APT::Install-Recommends", "0")
        apt_pkg.config.set("Dir", self.projectpath)
        apt_pkg.config.set("APT::Cache-Limit", "0")
        apt_pkg.config.set("APT::Cache-Start", "32505856")
        apt_pkg.config.set("APT::Cache-Grow", "2097152")
        apt_pkg.config.set("Dir::State", "state")
        apt_pkg.config.set("Dir::State::status", "status")
        apt_pkg.config.set("Dir::Cache", "cache")
        apt_pkg.config.set("Dir::Etc", "etc/apt")
        apt_pkg.config.set("Dir::Log", "log")
        apt_pkg.config.set("APT::Get::AllowUnauthenticated", "0")

        apt_pkg.init_system()

        self.source = apt_pkg.SourceList()
        self.source.read_main_list()
        self.cache = apt_pkg.Cache()
        try:
            self.cache.update(self, self.source)
        except:
            pass

        apt_pkg.config.set("APT::Default-Release", suite)

        self.cache = apt_pkg.Cache()
        try:
            self.cache.update(self, self.source)
        except:
            pass
Ejemplo n.º 10
0
def get_current_mirror_hostname():
    apt_pkg.init_config()
    apt_pkg.init_system()
    source_list_obj = apt_pkg.SourceList()
    source_list_obj.read_main_list()
    urls = source_list_obj.list
    all_uris = [url.uri for url in urls]
    filter_results = filter(lambda url: url.endswith("deepin/"), all_uris)
    if len(filter_results) > 0:
        return get_hostname(filter_results[0])
    else:
        filter_results = filter(lambda url: in_string("/deepin", url), all_uris)
        if len(filter_results) > 0:
            return get_hostname(filter_results[0])
        else:
            filter_results = filter(lambda url: in_string("deepin", url), all_uris)
            if len(filter_results) > 0:
                return get_hostname(filter_results[0])
            else:
                return get_hostname(all_uris[0])
Ejemplo n.º 11
0
        def __init__ (self, name, arch, suite, sources, prefs):

                print arch

                self.projectpath = mkdtemp()
                self.initialize_dirs ()

                self.create_apt_sources_list (sources)
                self.create_apt_prefs        (prefs)

                apt_pkg.config.set ("APT::Architecture", arch)
                apt_pkg.config.set ("APT::Install-Recommends", "0")
                apt_pkg.config.set ("Dir", self.projectpath)
                apt_pkg.config.set ("APT::Cache-Limit", "0")
                apt_pkg.config.set ("APT::Cache-Start", "32505856")
                apt_pkg.config.set ("APT::Cache-Grow", "2097152")
                apt_pkg.config.set ("Dir::State", "state")
                apt_pkg.config.set ("Dir::State::status", "status")
                apt_pkg.config.set ("Dir::Cache", "cache")
                apt_pkg.config.set ("Dir::Etc", "etc/apt")
                apt_pkg.config.set ("Dir::Log", "log")
                apt_pkg.config.set ("APT::Get::AllowUnauthenticated", "1")

                apt_pkg.init_system()

                self.source = apt_pkg.SourceList ()
                self.source.read_main_list()
                self.cache = apt_pkg.Cache ()
                try:
                    self.cache.update(self,self.source)
                except:
                    pass

                apt_pkg.config.set ("APT::Default-Release", suite)

                self.cache = apt_pkg.Cache ()
                try:
                    self.cache.update(self,self.source)
                except:
                    pass
Ejemplo 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._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")
Ejemplo n.º 13
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.__remap()

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

        progress.done()
        self._run_callbacks("cache_post_open")
Ejemplo n.º 14
0
def get_deb_versions_info(apt_pkg=None) -> Optional[DebVersionsInfo]:
    """Return versions information for Debian-based MAAS."""

    if apt_pkg is None:
        import apt_pkg

        apt_pkg.init()

    try:
        cache = apt_pkg.Cache(None)
    except SystemError:
        maaslog.error(
            "Installed version could not be determined. Ensure "
            "/var/lib/dpkg/status is valid."
        )
        return None

    depcache = apt_pkg.DepCache(cache)
    sources = apt_pkg.SourceList()
    sources.read_main_list()
    policy = apt_pkg.Policy(cache)
    policy.init_defaults()

    current, update, = (
        None,
        None,
    )
    for package in MAAS_PACKAGES:
        current, update = _get_deb_current_and_update(
            cache, depcache, sources, policy, package
        )
        if current:
            break
    else:
        return None
    return DebVersionsInfo(current=current, update=update)
Ejemplo n.º 15
0
    def __init__(self, xml):

        # pylint: disable=too-many-statements
        self.xml = xml

        arch = xml.text("project/buildimage/arch", key="arch")
        suite = xml.text("project/suite")

        self.basefs = TmpdirFilesystem()
        self.initialize_dirs()

        create_apt_prefs(self.xml, self.basefs)

        mirror = self.xml.create_apt_sources_list(build_sources=True,
                                                  initvm=False)
        self.basefs.write_file("etc/apt/sources.list", 0o644, mirror)

        self.setup_gpg()
        self.import_keys()

        apt_pkg.config.set("APT::Architecture", arch)
        apt_pkg.config.set("APT::Architectures", arch)
        apt_pkg.config.set("Acquire::http::Proxy::127.0.0.1", "DIRECT")
        apt_pkg.config.set("APT::Install-Recommends", "0")
        apt_pkg.config.set("Dir::Etc", self.basefs.fname('/'))
        apt_pkg.config.set("Dir::Etc::Trusted",
                           self.basefs.fname('/etc/apt/trusted.gpg'))
        apt_pkg.config.set("Dir::Etc::TrustedParts",
                           self.basefs.fname('/etc/apt/trusted.gpg.d'))
        apt_pkg.config.set("APT::Cache-Limit", "0")
        apt_pkg.config.set("APT::Cache-Start", "32505856")
        apt_pkg.config.set("APT::Cache-Grow", "2097152")
        apt_pkg.config.set("Dir::State", self.basefs.fname("state"))
        apt_pkg.config.set("Dir::State::status",
                           self.basefs.fname("state/status"))
        apt_pkg.config.set("Dir::Cache", self.basefs.fname("cache"))
        apt_pkg.config.set("Dir::Cache::archives",
                           self.basefs.fname("cache/archives"))
        apt_pkg.config.set("Dir::Etc", self.basefs.fname("etc/apt"))
        apt_pkg.config.set("Dir::Log", self.basefs.fname("log"))
        if self.xml.has('project/noauth'):
            apt_pkg.config.set("APT::Get::AllowUnauthenticated", "1")
            apt_pkg.config.set("Acquire::AllowInsecureRepositories", "1")
        else:
            apt_pkg.config.set("APT::Get::AllowUnauthenticated", "0")
            apt_pkg.config.set("Acquire::AllowInsecureRepositories", "0")

        apt_pkg.init_system()

        self.source = apt_pkg.SourceList()
        self.source.read_main_list()
        self.cache = apt_pkg.Cache()
        try:
            self.cache.update(self, self.source)
        except BaseException as e:
            print(e)

        apt_pkg.config.set("APT::Default-Release", suite)

        self.cache = apt_pkg.Cache()
        try:
            self.cache.update(self, self.source)
        except BaseException as e:
            print(e)

        try:
            self.depcache = apt_pkg.DepCache(self.cache)
            prefs_name = self.basefs.fname("/etc/apt/preferences")
            self.depcache.read_pinfile(prefs_name)
        except BaseException as e:
            print(e)

        self.downloads = {}
        self.acquire = apt_pkg.Acquire(self)
Ejemplo n.º 16
0
#!/usr/bin/python3

import apt_pkg

apt_pkg.init()

sources = apt_pkg.SourceList()
sources.read_main_list()

cache = apt_pkg.Cache()
depcache = apt_pkg.DepCache(cache)
pkg = cache["libimlib2"]
cand = depcache.get_candidate_ver(pkg)
for (f, i) in cand.file_list:
    index = sources.find_index(f)
    print(index)
    if index:
        print(index.size)
        print(index.is_trusted)
        print(index.exists)
        print(index.Haspackages)
        print(index.archive_uri("some/path"))
Ejemplo n.º 17
0
def f_getlist():
    list = apt_pkg.SourceList()
    list.read_main_list()
    for a in list:
        print a
Ejemplo n.º 18
0
    args = parser.parse_args()

    if args.fromaddr and not args.toaddr:
        parser.error("Can't specify from without to")
    if args.toaddr and not args.fromaddr:
        parser.error("Can't specify to without from")

    status = StringIO()

    apt_pkg.init()

	# Turn off cache to avoid concurrency issues
    apt_pkg.config.set("Dir::Cache::pkgcache","")

    # "apt-get update"
    sl = apt_pkg.SourceList()
    sl.read_main_list()
    tmpcache = apt_pkg.Cache(_DevNullProgress())
    tmpcache.update(_DevNullProgress(), sl)

    # Now do the actual check
    cache = apt_pkg.Cache(_DevNullProgress())
    depcache = apt_pkg.DepCache(cache)
    depcache.read_pinfile()
    depcache.init()

    if depcache.broken_count > 0:
        status.write("Depcache broken count is {0}\n\n".format(depcache.broken_count))

    depcache.upgrade(True)
Ejemplo n.º 19
0
def _apply_update(fname, status):

    try:
        xml = etree(fname)
    except:
        return "read %s failed" % fname

    fpl = xml.node("fullpkgs")

    sources = apt_pkg.SourceList()
    sources.read_main_list()

    status.log("initialize apt")
    apt_pkg.init()
    cache = apt_pkg.Cache(progress=ElbeOpProgress(cb=status.log))

    status.set_progress(1)
    status.log("updating package cache")
    cache.update(ElbeAcquireProgress(cb=status.log), sources)
    # quote from python-apt api doc: "A call to this method does not affect the
    # current Cache object, instead a new one should be created in order to use
    # the changed index files."
    cache = apt_pkg.Cache(progress=ElbeOpProgress(cb=status.log))
    depcache = apt_pkg.DepCache(cache)
    hl_cache = apt.cache.Cache(progress=ElbeOpProgress(cb=status.log))
    hl_cache.update(fetch_progress=ElbeAcquireProgress(cb=status.log))

    # go through package cache, if a package is in the fullpkg list of the XML
    #  mark the package for installation (with the specified version)
    #  if it is not mentioned in the fullpkg list purge the package out of the
    #  system.
    status.set_progress(2)
    status.log("calculating packages to install/remove")
    count = len(hl_cache)
    step = count / 10
    i = 0
    percent = 0
    for p in hl_cache:
        i = i + 1
        if not (i % step):
            percent = percent + 10
            status.log(str(percent) + "% - " + str(i) + "/" + str(count))
            status.set_progress(2, str(percent) + "%")

        pkg = cache[p.name]
        marked = False
        for fpi in fpl:
            if pkg.name == fpi.et.text:
                mark_install(depcache, pkg, fpi.et.get('version'),
                             fpi.et.get('auto'), status)
                marked = True

        if not marked:
            depcache.mark_delete(pkg, True)

    status.set_progress(3)
    status.log("applying snapshot")
    depcache.commit(ElbeAcquireProgress(cb=status.log),
                    ElbeInstallProgress(cb=status.log))
    del depcache
    del hl_cache
    del cache
    del sources

    version_file = open("/etc/updated_version", "w")
    version_file.write(xml.text("/project/version"))
    version_file.close()
Ejemplo n.º 20
0
    gc.collect()
    f = open("%s" % i, "w")
    for obj in gc.get_objects():
        f.write("%s\n" % str(obj))
    f.close()

# memleak
#for i in range(100):
#       cache = apt.Cache()
#       time.sleep(1)
#       cache = None
#       gc.collect()

# no memleak, but more or less the apt.Cache.open() code
for i in range(100):
    cache = apt_pkg.Cache()
    depcache = apt_pkg.DepCache(cache)
    records = apt_pkg.PackageRecords(cache)
    list = apt_pkg.SourceList()
    list.ReadMainList()
    dict = {}
    for pkg in cache.Packages:
        if len(pkg.VersionList) > 0:
            dict[pkg.Name] = apt.Package(cache, depcache, records, list, None,
                                         pkg)

    print cache["apt"]
    time.sleep(1)

    gc.collect()
Ejemplo n.º 21
0
def main():
    tagf = apt_pkg.TagFile(open('/var/lib/dpkg/status'))
    for section in tagfile:
        print section['Package']
    sl = apt_pkg.SourceList()
    print sl.list