--- portage.py	2003-08-07 13:29:11.000000000 -0700
+++ portage.py	2003-08-07 13:16:01.000000000 -0700
@@ -899,6 +899,7 @@
 			sys.exit(1)
 		self.configlist.append(self.mygcfg)
 		self.configdict["conf"]=self.configlist[-1]
+		self.configdict["conf"]["PUSEDICT"]=grabdict("/etc/portage/package.use")
 
 		for x in incrementals:
 			if os.environ.has_key(x):
@@ -927,7 +928,11 @@
 		self.regenerate()
 	
 	def regenerate(self,useonly=0):
-		global incrementals,usesplit,profiledir
+		global incrementals,usesplit,pkgusesplit,profiledir
+
+		# initialize per-package use variables to an empty dictionary
+		pkgusesplit={}
+
 		if useonly:
 			myincrementals=["USE"]
 		else:
@@ -970,6 +975,35 @@
 					#now append our new setting
 					if add:
 						mysetting.append(add)
+					#override per-package use flags
+					if mykey=="USE" and pkgusesplit!={}:
+						for y in pkgusesplit:
+							if add in pkgusesplit[y]:
+								pkgusesplit[y].remove(add)
+							if add[0]=="-" and add[1:] in pkgusesplit[y]:
+								pkgusesplit[y].remove(add[1:])
+							if add[0]!="-" and "-"+add in pkgusesplit[y]:
+								pkgusesplit[y].remove("-"+add)
+
+				# initialize per-package use flags
+				if mykey=="USE" and curdb.has_key("PUSEDICT"):
+					for x in curdb["PUSEDICT"]:
+						if x[0]=="#":
+							continue
+						mypkgusesplit=[]
+						for y in curdb["PUSEDICT"][x]:
+							if y=="-*" or y=="*":
+								continue
+							if y[0]=="-":
+								z=y[1:]
+							else:
+								z="-"+y
+							if y not in mysetting and y not in mypkgusesplit and z not in mypkgusesplit:
+								mypkgusesplit.append(y)
+						if mypkgusesplit:
+							pkgusesplit[x]=mypkgusesplit
+					del(curdb["PUSEDICT"])
+
 			#store setting in last element of configlist, the original environment:
 			self.configlist[-1][mykey]=string.join(mysetting," ")
 		#cache split-up USE var in a global
@@ -994,7 +1028,16 @@
 					if self.configdict["defaults"]["ARCH"] not in usesplit:
 						usesplit.insert(0,self.configdict["defaults"]["ARCH"])
 						self.configlist[-1]["USE"]=string.join(usesplit," ")
-	
+
+		# Store per-package setting in configlist for display in emerge --info
+		self.configlist[-1]["PKGUSE"]=""
+		mypkgusesplit=pkgusesplit.copy()
+		for x in mypkgusesplit:
+			if pkgusesplit[x]==[]:
+				del(pkgusesplit[x])
+			else:
+				self.configlist[-1]["PKGUSE"]=self.configlist[-1]["PKGUSE"]+"( "+x+" => "+string.join(pkgusesplit[x]," ")+" )"
+
 	def __getitem__(self,mykey):
 		if mykey=="CONFIG_PROTECT_MASK":
 			suffix=" /etc/env.d"
@@ -1563,6 +1606,9 @@
 		settings["PVR"]=mysplit[1]
 	else:
 		settings["PVR"]=mysplit[1]+"-"+mysplit[2]
+
+	settings["PUSE"]=string.join(dep_getpuse(category+"/"+pf)," ")
+
 	settings["SLOT"]=""
 	if settings.has_key("PATH"):
 		mysplit=string.split(settings["PATH"],":")
@@ -2594,7 +2640,23 @@
 		mydep=mydep[1:]
 	return prefix+cpv_expand(mydep,mydb)+postfix
 
-def dep_check(depstring,mydbapi,use="yes",mode=None):
+def dep_getpuse(mypkg):
+	global usesplit,pkgusesplit
+	if not len(mypkg):
+		return []
+	mylist=[]
+	for mykey in pkgusesplit:
+		if db["/"]["porttree"].dbapi.xmatch("bestmatch-list", mykey, None, None, [mypkg]):
+			for x in pkgusesplit[mykey]:
+				if x[0]=="-":
+					y=x[1:]
+				else:
+					y="-"+x
+				if y not in mylist and x not in mylist:
+					mylist.append(x)
+	return mylist
+		
+def dep_check(depstring,mydbapi,use="yes",mode=None,parent=None):
 	"""Takes a depend string and parses the condition."""
 	global usesplit
 	if use=="all":
@@ -2602,7 +2664,13 @@
 		myusesplit=["*"]
 	elif use=="yes":
 		#default behavior
-		myusesplit=usesplit
+		myusesplit=usesplit[:]
+		if parent:
+			for x in dep_getpuse(parent.split()[2]):
+				if x[0]=="-":
+					myusesplit.remove(x[1:])
+				else:
+					myusesplit.append(x)
 	else:
 		#we are being run by autouse(), don't consult USE vars yet.
 		myusesplit=[]
@@ -3824,6 +3892,7 @@
 		if mylist==None:
 			return []
 		newlist=[]
+		pkgdict=grabdict("/etc/portage/package.keywords")
 		for mycpv in mylist:
 			#we need to update this next line when we have fully integrated the new db api
 			auxerr=0
@@ -3836,15 +3905,19 @@
 				#print "!!! No KEYWORDS for "+str(mycpv)+" -- Untested Status"
 				continue
 			mygroups=myaux[0].split()
+			pgroups=groups[:]
 			match=0
+			for mykey in pkgdict:
+				if db["/"]["porttree"].dbapi.xmatch("bestmatch-list", mykey, None, None, [mycpv]):
+					pgroups.extend(pkgdict[mykey])
 			for gp in mygroups:
 				if gp=="*":
 					match=1
 					break
-				elif "-"+gp in groups:
+				elif "-"+gp in pgroups:
 					match=0
 					break
-				elif gp in groups:
+				elif gp in pgroups:
 					match=1
 					break
 			if match: