Advertisement
JohnKofee

simple_slog_tnk.py

Jan 25th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import os
  5. from time import strftime, gmtime, time
  6. import re
  7. from hashlib import sha1
  8.  
  9. help = '''
  10. Simple EMM sorter by Mr.Blue (set for TNK/TNK HD)
  11.  
  12.      ------------------------
  13.  
  14. Program needs file with logs (EMMs) in hexadecimal (tekst) format.
  15. One EMM must be in one line.
  16.  
  17. Program removes repeated EMMs.
  18.  
  19. Example of usage:
  20.         pyton slog filename.log
  21.     or
  22.         ./slog filename.log
  23.  
  24.     As output it creates 1 file:
  25.         emm_tnk_filename.log
  26.  
  27. The general file with logs stay untouched.
  28.  
  29. '''
  30. #8270 B4 000000 77398BFC 70AB6500 SD
  31. #8270 82 000000 003B9ACA 70796410 SD
  32. #8270 8B 000000 003B9FFC 70826510 SD
  33.  
  34. #8270 B4 000000 78FFD9F9 70AB6500 HD
  35. #8270 B4 000000 003C833E 70AB6510 HD
  36. #8270 B4 000000 00334492 70AB6410 HD
  37.  
  38.  
  39. emm = '^8270..000000........70..6[45][01]0'
  40.  
  41. def RemoveDuplicates(fname) :
  42.     # Create set where the EMMs hashes will be stored
  43.     emmlist = set()
  44.     dup_cnt = 0
  45.     # Create temporary filename
  46.     fname_tmp = fname + ".tmp"
  47.  
  48.     print "Removing duplicates from file:", fname
  49.  
  50.     # Open input and temporary file
  51.     fi = open(fname, "r")
  52.     fo = open(fname_tmp, "w")
  53.  
  54.     while 1 :
  55.         line = fi.readline()
  56.         # Check end of file
  57.         if line == "" :
  58.             break
  59.  
  60.         # Create SHA1 sum for current line
  61.         dig = sha1(line).digest()
  62.  
  63.         # Check if this line was ever analysed
  64.         if dig in emmlist :
  65.             # if yes, show msg and continue with next line
  66.             dup_cnt += 1
  67.             print "\rFound duplicates:",dup_cnt,
  68.             #print "Duplicate:", line
  69.             continue
  70.         else :
  71.             # if not append it to the list
  72.             emmlist.add(dig)
  73.  
  74.         # Write this line to temporary file
  75.         fo.write(line)
  76.  
  77.     # Close files
  78.     fo.close()
  79.     fi.close()
  80.  
  81.     print
  82.  
  83.     # Rename output file to input
  84.     os.remove(fname)
  85.     os.rename(fname_tmp, fname)
  86.  
  87.     # Show message if not duplicates found
  88.     if dup_cnt == 0 :
  89.         print "Duplicates not found"
  90.  
  91.     print
  92.  
  93. def Analyse (fn) :
  94.     # Create filenames for each group
  95.     f_emms = "emm_tnk_" + fn
  96.  
  97.     # Open main input file in readonly mode
  98.     fl = open(fn, "r")
  99.  
  100.     # Create output files
  101.     es = open(f_emms, "w")
  102.  
  103.     start = time()
  104.     print "Start at", strftime("%d-%m-%Y %H:%M:%S", gmtime())
  105.     print "Analysing file:", fn
  106.     print
  107.  
  108.     # Start selection and filtering
  109.     lcnt = emms_cnt = 0
  110.     fe = re.compile(emm).search
  111.    
  112.     while 1:
  113.         line = fl.readline()
  114.         if line == "" :
  115.             break
  116.  
  117.         lcnt += 1
  118.         if lcnt % 5000 == 0 :
  119.             print "\rProceed line:",lcnt,
  120.  
  121.         #if re.findall(emm, line):
  122.         if fe(line):
  123.             es.write(line);
  124.             emms_cnt += 1
  125.  
  126.     # Print simple summry
  127.     print
  128.     print
  129.     print "Lines   :", lcnt
  130.     print "EMM TNK :", emms_cnt
  131.     print
  132.  
  133.     # Close all files
  134.     fl.close()
  135.     es.close()
  136.  
  137.     # Remove duplicates in output files
  138.     RemoveDuplicates(f_emms)
  139.     print "Stop at", strftime("%H:%M:%S", gmtime()), "Total time: ",time()-start, "sec."
  140.  
  141. # -----------------------------------------------------------------------------
  142. if __name__ == '__main__' :
  143.     # Check input parameters (presence of the filename with logs)
  144.     if len(sys.argv) == 2 :
  145.         Analyse(sys.argv[1])
  146.     else :
  147.         print help
  148.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement