.
[wecker.git] / bdist_debian.py
blob4030af91931cb0e7aeea5154330c505d31272a2e
1 # bdist_debian.py
3 # Add 'bdist_debian' Debian binary package distribution support to 'distutils'.
5 # This command builds '.deb' packages and supports the "Maemo" extensions for the Nokia N770/N800/N810.
7 # Written by: Gene Cash <gene.cash@gmail.com> 16-NOV-2007
9 import os, base64
10 from distutils.core import Command, Distribution
11 from distutils.dir_util import remove_tree
12 from distutils.util import byte_compile
14 # make these legal keywords for setup()
15 Distribution.icon=None
16 Distribution.section=None
17 Distribution.depends=None
19 class ControlFile(object):
20 def __init__(self, Installed_Size=0, Long_Description='', Description='', Icon='', **kwargs):
21 self.options=kwargs
22 self.description=Description
23 self.long_description=Long_Description
24 self.icon=Icon
25 self.installed_size=Installed_Size
27 def getContent(self):
28 content=['%s: %s' % (k, v) for k,v in self.options.iteritems()]
30 content.append('Installed-Size: %d' % self.installed_size)
31 if self.description != 'UNKNOWN':
32 content.append('Description: %s' % self.description.strip())
33 if self.long_description != 'UNKNOWN':
34 self.long_description=self.long_description.replace('\n', '\n ')
35 content.append(' '+self.long_description.strip())
37 if self.icon:
38 # generate Base64-encoded icon
39 s=file(self.icon, 'rb').read()
40 x=base64.b64encode(s)
41 lw=72
42 # trailing blank is important!!
43 content.append('Maemo-Icon-26: ')
44 for i in range(0, len(x), lw):
45 content.append(' '+x[i:i+lw])
47 return '\n'.join(content)+'\n'
49 class bdist_debian(Command):
50 description=''
51 # List of option tuples: long name, short name (None if no short name), and help string.
52 user_options=[('name=', None, 'Package name'),
53 ('section=', None, 'Section (Only "user/*" will display in App Mgr usually)'),
54 ('priority=', None, 'Priority'),
55 ('depends=', None, 'Other Debian package dependencies (comma separated)'),
56 ('icon=', None, 'Name of icon file to be displayed by App Mgr')]
58 def initialize_options(self):
59 self.section=None
60 self.priority=None
61 self.depends=None
62 self.icon=None
64 def finalize_options(self):
65 if self.section is None:
66 self.section='user/other'
68 if self.priority is None:
69 self.priority='optional'
71 self.maintainer='%s <%s>' % (self.distribution.get_maintainer(), self.distribution.get_maintainer_email())
73 if self.depends is None:
74 self.depends='python2.5'
76 self.name=self.distribution.get_name()
77 self.description=self.distribution.get_description()
78 self.long_description=self.distribution.get_long_description()
79 self.version=self.distribution.get_version()
81 # process new keywords
82 if self.distribution.icon != None:
83 self.icon=self.distribution.icon
84 if self.distribution.section != None:
85 self.section=self.distribution.section
86 if self.distribution.depends != None:
87 self.depends=self.distribution.depends
89 def run(self):
90 build_dir=os.path.join(self.get_finalized_command('build').build_base, 'nokia')
91 dist_dir='dist'
92 binary_fn='debian-binary'
93 control_fn='control'
94 data_fn='data'
95 tgz_ext='.tar.gz'
97 # build everything locally
98 self.run_command('build')
99 install=self.reinitialize_command('install', reinit_subcommands=1)
100 install.root=build_dir
101 self.run_command('install')
103 # find out how much space it takes
104 installed_size=0
105 for root, dirs, files in os.walk('build'):
106 installed_size+=sum(os.path.getsize(os.path.join(root, name)) for name in files)
108 # make compressed tarball
109 self.make_archive(os.path.join(dist_dir, data_fn), 'gztar', root_dir=build_dir)
111 # remove all the stuff we just built
112 remove_tree(build_dir)
114 # create control file contents
115 ctl=ControlFile(Package=self.name, Version=self.version, Section=self.section, Priority=self.priority,
116 Installed_Size=installed_size/1024+1, Architecture='all', Maintainer=self.maintainer,
117 Depends=self.depends, Description=self.description, Long_Description=self.long_description,
118 Icon=self.icon).getContent()
120 # grab scripts
121 scripts={}
122 for fn in ('postinst', 'preinst', 'postrm', 'prerm', 'config'):
123 if os.path.exists(fn):
124 scripts[fn]=file(fn, 'rb').read()
126 # now to create the deb package
127 os.chdir(dist_dir)
129 # write control file
130 file(control_fn, 'wb').write(ctl)
132 # write any scripts and chmod a+rx them
133 files=[control_fn]
134 for fn in scripts:
135 files.append(fn)
136 file(fn, 'wb').write(scripts[fn])
137 os.chmod(fn, 0555)
139 # make "control" compressed tarball with control file and any scripts
140 self.spawn(['tar', '-czf', control_fn+tgz_ext]+files)
142 # make debian-binary file
143 file(binary_fn, 'wb').write('2.0\n')
145 # make final archive
146 package_filename='%s_%s_all.deb' % (self.name, self.version)
147 self.spawn(['ar', '-cr', package_filename, binary_fn, control_fn+tgz_ext, data_fn+tgz_ext])