Stephen Rasku
Stephen Rasku

Reputation: 2682

Installing dependencies of debian/control file

I am in the process of porting a Ruby file used in our build system to Python. The file looks for Depends lines in a debian/control file in our repository, checks every dependency, and apt-get installs everything that isn't installed. I am trying to reproduce this functionality.

As part of porting this to Python, I looked at the deb_pkg_tools module. I pip installed it and created a simple script, install-dep2.py.

#!/usr/bin/python
import deb_pkg_tools

controlDict = deb_pkg_tools.control.load_control_file('debian/control')

However, when I run this script, I get the following error:

$ build/bin/install-dep2.py
Traceback (most recent call last):
  File "build/bin/install-dep2.py", line 4, in <module>
    controlDict = deb_pkg_tools.control.load_control_file('debian/control')
AttributeError: 'module' object has no attribute 'control'

The debian/control file exists:

$ ls -l debian/control
-rw-rw-r-- 1 stephen stephen 2532 Jul 13 14:28 debian/control

How can I process this debian/control file? I don't need to use deb_pkg_tools if there is a better way.

Upvotes: 2

Views: 2717

Answers (2)

uml&#228;ute
uml&#228;ute

Reputation: 31274

You might want to have a look into mk-build-deps (from the devscripts package) that is a standard script that already does what you want to achieve.

$ mk-build-deps -i -s sudo

Upvotes: 2

Joseph Bisch
Joseph Bisch

Reputation: 176

The problem you have is not that Python thinks that debian/control does not exist, but rather that it seems like deb_pkg_tools.control does not exist.

I would use the python-debian package from Debian to parse the control file if I were you. Here is the code that will parse the control file to get the dependencies. It should work even for packages with multiple binary packages.

import deb822

for paragraph in deb822.Deb822.iter_paragraphs(open('debian/control')):
    for item in paragraph.items():
        if item[0] == 'Depends':
            print item[1]

Each item in the above example is a tuple that pairs the "key" with the "value", so item[0] gives us the "key" and item[1] gives us the "value".

Obviously the above sample just prints out the dependencies as they are in the control file, so the dependencies aren't in a format that is suitable to directly plug into apt-get install. Also, by parsing the control file, I got stuff like ${python:Depends} in addition to actual package names, so that is something you will have to consider. Here is an example of the output I got from the above example:

joseph@crunchbang:~$ python test.py 

 bittornado,
 ${python:Depends},
 python-psutil,
 python-qt4,
 python-qt4reactor,
 python-twisted,
 xdg-utils,
 ${misc:Depends},
 ${shlibs:Depends}

I found this bug report and the python-debian source code to be quite useful resources when answering your question.

Upvotes: 2

Related Questions