pycasso_tools.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. """
  2. PYCASSO tools
  3. """
  4. #-------------------------------------------------
  5. # routines
  6. #-------------------------------------------------
  7. def write_text_file( fname, text ) :
  8. """
  9. Write a text file.
  10. Arguments:
  11. fname : target file name
  12. text : list of strings, a line should end with '\n'
  13. """
  14. # external:
  15. import os
  16. import logging
  17. # info ...
  18. logging.debug( ' write %s ...' % fname )
  19. # write new text:
  20. f = open( fname, 'w' )
  21. f.writelines( text )
  22. f.close()
  23. # ok
  24. return
  25. #enddef
  26. # ***
  27. def diff_text_files( fname1, fname2 ) :
  28. """
  29. Return True if the files are different, False if they are the same.
  30. """
  31. # external:
  32. import os
  33. import logging
  34. # read files:
  35. f = open( fname1, 'r', encoding="utf-8" )
  36. lines1 = f.readlines()
  37. f.close()
  38. f = open( fname2, 'r', encoding="utf-8" )
  39. try:
  40. lines2 = f.readlines()
  41. except UnicodeDecodeError:
  42. print('Error in file: {}'.format(fname2))
  43. raise
  44. f.close()
  45. # compare:
  46. return lines1 != lines2
  47. #enddef
  48. # ***
  49. def update_text_file( fname, newtext ) :
  50. """
  51. Replace a file by a new text if the later differs
  52. from the current content.
  53. Arguments:
  54. fname : target file name
  55. newtext : list of strings, a line should end with '\n'
  56. """
  57. # external:
  58. import os
  59. import logging
  60. # file exists alread?
  61. if os.path.exists(fname) :
  62. # read current content:
  63. f = open( fname, 'r', encoding="utf-8" )
  64. oldtext = f.readlines()
  65. f.close()
  66. # differences ?
  67. rewrite = newtext != oldtext
  68. ## info ...
  69. #for iline in range(len(oldtext)) :
  70. # if iline < len(newtext) :
  71. # if oldtext[iline] != newtext[iline] :
  72. # logging.debug( ' first different lines:' )
  73. # logging.debug( ' old: %s' % oldtext[iline] )
  74. # logging.debug( ' new: %s' % newtext[iline] )
  75. # break
  76. # #endif
  77. # #endif
  78. ##endfor
  79. else :
  80. # no file yet, always rewrite:
  81. rewrite = True
  82. #endif
  83. # write file ?
  84. if rewrite :
  85. # for info message:
  86. stat = 'replace'
  87. # write new text:
  88. f = open( fname, 'w' )
  89. f.writelines( newtext )
  90. f.close()
  91. else :
  92. # for info message:
  93. stat = 'keep'
  94. #endif
  95. # info ...
  96. logging.debug( ' %-8s %-40s' % (stat,fname) )
  97. # ok
  98. return
  99. #enddef
  100. # ***
  101. def modify_text_file( fname, key, value ) :
  102. """
  103. Modify a text file by replacing a key by a new value.
  104. Arguments:
  105. fname : text file name
  106. key : value to be replaced
  107. value : replacement value
  108. """
  109. # read file into list of lines:
  110. f = open( fname, 'r', encoding="utf-8" )
  111. lines = f.readlines()
  112. f.close()
  113. # copy while replacing key with value:
  114. newlines = []
  115. for line in lines :
  116. # replace key with value:
  117. line = line.replace( key, value )
  118. # add:
  119. newlines.append(line)
  120. #endfor
  121. # write again:
  122. f = open( fname, 'w' )
  123. f.writelines(newlines)
  124. f.close()
  125. # ok
  126. return
  127. #enddef
  128. # ***
  129. def CreateDirs( dirname, forceclean=False ) :
  130. """
  131. Create a directory and report success.
  132. """
  133. # external:
  134. import os
  135. import shutil
  136. import logging
  137. # already present ?
  138. if os.path.isdir( dirname ) :
  139. # remove existing directory ?
  140. if forceclean:
  141. # info ...
  142. logging.info( 'remove existing %s ...' % dirname )
  143. # use the chainsaw:
  144. shutil.rmtree( dirname )
  145. #endif
  146. #endif
  147. # not present (anymore) ?
  148. if not os.path.isdir( dirname ) :
  149. # info ...
  150. logging.info( 'create new directory %s ...' % dirname )
  151. # create directory including parents if necesary:
  152. os.makedirs(dirname)
  153. #endif
  154. # ok
  155. return None
  156. #enddef
  157. #-------------------------------------------------
  158. # end
  159. #-------------------------------------------------