Updating from skeleton
[gonvert] / support / py2deb.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 ##
4 ##    Copyright (C) 2009 manatlan manatlan[at]gmail(dot)com
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published
8 ## by the Free Software Foundation; version 2 only.
9 ##
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 """
16 Known limitations :
17 - don't sign package (-us -uc)
18 - no distinctions between author and maintainer(packager)
19
20 depends on :
21 - dpkg-dev (dpkg-buildpackage)
22 - alien
23 - python
24 - fakeroot
25
26 changelog
27  - ??? ?/??/20?? (By epage)
28     - PEP8
29     - added recommends
30     - fixed bug where it couldn't handle the contents of the pre/post scripts being specified
31     - Added customization based on the targeted policy for sections (Maemo support)
32     - Added maemo specific tarball, dsc, changes file generation support (including icon support)
33     - Added armel architecture
34     - Reduced the size of params being passed around by reducing the calls to locals()
35     - Added respository, distribution, priority
36     - Made setting control file a bit more flexible
37  - 0.5 05/09/2009
38     - pre/post install/remove scripts enabled
39     - deb package install py2deb in dist-packages for py2.6
40  - 0.4 14/10/2008
41     - use os.environ USERNAME or USER (debian way)
42     - install on py 2.(4,5,6) (*FIX* do better here)
43
44 """
45
46 import os
47 import hashlib
48 import sys
49 import shutil
50 import time
51 import string
52 import StringIO
53 import stat
54 import commands
55 import base64
56 import tarfile
57 from glob import glob
58 from datetime import datetime
59 import socket # gethostname()
60 from subprocess import Popen, PIPE
61
62 #~ __version__ = "0.4"
63 __version__ = "0.5"
64 __author__ = "manatlan"
65 __mail__ = "manatlan@gmail.com"
66
67
68 PERMS_URW_GRW_OR = stat.S_IRUSR | stat.S_IWUSR | \
69                    stat.S_IRGRP | stat.S_IWGRP | \
70                    stat.S_IROTH
71
72 UID_ROOT = 0
73 GID_ROOT = 0
74
75
76 def run(cmds):
77     p = Popen(cmds, shell=False, stdout=PIPE, stderr=PIPE)
78     time.sleep(0.01)    # to avoid "IOError: [Errno 4] Interrupted system call"
79     out = string.join(p.stdout.readlines()).strip()
80     outerr = string.join(p.stderr.readlines()).strip()
81     return out
82
83
84 def deb2rpm(file):
85     txt=run(['alien', '-r', file])
86     return txt.split(" generated")[0]
87
88
89 def py2src(TEMP, name):
90     l=glob("%(TEMP)s/%(name)s*.tar.gz" % locals())
91     if len(l) != 1:
92         raise Py2debException("don't find source package tar.gz")
93
94     tar = os.path.basename(l[0])
95     shutil.move(l[0], tar)
96
97     return tar
98
99
100 def md5sum(filename):
101     f = open(filename, "r")
102     try:
103         return hashlib.md5(f.read()).hexdigest()
104     finally:
105         f.close()
106
107
108 class Py2changes(object):
109
110     def __init__(self, ChangedBy, description, changes, files, category, repository, **kwargs):
111       self.options = kwargs # TODO: Is order important?
112       self.description = description
113       self.changes=changes
114       self.files=files
115       self.category=category
116       self.repository=repository
117       self.ChangedBy=ChangedBy
118
119     def getContent(self):
120         content = ["%s: %s" % (k, v)
121                    for k,v in self.options.iteritems()]
122
123         if self.description:
124             description=self.description.replace("\n","\n ")
125             content.append('Description: ')
126             content.append(' %s' % description)
127         if self.changes:
128             changes=self.changes.replace("\n","\n ")
129             content.append('Changes: ')
130             content.append(' %s' % changes)
131         if self.ChangedBy:
132             content.append("Changed-By: %s" % self.ChangedBy)
133
134         content.append('Files:')
135
136         for onefile in self.files:
137             md5 = md5sum(onefile)
138             size = os.stat(onefile).st_size.__str__()
139             content.append(' ' + md5 + ' ' + size + ' ' + self.category +' '+self.repository+' '+os.path.basename(onefile))
140
141         return "\n".join(content) + "\n\n"
142
143
144 def py2changes(params):
145     changescontent = Py2changes(
146         "%(author)s <%(mail)s>" % params,
147         "%(description)s" % params,
148         "%(changelog)s" % params,
149         (
150             "%(TEMP)s/%(name)s_%(version)s.tar.gz" % params,
151             "%(TEMP)s/%(name)s_%(version)s.dsc" % params,
152         ),
153         "%(section)s" % params,
154         "%(repository)s" % params,
155         Format='1.7',
156         Date=time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
157         Source="%(name)s" % params,
158         Architecture="%(arch)s" % params,
159         Version="%(version)s" % params,
160         Distribution="%(distribution)s" % params,
161         Urgency="%(urgency)s" % params,
162         Maintainer="%(author)s <%(mail)s>" % params
163     )
164     f = open("%(TEMP)s/%(name)s_%(version)s.changes" % params,"wb")
165     f.write(changescontent.getContent())
166     f.close()
167
168     fileHandle = open('/tmp/py2deb2.tmp', 'w')
169     fileHandle.write('#!/bin/sh\n')
170     fileHandle.write("cd " +os.getcwd()+ "\n")
171     # TODO Renable signing
172     # fileHandle.write("gpg --local-user %(mail)s --clearsign %(TEMP)s/%(name)s_%(version)s.changes\n" % params)
173     fileHandle.write("mv %(TEMP)s/%(name)s_%(version)s.changes.asc %(TEMP)s/%(name)s_%(version)s.changes\n" % params)
174     fileHandle.write('\nexit')
175     fileHandle.close()
176     commands.getoutput("chmod 777 /tmp/py2deb2.tmp")
177     commands.getoutput("/tmp/py2deb2.tmp")
178
179     ret = []
180
181     l=glob("%(TEMP)s/%(name)s*.tar.gz" % params)
182     if len(l)!=1:
183         raise Py2debException("don't find source package tar.gz")
184     tar = os.path.basename(l[0])
185     shutil.move(l[0],tar)
186     ret.append(tar)
187
188     l=glob("%(TEMP)s/%(name)s*.dsc" % params)
189     if len(l)!=1:
190         raise Py2debException("don't find source package dsc")
191     tar = os.path.basename(l[0])
192     shutil.move(l[0],tar)
193     ret.append(tar)
194
195     l = glob("%(TEMP)s/%(name)s*.changes" % params)
196     if len(l)!=1:
197         raise Py2debException("don't find source package changes")
198     tar = os.path.basename(l[0])
199     shutil.move(l[0],tar)
200     ret.append(tar)
201
202     return ret
203
204
205 class Py2dsc(object):
206
207     def __init__(self, StandardsVersion, BuildDepends, files, **kwargs):
208       self.options = kwargs # TODO: Is order important?
209       self.StandardsVersion = StandardsVersion
210       self.BuildDepends=BuildDepends
211       self.files=files
212
213     @property
214     def content(self):
215         content = ["%s: %s" % (k, v)
216                    for k,v in self.options.iteritems()]
217
218         if self.BuildDepends:
219             content.append("Build-Depends: %s" % self.BuildDepends)
220         if self.StandardsVersion:
221             content.append("Standards-Version: %s" % self.StandardsVersion)
222
223         content.append('Files:')
224
225         for onefile in self.files:
226             print onefile
227             md5 = md5sum(onefile)
228             size = os.stat(onefile).st_size.__str__()
229             content.append(' '+md5 + ' ' + size +' '+os.path.basename(onefile))
230
231         return "\n".join(content)+"\n\n"
232
233
234 def py2dsc(TEMP, name, version, depends, author, mail, arch):
235     dsccontent = Py2dsc(
236         "%(version)s" % locals(),
237         "%(depends)s" % locals(),
238         ("%(TEMP)s/%(name)s_%(version)s.tar.gz" % locals(),),
239         Format='1.0',
240         Source="%(name)s" % locals(),
241         Version="%(version)s" % locals(),
242         Maintainer="%(author)s <%(mail)s>" % locals(),
243         Architecture="%(arch)s" % locals(),
244     )
245
246     filename = "%(TEMP)s/%(name)s_%(version)s.dsc" % locals()
247
248     f = open(filename, "wb")
249     try:
250         f.write(dsccontent.content)
251     finally:
252         f.close()
253
254     fileHandle = open('/tmp/py2deb.tmp', 'w')
255     try:
256         fileHandle.write('#!/bin/sh\n')
257         fileHandle.write("cd " + os.getcwd() + "\n")
258         # TODO Renable signing
259         # fileHandle.write("gpg --local-user %(mail)s --clearsign %(TEMP)s/%(name)s_%(version)s.dsc\n" % locals())
260         fileHandle.write("mv %(TEMP)s/%(name)s_%(version)s.dsc.asc %(filename)s\n" % locals())
261         fileHandle.write('\nexit')
262         fileHandle.close()
263     finally:
264         f.close()
265
266     commands.getoutput("chmod 777 /tmp/py2deb.tmp")
267     commands.getoutput("/tmp/py2deb.tmp")
268
269     return filename
270
271
272 class Py2tar(object):
273
274     def __init__(self, dataDirectoryPath):
275         self._dataDirectoryPath = dataDirectoryPath
276
277     def packed(self):
278         return self._getSourcesFiles()
279
280     def _getSourcesFiles(self):
281         directoryPath = self._dataDirectoryPath
282
283         outputFileObj = StringIO.StringIO() # TODO: Do more transparently?
284
285         tarOutput = tarfile.TarFile.open('sources',
286                                  mode = "w:gz",
287                                  fileobj = outputFileObj)
288
289         # Note: We can't use this because we need to fiddle permissions:
290         #       tarOutput.add(directoryPath, arcname = "")
291
292         for root, dirs, files in os.walk(directoryPath):
293             archiveRoot = root[len(directoryPath):]
294
295             tarinfo = tarOutput.gettarinfo(root, archiveRoot)
296             # TODO: Make configurable?
297             tarinfo.uid = UID_ROOT
298             tarinfo.gid = GID_ROOT
299             tarinfo.uname = ""
300             tarinfo.gname = ""
301             tarOutput.addfile(tarinfo)
302
303             for f in  files:
304                 tarinfo = tarOutput.gettarinfo(os.path.join(root, f),
305                                                os.path.join(archiveRoot, f))
306                 tarinfo.uid = UID_ROOT
307                 tarinfo.gid = GID_ROOT
308                 tarinfo.uname = ""
309                 tarinfo.gname = ""
310                 tarOutput.addfile(tarinfo, file(os.path.join(root, f)))
311
312         tarOutput.close()
313
314         data_tar_gz = outputFileObj.getvalue()
315
316         return data_tar_gz
317
318
319 def py2tar(DEST, TEMP, name, version):
320     tarcontent = Py2tar("%(DEST)s" % locals())
321     filename = "%(TEMP)s/%(name)s_%(version)s.tar.gz" % locals()
322     f = open(filename, "wb")
323     try:
324         f.write(tarcontent.packed())
325     finally:
326         f.close()
327     return filename
328
329
330 class Py2debException(Exception):
331     pass
332
333
334 SECTIONS_BY_POLICY = {
335     # http://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections
336     "debian": "admin, base, comm, contrib, devel, doc, editors, electronics, embedded, games, gnome, graphics, hamradio, interpreters, kde, libs, libdevel, mail, math, misc, net, news, non-free, oldlibs, otherosfs, perl, python, science, shells, sound, tex, text, utils, web, x11",
337     # http://maemo.org/forrest-images/pdf/maemo-policy.pdf
338     "chinook": "accessories, communication, games, multimedia, office, other, programming, support, themes, tools",
339     # http://wiki.maemo.org/Task:Package_categories
340     "diablo": "user/desktop, user/development, user/education, user/games, user/graphics, user/multimedia, user/navigation, user/network, user/office, user/science, user/system, user/utilities",
341     # http://wiki.maemo.org/Task:Fremantle_application_categories
342     "mer": "user/desktop, user/development, user/education, user/games, user/graphics, user/multimedia, user/navigation, user/network, user/office, user/science, user/system, user/utilities",
343     # http://wiki.maemo.org/Task:Fremantle_application_categories
344     "fremantle": "user/desktop, user/development, user/education, user/games, user/graphics, user/multimedia, user/navigation, user/network, user/office, user/science, user/system, user/utilities",
345 }
346
347
348 LICENSE_AGREEMENT = {
349         "gpl": """
350     This package is free software; you can redistribute it and/or modify
351     it under the terms of the GNU General Public License as published by
352     the Free Software Foundation; either version 2 of the License, or
353     (at your option) any later version.
354
355     This package is distributed in the hope that it will be useful,
356     but WITHOUT ANY WARRANTY; without even the implied warranty of
357     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
358     GNU General Public License for more details.
359
360     You should have received a copy of the GNU General Public License
361     along with this package; if not, write to the Free Software
362     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
363
364 On Debian systems, the complete text of the GNU General
365 Public License can be found in `/usr/share/common-licenses/GPL'.
366 """,
367         "lgpl":"""
368     This package is free software; you can redistribute it and/or
369     modify it under the terms of the GNU Lesser General Public
370     License as published by the Free Software Foundation; either
371     version 2 of the License, or (at your option) any later version.
372
373     This package is distributed in the hope that it will be useful,
374     but WITHOUT ANY WARRANTY; without even the implied warranty of
375     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
376     Lesser General Public License for more details.
377
378     You should have received a copy of the GNU Lesser General Public
379     License along with this package; if not, write to the Free Software
380     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
381
382 On Debian systems, the complete text of the GNU Lesser General
383 Public License can be found in `/usr/share/common-licenses/LGPL'.
384 """,
385         "bsd": """
386     Redistribution and use in source and binary forms, with or without
387     modification, are permitted under the terms of the BSD License.
388
389     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
390     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
391     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
392     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
393     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
394     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
395     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
396     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
397     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
398     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
399     SUCH DAMAGE.
400
401 On Debian systems, the complete text of the BSD License can be
402 found in `/usr/share/common-licenses/BSD'.
403 """,
404         "artistic": """
405     This program is free software; you can redistribute it and/or modify it
406     under the terms of the "Artistic License" which comes with Debian.
407
408     THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
409     WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES
410     OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
411
412 On Debian systems, the complete text of the Artistic License
413 can be found in `/usr/share/common-licenses/Artistic'.
414 """
415 }
416
417
418 class Py2deb(object):
419     """
420     heavily based on technic described here :
421     http://wiki.showmedo.com/index.php?title=LinuxJensMakingDeb
422     """
423     ## STATICS
424     clear = False  # clear build folder after py2debianization
425
426     SECTIONS = SECTIONS_BY_POLICY["debian"]
427
428     #http://www.debian.org/doc/debian-policy/footnotes.html#f69
429     ARCHS = "all i386 ia64 alpha amd64 armeb arm hppa m32r m68k mips mipsel powerpc ppc64 s390 s390x sh3 sh3eb sh4 sh4eb sparc darwin-i386 darwin-ia64 darwin-alpha darwin-amd64 darwin-armeb darwin-arm darwin-hppa darwin-m32r darwin-m68k darwin-mips darwin-mipsel darwin-powerpc darwin-ppc64 darwin-s390 darwin-s390x darwin-sh3 darwin-sh3eb darwin-sh4 darwin-sh4eb darwin-sparc freebsd-i386 freebsd-ia64 freebsd-alpha freebsd-amd64 freebsd-armeb freebsd-arm freebsd-hppa freebsd-m32r freebsd-m68k freebsd-mips freebsd-mipsel freebsd-powerpc freebsd-ppc64 freebsd-s390 freebsd-s390x freebsd-sh3 freebsd-sh3eb freebsd-sh4 freebsd-sh4eb freebsd-sparc kfreebsd-i386 kfreebsd-ia64 kfreebsd-alpha kfreebsd-amd64 kfreebsd-armeb kfreebsd-arm kfreebsd-hppa kfreebsd-m32r kfreebsd-m68k kfreebsd-mips kfreebsd-mipsel kfreebsd-powerpc kfreebsd-ppc64 kfreebsd-s390 kfreebsd-s390x kfreebsd-sh3 kfreebsd-sh3eb kfreebsd-sh4 kfreebsd-sh4eb kfreebsd-sparc knetbsd-i386 knetbsd-ia64 knetbsd-alpha knetbsd-amd64 knetbsd-armeb knetbsd-arm knetbsd-hppa knetbsd-m32r knetbsd-m68k knetbsd-mips knetbsd-mipsel knetbsd-powerpc knetbsd-ppc64 knetbsd-s390 knetbsd-s390x knetbsd-sh3 knetbsd-sh3eb knetbsd-sh4 knetbsd-sh4eb knetbsd-sparc netbsd-i386 netbsd-ia64 netbsd-alpha netbsd-amd64 netbsd-armeb netbsd-arm netbsd-hppa netbsd-m32r netbsd-m68k netbsd-mips netbsd-mipsel netbsd-powerpc netbsd-ppc64 netbsd-s390 netbsd-s390x netbsd-sh3 netbsd-sh3eb netbsd-sh4 netbsd-sh4eb netbsd-sparc openbsd-i386 openbsd-ia64 openbsd-alpha openbsd-amd64 openbsd-armeb openbsd-arm openbsd-hppa openbsd-m32r openbsd-m68k openbsd-mips openbsd-mipsel openbsd-powerpc openbsd-ppc64 openbsd-s390 openbsd-s390x openbsd-sh3 openbsd-sh3eb openbsd-sh4 openbsd-sh4eb openbsd-sparc hurd-i386 hurd-ia64 hurd-alpha hurd-amd64 hurd-armeb hurd-arm hurd-hppa hurd-m32r hurd-m68k hurd-mips hurd-mipsel hurd-powerpc hurd-ppc64 hurd-s390 hurd-s390x hurd-sh3 hurd-sh3eb hurd-sh4 hurd-sh4eb hurd-sparc armel".split(" ")
430
431     # license terms taken from dh_make
432     LICENSES = list(LICENSE_AGREEMENT.iterkeys())
433
434     def __setitem__(self, path, files):
435
436         if not type(files)==list:
437             raise Py2debException("value of key path '%s' is not a list"%path)
438         if not files:
439             raise Py2debException("value of key path '%s' should'nt be empty"%path)
440         if not path.startswith("/"):
441             raise Py2debException("key path '%s' malformed (don't start with '/')"%path)
442         if path.endswith("/"):
443             raise Py2debException("key path '%s' malformed (shouldn't ends with '/')"%path)
444
445         nfiles=[]
446         for file in files:
447
448             if ".." in file:
449                 raise Py2debException("file '%s' contains '..', please avoid that!"%file)
450
451
452             if "|" in file:
453                 if file.count("|")!=1:
454                     raise Py2debException("file '%s' is incorrect (more than one pipe)"%file)
455
456                 file, nfile = file.split("|")
457             else:
458                 nfile=file  # same localisation
459
460             if os.path.isdir(file):
461                 raise Py2debException("file '%s' is a folder, and py2deb refuse folders !"%file)
462
463             if not os.path.isfile(file):
464                 raise Py2debException("file '%s' doesn't exist"%file)
465
466             if file.startswith("/"):    # if an absolute file is defined
467                 if file==nfile:         # and not renamed (pipe trick)
468                     nfile=os.path.basename(file)   # it's simply copied to 'path'
469
470             nfiles.append((file, nfile))
471
472         nfiles.sort(lambda a, b: cmp(a[1], b[1]))    #sort according new name (nfile)
473
474         self.__files[path]=nfiles
475
476     def __getitem__(self, k):
477         return self.__files[k]
478
479     def __delitem__(self, k):
480         del self.__files[k]
481
482     def __init__(self,
483                     name,
484                     description="no description",
485                     license="gpl",
486                     depends="",
487                     section="utils",
488                     arch="all",
489
490                     url="",
491                     author = None,
492                     mail = None,
493
494                     preinstall = None,
495                     postinstall = None,
496                     preremove = None,
497                     postremove = None
498                 ):
499
500         if author is None:
501             author = ("USERNAME" in os.environ) and os.environ["USERNAME"] or None
502             if author is None:
503                 author = ("USER" in os.environ) and os.environ["USER"] or "unknown"
504
505         if mail is None:
506             mail = author+"@"+socket.gethostname()
507
508         self.name = name
509         self.prettyName = ""
510         self.description = description
511         self.upgradeDescription = ""
512         self.bugTracker = ""
513         self.license = license
514         self.depends = depends
515         self.recommends = ""
516         self.section = section
517         self.arch = arch
518         self.url = url
519         self.author = author
520         self.mail = mail
521         self.icon = ""
522         self.distribution = ""
523         self.respository = ""
524         self.urgency = "low"
525
526         self.preinstall = preinstall
527         self.postinstall = postinstall
528         self.preremove = preremove
529         self.postremove = postremove
530
531         self.__files={}
532
533     def __repr__(self):
534         name = self.name
535         license = self.license
536         description = self.description
537         depends = self.depends
538         recommends = self.recommends
539         section = self.section
540         arch = self.arch
541         url = self.url
542         author = self.author
543         mail = self.mail
544
545         preinstall = self.preinstall
546         postinstall = self.postinstall
547         preremove = self.preremove
548         postremove = self.postremove
549
550         paths=self.__files.keys()
551         paths.sort()
552         files=[]
553         for path in paths:
554             for file, nfile in self.__files[path]:
555                 #~ rfile=os.path.normpath(os.path.join(path, nfile))
556                 rfile=os.path.join(path, nfile)
557                 if nfile==file:
558                     files.append(rfile)
559                 else:
560                     files.append(rfile + " (%s)"%file)
561
562         files.sort()
563         files = "\n".join(files)
564
565
566         lscripts = [    preinstall and "preinst",
567                         postinstall and "postinst",
568                         preremove and "prerm",
569                         postremove and "postrm",
570                     ]
571         scripts = lscripts and ", ".join([i for i in lscripts if i]) or "None"
572         return """
573 ----------------------------------------------------------------------
574 NAME        : %(name)s
575 ----------------------------------------------------------------------
576 LICENSE     : %(license)s
577 URL         : %(url)s
578 AUTHOR      : %(author)s
579 MAIL        : %(mail)s
580 ----------------------------------------------------------------------
581 DEPENDS     : %(depends)s
582 RECOMMENDS  : %(recommends)s
583 ARCH        : %(arch)s
584 SECTION     : %(section)s
585 ----------------------------------------------------------------------
586 DESCRIPTION :
587 %(description)s
588 ----------------------------------------------------------------------
589 SCRIPTS : %(scripts)s
590 ----------------------------------------------------------------------
591 FILES :
592 %(files)s
593 """ % locals()
594
595     def generate(self, version, changelog="", rpm=False, src=False, build=True, tar=False, changes=False, dsc=False):
596         """ generate a deb of version 'version', with or without 'changelog', with or without a rpm
597             (in the current folder)
598             return a list of generated files
599         """
600         if not sum([len(i) for i in self.__files.values()])>0:
601             raise Py2debException("no files are defined")
602
603         if not changelog:
604             changelog="* no changelog"
605
606         name = self.name
607         description = self.description
608         license = self.license
609         depends = self.depends
610         recommends = self.recommends
611         section = self.section
612         arch = self.arch
613         url = self.url
614         distribution = self.distribution
615         repository = self.repository
616         urgency = self.urgency
617         author = self.author
618         mail = self.mail
619         files = self.__files
620         preinstall = self.preinstall
621         postinstall = self.postinstall
622         preremove = self.preremove
623         postremove = self.postremove
624
625         if section not in Py2deb.SECTIONS:
626             raise Py2debException("section '%s' is unknown (%s)" % (section, str(Py2deb.SECTIONS)))
627
628         if arch not in Py2deb.ARCHS:
629             raise Py2debException("arch '%s' is unknown (%s)"% (arch, str(Py2deb.ARCHS)))
630
631         if license not in Py2deb.LICENSES:
632             raise Py2debException("License '%s' is unknown (%s)" % (license, str(Py2deb.LICENSES)))
633
634         # create dates (buildDate, buildDateYear)
635         d=datetime.now()
636         buildDate=d.strftime("%a, %d %b %Y %H:%M:%S +0000")
637         buildDateYear=str(d.year)
638
639         #clean description (add a space before each next lines)
640         description=description.replace("\r", "").strip()
641         description = "\n ".join(description.split("\n"))
642
643         #clean changelog (add 2 spaces before each next lines)
644         changelog=changelog.replace("\r", "").strip()
645         changelog = "\n  ".join(changelog.split("\n"))
646
647         TEMP = ".py2deb_build_folder"
648         DEST = os.path.join(TEMP, name)
649         DEBIAN = os.path.join(DEST, "debian")
650
651         packageContents = locals()
652
653         # let's start the process
654         try:
655             shutil.rmtree(TEMP)
656         except:
657             pass
658
659         os.makedirs(DEBIAN)
660         try:
661             rules=[]
662             dirs=[]
663             for path in files:
664                 for ofile, nfile in files[path]:
665                     if os.path.isfile(ofile):
666                         # it's a file
667
668                         if ofile.startswith("/"): # if absolute path
669                             # we need to change dest
670                             dest=os.path.join(DEST, nfile)
671                         else:
672                             dest=os.path.join(DEST, ofile)
673
674                         # copy file to be packaged
675                         destDir = os.path.dirname(dest)
676                         if not os.path.isdir(destDir):
677                             os.makedirs(destDir)
678
679                         shutil.copy2(ofile, dest)
680
681                         ndir = os.path.join(path, os.path.dirname(nfile))
682                         nname = os.path.basename(nfile)
683
684                         # make a line RULES to be sure the destination folder is created
685                         # and one for copying the file
686                         fpath = "/".join(["$(CURDIR)", "debian", name+ndir])
687                         rules.append('mkdir -p "%s"' % fpath)
688                         rules.append('cp -a "%s" "%s"' % (ofile, os.path.join(fpath, nname)))
689
690                         # append a dir
691                         dirs.append(ndir)
692
693                     else:
694                         raise Py2debException("unknown file '' "%ofile) # shouldn't be raised (because controlled before)
695
696             # make rules right
697             rules= "\n\t".join(rules) + "\n"
698             packageContents["rules"] = rules
699
700             # make dirs right
701             dirs= [i[1:] for i in set(dirs)]
702             dirs.sort()
703
704             #==========================================================================
705             # CREATE debian/dirs
706             #==========================================================================
707             open(os.path.join(DEBIAN, "dirs"), "w").write("\n".join(dirs))
708
709             #==========================================================================
710             # CREATE debian/changelog
711             #==========================================================================
712             clog="""%(name)s (%(version)s) stable; urgency=low
713
714   %(changelog)s
715
716  -- %(author)s <%(mail)s>  %(buildDate)s
717 """ % packageContents
718
719             open(os.path.join(DEBIAN, "changelog"), "w").write(clog)
720
721             #==========================================================================
722             #Create pre/post install/remove
723             #==========================================================================
724             def mkscript(name, dest):
725                 if name and name.strip()!="":
726                     if os.path.isfile(name):    # it's a file
727                         content = file(name).read()
728                     else:   # it's a script
729                         content = name
730                     open(os.path.join(DEBIAN, dest), "w").write(content)
731
732             mkscript(preinstall, "preinst")
733             mkscript(postinstall, "postinst")
734             mkscript(preremove, "prerm")
735             mkscript(postremove, "postrm")
736
737
738             #==========================================================================
739             # CREATE debian/compat
740             #==========================================================================
741             open(os.path.join(DEBIAN, "compat"), "w").write("5\n")
742
743             #==========================================================================
744             # CREATE debian/control
745             #==========================================================================
746             generalParagraphFields = [
747                 "Source: %(name)s",
748                 "Maintainer: %(author)s <%(mail)s>",
749                 "Section: %(section)s",
750                 "Priority: extra",
751                 "Build-Depends: debhelper (>= 5)",
752                 "Standards-Version: 3.7.2",
753             ]
754
755             specificParagraphFields = [
756                 "Package: %(name)s",
757                 "Architecture: %(arch)s",
758                 "Depends: %(depends)s",
759                 "Recommends: %(recommends)s",
760                 "Description: %(description)s",
761             ]
762
763             if self.prettyName:
764                 prettyName = "XSBC-Maemo-Display-Name: %s" % self.prettyName.strip()
765                 specificParagraphFields.append("\n  ".join(prettyName.split("\n")))
766
767             if self.bugTracker:
768                 bugTracker = "XSBC-Bugtracker: %s" % self.bugTracker.strip()
769                 specificParagraphFields.append("\n  ".join(bugTracker.split("\n")))
770
771             if self.upgradeDescription:
772                 upgradeDescription = "XSBC-Maemo-Upgrade-Description: %s" % self.upgradeDescription.strip()
773                 specificParagraphFields.append("\n  ".join(upgradeDescription.split("\n")))
774
775             if self.icon:
776                 f = open(self.icon, "rb")
777                 try:
778                     rawIcon = f.read()
779                 finally:
780                     f.close()
781                 uueIcon = base64.b64encode(rawIcon)
782                 uueIconLines = []
783                 for i, c in enumerate(uueIcon):
784                     if i % 60 == 0:
785                         uueIconLines.append("")
786                     uueIconLines[-1] += c
787                 uueIconLines[0:0] = ("XSBC-Maemo-Icon-26:", )
788                 specificParagraphFields.append("\n  ".join(uueIconLines))
789
790             generalParagraph = "\n".join(generalParagraphFields)
791             specificParagraph = "\n".join(specificParagraphFields)
792             controlContent = "\n\n".join((generalParagraph, specificParagraph)) % packageContents
793             open(os.path.join(DEBIAN, "control"), "w").write(controlContent)
794
795             #==========================================================================
796             # CREATE debian/copyright
797             #==========================================================================
798             packageContents["txtLicense"] = LICENSE_AGREEMENT[license]
799             packageContents["pv"] =__version__
800             txt="""This package was py2debianized(%(pv)s) by %(author)s <%(mail)s> on
801 %(buildDate)s.
802
803 It was downloaded from %(url)s
804
805 Upstream Author: %(author)s <%(mail)s>
806
807 Copyright: %(buildDateYear)s by %(author)s
808
809 License:
810
811 %(txtLicense)s
812
813 The Debian packaging is (C) %(buildDateYear)s, %(author)s <%(mail)s> and
814 is licensed under the GPL, see above.
815
816
817 # Please also look if there are files or directories which have a
818 # different copyright/license attached and list them here.
819 """ % packageContents
820             open(os.path.join(DEBIAN, "copyright"), "w").write(txt)
821
822             #==========================================================================
823             # CREATE debian/rules
824             #==========================================================================
825             txt="""#!/usr/bin/make -f
826 # -*- makefile -*-
827 # Sample debian/rules that uses debhelper.
828 # This file was originally written by Joey Hess and Craig Small.
829 # As a special exception, when this file is copied by dh-make into a
830 # dh-make output file, you may use that output file without restriction.
831 # This special exception was added by Craig Small in version 0.37 of dh-make.
832
833 # Uncomment this to turn on verbose mode.
834 #export DH_VERBOSE=1
835
836
837
838
839 CFLAGS = -Wall -g
840
841 ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
842         CFLAGS += -O0
843 else
844         CFLAGS += -O2
845 endif
846
847 configure: configure-stamp
848 configure-stamp:
849         dh_testdir
850         # Add here commands to configure the package.
851
852         touch configure-stamp
853
854
855 build: build-stamp
856
857 build-stamp: configure-stamp
858         dh_testdir
859         touch build-stamp
860
861 clean:
862         dh_testdir
863         dh_testroot
864         rm -f build-stamp configure-stamp
865         dh_clean
866
867 install: build
868         dh_testdir
869         dh_testroot
870         dh_clean -k
871         dh_installdirs
872
873         # ======================================================
874         #$(MAKE) DESTDIR="$(CURDIR)/debian/%(name)s" install
875         mkdir -p "$(CURDIR)/debian/%(name)s"
876
877         %(rules)s
878         # ======================================================
879
880 # Build architecture-independent files here.
881 binary-indep: build install
882 # We have nothing to do by default.
883
884 # Build architecture-dependent files here.
885 binary-arch: build install
886         dh_testdir
887         dh_testroot
888         dh_installchangelogs debian/changelog
889         dh_installdocs
890         dh_installexamples
891 #       dh_install
892 #       dh_installmenu
893 #       dh_installdebconf
894 #       dh_installlogrotate
895 #       dh_installemacsen
896 #       dh_installpam
897 #       dh_installmime
898 #       dh_python
899 #       dh_installinit
900 #       dh_installcron
901 #       dh_installinfo
902         dh_installman
903         dh_link
904         dh_strip
905         dh_compress
906         dh_fixperms
907 #       dh_perl
908 #       dh_makeshlibs
909         dh_installdeb
910         dh_shlibdeps
911         dh_gencontrol
912         dh_md5sums
913         dh_builddeb
914
915 binary: binary-indep binary-arch
916 .PHONY: build clean binary-indep binary-arch binary install configure
917 """ % packageContents
918             open(os.path.join(DEBIAN, "rules"), "w").write(txt)
919             os.chmod(os.path.join(DEBIAN, "rules"), 0755)
920
921             ###########################################################################
922             ###########################################################################
923             ###########################################################################
924
925             generatedFiles = []
926
927             if build:
928                 #http://www.debian.org/doc/manuals/maint-guide/ch-build.fr.html
929                 ret = os.system('cd "%(DEST)s"; dpkg-buildpackage -tc -rfakeroot -us -uc' % packageContents)
930                 if ret != 0:
931                     raise Py2debException("buildpackage failed (see output)")
932
933                 l=glob("%(TEMP)s/%(name)s*.deb" % packageContents)
934                 if len(l) != 1:
935                     raise Py2debException("didn't find builded deb")
936
937                 tdeb = l[0]
938                 deb = os.path.basename(tdeb)
939                 shutil.move(tdeb, deb)
940
941                 generatedFiles = [deb, ]
942
943                 if rpm:
944                     rpmFilename = deb2rpm(deb)
945                     generatedFiles.append(rpmFilename)
946
947                 if src:
948                     tarFilename = py2src(TEMP, name)
949                     generatedFiles.append(tarFilename)
950
951             if tar:
952                 tarFilename = py2tar(DEST, TEMP, name, version)
953                 generatedFiles.append(tarFilename)
954
955             if dsc:
956                 dscFilename = py2dsc(TEMP, name, version, depends, author, mail, arch)
957                 generatedFiles.append(dscFilename)
958
959             if changes:
960                 changesFilenames = py2changes(packageContents)
961                 generatedFiles.extend(changesFilenames)
962
963             return generatedFiles
964
965         #~ except Exception,m:
966             #~ raise Py2debException("build error :"+str(m))
967
968         finally:
969             if Py2deb.clear:
970                 shutil.rmtree(TEMP)
971
972
973 if __name__ == "__main__":
974     try:
975         os.chdir(os.path.dirname(sys.argv[0]))
976     except:
977         pass
978
979     p=Py2deb("python-py2deb")
980     p.description="Generate simple deb(/rpm/tgz) from python (2.4, 2.5 and 2.6)"
981     p.url = "http://www.manatlan.com/page/py2deb"
982     p.author=__author__
983     p.mail=__mail__
984     p.depends = "dpkg-dev, fakeroot, alien, python"
985     p.section="python"
986     p["/usr/lib/python2.6/dist-packages"] = ["py2deb.py", ]
987     p["/usr/lib/python2.5/site-packages"] = ["py2deb.py", ]
988     p["/usr/lib/python2.4/site-packages"] = ["py2deb.py", ]
989     #~ p.postinstall = "s.py"
990     #~ p.preinstall = "s.py"
991     #~ p.postremove = "s.py"
992     #~ p.preremove = "s.py"
993     print p
994     print p.generate(__version__, changelog = __doc__, src=True)