#!/usr/bin/env python
import driveragent
#We parse the original usb.ids file and store their vendors and products in a memory dictionary.
def GetLinuxUSB(filename,vendorsList):
	#LINUX-USB PROJECT LIST
	#linuxusb_url = "http://www.linux-usb.org/usb.ids"
	#linuxusbFile = urllib.urlopen(linuxusb_url)
	linuxusbFile = open(filename)


	lastVendor = ""
	canStart = False
	totalProducts = 0

	for line in linuxusbFile.readlines():
		if line.find("interface  interface_name") != -1: 
			canStart = True
			continue
		
		if canStart and len(line.rstrip()):
			if line.find("List of known device") != -1:
				break

			#NEW VENDOR
			split_array = line.rstrip().split('\t')
			if len(split_array[0]):
				#print split_array[0]
				vID =  split_array[0][:len("0000")].lower()
				vName =  split_array[0][len("0000  "):]
				if not vID in vendorsList:
					vendorsList[vID] = {'vName': vName, 'productList' : {}}
			else:	
				#New product
				pID =  split_array[1][:len("0000")].lower()
				pName =  split_array[1][len("0000  "):]
				if vID in vendorsList:
					if pID not in vendorsList[vID]['productList']:
					#Adding New Product
						#totalNewProducts += 1
						vendorsList[vID]['productList'][pID] = pName
							
				else:
					#Adding New Vendor and Product
					#totalNewVendors += 1
					#totalNewProducts += 1
					vendorsList[vID] = {'vName': vName, 'productList' : {}}
					vendorsList[vID]['productList'][pID] = pName
						
				
	#print str(len(vendorsList)) + " companies. "+ str(totalProducts)+ " products"
	return vendorsList

def addFreeBSD(filename,vendorsList):
#FREEUSB USBDEVS PROJECT LIST
	#linuxusb_url = "http://www.freebsd.org/cgi/cvsweb.cgi/src/sys/dev/usb/usbdevs"
	#linuxusbFile = urllib.urlopen(linuxusb_url)
	linuxusbFile = open(filename)


	lastVendor = ""
	canStart = False
	vendorsListBsd = {}
	totalNewProducts = 0
	totalProducts = 0
	totalNewVendors = 0

	for line in linuxusbFile.readlines():
		if line.find("Unknown vendor") != -1: 
			canStart = True
		
		if canStart and len(line.rstrip()):
			if line.startswith("/*") or line.startswith(" *"):
				continue

			split_array = line.rstrip().split('\t')
			
			if split_array[0].startswith("vendor"):
				#NEW VENDOR
				vCodeName = split_array[0][len("vendor "):]
				position_id = 1
				if not len(split_array[1]):
					position_id = 2
				
				vID = split_array[position_id][len("0x"):]
				vName = split_array[position_id +1]
				#print vCodeName,vID,vName
				vendorsListBsd[vCodeName] = [vID,vName]

			if split_array[0].startswith("product"):
				#NEW PRODUCT
				totalProducts +=1
				#Taking care of the tabulators problems
				#print split_array
				all_strings = ""
				for i in split_array:
					if len(i):
						all_strings += i.strip()+" "
				product = all_strings.split(" ")[1:4]
				product.extend([split_array[-1]])
				product[2] = product[2][len("0x"):]

				#print product
				vCodeName = product[0]
				pID = product[2]
				pName = product[3]

				#Finding Vendor ID
				if vCodeName in vendorsListBsd:
					vID = vendorsListBsd[vCodeName][0]
					vName = vendorsListBsd[vCodeName][1]
					#Is that product already in the usb.ids ?
					if vID in vendorsList:
						if pID not in vendorsList[vID]['productList']:
						#Adding New Product
							totalNewProducts += 1
							vendorsList[vID]['productList'][pID] = pName
							
					else:
						#Adding New Vendor and Product
						totalNewVendors += 1
						totalNewProducts += 1
						vendorsList[vID] = {'vName': vName, 'productList' : {}}
						vendorsList[vID]['productList'][pID] = pName
						#print vID,pID,vName," - ",pName
				#else:
				#Bugs in the usbdevs file
					#print vCodeName

	#print str(totalNewProducts) + " new products of "+ str(totalProducts)+ " products. New vendors = "+ str(totalNewVendors)

#Usb.org has a list of many Vendor Names, let's compare with our list
def getVendorNamesFromUsbORG(filename,vendorsList):
	#linuxusb_url = "http://www.usb.org/developers/tools/comp_dump"
	#linuxusbFile = urllib.urlopen(linuxusb_url)
	linuxusbFile = open(filename)
	totalNewVendors = 0
	import string
	for line in linuxusbFile.readlines():
		decimalID = line.split("|")[0]
		vName = line.split("|")[1].rstrip()
		if len(decimalID) == 4:
			vID = string.lower(string.zfill("%X" % int(decimalID),4))
			if vID in vendorsList:
				#vendorsList[vID]['vName'] = vName
				pass
			else:
				totalNewVendors += 1
				vendorsList[vID] = {'vName': vName, 'productList' : {}}
	print "New vendors = "+ str(totalNewVendors)

#Now we have all the Products in our vendorsList, let's display using the usb.ids ordered and tabulated format.
def showAll(vendorsList):
	for vID in sorted(vendorsList.keys()):
		vInfo = vendorsList[vID]
		print vID+"  "+vInfo["vName"]
		products = vInfo["productList"]
		for pID in sorted(products.keys()):
			pName = products[pID]
			print "\t"+pID+"  "+pName

if __name__ == '__main__':
	import urllib
	##TEST-1 Getting the new Ids from FreeBSD
	#vendorsList = GetLinuxUSB("newusb.ids")
	#addFreeBSD("usbdevs",vendorsList)
	#showAll(vendorsList)

	#TEST-2 Getting the new Ids from FreeBSD
	#vendorsList = GetLinuxUSB("usb.ids")
	#getVendorNamesFromUsbORG("usb.if",vendorsList)
	#showAll(vendorsList)
	
	#TEST-3 Getting the new Ids from DRIVERAGENT.COM
	#vendorsList = {}
	#vendorsList = GetLinuxUSB("usb.ids",vendorsList)
	#driveragent.GetDriverAgentCOM(vendorsList)
	
	#TEST-4 Getting the new Ids from FLAMEYES http://dev.gentooexperimental.org/~flameeyes/usb.ids
	vendorsList = {}
	vendorsList = GetLinuxUSB("usb_newProducts.ids",vendorsList)
	vendorsList = GetLinuxUSB("usb2.ids",vendorsList)
	showAll(vendorsList)
	
	

