pycasso_tools.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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' )
  36. lines1 = f.readlines()
  37. f.close()
  38. f = open( fname2, 'r' )
  39. lines2 = f.readlines()
  40. f.close()
  41. # compare:
  42. return lines1 != lines2
  43. #enddef
  44. # ***
  45. def update_text_file( fname, newtext ) :
  46. """
  47. Replace a file by a new text if the later differs
  48. from the current content.
  49. Arguments:
  50. fname : target file name
  51. newtext : list of strings, a line should end with '\n'
  52. """
  53. # external:
  54. import os
  55. import logging
  56. # file exists alread?
  57. if os.path.exists(fname) :
  58. # read current content:
  59. f = open( fname, 'r' )
  60. oldtext = f.readlines()
  61. f.close()
  62. # differences ?
  63. rewrite = newtext != oldtext
  64. ## info ...
  65. #for iline in range(len(oldtext)) :
  66. # if iline < len(newtext) :
  67. # if oldtext[iline] != newtext[iline] :
  68. # logging.debug( ' first different lines:' )
  69. # logging.debug( ' old: %s' % oldtext[iline] )
  70. # logging.debug( ' new: %s' % newtext[iline] )
  71. # break
  72. # #endif
  73. # #endif
  74. ##endfor
  75. else :
  76. # no file yet, always rewrite:
  77. rewrite = True
  78. #endif
  79. # write file ?
  80. if rewrite :
  81. # for info message:
  82. stat = 'replace'
  83. # write new text:
  84. f = open( fname, 'w' )
  85. f.writelines( newtext )
  86. f.close()
  87. else :
  88. # for info message:
  89. stat = 'keep'
  90. #endif
  91. # info ...
  92. logging.debug( ' %-8s %-40s' % (stat,fname) )
  93. # ok
  94. return
  95. #enddef
  96. # ***
  97. def modify_text_file( fname, key, value ) :
  98. """
  99. Modify a text file by replacing a key by a new value.
  100. Arguments:
  101. fname : text file name
  102. key : value to be replaced
  103. value : replacement value
  104. """
  105. # read file into list of lines:
  106. f = open( fname, 'r' )
  107. lines = f.readlines()
  108. f.close()
  109. # copy while replacing key with value:
  110. newlines = []
  111. for line in lines :
  112. # replace key with value:
  113. line = line.replace( key, value )
  114. # add:
  115. newlines.append(line)
  116. #endfor
  117. # write again:
  118. f = open( fname, 'w' )
  119. f.writelines(newlines)
  120. f.close()
  121. # ok
  122. return
  123. #enddef
  124. # ***
  125. def CreateDirs( dirname, forceclean=False ) :
  126. """
  127. Create a directory and report success.
  128. """
  129. # external:
  130. import os
  131. import shutil
  132. import logging
  133. # already present ?
  134. if os.path.isdir( dirname ) :
  135. # remove existing directory ?
  136. if forceclean:
  137. # info ...
  138. logging.info( 'remove existing %s ...' % dirname )
  139. # use the chainsaw:
  140. shutil.rmtree( dirname )
  141. #endif
  142. #endif
  143. # not present (anymore) ?
  144. if not os.path.isdir( dirname ) :
  145. # info ...
  146. logging.info( 'create new directory %s ...' % dirname )
  147. # create directory including parents if necesary:
  148. os.makedirs(dirname)
  149. #endif
  150. # ok
  151. return None
  152. #enddef
  153. #-------------------------------------------------
  154. # end
  155. #-------------------------------------------------