Advertisement
luckytyphlosion

test2backup.py

Dec 10th, 2017
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.75 KB | None | 0 0
  1. import sys
  2. from sys import argv
  3. from multiprocessing import Process
  4. import os
  5. script, directory = argv
  6.  
  7. #def convert_to_tmlearn(file, target):
  8. #   line = target.read()
  9. #   print "Read Line: %s" % (line)
  10. #   return
  11.  
  12. dirs = os.listdir(directory)
  13. def find_line(line, linelength):
  14.     line
  15.     linelength = linelength + len(line)
  16.     line = target.readline(linelength)
  17.     return (line, linelength)
  18.  
  19. def reorganize_tms(buffer2,buffer3,num_ones_double,past_tm10_flag):
  20. # for some reason, this code won't work if used in a while true loop
  21.     multiplier = 2
  22.     if num_ones_double == 0:
  23.         buffer3 = "0 "
  24.         return (buffer3,multiplier)
  25.     if past_tm10_flag == True:
  26.         multiplier = 3
  27.    
  28.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  29.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  30.     num_ones_double -= 1
  31.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  32.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  33.     num_ones_double -= 1
  34.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  35.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  36.     num_ones_double -= 1
  37.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  38.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  39.     num_ones_double -= 1
  40.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  41.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  42.     num_ones_double -= 1
  43.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  44.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  45.     num_ones_double -= 1
  46.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  47.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  48.     num_ones_double -= 1
  49.     buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
  50.     buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
  51.     num_ones_double -= 1
  52.     return (buffer3,multiplier)
  53.    
  54. def convert_binary_to_tm_no(multiple_of_eight,line,past_tm10_flag,ifdef_flag):
  55.     line_buffer = ""
  56.     buffer2 = ""
  57.     some_count = 0
  58.     num_ones = 0
  59.     tm09_flag = False
  60.     past_tm10_flag = False
  61.     for line in line:
  62.         some_count += 1
  63.         which_tm = (8 * (multiple_of_eight) + 9) - (some_count - 4)
  64.         if line == "%":
  65.             if ifdef_flag:
  66.                 line_buffer += "    "
  67.                 some_count -= 1
  68.             line_buffer += "    tmlearn "
  69.         elif line == "1":
  70.             buffer2 += "%s," % (which_tm)
  71.             if which_tm == 9:
  72.                 tm09_flag = True
  73.             elif which_tm >= 10:
  74.                 past_tm10_flag = True
  75.             num_ones += 1
  76.                
  77.     num_ones_double = num_ones
  78.     buffer3 = ""
  79.     (buffer3,multiplier) = reorganize_tms(buffer2,buffer3,num_ones_double,past_tm10_flag)
  80.     tm09_thing = multiplier
  81.     if tm09_flag == True:
  82.         tm09_thing = 2
  83.     line_buffer += buffer3[:tm09_thing+(num_ones-1)*multiplier-1] + "\n"
  84.     ifdef_flag = False
  85.     return (line_buffer,past_tm10_flag,ifdef_flag)
  86.    
  87. for file in dirs:
  88.     if file == "test2backup.py":
  89.         continue
  90.     target = open(file, 'rw+')
  91.     line = target.readline()
  92.     linelength = 0
  93.     line0 = line
  94.     while True:
  95.         (line, linelength) = find_line(line, linelength)
  96.         if line == "; learnset\n":
  97.             break
  98.    
  99.     (line, linelength) = find_line(line, linelength)
  100.     saved_linelength = linelength
  101.     main_line = ""
  102.     multiple_of_eight = 0
  103.     past_tm10_flag = False
  104.     ifdef_flag = False
  105.     multiple_copy = 0
  106.     completed_ifdef = True
  107.     completed_flag = False
  108.     if_flag = False
  109.     else_flag = False
  110.    
  111.     print file
  112.     while not completed_flag:
  113.         if line == "ENDC\n":
  114.             #print "we did it"
  115.             multiple_copy = 0
  116.             main_line += "ENDC\n"
  117.             completed_ifdef = True
  118.             else_flag = False
  119.             (line, linelength) = find_line(line, linelength)
  120.             if line != "db 0 ; padding\n":
  121.                 (line, linelength) = find_line(line, linelength)
  122.         if line == "IF DEF(_YELLOW)\n":
  123.             main_line += "IF DEF(_YELLOW)\n"
  124.             (line, linelength) = find_line(line, linelength)
  125.             ifdef_flag = True
  126.             if_flag = True
  127.             completed_ifdef = False
  128.             multiple_copy = multiple_of_eight
  129.             anotherFlag = True
  130.             continue
  131.         elif line == "ELSE\n":
  132.             multiple_of_eight = multiple_copy
  133.             if_flag = False
  134.             else_flag = True
  135.             #print "it worked!"
  136.             main_line += "ELSE\n"
  137.             (line, linelength) = find_line(line, linelength)
  138.             ifdef_flag = True
  139.             continue
  140.         elif if_flag:
  141.             ifdef_flag = True
  142.         elif else_flag:
  143.             ifdef_flag = True
  144.         if multiple_of_eight >= 7 and completed_ifdef == True:
  145.             completed_flag = True
  146.             #(line, linelength) = find_line(line, linelength)
  147.             continue
  148.            
  149.         (line_buffer,past_tm10_flag,ifdef_flag) = convert_binary_to_tm_no(multiple_of_eight,line,past_tm10_flag,ifdef_flag)
  150.         main_line += line_buffer
  151.         multiple_of_eight += 1
  152.         (line, linelength) = find_line(line, linelength)
  153.         if multiple_of_eight >= 12:
  154.             sys.exit("rekt")
  155.     target.seek(linelength)
  156.     line2 = line
  157.     main_line += line2
  158.    
  159.     #print main_line
  160.     target.seek(saved_linelength)
  161.     target.write(main_line)
  162.     target.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement