CRCopyright.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #! /usr/bin/env bash
  2. #
  3. # Copyright (C) 2013-2017 Bilibili
  4. # Copyright (C) 2013-2017 Zhang Rui <bbcallen@gmail.com>
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import re
  19. class CRCopyright:
  20. regexp = None
  21. def __init__(self, name, url = None, prefix = None, symbol = None, years = None):
  22. self.name = name
  23. self.url = url
  24. self.prefix = prefix
  25. self.symbol = symbol
  26. self.years = years
  27. assert prefix
  28. assert symbol
  29. assert years
  30. assert name
  31. def duplicate(self):
  32. return CRCopyright(
  33. name = self.name,
  34. url = self.url,
  35. prefix = self.prefix,
  36. symbol = self.symbol,
  37. years = self.years,
  38. )
  39. def get_line(self):
  40. line = '%sCopyright %s %s %s' % (self.prefix, self.symbol, self.years, self.name)
  41. if self.url:
  42. line += ' <%s>' % self.url
  43. return line
  44. def match_name(self, name):
  45. if not self.name or not name:
  46. return False
  47. return self.name.lower() == name.lower()
  48. def match_name(self, url):
  49. if not self.url or not url:
  50. return False
  51. return self.url.lower() == url.lower()
  52. @staticmethod
  53. def scan_line(context, line):
  54. if line[-1] == '\n':
  55. line = line[0:-1]
  56. if not CRCopyright.regexp:
  57. # CRCopyright.regexp = re.compile("^([#\*\/\s]+)Copyright(?:\s+)([\(\)\w]+)(?:\s+)([\d]+|[\d]+-[\d]+)(?:\s+)([\w\s]+)[$|(<[\w\.@]+>)$]")
  58. # 1 prefix
  59. # 3 symbol
  60. # 5 year
  61. # 6 name
  62. # 7 url
  63. CRCopyright.regexp = re.compile(
  64. pattern = "^([#\*\/\s]+)Copyright(\s+)([\(\)\w]+)(\s+)([0-9\-]+)([^<]+)(.*)$",
  65. flags = re.IGNORECASE,
  66. )
  67. m = CRCopyright.regexp.match(line)
  68. if m:
  69. prefix = m.group(1)
  70. symbol = m.group(3).strip()
  71. years = m.group(5).strip()
  72. name = m.group(6).strip()
  73. url = m.group(7).strip().strip('<>')
  74. if prefix and symbol and years and name:
  75. cr = CRCopyright(
  76. prefix = prefix,
  77. symbol = symbol,
  78. years = years,
  79. name = name,
  80. url = url,
  81. )
  82. return cr
  83. # print '>====='
  84. # for i in range(1, 8):
  85. # print '> %d "%s"' % (i, match.group(i))
  86. # print '>====='
  87. return None