Commits

Michał Górny committed 719da99

Support --strict mode.

Comments (0)

Files changed (2)

lib/flaggie/action.py

 class ParserError(Exception):
 	pass
 
+class ParserWarning(Exception):
+	pass
+
 class Action(object):
 	class BaseAction(object):
 		class Pattern(object):
 					self.args.add(self.Pattern(arg))
 					return
 
+			warn = None
 			if not pkgs:
 				wis = cache.glob_whatis(arg, restrict = ns)
 				if len(wis) > 1:
 					ns = wis.pop()
 				elif ns:
 					ns = ns.pop()
-					print('Warning: %s seems to be an incorrect global %s' % \
-							(arg, cache.describe(ns)))
+					warn = '%s seems to be an incorrect global %s' % \
+							(arg, cache.describe(ns))
 				else:
 					ns = 'use'
-					print('Warning: %s seems to be an incorrect global flag' % arg)
+					warn = '%s seems to be an incorrect global flag' % arg
 			else:
 				for p in pkgs:
 					wis = cache.whatis(arg, p, restrict = ns)
 					else:
 						if gwis:
 							ns = gwis.pop()
-							print('Warning: %s seems to be an incorrect %s for %s' % \
-									(arg, cache.describe(ns), p))
+							warn = '%s seems to be an incorrect %s for %s' % \
+									(arg, cache.describe(ns), p)
 						else:
 							ns = 'use'
-							print('Warning: %s seems to be an incorrect flag for %s' % (arg, p))
+							warn = '%s seems to be an incorrect flag for %s' % (arg, p)
+
 			self.ns = frozenset((ns,))
 			self.args.add(arg)
+			if warn:
+				raise ParserWarning(warn)
 
 		def append(self, arg):
 			if isinstance(arg, self.__class__):
 
 	def append(self, item):
 		if isinstance(item, Action.BaseAction):
-			item.clarify(self.pkgs, self._cache)
+			reraising = False
+			try:
+				item.clarify(self.pkgs, self._cache)
+			except ParserWarning:
+				reraising = True
+
 			for a in self:
 				if isinstance(item, a.__class__) and item.ns == a.ns:
 					a.append(item)
 					break
 			else:
 				list.append(self, item)
+
+			if reraising:
+				raise
 		else:
 			self.pkgs.append(item)
 
 from portage.exception import AmbiguousPackageName, InvalidAtom
 
 from flaggie import PV
-from flaggie.action import Action, ActionSet, ParserError
+from flaggie.action import Action, ActionSet, ParserError, ParserWarning
 from flaggie.cache import Caches
 from flaggie.cleanup import DropIneffective, SortEntries, SortFlags
 from flaggie.packagefile import PackageFiles
 
-def parse_actions(args, dbapi, settings):
+def parse_actions(args, dbapi, settings, strict = False):
 	out = []
 	cache = Caches(dbapi)
 	actset = ActionSet(cache = cache)
 				if atom.startswith('null/'):
 					raise ParserError('unable to determine the category (mistyped name?)')
 				actset.append(atom)
+			except ParserWarning as w:
+				actset.append(act)
+				raise
 			else:
 				actset.append(act)
-		except ParserError as e:
+		except (ParserError, ParserWarning) as e:
 			print('At argv[%d]=\'%s\': %s' % (i + 1, a, e))
+			if strict:
+				print('Strict mode, aborting.')
+				return None
 
 	if actset and (actset.pkgs or not had_pkgs):
 		out.append(actset)
 
 def main(argv):
 	cleanup_actions = set()
+	strict = False
 
 	for a in list(argv[1:]):
 		if a.startswith('--'):
 %s [<options>] [<global-actions>] [<packages> <actions>] [...]
 
 Options:
+	--strict		Abort if at least a single flag is invalid
+
 	--drop-ineffective	Drop ineffective flags (those which are
 				overriden by later declarations)
 	--sort-entries		Sort package.* file entries by package
 A package specification can be any atom acceptable for Portage (in the same
 format as taken by emerge).''' % os.path.basename(argv[0]))
 				return 0
+			elif a == '--strict':
+				strict = True
 			elif a == '--drop-ineffective':
 				cleanup_actions.add(DropIneffective)
 			elif a == '--sort-entries':
 			target_root = os.environ.get('ROOT'))
 	porttree = trees[max(trees)]['porttree']
 
-	act = parse_actions(argv[1:], porttree.dbapi, porttree.settings)
+	act = parse_actions(argv[1:], porttree.dbapi, porttree.settings, strict = strict)
+	if act is None:
+		return 1
 	if not act and not cleanup_actions:
 		main([argv[0], '--help'])
 		return 0