Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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.

share|improve this answer
up vote 2 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()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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