Howto Build Python App With Autotools

Author Avatar
Mutse Young 1月 14, 2013

午休,无意发现一个 git 项目 autotools tutorial python gtk,困扰我一年的问题,终于得以解决,也可说是“柳暗花明又一村”。问题是如何使用 Autotools 工具打包 Python 程序?

准备工作

在使用 autotools 之前,先准备好目录结构:

首先,创建工程根目录,如下:

1
$ mkdir hello-1.0

进入 hello-1.0 目录下,再创建 src 和 hello 目录,用于存放 python 脚本:

1
2
$ cd hello-1.0
$ mkdir -p src/hello

编写 python 源码:

1
2
$ cd src/hello
$ vim app.py

app.py 源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/python

import pygtk
pygtk.require('2.0')
import gtk

class Application(object):
def __init__(self, *args, **kwargs):
for key in kwargs:
setattr(self, key, kwargs[key])
self._create_main_window()

def _create_main_window(self):
self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.win.connect("destroy", gtk.main_quit)
self.win.set_size_request(300, 150)
self.win.set_border_width(20)
self.win.set_position(gtk.WIN_POS_CENTER)
self.win.add(gtk.Label("Hello World"))

def run(self):
self.win.show_all()
gtk.main()

创建一个空白的 init.py 文件:

1
$ touch __init__.py

返回至工程根目录下:

1
$ cd ../../

使用 Autotools

接下来,我们开始使用 autotools 等命令。

(1) 在终端中运行autoscan命令;

1
$ autoscan

(2) 使用mv命令将onfigure.scan重命名为configure.ac或configure.in并修改文件内容:

修改前文件:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])

# Checks for programs.

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT

修改后文件:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
AC_INIT([hello-pygtk], [0.1], [yyhoo2.young@gmail.com])
AM_INIT_AUTOMAKE
AM_PATH_PYTHON([2.7])

# Checks for programs.

# Checks for libraries.

# Checks for header files.

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_CONFIG_FILES([
	Makefile
	src/Makefile
	src/hello/Makefile])

AC_OUTPUT(Makefile)

(3) 在工程根目录下新建Makefile.am文件,文件内容如下:

SUBDIRS = src

(4) 拷贝 py-compile 脚本:

1
$ cp /usr/share/automake-1.11/py-compile .

(5) 在当前目录下使用touch命令新建NEWS、 README、 ChangeLog 、AUTHORS 四个文件,否则运行automake --add-missing命令时将会报错

1
$ touch NEWS README ChangeLog AUTHORS

(6) 创建 src/Makefile.am,其内容如下:

SUBDIRS = hello

bin_SCRIPTS = hello-pygtk
CLEANFILES = $(bin_SCRIPTS)
EXTRA_DIST = hello-pygtk.in

do_substitution = sed -e 's,[@]pythondir[@],$(pythondir),g' \
				  -e 's,[@]PACKAGE[@],$(PACKAGE),g' \
				  -e 's,[@]VERSION[@],$(VERSION),g'

hello-pygtk: hello-pygtk.in Makefile
	$(do_substitution) < $(srcdir)/hello-pygtk.in > hello-pygtk
	chmod +x hello-pygtk

(7) 创建 hello-pygtk.in,内容如下:

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/python

import sys
sys.path.insert(1, '@pythondir@')

from hello.app import Application

if __name__ == "__main__":
app = Application(package="@PACKAGE@", version="@VERSION@")
app.run()

(8) 创建 src/hello/Makefile.am,其内容如下:

hello_PYTHON = \
		app.py \
		__init__.py

hellodir = $(pythondir)/hello

(9) 打开终端,在工程根目录下依次运行以下命令:

1
2
3
$ aclocal
$ autoconf
$ automake --add-missing

(10)如果没有任何错误,继续运行./configure命令:

1
$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for a Python interpreter with version >= 2.7... python
checking for python... /usr/bin/python
checking for python version... 2.7
checking for python platform... linux2
checking for python script directory... ${prefix}/lib/python2.7/dist-packages
checking for python extension module directory... ${exec_prefix}/lib/python2.7/dist-packages
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating src/hello/Makefile

即生成Makefile文件。

(11) 运行 make

1
$ make

验证

安装

使用一下方式安装

1
$ sudo make install

安装完毕,在终端中运行 hello-pygtk,即可见 python 程序。

卸载

在工程根目录下,使用一下方法卸载:

1
$ sudo make uninstall

源码下载

参考资料