Quickly change Debian repositories

Apt is awesome. Plain and simple.

But it is kinda static. By this I mean it’s not particularly suited to environments where things change frequently. For example, we have a local mirror at uni, which of course it much faster than using the external Ubuntu or Debian ones, however as this is only available from internal University of Auckland IP addresses I would have to change my /etc/apt/sources.list file if I wanted to install something from home.

Today I knocked together a quick Python script to fix this, all it does is basically manipulate a symlink which points to the real /etc/apt/sources.list file, but I thought I’d share it anyway:

#!/usr/bin/env python
import sys, os
def usage():
print "Usage: %s " % (sys.argv[0],)
def main(argv):
if argv[1] != None:
sources_path = "/etc/apt/sources.list.%s" % (argv[1],)
if os.path.exists(sources_path):
os.unlink("/etc/apt/sources.list")
os.symlink(sources_path, "/etc/apt/sources.list")
print "SUCCESS: Don't for get to run 'sudo apt-get update'"
return 0
else:
print "ERROR: Unknown repository set '%s'" % (argv[1],)
usage()
else:
usage()
return 1
if __name__ == '__main__':
sys.exit(main(sys.argv))

Unfortunately wordpress screwed with my formatting there (which kinda matters in a python script) so here is the file (repo-switch.py). You’ll need to download it and put it somewhere in your $PATH. Then make it execuatable:

chmod +x repo-switch.py

The next step is to set up your lists of repositories. Each list will have a suffix which the script will use to identify it. First I copied my main repositories (for the NZ Ubuntu mirrors):

sudo cp /etc/apt/sources.list /etc/apt/sources.list.nz

Obviously replace ‘nz’ with a suffix of your choice. Then I dropped the list of repos for the local mirror into another file (in my case /etc/apt/sources.list.ece). You can set up as many different sets as you like, but I only need two.

Next step is to practice switching between them:

sudo repo-switch.py nz

…sets apt to use the file /etc/apt/sources.list.nz by changing /etc/apt.sources.list to a symlink pointing to the relevant place. Calling the script again with a different set (e.g. ‘ece’ in my case) will set it up to use one of your other sets.

Obviously, you’ll need to run ‘sudo apt-get update’ each time to make apt work with the new repos, but I added a line to the script to remind you!

Magic!

Leave a Reply

Your email address will not be published. Required fields are marked *

Bad Behavior has blocked 788 access attempts in the last 7 days.