~jderose/snakebite/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python

# SnakeBite - Transcodes GStreamer-supported audio to MP3
# Copyright (C) 2008 Jason Gerard DeRose <jderose@jasonderose.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

from SnakeBite.info import info
import os
import sys
import time
import re

WIDTH = 70

def rfc_2822():
	"""
	Returns the current UTC time as an RFC-2822 formatted string.
	"""
	return time.strftime(
		'%a, %d %b %Y %H:%M:%S +0000',
		time.gmtime()
	)
	
def format_long_description(string):
	format = ''
	col = 0
	for word in re.split(r'\s', string):
		if col + len(word) > WIDTH:
			format += '\n'
			col = 0
		format += ' ' + word
		col += 1 + len(word)
	return format


class Release(object):
	
	def __init__(self, info, *templates):
		assert isinstance(info, dict)
		for t in templates:
			assert isinstance(t, basestring)
		self.__info = dict(info)
		
		self['rfc_2822'] = rfc_2822()
		self['maintainer'] = '%s %s' % (
			self['author'],
			self['author_email'],
		)

		self['debian_version'] = '%s-%s' % (
			self['version'],
			self['debian_revision'],
		)
		
		self['debian_name'] = self['name']
		
		self['debian_long_description'] = \
			format_long_description(self['long_description'])
		
		
		self.__templates = set([
			'debian/changelog',
			'debian/control',
		])	
		self.__templates.update(templates)
		
	def __get_info(self):
		return dict(self.__info)
	info = property(__get_info)
		
	def __get_templates(self):
		"""
		Returns a copy of __templates as a list.
		"""
		return list(self.__templates)
	templates = property(__get_templates)		
	
	def __getitem__(self, key):
		return self.__info[key]
		
	def __setitem__(self, key, value):
		self.__info[key] = value
		
	def __iter__(self):
		for key in self.__info:
			yield key
			
	def write_templates(self):
		"""
		Writes all templates using % substitution from info.
		
		This will always write at least:
			debian/changelog
			debian/control
		"""
		for fname in self.templates:
			s = open(fname, 'r').read()
			open(fname, 'w').write(s % self.info)
			

r = Release(info)
r.write_templates()