I need to add a ppa to remote servers using a python script. The bash equivalent of what I want to do is:

$ add-apt-repository ppa:user/ppa-name

I'm assuming it would look something like this:

import apt
cache = apt.Cache()
# ?? add the ppa here ??
cache.update()
cache.open(None)
cache['package_from_ppa'].mark_install()
cache.upgrade()
cache.commit()

but I haven't been able to find much in the apt module source related to adding repositories.

link|improve this question
feedback

2 Answers

add-apt-repository is written in Python; it should be fairly trivial to examine what it's doing and replicate the necessary lines of code in your own program.

link|improve this answer
feedback
up vote 0 down vote accepted

Here's what I ended up doing.

Install the software-properties package:

$ sudo apt-get install python-software-properties

Then in your python script:

import apt
from softwareproperties.SoftwareProperties import SoftwareProperties

sp = SoftwareProperties()
to_add = 'ppa:user/repository'
sp.add_source_from_line(to_add)
sp.sourceslist.save()

cache = apt.Cache()
cache.update()
cache.open(None)
cache['package_from_ppa'].mark_install()
cache.upgrade()
cache.commit()
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.