aboutsummaryrefslogtreecommitdiff
blob: a435f5c377ea1b39d11aea11bae300993c84b241 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# R Overlay -- ebuild creation, ebuild class
# Copyright 2006-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import copy

import roverlay.config

from roverlay.util import shorten_str


class Ebuild ( object ):
	EBUILD_INDENT = roverlay.config.get ( 'EBUILD.indent', '\t' )

	ADD_REMAP = {
		# pkg vs package
		'package_name'     : 'pkg_name',
		'package_version'  : 'pkg_version',
		'package_revision' : 'pkg_revision',
		# TITLE is in DESCRIPTION
		'TITLE'            : 'DESCRIPTION',

		# TODO: remove these entries by fixing ebuildcreator/ebuildjob
		'DEPENDS'          : 'DEPEND',
		'RDEPENDS'         : 'RDEPEND',
		'RSUGGESTS'        : 'R_SUGGESTS',
	}

	def __init__ ( self, logger ):
		"""Initializes an Ebuild.
		This is an abstraction layer between the verified + calculated data
		and the ebuild data, which can be written into a file / stdout / stdin.
		Most functions here assume that everything is fine when it reaches them.

		arguments:
		* logger -- logger for this Ebuild
		"""

		self.logger = logger

		# elements in ebuild_data are either a str or a list of str
		self._data = dict ()
		self._ebuild_lines = None
		self._ebuild_name = None
		self.has_rsuggests = False

	# --- end of __init__ (...) ---

	def cleanup ( self ):
		"""Removes stored data if ebuild_lines have already been calculated.
		This saves some memory but makes this Ebuild read-only.
		"""
		if self._ebuild_lines:
			# determine the ebuild name first
			self._ebuild_name = self.suggest_name()
			del self._data
			self._data = None

	# --- end of cleanup (...) ---

	def prepare ( self, force_update=False, cleanup_after=False ):
		"""Tells this Ebuild to create ebuild lines.

		arguments:
		* force_update -- create ebuild lines if they exist; defaults to False
								and ignored if this Ebuild has been cleaned up
		* cleanup_after -- run cleanup() after successful creation

		Returns True if ebuild_lines have been created or if they exist and
		an update has not been enforced. Else returns False.
		"""
		if self._ebuild_lines and not force_update:
			return True
		elif self._data:
			self._ebuild_lines = self._make_ebuild_lines()
			if self._ebuild_lines:
				if cleanup_after: self.cleanup()
				return True
		elif self._ebuild_lines:
			# self._data is None
			return True

		return False

	# --- end of prepare (...) ---

	def has_ebuild ( self ):
		"""Returns True if this object has ebuild text lines else False."""
		return bool ( self._ebuild_lines )
	# --- end of has_ebuild (...) ---

	def add ( self, key, value, append=True ):
		"""Adds data to this Ebuild.

		arguments:
		* key -- identifier of the data (e.g. DEPEND).
		         May be remapped (e.g. merging 'Title' and 'Description')
		         or even refused here
		* value --
		* append -- whether to append values or overwrite existing ones,
		            defaults to True.

		raises: Exception when ebuild data are readonly
		"""
		if self._data is None:
			# -- todo
			raise Exception ("Ebuild is readonly.")

		_key = Ebuild.ADD_REMAP [key] if key in Ebuild.ADD_REMAP else key

		if _key is None:
			self.logger.debug ( "add (%s, %s): filtered key.", key, value )
		else:
			if append and _key in self._data:
				if not isinstance ( self._data [_key], list ):
					self._data [_key] = [ self._data [_key] ]

				if isinstance ( value, list ):
					self._data [_key].extend ( value )
				else:
					self._data [_key].append ( value )

			else:
				self._data [_key] = value

	# --- end of add (...) ---

	def write ( self, file_to_write ):
		"""Writes an ebuild file.

		arguments:
		* file_to_write -- path to the file that should be written
		"""
		# prepare ebuild lines and open file handle after that
		if self.prepare ( False, False ):
			try:
				fh = open ( file_to_write, 'w' )
				self.show ( fh )
				fh.close()
				del fh
			except IOError as err:
				self.logger.exception ( err )
				raise

		else:
				self.logger.warning (
					'Cannot write ebuild - it\'s empty! '
					'(check with has_ebuild() before calling this method.)'
				)

	# --- end of write (...) ---

	def show ( self, file_handle ):
		"""Prints the ebuild content into a file_handle.

		arguments:
		file_handle -- object that has a writelines ( list ) method, e.g. file.

		Returns True if writing was successful, else False.
		"""
		if self.prepare ( False, False ):
			lines = [ line + "\n" for line in self._ebuild_lines ]
			file_handle.writelines ( lines )
			del lines
			return True
		else:
			return False

	# --- end of show (...) ---

	def suggest_dir_name ( self ):
		"""Suggests a direcory name for this Ebuild."""
		if self._data is None:
			return self._ebuild_name.partition ( '-' ) [0]
		elif 'pkg_name' in self._data:
			return self._data ['pkg_name']
		else:
			return self.suggest_name().partition ( '-' ) [0]
	# --- end of suggest_dir_name (...) ---

	def suggest_name ( self, fallback_name='' ):
		"""Suggests a file name for the ebuild. This is calculated using
		pkg_name/version/revision. Returns a fallback_name if this is not
		possible.

		arguments:
		fallback_name -- name to return if no suggestion available,
		                 defaults to empty string
		"""

		if self._ebuild_name:
			return self._ebuild_name
		elif not self._data is None and 'pkg_name' in self._data:
			name_components = [ self._data ['pkg_name'] ]

			if 'pkg_version' in self._data:
				name_components.append ( self._data ['pkg_version'] )
			else:
				# default ver
				name_components.append ( '1.0' )

			if 'pkg_revision' in self._data:
				rev = self._data ['pkg_revision']

				# omit rev == 0 and invalid revisions
				if isinstance ( rev, int ) and rev > 0:
					name_components.append ( 'r%i' % rev )

			return '-'.join ( name_components )

		else:
			return fallback_name

	# --- end of suggest_name (...) ---

	def _make_ebuild_lines ( self ):
		"""Creates text lines for this Ebuild.
		It assumes that enough data to do this are available.
		Exceptions (KeyError, NameError, ...) are passed if that's not the case.
		"""

		def get_dep_and_use():
			"""Creates values for the DEPEND, RDEPEND, IUSE and, if possible,
			R_SUGGESTS variables and returns them as dict { VARNAME -> VALUE }.
			"""

			# have suggests if they're set and not empty
			self.has_rsuggests = bool (
				'R_SUGGESTS' in self._data and self._data ['R_SUGGESTS']
			)

			# set defaults: inherit eclass + include depend in rdepend
			# TODO: is ${DEPEND:-},... necessary?
			ret = dict (
				DEPEND  = [ '${DEPEND:-}' ],
				# assuming that the eclass includes it's DEPEND in RDEPEND
				RDEPEND = [ '${RDEPEND:-}' ],
				IUSE    = [ '${IUSE:-}' ],
			)

			for kw in ( x for x in ( 'DEPEND', 'RDEPEND' ) if x in self._data ):
				if isinstance ( self._data [kw], list ):
					ret [kw].extend ( self._data [kw] )
				else:
					ret [kw].append ( self._data [kw] )


			if self.has_rsuggests:
				ret ['R_SUGGESTS'] = self._data ['R_SUGGESTS']

				# +R_SUGGESTS, -R_SUGGESTS?
				ret ['IUSE'].append ( 'R_suggests' )
				# do these braces help or confuse? TODO FIXME
				ret ['RDEPEND'].append ( '( R_suggests ? ${R_SUGGESTS} )' )

			return ret

		# --- end of get_dep_and_use () ---

		def make_var (
			varname,
			value=None, oneline_list=True, indent_list=True, indent_level=0
		):
			"""Creates a <name>=<value> statement for ebuilds.

			arguments:
			* varname      -- name of the variable
			* value        -- value of the variable.
			                   This has to be either None (the default), str,
			                   or list of str.
			* oneline_list -- if value is a list: controls whether its components
			                   should be put into one line (True) or multiple.
			                   Defaults to True.
			* indent_list  -- if value is a list and not oneline_list:
			                   controls whether each value line should be
			                   indentend (by indent_level + 1) or not ("by 0").
			                   Defaults to True.
			* indent_level -- current indentation level, defaults to 0

			"""

			# assumption: value is either None,
			#              scalar with str representation or list of str
			var_value = None

			if not value:
				var_value = ""

			elif isinstance ( value, list ):
				if oneline_list:
					var_value = ' '.join ( value )
				elif indent_list:
					var_value = (
						'\n' + (indent_level + 1) * Ebuild.EBUILD_INDENT
					).join ( value )
				else:
					'\n'.join ( value )

			else:
				var_value = str ( value )


			# (TODO)
			# fixing ebuild var values here

			# cut DESCRIPTION line if too long
			if varname == 'DESCRIPTION':
				var_value = shorten_str ( var_value, 45, '... (see metadata)' )


			ret ='%s%s="%s"' % (
				indent_level * Ebuild.EBUILD_INDENT,
				varname,
				var_value
			)

			# (TODO)
			# fixing ebuild var lines here

			return ret

		# --- end of make_var (...) ---

		def remove_newlines ( line_list ):
			"""Removes leading, ending and repeated blank lines in line_list.

			arguments:
			* line_list --

			returns: filtered lines

			TODO: check if a filter function could be used for this
			"""
			lines = []
			line = None
			last_line_empty = False

			for line in line_list:
				line = line.rstrip()
				# re.sub \n{2,} \n :: FIXME?

				if line:
					last_line_empty = False
				elif not last_line_empty:
					last_line_empty = True
				else:
					continue

				lines.append ( line )

			# remove last line if empty
			##if last_line_empty: (?)
			if len ( lines ) and not lines [-1]:
				del lines [-1]

			return lines

		# --- end of remove_newlines (...) ---

		def add_easyvar (
			ebuild_content, varname,
			value_key=None, add_newline=False
		):
			"""Adds a 'simple' variable to the ebuild lines.
			This means that it can directly be taken from self._data [value_key].
			This method assumes that value_key exists in self._data,
			any exceptions (KeyError) will be passed.

			arguments:
			* ebuild_content -- list of ebuild text lines, will be modified
			                     directly, so copy it before calling addvar if
			                     you need the original list.
			* varname        -- name of the variable.
			                     Nothing happens if this is None.
			* value_key      -- key of the value,
			                     defaults to varname if it is None
			* add_newline    -- adds a newline after the var statement,
			                     defaults to False

			returns: given+modified list (ebuild_content)
			"""

			if not varname is None:
				if value_key is None:
					ebuild_content.append (
						make_var ( varname, self._data [varname] )
					)
				else:
					ebuild_content.append (
						make_var ( varname, self._data [value_key] )
					)

				if add_newline:
					ebuild_content.append ( "" )

			return ebuild_content

		# --- end of add_easyvar (...) ---

		# -- actual start of _make_ebuild_lines (...) --
		try:
			ebuild_lines = []

			if 'ebuild_header' in self._data:
				ebuild_lines = copy.copy ( self._data ['ebuild_header'] )
				ebuild_lines.append ( "" )

			add_easyvar ( ebuild_lines, "PKG_FILE" )
			if 'PKG_ORIGIN' in self._data:
				add_easyvar ( ebuild_lines, "PKG_ORIGIN", None, False )

			ebuild_lines.append ( "" )

			# TODO/FIXME: this makes DESCRIPTION mandatory, maybe check with
			#  >if 'DESCRIPTION' in self._data<
			add_easyvar ( ebuild_lines, "DESCRIPTION" )

			add_easyvar ( ebuild_lines, "SRC_URI", add_newline=True )

			# FIXME/TODO: LICENSE?

			dep_and_use = get_dep_and_use ()

			# check that IUSE has more than one element,
			#  don't write IUSE="${IUSE:-}" etc.
			if len ( dep_and_use ['IUSE'] ) > 1:
				ebuild_lines.append (
					make_var ( "IUSE", dep_and_use ['IUSE'], True )
				)

			if 'R_SUGGESTS' in dep_and_use:
				ebuild_lines.append (
					make_var ( "R_SUGGESTS", dep_and_use ['R_SUGGESTS'], False )
				)

			# see IUSE
			if len ( dep_and_use ['DEPEND'] ) > 1:
				ebuild_lines.append (
					make_var ( "DEPEND", dep_and_use ['DEPEND'], False )
				)

			# see IUSE
			if len ( dep_and_use ['RDEPEND'] ) > 1:
				ebuild_lines.append (
					make_var ( "RDEPEND", dep_and_use ['RDEPEND'], False )
				)

			del dep_and_use
			return remove_newlines ( ebuild_lines )

		except ( ValueError, KeyError, NameError ) as err:
			self.logger.exception ( err )
			self.logger.error ( "Cannot create ebuild text lines." )
			return None

		# --- end of make_ebuild_lines (...) ---