Statistics
| Branch: | Tag: | Revision:

root / autotools / check-header @ 084565ac

History | View | Annotate | Download (3.8 kB)

1 5d3c30df Michael Hanselmann
#!/usr/bin/python
2 5d3c30df Michael Hanselmann
#
3 5d3c30df Michael Hanselmann
4 5d3c30df Michael Hanselmann
# Copyright (C) 2011 Google Inc.
5 5d3c30df Michael Hanselmann
#
6 5d3c30df Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 5d3c30df Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 5d3c30df Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 5d3c30df Michael Hanselmann
# (at your option) any later version.
10 5d3c30df Michael Hanselmann
#
11 5d3c30df Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 5d3c30df Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 5d3c30df Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 5d3c30df Michael Hanselmann
# General Public License for more details.
15 5d3c30df Michael Hanselmann
#
16 5d3c30df Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 5d3c30df Michael Hanselmann
# along with this program; if not, write to the Free Software
18 5d3c30df Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 5d3c30df Michael Hanselmann
# 02110-1301, USA.
20 5d3c30df Michael Hanselmann
21 5d3c30df Michael Hanselmann
22 5d3c30df Michael Hanselmann
"""Script to verify file header.
23 5d3c30df Michael Hanselmann
24 5d3c30df Michael Hanselmann
"""
25 5d3c30df Michael Hanselmann
26 3f006be3 Andrea Spadaccini
# pylint: disable=C0103
27 5d3c30df Michael Hanselmann
# [C0103] Invalid name
28 5d3c30df Michael Hanselmann
29 5d3c30df Michael Hanselmann
import sys
30 5d3c30df Michael Hanselmann
import re
31 5d3c30df Michael Hanselmann
import itertools
32 5d3c30df Michael Hanselmann
33 5d3c30df Michael Hanselmann
from ganeti import constants
34 5d3c30df Michael Hanselmann
from ganeti import utils
35 5d3c30df Michael Hanselmann
from ganeti import compat
36 5d3c30df Michael Hanselmann
37 5d3c30df Michael Hanselmann
38 5d3c30df Michael Hanselmann
#: Assume header is always in the first 8kB of a file
39 5d3c30df Michael Hanselmann
_READ_SIZE = 8 * 1024
40 5d3c30df Michael Hanselmann
41 5d3c30df Michael Hanselmann
_GPLv2 = [
42 5d3c30df Michael Hanselmann
  "This program is free software; you can redistribute it and/or modify",
43 5d3c30df Michael Hanselmann
  "it under the terms of the GNU General Public License as published by",
44 5d3c30df Michael Hanselmann
  "the Free Software Foundation; either version 2 of the License, or",
45 5d3c30df Michael Hanselmann
  "(at your option) any later version.",
46 5d3c30df Michael Hanselmann
  "",
47 5d3c30df Michael Hanselmann
  "This program is distributed in the hope that it will be useful, but",
48 5d3c30df Michael Hanselmann
  "WITHOUT ANY WARRANTY; without even the implied warranty of",
49 5d3c30df Michael Hanselmann
  "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU",
50 5d3c30df Michael Hanselmann
  "General Public License for more details.",
51 5d3c30df Michael Hanselmann
  "",
52 5d3c30df Michael Hanselmann
  "You should have received a copy of the GNU General Public License",
53 5d3c30df Michael Hanselmann
  "along with this program; if not, write to the Free Software",
54 5d3c30df Michael Hanselmann
  "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA",
55 5d3c30df Michael Hanselmann
  "02110-1301, USA.",
56 5d3c30df Michael Hanselmann
  ]
57 5d3c30df Michael Hanselmann
58 5d3c30df Michael Hanselmann
59 5d3c30df Michael Hanselmann
_SHEBANG = re.compile(r"^#(?:|!(?:/usr/bin/python(?:| -u)|/bin/(?:|ba)sh))$")
60 5d3c30df Michael Hanselmann
_COPYRIGHT_YEAR = r"20[01][0-9]"
61 5d3c30df Michael Hanselmann
_COPYRIGHT = re.compile(r"# Copyright \(C\) (%s(?:, %s)*) Google Inc\.$" %
62 5d3c30df Michael Hanselmann
                        (_COPYRIGHT_YEAR, _COPYRIGHT_YEAR))
63 5d3c30df Michael Hanselmann
_COPYRIGHT_DESC = "Copyright (C) <year>[, <year> ...] Google Inc."
64 5d3c30df Michael Hanselmann
_AUTOGEN = "# This file is automatically generated, do not edit!"
65 5d3c30df Michael Hanselmann
66 5d3c30df Michael Hanselmann
67 5d3c30df Michael Hanselmann
class HeaderError(Exception):
68 5d3c30df Michael Hanselmann
  pass
69 5d3c30df Michael Hanselmann
70 5d3c30df Michael Hanselmann
71 5d3c30df Michael Hanselmann
def _Fail(lineno, msg):
72 5d3c30df Michael Hanselmann
  raise HeaderError("Line %s: %s" % (lineno, msg))
