gen_utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/python3
  2. #
  3. # gen_utils.py - utils for gen_header and gen_tables
  4. #
  5. # Copyright (C) 2013 Matthew R. Wette
  6. # mwette@alumni.caltech.edu
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License as
  10. # published by the Free Software Foundation; either version 2 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. # 02111-1307, USA.
  22. #
  23. # The GNU General Public License is contained in the file COPYING.
  24. #
  25. # v130224a
  26. import sys, os, re, difflib
  27. import pdb
  28. def write_w_fill(f1, col, text, leader = " ", fillcol = 76, cc=""):
  29. """
  30. Write text to f1 and increment column. If column > fillcol, wrap
  31. with leader
  32. """
  33. ln = len(text)
  34. if col + ln > fillcol:
  35. f1.write(cc + "\n" + leader)
  36. col = len(leader)
  37. f1.write(text)
  38. col = col + ln
  39. return col
  40. def replace_in_code(arg, fname, tag, rout):
  41. """
  42. Generate filename.new with lines between /* tag: beg */ and /* tag: end */
  43. replaced by lines generated by the function rout(arg, f) where arg is a
  44. user argument, and f is the file handle for writing.
  45. """
  46. f0 = open(fname, 'r')
  47. f1 = open(fname + ".new", 'w')
  48. tbeg = "/* %s: beg */" % (tag,)
  49. tend = "/* %s: end */" % (tag,)
  50. # Copy part upto the begin-tag.
  51. l0 = f0.readline()
  52. while l0:
  53. f1.write(l0)
  54. if l0.lstrip().startswith(tbeg):
  55. break
  56. l0 = f0.readline()
  57. if not l0:
  58. # no tag found
  59. f0.close()
  60. f1.close()
  61. print("*** no beg tag found")
  62. return
  63. # Insert user code.
  64. rout(arg, f1)
  65. # Skip over guts and continue with rest of source.
  66. l0 = f0.readline()
  67. while l0:
  68. if l0.lstrip().startswith(tend):
  69. f1.write(l0)
  70. break
  71. l0 = f0.readline()
  72. if not l0:
  73. # no tag found
  74. f0.close()
  75. f1.close()
  76. return
  77. l0 = f0.readline()
  78. while l0:
  79. f1.write(l0)
  80. l0 = f0.readline()
  81. f0.close()
  82. f1.close()
  83. def move_if_changed(fname):
  84. """
  85. If fname.new is changed wrt fname, then rename fname to fname.old and
  86. fname.new to fname.
  87. """
  88. fname_old = fname + ".old"
  89. fname_new = fname + ".new"
  90. if os.system("cmp " + fname + " " + fname_new + ">/dev/null"):
  91. os.rename(fname, fname_old)
  92. os.rename(fname_new, fname)
  93. return True
  94. else:
  95. return False
  96. # --- last line ---