I have a script I use to prune packages that have gone stale from my custom Debian repository (i.e. they no longer exists on any of the official repositories):
import apt_pkg
import gzip
import subprocess
CUSTOM_REPO = ("/home/tshepang/.custom_repo/dists/tshepang/main/"
"binary-amd64/Packages.gz")
TEMPLATE = ("/var/lib/apt/lists/http.debian.net_debian_dists_{}_{}_"
"binary-amd64_Packages")
CODENAMES = 'jessie sid experimental'.split()
ARCHIVE_AREAS = "main contrib non-free".split()
def main():
custom_repo = apt_pkg.TagFile(gzip.open(CUSTOM_REPO))
wheezy_packages = list()
for codename in CODENAMES:
for archive_area in ARCHIVE_AREAS:
repo = TEMPLATE.format(codename, archive_area)
repo = apt_pkg.TagFile(gzip.open(repo, "rb"))
wheezy_packages.extend([pkg["Package"] for pkg in repo])
for package in custom_repo:
package_name = package["Package"]
if package_name not in wheezy_packages:
cmd = "apt-cache policy " + package_name
subprocess.call(cmd.split())
choice = raw_input("remove from cache [Y/n]? ")
if not choice or choice.lower().startswith("y"):
cmd = ("reprepro -vv --basedir /home/tshepang/.custom_repo/ "
"remove tshepang " + package_name)
subprocess.call(cmd.split())
if __name__ == "__main__":
main()
I strongly suspect the code can be better.
As a sidenote, here is sample UI:
apache2.2-common: Installed: (none) Candidate: 2.2.22-13 Version table: 2.2.22-13 0 500 file:/home/tshepang/.custom_repo/ tshepang/main amd64 Packages remove from cache [Y/n]? removing 'apache2.2-common' from 'tshepang|main|amd64'... Exporting indices... Deleting files no longer referenced... deleting and forgetting pool/main/a/apache2/apache2.2-common_2.2.22-13_amd64.deb
1 Answer 1
Your code looks very good, so I'll just propose some minor (and subjective) improvements:
Imports: PEP8 has some recommendations about grouping imports that sound pretty reasonable.
("s1" "s2")
: I don't know what the orthodoxy says, but I prefer an explicit+
in between.list()
: Why not[]
?wheezy_packages.extend([pkg["Package"] for pkg in repo])
->wheezy_packages.extend(pkg["Package"] for pkg in repo)
.repo = a
, thenrepo = f(repo)
. IMHO this is as common a pattern as is undesirable. Different values deserve different variable names (key point of functional programming).Function too long: I'd split the code in at least two auxiliary functions, one to get
wheezy_packages
and another to run the commands.
-
\$\begingroup\$
list()
was inspired by stackoverflow.com/a/2745292 \$\endgroup\$tshepang– tshepang2013年08月03日 12:21:38 +00:00Commented Aug 3, 2013 at 12:21 -
\$\begingroup\$ Is the explicit
+
in this case common practice? \$\endgroup\$tshepang– tshepang2013年08月03日 12:22:45 +00:00Commented Aug 3, 2013 at 12:22 -
\$\begingroup\$ Regarding the different values comment, see codereview.stackexchange.com/a/1719. \$\endgroup\$tshepang– tshepang2013年08月03日 12:33:17 +00:00Commented Aug 3, 2013 at 12:33
-
1\$\begingroup\$ 1)
list()
A.M. is certainly a reference to follow. 2)+
I prefer to write it, but not really sure what's better/more idiomatic. 3) I strongly disagree with that answer. In that example I'd usemessage_pattern
andpattern
, as I said in the answer, different values, different names. Do you know some Ruby. Those are my ideas about FP (99% can be applied to Python): slideshare.net/tokland/functional-programming-with-ruby-9975242 \$\endgroup\$tokland– tokland2013年08月03日 12:37:48 +00:00Commented Aug 3, 2013 at 12:37 -
\$\begingroup\$ Am not really familiar with Ruby. \$\endgroup\$tshepang– tshepang2013年08月03日 13:08:35 +00:00Commented Aug 3, 2013 at 13:08