#!/usr/bin/python

import argparse
import re
import locale
import subprocess
from subprocess import PIPE
import sys
import os
import time

dirpath = '/home/Research/results/malware'
sens_apis = set([])
if os.path.isfile('record_pkgs.txt'):
	os.remove('record_pkgs.txt')

if os.path.isdir(dirpath):
	for pkgname in os.listdir(dirpath):
		hasRecord = False
		pkgdir = os.path.join(dirpath, pkgname)
		app_droidbot = os.path.join(pkgdir, 'droidbot') 
		api_record = os.path.join(pkgdir, 'api_record.txt') 
		if os.path.isfile(api_record):
			os.remove(api_record)
		if os.path.isdir(app_droidbot):
			logcatfile = os.path.join(app_droidbot, 'logcat.txt')
			if os.path.isfile(logcatfile):
				lines = set(line.strip() for line in open(logcatfile))
				for ln in lines:
					if 'jie-record' in ln:
						recordFile = open(api_record, 'a')
						jie_record = ln.split('jie-record: ')[-1]
						recordFile.write('%s\n' % (jie_record))
						recordFile.close()					
						hasRecord = True
						sens_apis.add(jie_record.split(' ++ ')[1])

		if hasRecord:
			print pkgdir
			recordFiles = open('record_pkgs.txt', 'a')
			recordFiles.write('%s\n' % (pkgdir))
			recordFiles.close()		


for api in sens_apis:
	print api