def process_file(in_fh, skip):
	content = in_fh.read().split('\n')
	activities = []
	edges = []
	count = 0
	for line in content:
		# TODO: fix this, it is messy
		if(len(line) < 1):
			continue
		# TODO: fix this, it is messy
		line = line.split(gc.MESSY_SPLIT_TOKEN)[1]
		if not skip and len(line) > 1:
			activity_line = re.match(gc.ACTIVITY_REGEX, line)
			edge_line = re.match(gc.EDGE_REGEX, line)
			if activity_line:
				activities.append(activity_line.group(1))
			elif edge_line:
				edges.append([gu.clean_line(edge_line.group(1)), gu.clean_line(edge_line.group(3))])
			else:
				if not line.startswith(gc.NOPARSE):
					gu.tprint('WARNING line %d malformed. %s' % (count, line))
		else:
			if '-->' not in line:
				activities.append(line)
			else:
				edge_line_skip = re.match(gc.SKIP_NODE_REGEX, line) 
				if edge_line_skip:
					edges.append([edge_line_skip.group(1), edge_line_skip.group(2)])
				else:
					gu.tprint('WARNING line %d malformed. %s' % (count, line))
		count += 1
	return [activities, edges]
	def render(self):
		self.fh.write('}\n')
		self.fh.close()
		# Verbose
		if self.verbose:
			command = ['neato', self.file_name, '-v', '-Tsvg', '-o %s.svg' % self.file_name]
		else:
			command = ['neato', self.file_name, '-Tsvg', '-o %s.svg' % self.file_name]
		gu.tprint('Calling: %s' % ' '.join(command))
		sp = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
		for line in iter(sp.stdout.readline, ''):
			sys.stdout.write(line)
	def add_edge(self, edge):
		if edge[0] not in self.nodes.keys():
			if self.verbose:
				gu.tprint('verbose --> WARNING (from) node \'%s\' does not exist. skipping.' % edge[0])
		elif edge[1] not in self.nodes.keys():
			if self.verbose:
				gu.tprint('verbose --> WARNING (to) node \'%s\' does not exist. skipping.' % edge[1])
		else:
			line = '%s -> %s' % (self.nodes[edge[0]], self.nodes[edge[1]])
			if line not in self.edges:
				self.edges.append(line)
				self.fh.write('\t%s;\n' % line)
Example #4
0
def check_results(input_file):
    fh = open(input_file, "r+")
    edges = process_file_checker(fh)
    fh.close()
    for edge in edges:
        edge[1] = edge[1].replace(".", os.sep)
    for edge in edges:
        filename = edge[0] + ".jimple"
        if os.path.isfile(os.path.join(gc.DEFAULT_SOOT_OUTPUT, filename)):
            # thanks stack overflow!
            # http://stackoverflow.com/a/4944929
            f = open(os.path.join(gc.DEFAULT_SOOT_OUTPUT, filename))
            s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
            if not s.find(edge[1]) != -1:
                gu.tprint("Edge: %s --> %s does not exist" % (edge[0], edge[1]))
                return False
            f.close()
    return True
	def add_node(self, class_name, skip):
		if class_name not in self.nodes:
			if not skip:
				name_match = re.match(gc.NAME_REGEX, class_name)
				if name_match:
					short_name = name_match.group(1)
					self.nodes[class_name] = short_name
					self.fh.write('\t%s [label=\"%s\"];\n' % (short_name, short_name))
				else: 
					gu.tprint('ERROR name \'%s\' is malformed. skipping' % class_name)
			else:
				name_match = re.match(gc.SKIP_NODE_ADD_REGEX, class_name)
				if name_match:
					short_name = name_match.group(1)
					self.nodes[class_name] = short_name
					self.fh.write('\t%s [label=\"%s\"];\n' % (short_name, short_name))
				else:
					gu.tprint('ERROR name \'%s\' is malformed. skipping' % class_name)
def main(input_file, output_name, verbose, skip, maxiter, epsilon):
	if skip:
		in_fh = open(input_file, 'r')
		nodes, edges = process_file(in_fh, skip)
		in_fh.close()
		gen_dot_file(nodes, edges, output_name, verbose, skip, maxiter, epsilon)
	elif checker.check_results(input_file):
		gu.tprint('Checker Passed!')
		gu.tprint('Input File: %s' % input_file)
		in_fh = open(input_file, 'r')
		activities, edges = process_file(in_fh, skip)
		in_fh.close()
		gen_dot_file(activities, edges, output_name, verbose, skip, maxiter, epsilon)
	else:
		gu.tprint('Checker Failed.')