Is there a standard way to create Debian packages for distributing Python programs?

我与影子孤独终老i 提交于 2019-11-27 05:04:13

问题


There is a ton of information on how to do this, but since "there is more than one way to skin a cat", and all the tutorials/manuals that cover a bit of the process seem to make certain assumptions which are different from other tutorials, I still didn't manage to grasp it.

So far this is what I think I understood.

  1. My final goal should be that of creating a "binary" .deb package. Such package will be platform-independent (32/64 bit) as all Python programs are such.
  2. To create a "binary" package I need first to create a source package.
  3. To create the source package I can use either CDBS or debhelper. Debhelper is the recommended way for beginners.
  4. The core of creating a source package is populating the DEBIAN directory in the source directory with a number of files clarifying where files need to be copied, what copyright and licensing scheme they are subject to, what dependencies they have, etc...
  5. Step #4 can be largely automated the dh_makecommand if the Python source also comes with a distutils' setup.py script.

Now my questions:

  1. Is my understanding of the process correct? Is there anything I am missing, or anything that I got wrong?
  2. Step #5 is really the more confusing to me: specifically the two points that remains most obscure to me are:
    • How do I write a setup.py script that install a stand-alone programme? EDIT: By standalone programme I mean a program intended to be used by a desktop user (as opposed to a module which I understand like a collection of functionality to be used by other software after having been imported). In my specific case I would actually need two such "programs": the main software and a separate utility (in effect a second "program" that should be in the same package with the other one).
    • What are the specificities of such a script for DEB packages? The official documentation only seems to deal with RPM and Windows stuff...

BTW: These are the best sources of information that I could find myself so far. If you have anything better than this, please share! :)

  • Ubuntu's Python packaging guide
  • Creating a .deb package from a python setup.py (it shows the steps, but it doesn't explain them enough for me to follow along)
  • ShowMeDo video on "creating a .deb package out of a python program" (it doesn't seem up-to-date and - if I got it right - will produce packages for personal use, without dependencies and without a signed changelog and other key data that will make it incompatible with the Debian policy).

回答1:


It looks like stdeb will do what you want.

Also, for installing scripts, I strongly recommend distribute's console_scripts entry point support.




回答2:


This article by Barry Warsaw helped me in getting quite far through the process. I still had to do a lot of searching on the side, though, and I read most of the Ubuntu packaging guide some time in the past.

Having a good setup.py is a really good advice. I found these two guides quite good:

  • Hitchhiker's Guide to Packaging
  • Distribute documentation



回答3:


The right way of building a deb package is using dpkg-buildpackage, but sometimes it is a little bit complicated. Instead you can use dpkg -b <folder>, and it will create your Debian package.

These are the basics for creating a Debian package with dpkg -b <folder> with any binary or with any kind of script that runs automatically without needing manual compilation (Python, Bash, Perl, and Ruby):

  1. Create the files and folders in order to recreate the following structure:

    ProgramName-Version/
    ProgramName-Version/DEBIAN
    ProgramName-Version/DEBIAN/control
    ProgramName-Version/usr/
    ProgramName-Version/usr/bin/
    ProgramName-Version/usr/bin/your_script
    

    The scripts placed at /usr/bin/ are directly called from the terminal, note that I didn't add an extension to the script. Also you can notice that the structure of the deb package will be the structure of the program once it's installed. So if you follow this logic if your program has a single file, you can directly place it under ProgramName-Version/usr/bin/your_script, but if you have multiple files, you should place them under ProgramName-Version/usr/share/ProgramName/all your files and place only one file under /usr/bin/ that will call your scripts from /usr/share/ProgramName/

  2. Change all the folder permission to root:

    chown root:root -R /path/to/ProgramName-Version
    
  3. Change the script's permissions:

    chmod 0755 /path/to/the/script
    
  4. Finally, you can run: dpkg -b /path/to/the/ProgramName-Version and your deb package will be created! (You can also add the post/pre inst scripts and everything you want, it works like a normal Debian package)


Here is an example of the control file. You only need to copy-paste it in to an empty file called "control" and put it in the DEBIAN folder.

Package: ProgramName
Version: VERSION
Architecture: all
Maintainer: YOUR NAME <EMAIL>
Depends: python2.7, etc , etc,
Installed-Size: in_kb
Homepage: http://example.com
Description: Here you can put a one line description. This is the short Description.
 Here you put the long description, indented by one space.



回答4:


There are several libraries out there which abstract away all the necessary steps and let you transform your Python package into a Debian package with a single command.

Assuming your python package already has the setup.py, in the directory where setup.py is located, you could use:

  • stdeb (Already mentioned in this answer, install with pip install stdeb). To create a Debian package, run:

    python setup.py --command-packages=stdeb.command bdist_deb
    

    The output .deb file will be located in the bdist_deb directory.

  • fpm (install with gem install --no-ri --no-rdoc fpm). To create a Debian package, run:

    fpm -s python -t deb setup.py
    
  • py2deb (install with pip install py2deb). To create a Debian package, run:

    py2deb -r . .
    

Each of these libraries has its own caveats, so you might want to try what works best for you.



来源:https://stackoverflow.com/questions/7110604/is-there-a-standard-way-to-create-debian-packages-for-distributing-python-progra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!