text: import yara
  from os import listdir, getcwd, walk, remove
  from os.path import normpath, join, isfile, dirname
  import zipfile
  from shutil import copy
  import magic
  from const import *
  from youracomp import comp_gen, big_rule_gen
  
  
  def mycallback(data):
      with open(normpath(join(dirname(__file__), 'logs/'+str(TID)+'yar.txt')), 'a') as f:
          f.write(str(data['rule']))
      return yara.CALLBACK_CONTINUE
  
  
  def applyara():
      copy(FILE_TO_SCAN, normpath(join(dirname(__file__), 'yara')))
      rules = yara.load(normpath(join(dirname(__file__), 'my_compiled_rules')))
  
      prevcount, zipcount = 0, 0
      while True:
          for root, subdir, files in walk(normpath(join(dirname(__file__), 'yara'))):
              for filename in files:
                  file_path = join(root, filename)
                  file_type = magic.from_file(file_path, mime=True)
                  if file_type == 'application/zip':
                      zip_ref = zipfile.ZipFile(file_path, 'r')
                      zip_ref.extractall(FILE_TO_SCAN)
                      zip_ref.close()
                      remove(file_path)
                      zipcount += 1
          if prevcount is zipcount:
              break
          else:
              prevcount += 1
  
      for root, subdir, files in walk(normpath(join(dirname(__file__), 'yara'))):
          for filename in files:
              file_path = join(root, filename)
              file_type = magic.from_file(file_path, mime=True)
              if file_type != 'application/zip':
                  print 'tried to match'
                  matches = rules.match(file_path, callback=mycallback,
                                        which_callbacks=yara.CALLBACK_MATCHES)
  
  
  if __name__ == "__main__":
      if not isfile(normpath(join(dirname(__file__), 'my_compiled_rules'))):
          comp_gen()
      if not isfile(normpath(join(dirname(__file__), 'malware_rules.yar'))):
          big_rule_gen()
      applyara()
  