-
-
Save duythien/06c2344f27d5a5270065 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import os | |
| import shutil | |
| import pdb | |
| import sqlite3 as lite #Library sqlite | |
| from subprocess import PIPE, Popen, STDOUT | |
| class classification: | |
| root_dir = 'JavaBuildExample' | |
| dest_dir = 'BuildedExample' | |
| src_dir = 'JavaSourceCode' | |
| keywords = [] | |
| con = None #Connection variable | |
| cur = None #Cursor of Connection | |
| def __init__(self): | |
| """ | |
| :return: | |
| """ | |
| try: | |
| classification.con = lite.connect("Database/database.db") | |
| classification.cur = classification.con.cursor() | |
| #Find and loop all folder level 1 in root director | |
| self.get_list_keyword() | |
| self.list_books = next(os.walk(classification.dest_dir))[1] | |
| for book in self.list_books: | |
| if os.path.exists("temp"): | |
| shutil.rmtree("temp") | |
| self.book_read(book) | |
| except lite.Error, e: | |
| print "Error %s:" % e.args[0] | |
| finally: | |
| if classification.con: | |
| classification.con.close() | |
| def get_list_keyword(self): | |
| """ | |
| :return: | |
| """ | |
| classification.cur.execute('SELECT * FROM test_key') | |
| while True: | |
| row = classification.cur.fetchone() | |
| if row == None: | |
| break | |
| classification.keywords.append(row) | |
| def get_file_id(self,book,chap,file_name): | |
| classification.cur.execute('SELECT id FROM test_file WHERE book = ? AND chap = ? AND file_name = ?',(book,chap,file_name)) | |
| id_file= classification.cur.fetchone() | |
| return id_file[0] | |
| def book_read(self, book): | |
| #Find all folder in given book folder | |
| """ | |
| :param book: | |
| """ | |
| self.list_chaps = next(os.walk(classification.dest_dir+'/'+book))[1] | |
| for chap in self.list_chaps: | |
| if book == "ThinkingInJava": | |
| self.thinking_in_java(book,chap) | |
| else: | |
| self.chap_read(book,chap) | |
| #Input : command line | |
| #Output : Display output of given command | |
| def cmdline(self,command): | |
| """ | |
| :param command: | |
| :return: | |
| """ | |
| process = Popen( | |
| args=command, | |
| stdout=PIPE, | |
| shell=True, | |
| stderr=STDOUT | |
| ) | |
| data = process.stdout.read() | |
| process.communicate() | |
| return data | |
| def thinking_in_java(self,book,chap): | |
| """ | |
| :param book: | |
| :param chap: | |
| :return: | |
| """ | |
| print "\n\n -------------------------------------------------------- \nChapter "+chap | |
| for file in os.listdir(classification.dest_dir+'/'+book+'/'+chap): | |
| if file.endswith(".jar"): | |
| print "\t File "+file | |
| print "\t", | |
| current_file = os.path.join(classification.dest_dir+'/'+book+'/'+chap,file) | |
| list_file=self.cmdline("jar -tf "+current_file) | |
| list_file = list_file.split("\n") | |
| list_keyword_in_file = [] | |
| for file_in_jar in list_file: | |
| if ".class" in file_in_jar: | |
| file_in_jar = file_in_jar.replace(".class",".java") | |
| if os.path.isfile(classification.root_dir+'/'+book+'/'+chap+'/'+file_in_jar): | |
| with open(classification.root_dir+'/'+book+'/'+chap+'/'+file_in_jar,'r') as f: | |
| for line in f: | |
| for key in classification.keywords: | |
| if key[1] in line: | |
| if key not in list_keyword_in_file: | |
| list_keyword_in_file.append(key) | |
| elif os.path.isfile(classification.root_dir+'/'+book+'/'+file_in_jar): | |
| with open(classification.root_dir+'/'+book+'/'+file_in_jar,'r') as f: | |
| for line in f: | |
| for key in classification.keywords: | |
| if key[1] in line: | |
| if key not in list_keyword_in_file: | |
| list_keyword_in_file.append(key) | |
| file_id = self.get_file_id(book,chap,file) | |
| for key in list_keyword_in_file: | |
| classification.cur.execute('INSERT INTO key_jar (id_file,id_key) VALUES(?,?)',(file_id,key[0])) | |
| classification.con.commit() | |
| print key[1], | |
| print "Done" | |
| obfus = classification() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment