/Tools/scripts/fixnotice.py

http://unladen-swallow.googlecode.com/ · Python · 113 lines · 81 code · 3 blank · 29 comment · 8 complexity · 67d68d69c20a71d8453a3645a93110f1 MD5 · raw file

  1. #! /usr/bin/env python
  2. """(Ostensibly) fix copyright notices in files.
  3. Actually, this sript will simply replace a block of text in a file from one
  4. string to another. It will only do this once though, i.e. not globally
  5. throughout the file. It writes a backup file and then does an os.rename()
  6. dance for atomicity.
  7. Usage: fixnotices.py [options] [filenames]
  8. Options:
  9. -h / --help
  10. Print this message and exit
  11. --oldnotice=file
  12. Use the notice in the file as the old (to be replaced) string, instead
  13. of the hard coded value in the script.
  14. --newnotice=file
  15. Use the notice in the file as the new (replacement) string, instead of
  16. the hard coded value in the script.
  17. --dry-run
  18. Don't actually make the changes, but print out the list of files that
  19. would change. When used with -v, a status will be printed for every
  20. file.
  21. -v / --verbose
  22. Print a message for every file looked at, indicating whether the file
  23. is changed or not.
  24. """
  25. OLD_NOTICE = """/***********************************************************
  26. Copyright (c) 2000, BeOpen.com.
  27. Copyright (c) 1995-2000, Corporation for National Research Initiatives.
  28. Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
  29. All rights reserved.
  30. See the file "Misc/COPYRIGHT" for information on usage and
  31. redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  32. ******************************************************************/
  33. """
  34. import os
  35. import sys
  36. import getopt
  37. NEW_NOTICE = ""
  38. DRYRUN = 0
  39. VERBOSE = 0
  40. def usage(code, msg=''):
  41. print __doc__ % globals()
  42. if msg:
  43. print msg
  44. sys.exit(code)
  45. def main():
  46. global DRYRUN, OLD_NOTICE, NEW_NOTICE, VERBOSE
  47. try:
  48. opts, args = getopt.getopt(sys.argv[1:], 'hv',
  49. ['help', 'oldnotice=', 'newnotice=',
  50. 'dry-run', 'verbose'])
  51. except getopt.error, msg:
  52. usage(1, msg)
  53. for opt, arg in opts:
  54. if opt in ('-h', '--help'):
  55. usage(0)
  56. elif opt in ('-v', '--verbose'):
  57. VERBOSE = 1
  58. elif opt == '--dry-run':
  59. DRYRUN = 1
  60. elif opt == '--oldnotice':
  61. fp = open(arg)
  62. OLD_NOTICE = fp.read()
  63. fp.close()
  64. elif opt == '--newnotice':
  65. fp = open(arg)
  66. NEW_NOTICE = fp.read()
  67. fp.close()
  68. for arg in args:
  69. process(arg)
  70. def process(file):
  71. f = open(file)
  72. data = f.read()
  73. f.close()
  74. i = data.find(OLD_NOTICE)
  75. if i < 0:
  76. if VERBOSE:
  77. print 'no change:', file
  78. return
  79. elif DRYRUN or VERBOSE:
  80. print ' change:', file
  81. if DRYRUN:
  82. # Don't actually change the file
  83. return
  84. data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
  85. new = file + ".new"
  86. backup = file + ".bak"
  87. f = open(new, "w")
  88. f.write(data)
  89. f.close()
  90. os.rename(file, backup)
  91. os.rename(new, file)
  92. if __name__ == '__main__':
  93. main()