def main():
	global allWeeks
	global numRouters
	global averageDegree
	global disagreement

	# parse command line options
	if len(sys.argv) < 3:
		print "Usage: " + sys.argv[0] + " iffinder_analysis pl_archives [options]"
		print "Options:"
		print "\t--num-routers\tGet number of routers"
		print "\t--average-degree\tGet ratio of interface/routers"
		print "\t--disagreement\tGet number of disagreements between DNS and iffinder"
		sys.exit(1)

	# parse options
	options = set(sys.argv[3:])
	if "--num-routers" in options:
		numRouters = True
	if "--average-degree" in options:
		averageDegree = True
	if "--disagreement" in options:
		disagreement = True

	# perform analysis
	try:
		iffinder_analysis = sys.argv[1]
		pl_archives = sys.argv[2]

		# get weeks available in directory
		weeks = set()
		for weekfile in os.listdir(iffinder_analysis):
			weekNumber= weekfile.split(".")
			weeks.add(weekNumber[0])

		# open week files
		selectedDictList = []
		for week in weeks:
			try:
				stdoutFilename = iffinder_analysis + "/" + week + ".stdout.txt"
				stdoutFile = open(stdoutFilename, "r")
				stderrFilename = iffinder_analysis + "/" + week + ".stderr.txt"
				stderrFile = open(stderrFilename, "r")

				# get physical interface IPs
				masterAtlas = open(pl_archives + "/" + week + "/" + "COGENT-MASTER-ATLAS-FIXED.txt", "r")
				physicalInterfaces, virtualInterfaces, unknownInterfaces = getBreakdown(masterAtlas)
				masterAtlas.close()
				
				# get information and organize it according to options
				selectedDict = { "Week" : int(week) }
				disagreements, routerNameToIPs = getIffinderInfo(stdoutFile, stderrFile)
				
				# divide information into physical, virtual, and all
				physRouterToIPs = {}
				virtRouterToIPs = {}
				for key, value in routerNameToIPs.items():
					phys = []
					virt = []
					for ip in value:
						if ip in physicalInterfaces:
							phys.append(ip)
						elif ip in virtualInterfaces:
							virt.append(ip)
					if len(phys) != 0:
						physRouterToIPs[key] = phys
					if len(virt) != 0:
						virtRouterToIPs[key] = virt

				# print out specified information
				printOrder = ["Week"]
				if numRouters:
					selectedDict["RouterPhysCount"] = len(physRouterToIPs)
					printOrder.append("RouterPhysCount")
					selectedDict["RouterVirtCount"] = len(virtRouterToIPs)
					printOrder.append("RouterVirtCount")
					selectedDict["RouterCount"] = len(routerNameToIPs)
					printOrder.append("RouterCount")
				if averageDegree:
					selectedDict["AvgPhysDegree"] = float(sum(map(len, physRouterToIPs.values())))/float(len(physRouterToIPs))
					printOrder.append("AvgPhysDegree")
					selectedDict["AvgVirtDegree"] = float(sum(map(len, virtRouterToIPs.values())))/float(len(virtRouterToIPs))
					printOrder.append("AvgVirtDegree")
					selectedDict["AvgDegree"] = float(sum(map(len, routerNameToIPs.values())))/float(len(routerNameToIPs))
					printOrder.append("AvgDegree")
				if disagreement:
					selectedDict["DisagreementCount"] = disagreements
					printOrder.append("DisagreementCount")
				selectedDictList.append(selectedDict)
			except:
				sys.stderr.write("Skipping week " + week + "\n")

		selectedDictList = sorted(selectedDictList, key=lambda x:x["Week"])
		printGNUPlotData(selectedDictList, printOrder)
	except:
		sys.stderr.write("Could not open directory " +	iffinder_analysis + "\n")
		sys.exit(1)
