aboutsummaryrefslogtreecommitdiff
blob: b7644bed496a590344b87e3cf600d1b077bc8f85 (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
# Copyright(c) 2009, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2 or higher
#
# $Header: $

"""Display the  Gentoo ChangeLog entry for the latest installable version of a
given package
"""

# Move to Imports sections when Python 2.6 is stable
from __future__ import with_statement

__author__ = 'Douglas Anderson'
__docformat__ = 'epytext'

# =======
# Imports
# =======

import os
import sys
from getopt import gnu_getopt, GetoptError

from portage.versions import pkgsplit

import gentoolkit.pprinter as pp
from gentoolkit import errors
from gentoolkit.equery import format_options, mod_usage
from gentoolkit.helpers2 import find_best_match, find_packages
from gentoolkit.package import Package, VersionMatch

# =======
# Globals
# =======

QUERY_OPTS = {
	'onlyLatest': False,
	'showFullLog': False,
	'limit': None,
	'from': None,
	'to': None
}

# =========
# Functions
# =========

def print_help(with_description=True):
	"""Print description, usage and a detailed help message.
	
	@type with_description: bool
	@param with_description: if true, print module's __doc__ string
	"""

	if with_description:
		print __doc__.strip()
		print
	print mod_usage(mod_name="changes")
	print
	print pp.emph("examples")
	print (" c portage                                # show latest visible "
	       "version's entry")
	print " c portage --full --limit=3               # show 3 latest entries"
	print " c '=sys-apps/portage-2.1.6*'             # use atom syntax"
	print " c portage --from=2.2_rc20 --to=2.2_rc30  # use version ranges"
	print
	print pp.command("options")
	print format_options((
		(" -h, --help", "display this help message"),
		(" -l, --latest", "display only the latest ChangeLog entry"),
		(" -f, --full", "display the full ChangeLog"),
		("     --limit=NUM",
			"limit the number of entries displayed (with --full)"),
		("     --from=VER", "set which version to display from"),
		("     --to=VER", "set which version to display to"),
	))


def get_logpath(pkg):
	"""Test that the package's ChangeLog path is valid and readable, else
	die.

	@type pkg: gentoolkit.package.Package
	@param pkg: package to find logpath for
	@rtype: str
	@return: a path to a readable ChangeLog
	"""

	logpath = os.path.join(pkg.get_package_path(), 'ChangeLog')
	if not os.path.isfile(logpath) or not os.access(logpath, os.R_OK):
		pp.die(1, "%s does not exist or is unreadable"
			% pp.path(logpath))
	
	return logpath


def get_match(query):
	"""Find a valid package to get the ChangeLog path from or raise
	GentoolkitNoMatches.
	"""

	match = matches = None
	match = find_best_match(query)
		
	if not match:
		matches = find_packages(query, include_masked=True)
	else:
		matches = [match]

	if not matches:
		pp.print_warn("Try using an unversioned query with "
			"--from and --to.")
		raise errors.GentoolkitNoMatches(query)

	return matches[0]


def index_changelog(entries):
	"""Convert the list from split_changelog into a dict with VersionMatch
	instance as the index.

	@todo: UPDATE THIS
	@type entries: list
	@param entries: output of split_changelog
	@rtype: dict
	@return: dict with gentoolkit.package.Package instances as keys and the
		corresponding ChangeLog entree as its value
	"""

	result = []
	for entry in entries:
		# Extract the package name from the entry, ex: 
		# *xterm-242 (07 Mar 2009) => xterm-242
		pkg_name = entry.split(' ', 1)[0].lstrip('*')
		pkg_split = pkgsplit(pkg_name)
		result.append(
			(VersionMatch(op="=", ver=pkg_split[1], rev=pkg_split[2]), entry))
	
	return result


def is_ranged(atom):
	"""Return True if an atom string appears to be ranged, else False."""

	return atom.startswith(('~', '<', '>')) or atom.endswith('*')


def parse_module_options(module_opts):
	"""Parse module options and update GLOBAL_OPTS"""

	opts = (x[0] for x in module_opts)
	posargs = (x[1] for x in module_opts)
	for opt, posarg in zip(opts, posargs):
		if opt in ('-h', '--help'):
			print_help()
			sys.exit(0)
		elif opt in ('-f', '--full'):
			QUERY_OPTS['showFullLog'] = True
		elif opt in ('-l', '--latest'):
			QUERY_OPTS['onlyLatest'] = True
		elif opt in ('--limit',):
			set_limit(posarg)
		elif opt in ('--from',):
			set_from(posarg)
		elif opt in ('--to',):
			set_to(posarg)


def print_matching_entries(indexed_entries, pkg, first_run):
	"""Print only the entries which interect with the pkg version."""

	from_restriction = QUERY_OPTS['from']
	to_restriction = QUERY_OPTS['to']

	for entry_set in indexed_entries:
		i, entry = entry_set
		# a little hackery, since versionmatch doesn't store the
		# package key, but intersects checks that it matches.
		i.key = pkg.key
		if from_restriction or to_restriction:
			if from_restriction and not from_restriction.match(i):
				continue
			if to_restriction and not to_restriction.match(i):
				continue
		elif not pkg.intersects(i):
			continue

		if not first_run:
			print "\n"
		print entry.strip()
		first_run = False
	
	return first_run


def set_from(posarg):
	"""Set a starting version to filter the ChangeLog with or die if posarg
	is not a valid version.
	"""

	pkg_split = pkgsplit('null-%s' % posarg)

	if pkg_split and not is_ranged(posarg):
		ver_match = VersionMatch(
			op=">=",
			ver=pkg_split[1], 
			rev=pkg_split[2] if pkg_split[2] != 'r0' else '')
		QUERY_OPTS['from'] = ver_match
	else:
		err = "Module option --from requires valid unranged version (got '%s')"
		pp.print_error(err % posarg)
		print
		print_help(with_description=False)
		sys.exit(2)


def set_limit(posarg):
	"""Set a limit in QUERY_OPTS on how many ChangeLog entries to display or
	die if posarg is not an integer.
	"""

	if posarg.isdigit():
		QUERY_OPTS['limit'] = int(posarg)
	else:
		err = "Module option --limit requires integer (got '%s')"
		pp.print_error(err % posarg)
		print
		print_help(with_description=False)
		sys.exit(2)


def set_to(posarg):
	"""Set an ending version to filter the ChangeLog with or die if posarg
	is not a valid version.
	"""

	pkg_split = pkgsplit('null-%s' % posarg)
	if pkg_split and not is_ranged(posarg):
		ver_match = VersionMatch(
			op="<=",
			ver=pkg_split[1], 
			rev=pkg_split[2] if pkg_split[2] != 'r0' else '')
		QUERY_OPTS['to'] = ver_match
	else:
		err = "Module option --to requires valid unranged version (got '%s')"
		pp.print_error(err % posarg)
		print
		print_help(with_description=False)
		sys.exit(2)


def split_changelog(logpath):
	"""Split the changelog up into individual entries.
	
	@type logpath: str
	@param logpath: valid path to ChangeLog file
	@rtype: list
	@return: individual ChangeLog entrees
	"""

	result = []
	partial_entries = []
	with open(logpath) as log:
		for line in log:
			if line.startswith('#'):
				continue
			elif line.startswith('*'):
				# Append last entry to result...
				entry = ''.join(partial_entries)
				if entry and not entry.isspace():
					result.append(entry)
				# ... and start a new entry
				partial_entries = [line]
			else:
				partial_entries.append(line)
		else:
			# Append the final entry
			entry = ''.join(partial_entries)
			result.append(entry)

	return result


def main(input_args):
	"""Parse input and run the program"""

	short_opts = "hlf"
	long_opts = ('help', 'full', 'from=', 'latest', 'limit=', 'to=')

	try:
		module_opts, queries = gnu_getopt(input_args, short_opts, long_opts)
	except GetoptError, err:
		pp.print_error("Module %s" % err)
		print
		print_help(with_description=False)
		sys.exit(2)

	parse_module_options(module_opts)

	if not queries:
		print_help()
		sys.exit(2)

	first_run = True
	for query in queries:
		if not first_run:
			print

		ranged_query = None
		if is_ranged(query):
			# Raises GentoolkitInvalidCPV here if invalid
			ranged_query = Package(query)

		pkg = get_match(query)
		logpath = get_logpath(pkg)
		log_entries = split_changelog(logpath)
		indexed_entries = index_changelog(log_entries)

		#
		# Output
		#

		if QUERY_OPTS['onlyLatest']:
			print log_entries[0].strip()
		elif QUERY_OPTS['showFullLog']:
			end = QUERY_OPTS['limit'] or len(log_entries)
			for entry in log_entries[:end]:
				print entry
			first_run = False
		else:
			if ranged_query:
				pkg = ranged_query
			first_run = print_matching_entries(indexed_entries, pkg, first_run)

		first_run = False