aboutsummaryrefslogtreecommitdiff
blob: 513346d1b2070de715a15527ea25aa7f74cd01c5 (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
#!/usr/bin/python

# Copyright 2003-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2


from __future__ import print_function


import os
import sys
if sys.hexversion < 0x3000000:
	from io import open
import re
import portage
from portage import _encodings, _unicode_decode, _unicode_encode

from gentoolkit.pprinter import warn

# Misc. shortcuts to some portage stuff:
listdir = portage.listdir

FILENAME_RE = [re.compile(r'(?P<pkgname>[-a-zA-z0-9\+]+)(?P<ver>-\d+\S+)'),
	re.compile(r'(?P<pkgname>[-a-zA-z]+)(?P<ver>_\d+\S+)'),
	re.compile(r'(?P<pkgname>[-a-zA-z_]+)(?P<ver>\d\d+\S+)'),
	re.compile(r'(?P<pkgname>[-a-zA-z0-9_]+)(?P<ver>-default\S+)'),
	re.compile(r'(?P<pkgname>[-a-zA-z0-9]+)(?P<ver>_\d\S+)'),
	re.compile(r'(?P<pkgname>[-a-zA-z0-9\+\.]+)(?P<ver>-\d+\S+)'),
	re.compile(r'(?P<pkgname>[-a-zA-z0-9\+\.]+)(?P<ver>.\d+\S+)')]

debug_modules = []

def dprint(module, message):
	if module in debug_modules:
		print(message)

def isValidCP(cp):
	"""Check whether a string is a valid cat/pkg-name.

	This is for 2.0.51 vs. CVS HEAD compatibility, I've not found any function
	for that which would exists in both. Weird...

	@param cp: catageory/package string
	@rtype: bool
	"""

	if not '/' in cp:
		return False
	try:
		portage.cpv_getkey(cp+"-0")
	except:
		return False
	else:
		return True


class ParseExcludeFileException(Exception):
	"""For parseExcludeFile() -> main() communication.

	@param value: Error message string
	"""
	def __init__(self, value):
		self.value = value
	def __str__(self):
		return repr(self.value)


def parseExcludeFile(filepath, output):
	"""Parses an exclusion file.

	@param filepath: file containing the list of cat/pkg's to exclude
	@param output: --verbose enabled output method or "lambda x: None"

	@rtype: dict
	@return: an exclusion dict
	@raise ParseExcludeFileException: in case of fatal error
	"""

	exclude = {
			'categories': {},
			'packages': {},
			'anti-packages': {},
			'filenames': {}
		}
	output("Parsing Exclude file: " + filepath)
	try:
		file_ = open(_unicode_encode(filepath, 
			encoding=_encodings['fs']), mode="r", encoding=_encodings['content'])
	except IOError:
		raise ParseExcludeFileException("Could not open exclusion file: " +
			filepath)
	filecontents = file_.readlines()
	file_.close()
	cat_re = re.compile('^(?P<cat>[a-zA-Z0-9]+-[a-zA-Z0-9]+)(/\*)?$')
	cp_re = re.compile('^(?P<cp>[-a-zA-Z0-9_]+/[-a-zA-Z0-9_]+)$')
	# used to output the line number for exception error reporting
	linenum = 0
	for line in filecontents:
		# need to increment it here due to continue statements.
		linenum += 1
		line = line.strip()
		if not len(line): # skip blank a line
			continue
		if line[0] == '#': # skip a comment line
			continue
		#print( "parseExcludeFile: line=", line)
		try: # category matching
			cat = cat_re.match(line).group('cat')
			#print( "parseExcludeFile: found cat=", cat)
		except:
			pass
		else:
			if not cat in portage.settings.categories:
				raise ParseExcludeFileException("Invalid category: "+cat +
					" @line # " + str(linenum))
			exclude['categories'][cat] = None
			continue
		dict_key = 'packages'
		if line[0] == '!': # reverses category setting
			dict_key = 'anti-packages'
			line = line[1:]
		try: # cat/pkg matching
			cp = cp_re.match(line).group('cp')
			#print( "parseExcludeFile: found cp=", cp)
			if isValidCP(cp):
				exclude[dict_key][cp] = None
				continue
			else:
				raise ParseExcludeFileException("Invalid cat/pkg: "+cp +
					" @line # " + str(linenum))
		except:
			pass
		#raise ParseExcludeFileException("Invalid line: "+line)
		try: # filename matching.
			exclude['filenames'][line] = re.compile(line)
			#print( "parseExcludeFile: found filenames", line)
		except:
			try:
				exclude['filenames'][line] = re.compile(re.escape(line))
				#print( "parseExcludeFile: found escaped filenames", line)
			except:
				raise ParseExcludeFileException("Invalid file name/regular " +
					"expression: @line # " + str(linenum) + " line=" +line)
	output("Exclude file parsed. Found " +
		"%d categories, %d packages, %d anti-packages %d filenames"
		%(len(exclude['categories']), len(exclude['packages']),
		len(exclude['anti-packages']), len(exclude['filenames'])))
	#print()
	#print( "parseExcludeFile: final exclude_dict = ", exclude)
	#print()
	return exclude

def cp_all(categories, portdb=portage.portdb ):
		"""temp function until the new portdb.cp_all([cat,...])
		behaviour is fully available.

		@param categories: list of categories to get all packages for
				eg. ['app-portage', 'sys-apps',...]
		@rtype: list of cat/pkg's  ['foo/bar', 'foo/baz']
		"""
		try:
			cps = portdb.cp_all(categories)
			# NOTE: the following backup code should be removed
			# when all available versions of portage have the
			# categories parameter in cp_all()
		except:  # new behaviour not available
			#~ message =  "Exception: eclean.exclude.cp_all() " +\
				#~ "new portdb.cp_all() behavior not found. using fallback code"
			#~ print( warn(message), file=sys.stderr)
			cps = []
			# XXX: i smell an access to something which is really out of API...
			_pkg_dir_name_re = re.compile(r'^\w[-+\w]*$')
			for tree in portdb.porttrees:
				for cat in categories:
					for pkg in listdir(os.path.join(tree,cat),
								EmptyOnError=1, ignorecvs=1, dirsonly=1):
						if not _pkg_dir_name_re.match(pkg) or pkg == "CVS":
							continue
						cps.append(cat+'/'+pkg)
		#print( "cp_all: new cps list=", cps)
		return cps

def exclDictExpand(exclude):
	"""Returns a dictionary of all CP/CPV from porttree which match
	the exclusion dictionary.
	"""
	d = {}
	if 'categories' in exclude:
		# replace the following cp_all call with
		# portage.portdb.cp_all([cat1, cat2])
		# when it is available in all portage versions.
		cps = cp_all(exclude['categories'])
		for cp in cps:
			d[cp] = None
	if 'packages' in exclude:
		for cp in exclude['packages']:
			d[cp] = None
	if 'anti-packages' in exclude:
		for cp in exclude['anti-packages']:
			if cp in d:
				del d[cp]
	return d

def exclDictMatchCP(exclude,pkg):
	"""Checks whether a CP matches the exclusion rules."""
	if pkg is None:
		return False
	if 'anti-packages' in exclude and pkg in exclude['anti-packages']:
		return False
	if 'packages' in exclude and pkg in exclude['packages']:
		return True
	try:
		cat = pkg.split('/')[0]
	except:
		dprint( "exclude", "exclDictMatchCP: Invalid package name: " +\
			"%s, Could not determine category" %pkg)
		cat = ''
	if 'categories' in exclude and cat in exclude['categories']:
			return True
	return False

def exclDictExpandPkgname(exclude):
	"""Returns a set of all pkgnames  from porttree which match
	the exclusion dictionary.
	"""
	p = set()
	if 'categories' in exclude:
		# replace the following cp_all call with
		# portage.portdb.cp_all([cat1, cat2])
		# when it is available in all portage versions.
		cps = cp_all(exclude['categories'])
		for cp in cps:
			pkgname = cp.split('/')[1]
			p.add(pkgname)
	if 'packages' in exclude:
		for cp in exclude['packages']:
			pkgname = cp.split('/')[1]
			p.add(pkgname)
	if 'anti-packages' in exclude:
		for cp in exclude['anti-packages']:
			if cp in p:
				p.remove(cp)
	return p


def exclMatchFilename(exclude_names, filename):
	"""Attempts to split the package name out of a filename
	and then checks if it matches any exclusion rules.

	This is intended to be run on the cleaning list after all
	normal checks and removal of protected files.  This will reduce
	the number of files to perform this last minute check on

	@param exclude_names: a set of pkgnames to exlcude
	@param filename:

	@rtype: bool
	"""
	found = False
	index = 0
	while not found and index < len(FILENAME_RE):
		found = FILENAME_RE[index].match(filename)
		index += 1
	if not found:
		dprint( "exclude", "exclMatchFilename: filename: " +\
			"%s, Could not determine package name" %filename)
		return False
	pkgname = found.group('pkgname')
	dprint("exclude", "exclMatchFilename: found pkgname = " +
		"%s, %s, %d, %s" %(pkgname, str(pkgname in exclude_names),
		index-1, filename))
	return (pkgname in exclude_names)