def main():
	global physicalOnly
	global virtualOnly

	# parse command line options
	if len(sys.argv) < 3:
		print "Usage: " + sys.argv[0] + " <iffinder_analysis> <pl_archives> [OPTIONS]"
		print "Options:"
		print "\t--physical - print degree information in terms of physical interfaces"
		print "\t--virtual - print degree information in terms of virtual interfaces"
		print "\tNOTE: without options, degree information includes all interfaces"
		sys.exit(1)
	if "--physical" in sys.argv[3:]:
		physicalOnly = True
	elif "--virtual" in sys.argv[3:]:
		virtualOnly = True
	

	# perform analysis
	try:
		# process stdout dump
		filename = sys.argv[1]	
		pl_archives = sys.argv[2]
		if os.path.isfile(filename):
			# get physical interface IPs
			weekNumber = re.findall(r'\d+', filename)[0]
			masterAtlas = open(pl_archives + "/" + weekNumber + "/" + "COGENT-MASTER-ATLAS-FIXED.txt", "r")
			interfaceBreakdown = getBreakdown(masterAtlas)
			masterAtlas.close()

			# process single file without binning
			stdoutFile = open(filename, "r")
			listOfDegrees = getDegrees(stdoutFile, interfaceBreakdown)
			histogramDict = generateHistogram(listOfDegrees)

			# make printable for GNUPlot
			selectedDictList = []
			for key, value in histogramDict.items():
				selectedDictList.append({ "Degree" : int(key), "Count" : value })	
			selectedDictList = sorted(selectedDictList, key=lambda x:x["Degree"])
			printGNUPlotData(selectedDictList, ["Degree", "Count"])

		else:
			# get weeks available in directory
			weeks = set()
			for weekfile in os.listdir(filename):
				weekNumber= weekfile.split(".")
				weeks.add(weekNumber[0])

			print >> sys.stderr, "Found ",len(weeks)," week files"	

			# open week files
			#selectedDictList = []
			print "# Degree\tWeek"
			for week in sorted(weeks, key=lambda x: int(x)):
				try:
					# open stdout dump file
					stdoutFilename = filename + "/" + week + ".stdout.txt"
					stdoutFile = open(stdoutFilename, "r")
					
					# get physical interface IPs
					masterAtlas = open(pl_archives + "/" + week + "/" + "COGENT-MASTER-ATLAS-FIXED.txt", "r")
					interfaceBreakdown = getBreakdown(masterAtlas)
					masterAtlas.close()

					# get degree information from file
					listOfDegrees = getDegrees(stdoutFile, interfaceBreakdown)
					#histogramDict = generateHistogram(listOfDegrees)

					# print columns of data
					for degree in sorted(listOfDegrees):
						print str(degree) + "\t" + str(week)

					print
					print
					# bin the information from file
					#selectedDict = createBinDict(int(week))
					#for key, value in histogramDict.items():
					#	binTag = getBin(key)
					#	selectedDict[binTag] += value
					#selectedDictList.append(selectedDict)

				except:
					raise
					sys.stderr.write("Skipping week " + week + "\n")

			# print GNUPlot compatible data
			#selectedDictList = sorted(selectedDictList, key=lambda x:x["Week"])
			#columnKeyList = ["Week"]
			#columnKeyList.extend([item[0] for item in bins])
			#printGNUPlotData(selectedDictList, columnKeyList)
	except:
		raise
		sys.stderr.write("Could not open file/directory " +	iffinder_analysis + "\n")
		sys.exit(1)
def main():
	weeks = None

	# parse command line options
	if len(sys.argv) < 2:
		print "Usage: " + sys.argv[0] + " dnsRecords [weekNumbers]"
		print "Options:"
		print "\t[weekNumbers] are white-space separated weeks of interest"
		sys.exit(1)
	elif len(sys.argv) > 2:
		for option in sys.argv[2:]:
			try:
				aweek = int(option)
				if weeks == None:
					weeks = [aweek]
				else:
					weeks.append(aweek)
			except ValueError:
				continue

	dnsRecordsFilename = sys.argv[1]
		
	try:
		# get weeks if didn't get them from the options
		if weeks == None:
			weeks = []
			for week in os.listdir(dnsRecordsFilename):
				try:
					# take only directories that are numbers
					weeks.append(int(week))
				except ValueError:
					sys.stderr.write("Directory/file \"" + week + "\"" \
							"is not a week number as expected\n")
					continue
			# sort weeks and chop off ends so we don't index poorly
			weeks = sorted(weeks)
			weeks = weeks[1:-1]

		# iterate over weeks and look for anomalies
		print "# Week\tNumNewPhyInterfaces"
		for weekN in weeks:
			try:
				# get weeks
				#NOTE: Results are a tuple (ipToName, nameToIPs, duplicateIPs)
				prevWeek = weekN - 1
				prevWeekFilename = dnsRecordsFilename + "/" + str(prevWeek) + "/COGENT-MASTER-ATLAS.txt"
				prevWeekFile = open(prevWeekFilename, "r")
				prevWeekResults = parseDNSRecords(prevWeekFile)
				prevWeekFile.close()	
				prevWeekFile = open(prevWeekFilename, "r")
				prevPhysicalInterfaces, _, _ = getBreakdown(prevWeekFile)

				currWeek = weekN
				currWeekFilename = dnsRecordsFilename + "/" + str(currWeek) + "/COGENT-MASTER-ATLAS.txt"
				currWeekFile = open(currWeekFilename, "r")
				currWeekResults = parseDNSRecords(currWeekFile)
				currWeekFile.close()	
				currWeekFile = open(currWeekFilename, "r")
				currPhysicalInterfaces, _, _ = getBreakdown(currWeekFile)

				# find new IPs
				prevIPs = set(filter(lambda x: x in prevPhysicalInterfaces, prevWeekResults[0].keys()))
				currIPs = set(filter(lambda x: x in currPhysicalInterfaces, currWeekResults[0].keys()))
				newIPs = currIPs - prevIPs

				# print count
				print str(currWeek) + "\t" + str(len(newIPs))
			except:
				raise
				sys.stderr.write("Skipping week " + str(weekN) + "\n")

	except:
		raise
		print "Error: Could not open \"" + dnsRecordsFilename + "\""
		sys.exit(1)