1. debファイルについて

debファイルは数あれど、その作成方法を解説しているサイトは少ない。
バカみたいに少ない。英語のサイトをたどっても、それでも少ない。
PPAを使う方法もあるが、PPA自体処理が重すぎて話にならない。
そこで(Ubuntu向け)debファイル作成方法を調査・掲載。

2. debファイル作成法

2.1. 使用コマンド

 →以下、コマンドが見つからなかったら、適宜apt-get

2.2. ファイル等準備

 →配布したいファイル。自作ソフトなど。
 →今回はpython言語を用いたソフトを念頭に置く。

2.3. フォルダ用意(パッケージ名-バージョン)

 →配布したいファイルを入れる

2.4. インストール用Makefile用意

例)

注1:
Makefileは通常のものではダメ
(後述dh_makeで作成されるフォルダである)./debian/パッケージ名/を
ローカルの/に見立てて、そこにインストールするようなMakefileが必要
   ↓
なお、rules(これもdh_makeで作成)に
override_dh_auto_install:
	make install dest=debian/パッケージ名
といった行を加え、Makefileを
dest=
install:
	cp ./パッケージ名 $(dest)/usr/bin
のようにすれば、通常のインストールもできるMakefileができる。

注2:疑問点
orgフォルダ→(仮インストール)→debian/パッケージ名以下 →(パッケージによるインストール)→ 実際のフォルダか?

2.5. Gnomeメニュー登録用ファイルは任意で

・パッケージ(プログラム)名.desktop →インストール時に/usr/share/applications/に複製される
例)
		[Desktop Entry]
		Encoding=UTF-8
		Name=Twisted Zombie
		Version=1.0
		Comment=Game developed for PyWeek 5
		GenericName=Twisted Zombie Game
		Terminal=false
		Icon=twisted-zombie-icon.png  ←必ず編集
		Type=Application
		Exec=python /usr/share/twisted-zombie/run_game.py ←必ず編集
		Categories=Game;
・icon用pngファイル        →インストール時に/usr/share/pixmaps/に複製される

2.6. dh_makeで色々作成補助

・dh_make --createorig -s -c GPL -e メアド   ←-c gplと小文字の方が良いのか?
 →origフォルダ作成、シングルバイナリ、コピーライトはGPL

2.7. dh_makeでできたファイル編集

