setcopyright.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. # Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  3. # All rights reserved.
  4. #
  5. # This software is supplied "AS IS" without any warranties.
  6. # RDA assumes no responsibility or liability for the use of the software,
  7. # conveys no license or title under any patent, copyright, or mask work
  8. # right to the product. RDA reserves the right to make changes in the
  9. # software without notification. RDA also make no representation or
  10. # warranty that such application will be suitable for the specified use
  11. # without further testing or modification.
  12. import os
  13. import sys
  14. import io
  15. import argparse
  16. def main(argv):
  17. parser = argparse.ArgumentParser(usage='change or add copyright header')
  18. parser.add_argument('--force', dest='force_mode', choices=['c', 'sharp'],
  19. help='force file mode')
  20. parser.add_argument('fnames', nargs='+', help='file names')
  21. args = parser.parse_args(argv)
  22. copy_c = '''/* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  23. * All rights reserved.
  24. *
  25. * This software is supplied "AS IS" without any warranties.
  26. * RDA assumes no responsibility or liability for the use of the software,
  27. * conveys no license or title under any patent, copyright, or mask work
  28. * right to the product. RDA reserves the right to make changes in the
  29. * software without notification. RDA also make no representation or
  30. * warranty that such application will be suitable for the specified use
  31. * without further testing or modification.
  32. */
  33. '''
  34. copy_s = '''# Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  35. # All rights reserved.
  36. #
  37. # This software is supplied "AS IS" without any warranties.
  38. # RDA assumes no responsibility or liability for the use of the software,
  39. # conveys no license or title under any patent, copyright, or mask work
  40. # right to the product. RDA reserves the right to make changes in the
  41. # software without notification. RDA also make no representation or
  42. # warranty that such application will be suitable for the specified use
  43. # without further testing or modification.
  44. '''
  45. for fname in args.fnames:
  46. with open(fname, 'r') as fh:
  47. lines = fh.readlines()
  48. hashbang = None
  49. detect_c = False
  50. detect_s = False
  51. if lines and lines[0].startswith('#!'):
  52. hashbang = lines[0]
  53. lines = lines[1:]
  54. while lines:
  55. if lines[0].strip() == '':
  56. lines = lines[1:]
  57. else:
  58. break
  59. if lines:
  60. if lines[0].startswith('/* Copyright') and 'RDA' in lines[0]:
  61. detect_c = True
  62. while lines:
  63. line = lines[0]
  64. lines = lines[1:]
  65. if line.strip() == '*/':
  66. break
  67. elif lines[0].startswith('# Copyright') and 'RDA' in lines[0]:
  68. detect_s = True
  69. while lines:
  70. line = lines[0]
  71. if not line.startswith('#'):
  72. break
  73. lines = lines[1:]
  74. while lines:
  75. if lines[0].strip() == '':
  76. lines = lines[1:]
  77. else:
  78. break
  79. mode = None
  80. if args.force_mode:
  81. mode = args.force_mode
  82. elif detect_c:
  83. mode = 'c'
  84. elif detect_s:
  85. mode = 'sharp'
  86. else:
  87. print('{}: unknown file type, ignored'.format(fname))
  88. continue
  89. if mode == 'c' and detect_s:
  90. print('{}: unlike to be c, ignored'.format(fname))
  91. continue
  92. if mode == 'sharp' and detect_c:
  93. print('{}: unlike to use #, ignored'.format(fname))
  94. continue
  95. if mode == 'c':
  96. with open(fname, 'w') as fh:
  97. fh.write(copy_c)
  98. fh.write(''.join(lines))
  99. else:
  100. with open(fname, 'w') as fh:
  101. if hashbang:
  102. fh.write(hashbang)
  103. fh.write(copy_s)
  104. fh.write(''.join(lines))
  105. return 0
  106. if __name__ == '__main__':
  107. sys.exit(main(sys.argv[1:]))