73 5d3c30df Michael Hanselmann
74 5d3c30df Michael Hanselmann
75 5d3c30df Michael Hanselmann
def _CheckHeader(getline_fn):
76 5d3c30df Michael Hanselmann
  (lineno, line) = getline_fn()
77 5d3c30df Michael Hanselmann
78 5d3c30df Michael Hanselmann
  if line == _AUTOGEN:
79 5d3c30df Michael Hanselmann
    return
80 5d3c30df Michael Hanselmann
81 5d3c30df Michael Hanselmann
  if not _SHEBANG.match(line):
82 5d3c30df Michael Hanselmann
    _Fail(lineno, ("Must contain nothing but a hash character (#) or a"
83 5d3c30df Michael Hanselmann
                   " shebang line (e.g. #!/bin/bash)"))
84 5d3c30df Michael Hanselmann
85 5d3c30df Michael Hanselmann
  (lineno, line) = getline_fn()
86 5d3c30df Michael Hanselmann
87 5d3c30df Michael Hanselmann
  if line == _AUTOGEN:
88 5d3c30df Michael Hanselmann
    return
89 5d3c30df Michael Hanselmann
90 5d3c30df Michael Hanselmann
  if line != "#":
91 5d3c30df Michael Hanselmann
    _Fail(lineno, "Must contain nothing but hash character (#)")
92 5d3c30df Michael Hanselmann
93 5d3c30df Michael Hanselmann
  (lineno, line) = getline_fn()
94 5d3c30df Michael Hanselmann
  if line:
95 5d3c30df Michael Hanselmann
    _Fail(lineno, "Must be empty")
96 5d3c30df Michael Hanselmann
97 5d3c30df Michael Hanselmann
  (lineno, line) = getline_fn()
98 5d3c30df Michael Hanselmann
  if not _COPYRIGHT.match(line):
99 5d3c30df Michael Hanselmann
    _Fail(lineno, "Must contain copyright information (%s)" % _COPYRIGHT_DESC)
100 5d3c30df Michael Hanselmann
101 5d3c30df Michael Hanselmann
  (lineno, line) = getline_fn()
102 5d3c30df Michael Hanselmann
  if line != "#":
103 5d3c30df Michael Hanselmann
    _Fail(lineno, "Must contain nothing but hash character (#)")
104 5d3c30df Michael Hanselmann
105 5d3c30df Michael Hanselmann
  for licence_line in _GPLv2:
106 5d3c30df Michael Hanselmann
    (lineno, line) = getline_fn()
107 5d3c30df Michael Hanselmann
    if line != ("# %s" % licence_line).rstrip():
108 5d3c30df Michael Hanselmann
      _Fail(lineno, "Does not match expected licence line (%s)" % licence_line)
109 5d3c30df Michael Hanselmann
110 5d3c30df Michael Hanselmann
  (lineno, line) = getline_fn()
111 5d3c30df Michael Hanselmann
  if line:
112 5d3c30df Michael Hanselmann
    _Fail(lineno, "Must be empty")
113 5d3c30df Michael Hanselmann
114 5d3c30df Michael Hanselmann
115 5d3c30df Michael Hanselmann
def Main():
116 5d3c30df Michael Hanselmann
  """Main program.
117 5d3c30df Michael Hanselmann
118 5d3c30df Michael Hanselmann
  """
119 5d3c30df Michael Hanselmann
  fail = False
120 5d3c30df Michael Hanselmann
121 5d3c30df Michael Hanselmann
  for filename in sys.argv[1:]:
122 5d3c30df Michael Hanselmann
    content = utils.ReadFile(filename, size=_READ_SIZE)
123 5d3c30df Michael Hanselmann
    lines = zip(itertools.count(1), content.splitlines())
124 5d3c30df Michael Hanselmann
125 5d3c30df Michael Hanselmann
    try:
126 5d3c30df Michael Hanselmann
      _CheckHeader(compat.partial(lines.pop, 0))
127 5d3c30df Michael Hanselmann
    except HeaderError, err:
128 5d3c30df Michael Hanselmann
      report = str(err)
129 5d3c30df Michael Hanselmann
      print "%s: %s" % (filename, report)
130 5d3c30df Michael Hanselmann
      fail = True
131 5d3c30df Michael Hanselmann
132 5d3c30df Michael Hanselmann
  if fail:
133 5d3c30df Michael Hanselmann
    sys.exit(constants.EXIT_FAILURE)
134 5d3c30df Michael Hanselmann
  else:
135 5d3c30df Michael Hanselmann
    sys.exit(constants.EXIT_SUCCESS)
136 5d3c30df Michael Hanselmann
137 5d3c30df Michael Hanselmann
138 5d3c30df Michael Hanselmann
if __name__ == "__main__":
139 5d3c30df Michael Hanselmann
  Main()