・debian/*.ex *.EXは不要なので削除
・debian/copyrightは適当に編集
・debian/control fileの例
例)
		Source: cutright
		Section: utils       ← 削るとppa登録する場合失敗する。まあ書いといたほうが良いか
		Priority: extra
		Maintainer: olitoshista 
		Build-Depends: debhelper (>= 7)
		Standards-Version: 3.8.4
		Homepage: http://example.com/python-hello

		Package: cutright
		Architecture: all
		Depends:  python   ←今回は${shlibs:Depends}, ${misc:Depends},をカットしたが・・・どうなん?
		Description: Example python module pakage
		 testfddsafdsadafdsafdas

・debian/source/formatを編集
   →3.0 (native)に
   →quiltだとエラーが出るが、なぜかは不明
・changelog修正必須!
例)
	一行目を
	パッケージ名 (0.0.1-1) maverick; urgency=low
	のように、対応するデストリを↑指定する

・実行ファイルについて
pythonプログラム等の場合、カレントディレクトリを設定する必要がある場合は
実行ファイルに修正を加えるのを忘れずに。まあ任意で。
   import os
   os.chdir('設定するパス')

2.8. パッケージビルド

    dpkg-buildpackage -uc -us
  →エラーが起きた場合、エラーが起きた箇所をrulesでoverrideしてやると、ビルド通ることもある。


3. debファイル作成研究

3.1. 注意

いろいろな情報がゴチャマゼになっているので、
混乱している場合は読まない。

3.2. 前提知識

debはarアーカイブ↓
	% ar vx hoge.deb
	x - debian-binary
	x - control.tar.gz
	x - data.tar.gz



cf. Build dependencies/conflicts unsatisfied; aborting.というエラーは、依存関係のあるモジュールがapt-getされてない。
cf. sudo apt-get install build-essential devscripts dpkg-dev
cf. dgetコマンドでdscファイルを指定すると,必要なソースパッケージを取得することができます
cf. sudo apt-get build-dep hoge →hogeをソースからビルドする際に依存するパッケージをインストールする.
	                     当然, hogeそのものはインストールされない.

cf.既存パッケージの解析にはar -x packagename.deb
cf.debの解凍 → dpkg-deb -x hoge.deb ./
cf.debのアンインストール → sudo dpkg -P パッケージ名
cf.changelog作成:debchange --create
cf.changelog修正コマンド
   →dch -v バージョン(例:0.0.1-1nmu1)
   →ctrl+oで保存
cf.makefileその他の例
	DESTDIR =
	PREFIX = /usr/local

	all:

	install: myhello (←注意 プログラム本体と同名である必要)
		install -d $(DESTDIR)$(PREFIX)/bin
		install $< $(DESTDIR)$(PREFIX)/bin

	.PHONY: all install

3.3. 疑問点

dh-helperとdpgk-buildpackageの関係
rulesはどこにあるファイルをmakeするのか
dh-helper7での挙動の変更?
dpkg-buildpackage -rfakerootは必要?


3.4. 英語ページ(PPA)に書かれていたこと

3.4.1. 概説

debianパッケージの作成
・Chrootによる方法が推奨されている
・その他、3つの方法
  ①一から全部作る方法(ビルドヘルパを使わない方法)
  ②debhelperを使う方法
  ③CDBS(CommonDebianDuildSystem)を使う方法
	

3.4.2. ①一から全部作る方法

・必要なもの:build-essential, automake, gnupg, lintian, fakeroot, pbuilder, debhelper, dpatch, and dh-make
・ビルド対象となるサンプル:http://ftp.gnu.org/gnu/hello/hello-2.4.tar.gz
   →<パッケージ名>-のフォルダにソースを用意
   →このオリジナルtarball(upstream)を<パッケージ名>_.orig.tar.gzの形式にする(アンダーバーであることに注意)
・dh_make -e your.maintainer@address実行、single binaryを選択
・不要ファイルの削除
  ・*ex,*EXファイル:テンプレート
  ・README.Debian:エンドユーザー向けドキュ
  ・dirs:インストーラーが必要なディレクトリを作るために使われる
  ・docs
  ・info
    →changelog,compat,cotrol,copyright,rulesが残る
  ・changelogについて
    ・各バージョンでの変化を記載
    ・GPGと同じメルアドであること
    ・日付は特定のフォーマットで記載。date -Rで取得
    ・dchコマンドで編集
    ・-はマイナーな点、*はメジャーな点
    ・dhc --createで作れる
    ・hello (2.4-0Ubuntu1) jaunty;のようなフォーマットに注意
      →Debianとパッケージ名衝突を防ぐため、バージョン名+ubuntuX(xはUbuntuのリビジョンNo)
      →Debianのhello2.4-1がUbuntuに移植されたら、hello2.4-1ubuntu1
      →Debianにないパッケージならhello2.4-0ubuntu1
  ・controlについて
    ・パッケージマネージャーが使う情報を記載
    ・UbuntuではMaintainerフィールドには、汎用的なアドレスを記載?
    ・Source:ソースパッケージ名
    ・Section:カテゴライズ
    ・Priority:Required,Important,Standard,Optional,Extra
    ・Maintainer:
    ・Standards-Version:DebianPolicy?
			apt-cache show debian-policy | grep Versionで取得
      
    ・Build-Depends:
    ・Homepage:
    ・Package: e.g. helllo
    ・Architecture:all,any,i386,amd64,ppcなど
    ・Depends
    ・Suggests
    ・Conflicts:片方がインストールされると、片方はアンインストールされる
    ・Description:
    ・XSBC-Orighnal-Maintainer:に自分の名前を書く
  ・copyright
  ・debian/rules
    ・Makefile的
    ・ビルドターゲットをdhの引数として渡す役割。必要なdh_*コマンドを呼び出す。
    ・例1)
		#!/usr/bin/make -f
		# -*- makefile -*-

		# Uncomment this to turn on verbose mode.
		#export DH_VERBOSE=1

		%:
			dh  $@


    ・例2)
		#!/usr/bin/make -f
		#export DH_VERBOSE=1
		%:      
			dh $@

		build: build-stamp
		build-stamp:
			dh build --before configure
			libtoolize --force
			aclocal
			autoheader
			autoconf
			dh_auto_configure -- \
				--disable-rpath \
				--with-chroot-dir=/var/lib/unbound \
				--with-pidfile=/var/lib/unbound/unbound.pid
			dh_auto_build
			dh build --after test
			touch $@

		clean:  
			dh clean

		install: build
			dh install
			install -m 0644 doc/example.conf debian/unbound/etc/unbound/unbound.conf

		binary-arch: install
			dh binary-arch

		binary: binary-arch binary-indep

    ・取りうるターゲット:"build", "clean", "install", "binary-arch", "binary-indep", and "binary"
    ・"override_dh_command"のようなターゲット名があると、dhはそれを実行する
      →debhelper 7.0.50以上でないとダメ
  ・postinst/prermファイル
    →インストール後や削除前に実行されるシェルスクリプト



3.4.3. ②dpkg-debを使う方法(簡易なもの)

  ①以下の構成でファイル準備
	./
	/debian
		/DEBIAN/control
		/usr/bin/実行ファイル (インストール場所と同じ構成)
  ②controlファイルの例
		Package: linuxstatus
		Version: 1.1-1
		Section: base
		Priority: optional
		Architecture: all
		Depends: bash (>= 2.05a-11), textutils (>= 2.0-12), awk, procps (>= \
		1:2.0.7-8), sed (>= 3.02-8), grep (>= 2.4.2-3), coreutils (>= 5.0-5)
		Maintainer: Chr. Clemens Lee 
		Description: Linux system information
		 This script provides a broad overview of different
		 system aspects.
  ③dpkg-deb --build debian、でdebianパッケージ作成
  ④チェッカーでチェックするとエラーも多い→修正の必要
   lintian debファイル
  ⑤修正
   ・manページ  : ./debian/usr/share/man/man1/パッケージ名.1
              →gzip --best ファイル名、で圧縮
   ・copyright : ./debian/usr/share/doc/パッケージ名/copyright
   ・prerm/postinstスクリプト :./debian/DEBIAN内

3.4.4. ③

 (ry

3.4.5. pythonの場合 ☆失敗


  ①対象ファイル・フォルダを用意
   ・プログラム本体
   ・README.txt
   ・setup.py
	例
		from distutils.core import setup

		setup(
		    name="hello",
		    version="1.0.0",
		    py_modules=["hello"],
		    )
       ↓
     モジュール名-バージョン、というフォルダの中に入れる

  ②debianフォルダ等生成
   ・dh_make --createorig -b -c bsd
   ・不要ファイル削除
	rm debian/*.ex debian/*.EX
	rm debian/docs
	rm debian/README.source
  ③ファイル編集
   ・devian/controlを編集
	例
		Source: python-hello
		Section: python
		Priority: extra
		Maintainer: bellbind 
		Build-Depends: debhelper (>= 7),  python-support,
		 python-setuptools, python3-setuptools
		Standards-Version: 3.8.4
		XS-Python-Version: 2.6, 3.1
		Homepage: http://example.com/python-hello

		Package: python-hello
		Architecture: all
		Depends: ${shlibs:Depends}, ${misc:Depends}, python
		Description: Example python module pakage
		 Usage:
		 .
		      import hello
		      print(hello.world())
		 .

		Package: python3-hello
		Architecture: all
		Depends: ${shlibs:Depends}, ${misc:Depends}, python3
		Description: Example python module pakage
		 Usage:
		 .
		      import hello
		      print(hello.world())
		 .
	注)XS-Python-Versionにpython2とpython3の両方のバージョンを書く。
	注)Build-DependsとDependsも適切なものを指定
	注)2.6系と3.1系バイナリでメジャーパッケージのProvidesを設定してもよいでしょう。
		Package: python2.6-hello
		Provides: python-hello
		...

		将来、2.7系がpythonのメジャーパッケージに変わったらReplacesを設定して置き換えることになります。
		たとえば、1.0.1からpython2.7系をpython-helloのメインにする場合以下のようになります。
		Package: python2.7-hello
		Provides: python-hello
		Replaces: python-hello (<< 1.0.1)
  ④ファイル追加
   ・パッケージ名.install
	マルチバイナリパッケージの場合、debian/tmp以下にinstall後のファイル構造が作られます。
	その中にあるファイルを各バイナリパッケージに振り分ける必要があります。
		↓
     ・debian/python-hello.install
      →内容:debian/tmp/usr/lib/python2.6/*
     ・debian/python3-hello.install
      →debian/tmp/usr/lib/python3.1/*

   ・パッケージ名.docs
    同様に、パッケージの追加ドキュメントになるファイルは、*.docsファイルで指定します。

    debian/python-hello.docs
     →内容:README.txt
    debian/python3-hello.docs
     →内容:README.txt
  ⑤パッケージビルド
   debuild -uc -us

4. pythonの場合、debに代替する方法

4.1. stdebによる方法

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


  ・python setup.py --command-packages=stdeb.command sdist_dsc
   dpkg-buildpackage -rfakeroot -uc -us

  ・py2dsc takes a .tar.gz source package and build a Debian source package from it:

4.2. checkinstallを使う方法

  →checkinstallをapt-getする
  →makeしたフォルダで、checkinstall実行


4.3. その他、よく分からんメモ

	my-hello
	|-- Makefile       # DESTDIR と PREFIX
	|-- debian/
	|   |-- changelog  # debchange コマンド
	|   |-- compat     # 7
	|   |-- control    # Source:, Maintainer:, Package:, Architecture:, Description:
	|   `-- rules*     # override_dh_xxx:
	`-- myhello     # 配布する実行ファイル

6. Summary
Not to get confused, let us recapture all steps we have taken to build our binary Debian package.
Prerequisite files:
1. one or more binary executable or shell script files
2. a man page for each executable file
3. a 'control' file
4. a 'copyright' file
5. a 'changelog' and 'changelog.Debian' file

Setup temporary 'debian' directories:
1. create 'debian/usr/bin' directory (or wherever you plan to place your executable files)
2. create 'debian/usr/share/man/man1' (or whatever section your man page belongs into)
3. create 'debian/DEBIAN' directory
4. create 'debian/usr/share/doc/'
5. make sure all sub directories of 'debian' have file permission 0755

Copy files into temporary 'debian' tree:
1. copy executable file into 'debian/usr/bin' directory (or wherever you plan to place your executable files)
2. copy man page file into 'debian/usr/share/man/man1' directory
3. copy 'control' file into 'debian/DEBIAN' directory
4. copy 'copyright', 'changelog', and 'changelog.Debian' files into 'debian/usr/share/doc/'
5. gzip man page, 'copyright', 'changelog', and 'changelog.Debian' files with option '--best' inside the temporary 'debian' tree

Build and check binary Debian package:
1. invoke 'dpkg-deb --build' using 'fakeroot' on the 'debian' directory
2. rename resulting 'debian.deb' file to its final package name including version and architecture information
3. check resulting .deb package file for Debian policy compliance using 'lintian'