diff options
Diffstat (limited to '')
-rw-r--r-- | doc/source/examples/apt-cdrom.py | 79 | ||||
-rwxr-xr-x | doc/source/examples/cache-packages.py | 23 | ||||
-rwxr-xr-x | doc/source/examples/cache-pkgfile.py | 30 | ||||
-rwxr-xr-x | doc/source/examples/dpkg-contents.py | 68 | ||||
-rwxr-xr-x | doc/source/examples/dpkg-extract.py | 27 | ||||
-rwxr-xr-x | doc/source/examples/dpkg-info.py | 22 | ||||
-rwxr-xr-x | doc/source/examples/missing-deps.py | 59 | ||||
-rwxr-xr-x | doc/source/examples/update-print-uris.py | 24 |
8 files changed, 332 insertions, 0 deletions
diff --git a/doc/source/examples/apt-cdrom.py b/doc/source/examples/apt-cdrom.py new file mode 100644 index 0000000..13bfb97 --- /dev/null +++ b/doc/source/examples/apt-cdrom.py @@ -0,0 +1,79 @@ +#!/usr/bin/python3 +import sys + +import apt_pkg + +import apt + + +def show_help(): + print(f"apt {apt_pkg.VERSION} compiled on {apt_pkg.DATE} {apt_pkg.TIME}") + if apt_pkg.config.find_b("version"): + return 0 + + # Copied from apt-cdrom + print( + "Usage: apt-cdrom [options] command\n" + "\n" + "apt-cdrom is a tool to add CDROM's to APT's source list. The\n" + "CDROM mount point and device information is taken from apt.conf\n" + "and /etc/fstab.\n" + "\n" + "Commands:\n" + " add - Add a CDROM\n" + " ident - Report the identity of a CDROM\n" + "\n" + "Options:\n" + " -h This help text\n" + " -d CD-ROM mount point\n" + " -r Rename a recognized CD-ROM\n" + " -m No mounting\n" + " -f Fast mode, don't check package files\n" + " -a Thorough scan mode\n" + " -c=? Read this configuration file\n" + " -o=? Set an arbitrary configuration option, eg -o " + "dir::cache=/tmp\n" + "See fstab(5)" + ) + return 0 + + +def main(args): + arguments = apt_pkg.parse_commandline( + apt_pkg.config, + [ + ("h", "help", "help"), + ("v", "version", "version"), + ("d", "cdrom", "Acquire::cdrom::mount", "HasArg"), + ("r", "rename", "APT::CDROM::Rename"), + ("m", "no-mount", "APT::CDROM::NoMount"), + ("f", "fast", "APT::CDROM::Fast"), + ("n", "just-print", "APT::CDROM::NoAct"), + ("n", "recon", "APT::CDROM::NoAct"), + ("n", "no-act", "APT::CDROM::NoAct"), + ("a", "thorough", "APT::CDROM::Thorough"), + ("c", "config-file", "", "ConfigFile"), + ("o", "option", "", "ArbItem"), + ], + args, + ) + + if apt_pkg.config.find_b("help") or apt_pkg.config.find_b("version"): + return show_help() + + progress = apt.progress.text.CdromProgress() + cdrom = apt_pkg.Cdrom() + + if not arguments: + return show_help() + elif arguments[0] == "add": + cdrom.add(progress) + elif arguments[0] == "ident": + cdrom.ident(progress) + else: + sys.stderr.write("E: Invalid operation %s\n" % arguments[0]) + return 1 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/doc/source/examples/cache-packages.py b/doc/source/examples/cache-packages.py new file mode 100755 index 0000000..14ba85a --- /dev/null +++ b/doc/source/examples/cache-packages.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 +"""Example for packages. Print all essential and important packages""" + +import apt_pkg + + +def main(): + """Main.""" + apt_pkg.init_config() + apt_pkg.init_system() + cache = apt_pkg.Cache() + print("Essential packages:") + for pkg in cache.packages: + if pkg.essential: + print(" ", pkg.name) + print("Important packages:") + for pkg in cache.packages: + if pkg.important: + print(" ", pkg.name) + + +if __name__ == "__main__": + main() diff --git a/doc/source/examples/cache-pkgfile.py b/doc/source/examples/cache-pkgfile.py new file mode 100755 index 0000000..4e94a7a --- /dev/null +++ b/doc/source/examples/cache-pkgfile.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 +import apt_pkg + + +def main(): + """Example for PackageFile()""" + apt_pkg.init() + cache = apt_pkg.Cache() + for pkgfile in cache.file_list: + print("Package-File:", pkgfile.filename) + print("Index-Type:", pkgfile.index_type) # 'Debian Package Index' + if pkgfile.not_source: + print("Source: None") + else: + if pkgfile.site: + # There is a source, and a site, print the site + print("Source:", pkgfile.site) + else: + # It seems to be a local repository + print("Source: Local package file") + if pkgfile.not_automatic: + # The system won't be updated automatically (eg. experimental) + print("Automatic: No") + else: + print("Automatic: Yes") + print() + + +if __name__ == "__main__": + main() diff --git a/doc/source/examples/dpkg-contents.py b/doc/source/examples/dpkg-contents.py new file mode 100755 index 0000000..e6ff620 --- /dev/null +++ b/doc/source/examples/dpkg-contents.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 +"""Emulate dpkg --contents""" + +import grp +import pwd +import stat +import sys +import time + +import apt_inst + + +def format_mode(member): + """Return the symbolic mode""" + mode = member.mode + if member.isdir(): + s_mode = "d" + elif member.islnk(): + s_mode = "h" + else: + s_mode = "-" + s_mode += (mode & stat.S_IRUSR) and "r" or "-" + s_mode += (mode & stat.S_IWUSR) and "w" or "-" + s_mode += ( + (mode & stat.S_IXUSR) + and (mode & stat.S_ISUID and "s" or "x") + or (mode & stat.S_ISUID and "S" or "-") + ) + s_mode += (mode & stat.S_IRGRP) and "r" or "-" + s_mode += (mode & stat.S_IWGRP) and "w" or "-" + s_mode += ( + (mode & stat.S_IXGRP) + and (mode & stat.S_ISGID and "s" or "x") + or (mode & stat.S_ISGID and "S" or "-") + ) + s_mode += (mode & stat.S_IROTH) and "r" or "-" + s_mode += (mode & stat.S_IWOTH) and "w" or "-" + s_mode += (mode & stat.S_IXOTH) and "x" or "-" + return s_mode + + +def callback(member, data): + """callback for deb_extract""" + s_mode = format_mode(member) + s_owner = f"{pwd.getpwuid(member.uid)[0]}/{grp.getgrgid(member.gid)[0]}" + s_size = "%9d" % member.size + s_time = time.strftime("%Y-%m-%d %H:%M", time.localtime(member.mtime)) + s_name = member.name if member.name.startswith(".") else ("./" + member.name) + if member.islnk(): + s_name += " link to %s" % member.linkname + print(s_mode, s_owner, s_size, s_time, s_name) + + +def main(): + """Main function""" + if len(sys.argv) < 2: + print("need filename argumnet", file=sys.stderr) + sys.exit(1) + + fobj = open(sys.argv[1]) + try: + apt_inst.DebFile(fobj).data.go(callback) + finally: + fobj.close() + + +if __name__ == "__main__": + main() diff --git a/doc/source/examples/dpkg-extract.py b/doc/source/examples/dpkg-extract.py new file mode 100755 index 0000000..02a9fdd --- /dev/null +++ b/doc/source/examples/dpkg-extract.py @@ -0,0 +1,27 @@ +#!/usr/bin/python3 +"""Emulate dpkg --extract package.deb outdir""" + +import os +import sys + +import apt_inst + + +def main(): + """Main function.""" + if len(sys.argv) < 3: + print("Usage: %s package.deb outdir\n" % (__file__), file=sys.stderr) + sys.exit(1) + if not os.path.exists(sys.argv[2]): + print("The directory %s does not exist\n" % (sys.argv[2]), file=sys.stderr) + sys.exit(1) + + fobj = open(sys.argv[1]) + try: + apt_inst.DebFile(fobj).data.extractall(sys.argv[2]) + finally: + fobj.close() + + +if __name__ == "__main__": + main() diff --git a/doc/source/examples/dpkg-info.py b/doc/source/examples/dpkg-info.py new file mode 100755 index 0000000..833c4ed --- /dev/null +++ b/doc/source/examples/dpkg-info.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +"""Emulate dpkg --info package.deb control-file""" + +import sys + +from apt_inst import DebFile + + +def main(): + """Main function.""" + if len(sys.argv) < 3: + print("Usage: tool file.deb control-file\n", file=sys.stderr) + sys.exit(0) + fobj = open(sys.argv[1]) + try: + print(DebFile(fobj).control.extractdata(sys.argv[2])) + finally: + fobj.close() + + +if __name__ == "__main__": + main() diff --git a/doc/source/examples/missing-deps.py b/doc/source/examples/missing-deps.py new file mode 100755 index 0000000..c3c56b8 --- /dev/null +++ b/doc/source/examples/missing-deps.py @@ -0,0 +1,59 @@ +#!/usr/bin/python3 +"""Check the archive for missing dependencies""" +import apt_pkg + + +def fmt_dep(dep): + """Format a Dependency object [of apt_pkg] as a string.""" + ret = dep.target_pkg.name + if dep.target_ver: + ret += f" ({dep.comp_type} {dep.target_ver})" + return ret + + +def check_version(pkgver): + """Check the version of the package""" + missing = [] + + for or_group in pkgver.depends_list.get( + "Pre-Depends", [] + ) + pkgver.depends_list.get("Depends", []): + if not any(dep.all_targets() for dep in or_group): + # If none of the or-choices can be satisfied, add it to missing + missing.append(or_group) + + if missing: + print("Package:", pkgver.parent_pkg.name) + print("Version:", pkgver.ver_str) + print("Missing:") + print( + ", ".join(" | ".join(fmt_dep(dep) for dep in or_group)) + for or_group in missing + ) + print() + + +def main(): + """The main function.""" + apt_pkg.init_config() + apt_pkg.init_system() + + cache = apt_pkg.Cache() + + for pkg in sorted(cache.packages, key=lambda pkg: pkg.name): + # pkg is from a list of packages, sorted by name. + for version in pkg.version_list: + # Check every version + for pfile, _ in version.file_list: + if ( + pfile.origin == "Debian" + and pfile.component == "main" + and pfile.archive == "unstable" + ): + # We only want packages from Debian unstable main. + check_version(version) + break + + +if __name__ == "__main__": + main() diff --git a/doc/source/examples/update-print-uris.py b/doc/source/examples/update-print-uris.py new file mode 100755 index 0000000..02981f8 --- /dev/null +++ b/doc/source/examples/update-print-uris.py @@ -0,0 +1,24 @@ +#!/usr/bin/python3 +"""Print out the URIs of all indexes files. + +This behaves somewhat like apt-get --print-uris update.""" +import apt_pkg + + +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) + + +if __name__ == "__main__": + main() |