Subversion Repositories pentevo

Rev

Rev 1154 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #!/usr/bin/env python
  2.  
  3. #import argparse,glob,os,subprocess,re,sys,pathlib,pickle,socket
  4. import os,re,sys
  5.  
  6. startnum_re = re.compile(r'^[ \t]*///[ \t]*<<<[ /t]*NedoPC_RENUMBER_STARTNUM[ \t]+(\d+)[ \t]*>>>[ \t]*$')
  7. step_re     = re.compile(r'^[ \t]*///[ \t]*<<<[ /t]*NedoPC_RENUMBER_STEP[ \t]+(\d+)[ \t]*>>>[ \t]*$')
  8. begin_re    = re.compile(r'^[ \t]*///[ \t]*<<<[ /t]*NedoPC_RENUMBER_BEGIN[ \t]*>>>[ \t]*$')
  9. end_re      = re.compile(r'^[ \t]*///[ \t]*<<<[ /t]*NedoPC_RENUMBER_END[ \t]*>>>[ \t]*$')
  10. define_re   = re.compile(r'^[ \t]*#define[ \t]+([_A-Za-z][_A-Za-z0-9]*)[ \t]+(\d+)(.*)$')
  11.  
  12.  
  13.  
  14. def regenerate(startnum, step, ilines):
  15.  
  16.     # collect width info for numbers and names
  17.     max_name_width = 1
  18.  
  19.     num_defines = 0
  20.  
  21.     for t in ilines:
  22.  
  23.         if len(t)==3:
  24.  
  25.             num_defines += 1
  26.  
  27.             _, name, _ = t
  28.  
  29.             max_name_width = max(max_name_width, len(name))
  30.  
  31.     max_num = startnum + step * (num_defines-1)
  32.  
  33.     max_number_width = len(str(max_num))
  34.  
  35.  
  36.     # regenerate lines of text with new numbering
  37.  
  38.     new_lines = []
  39.     current_number = startnum
  40.  
  41.     for t in ilines:
  42.         if len(t)==2:
  43.             _, line = t
  44.             new_lines += [line] # \n is already included in the line
  45.         elif len(t)==3:
  46.             _, name, end = t
  47.  
  48.             spaces_to_append = 1 + max_name_width - len(name)
  49.  
  50.             str_number = str(current_number)
  51.  
  52.             spaces_to_append += max_number_width - len(str_number)
  53.  
  54.             new_line = '#define ' + name + ' '*spaces_to_append + str_number + end + '\n'
  55.  
  56.             new_lines += [new_line]
  57.         else:
  58.             sys.stderr.write('Wrong tuple length!')
  59.             sys.exit(1)
  60.  
  61.         current_number += step
  62.  
  63.     return new_lines
  64.  
  65.  
  66.  
  67. def main():
  68.  
  69.     filename = 'resource.h'
  70.  
  71.     with open(filename,'r+') as f:
  72.         all_lines = f.readlines()
  73.  
  74.  
  75.  
  76.         before_lines = []
  77.         after_lines = []
  78.  
  79.         regen_lines = []
  80.  
  81.        
  82.         STATE_BEFORE = 1
  83.         STATE_INSIDE = 2
  84.         STATE_AFTER  = 3
  85.         state = STATE_BEFORE
  86.  
  87.         startnum = None
  88.         step     = None
  89.  
  90.  
  91.         for l in all_lines:
  92.  
  93.             if state == STATE_BEFORE:
  94.                 before_lines += [l]
  95.  
  96.                 startnum_match = startnum_re.match(l)
  97.                 if startnum_match:
  98.                     if startnum is None:
  99.                         startnum = int(startnum_match.group(1))
  100.                     else:
  101.                         sys.stderr.write('NedoPC_RENUMBER_STARTNUM is encountered multiple times!\n')
  102.                         sys.exit(1)
  103.  
  104.                 step_match = step_re.match(l)
  105.                 if step_match:
  106.                     if step is None:
  107.                         step = int(step_match.group(1))
  108.                     else:
  109.                         sys.stderr.write('NedoPC_RENUMBER_STEP is encountered multiple times!\n')
  110.                         sys.exit(1)
  111.  
  112.                 if begin_re.match(l):
  113.                     state = STATE_INSIDE
  114.                     continue
  115.  
  116.             elif state == STATE_INSIDE:
  117.                
  118.                 if end_re.match(l):
  119.                     after_lines += [l]
  120.                     state = STATE_AFTER
  121.                     continue
  122.  
  123.                 define_match = define_re.match(l)
  124.                 if define_match:
  125.                     name    = define_match.group(1)
  126.                     comment = define_match.group(3)
  127.                     regen_lines += [(True,name,comment)]
  128.                 else:
  129.                     regen_lines += [(False,l)]
  130.  
  131.  
  132.             elif state == STATE_AFTER:
  133.                 after_lines += [l]
  134.  
  135.  
  136.  
  137.         if state != STATE_AFTER or startnum is None or step is None:
  138.             sys.stderr.write('Wrong structure, some of NedoPC_* statements are not present')
  139.             sys.exit(1)
  140.  
  141.  
  142.         #print(before_lines)
  143.         #print(regen_lines)
  144.         #print(after_lines)
  145.  
  146.         new_lines = regenerate(startnum,step,regen_lines)
  147.  
  148.         #print(new_lines)
  149.  
  150.         #with open('TEZD','w') as g:
  151.         #    g.writelines(before_lines + new_lines + after_lines)
  152.  
  153.         f.seek(0)
  154.  
  155.         f.writelines(before_lines + new_lines + after_lines)
  156.  
  157.         f.truncate()
  158.  
  159.  
  160.  
  161. if __name__=="__main__":
  162.     main()
  163.