5
\$\begingroup\$

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
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 2, 2013 at 20:23
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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, then repo = 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.

answered Aug 3, 2013 at 12:03
\$\endgroup\$
5
  • \$\begingroup\$ list() was inspired by stackoverflow.com/a/2745292 \$\endgroup\$ Commented Aug 3, 2013 at 12:21
  • \$\begingroup\$ Is the explicit + in this case common practice? \$\endgroup\$ Commented Aug 3, 2013 at 12:22
  • \$\begingroup\$ Regarding the different values comment, see codereview.stackexchange.com/a/1719. \$\endgroup\$ Commented 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 use message_pattern and pattern, 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\$ Commented Aug 3, 2013 at 12:37
  • \$\begingroup\$ Am not really familiar with Ruby. \$\endgroup\$ Commented Aug 3, 2013 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.