main.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. '''
  2. Copyright 2011 Google Inc.
  3. Use of this source code is governed by a BSD-style license that can be
  4. found in the LICENSE file.
  5. '''
  6. '''
  7. Updates all copyright headers within our code:
  8. - For files that already have a copyright header, the header is modified
  9. while keeping the year and holder intact.
  10. - For files that don't have a copyright header, we add one with the current
  11. year and default holder.
  12. @author: epoger@google.com
  13. '''
  14. import os
  15. import sys
  16. import fileparser
  17. # Only modify copyright stanzas if the copyright holder is one of these.
  18. ALLOWED_COPYRIGHT_HOLDERS = [
  19. 'Google Inc.',
  20. 'Skia',
  21. 'The Android Open Source Project',
  22. ]
  23. def Main(root_directory):
  24. """Run everything.
  25. @param root_directory root directory within which to modify all files
  26. """
  27. filepaths = GetAllFilepaths(root_directory)
  28. for filepath in filepaths:
  29. parser = fileparser.CreateParser(filepath)
  30. if not parser:
  31. ReportWarning('cannot find a parser for file %s, skipping...' %
  32. filepath)
  33. continue
  34. old_file_contents = ReadFileIntoString(filepath)
  35. comment_blocks = parser.FindAllCommentBlocks(old_file_contents)
  36. if not comment_blocks:
  37. ReportWarning('cannot find any comment blocks in file %s' %
  38. filepath)
  39. old_copyright_block = parser.FindCopyrightBlock(comment_blocks)
  40. if not old_copyright_block:
  41. ReportWarning('cannot find copyright block in file %s' % filepath)
  42. (year, holder) = parser.GetCopyrightBlockAttributes(old_copyright_block)
  43. if holder and not ConfirmAllowedCopyrightHolder(holder):
  44. ReportWarning(
  45. 'unrecognized copyright holder "%s" in file %s, skipping...' % (
  46. holder, filepath))
  47. continue
  48. new_copyright_block = parser.CreateCopyrightBlock(year, holder)
  49. if old_copyright_block:
  50. new_file_contents = old_file_contents.replace(
  51. old_copyright_block, new_copyright_block, 1)
  52. else:
  53. new_file_contents = new_copyright_block + old_file_contents
  54. WriteStringToFile(new_file_contents, filepath)
  55. def GetAllFilepaths(root_directory):
  56. """Return a list of all files (absolute path for each one) within a tree.
  57. @param root_directory root directory within which to find all files
  58. """
  59. path_list = []
  60. for dirpath, _, filenames in os.walk(root_directory):
  61. for filename in filenames:
  62. path_list.append(os.path.abspath(os.path.join(dirpath, filename)))
  63. return path_list
  64. def ReportWarning(text):
  65. """Report a warning, but continue.
  66. """
  67. print 'warning: %s' % text
  68. def ReportError(text):
  69. """Report an error and raise an exception.
  70. """
  71. raise IOError(text)
  72. def ReadFileIntoString(filepath):
  73. """Returns the full contents of this file as a string.
  74. """
  75. with open(filepath, 'r') as file_handle:
  76. contents = file_handle.read()
  77. return contents
  78. def WriteStringToFile(string, filepath):
  79. """Writes this string out to filepath, replacing the file if it already
  80. exists.
  81. """
  82. with open(filepath, 'w') as file_handle:
  83. file_handle.write(string)
  84. def ConfirmAllowedCopyrightHolder(holder):
  85. """Returns True if this is one of our allowed copyright holders.
  86. @param holder copyright holder as a string
  87. """
  88. return holder in ALLOWED_COPYRIGHT_HOLDERS
  89. Main(sys.argv[1])