summaryrefslogtreecommitdiff
path: root/chromium/tools/boilerplate.py
blob: 03e71f5ef0be45ada6bc9780bcb909b9a03b5bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Create files with copyright boilerplate and header include guards.

Usage: tools/boilerplate.py path/to/file.{h,cc}
"""

from datetime import date
import os
import os.path
import sys

LINES = [
    'Copyright %d The Chromium Authors. All rights reserved.' %
        date.today().year,
    'Use of this source code is governed by a BSD-style license that can be',
    'found in the LICENSE file.'
]

EXTENSIONS_TO_COMMENTS = {
    'h': '//',
    'cc': '//',
    'mm': '//',
    'js': '//',
    'py': '#',
    'gn': '#',
    'gni': '#',
}

def _GetHeader(filename):
  _, ext = os.path.splitext(filename)
  ext = ext[1:]
  comment = EXTENSIONS_TO_COMMENTS[ext] + ' '
  return '\n'.join([comment + line for line in LINES])


def _CppHeader(filename):
  guard = filename.upper() + '_'
  for char in '/\\.+':
    guard = guard.replace(char, '_')
  return '\n'.join([
    '',
    '#ifndef ' + guard,
    '#define ' + guard,
    '',
    '#endif  // ' + guard,
    ''
  ])


def _RemoveTestSuffix(filename):
  base, _ = os.path.splitext(filename)
  suffixes = [ '_test', '_unittest', '_browsertest' ]
  for suffix in suffixes:
    l = len(suffix)
    if base[-l:] == suffix:
      return base[:-l]
  return base


def _IsIOSFile(filename):
  if os.path.splitext(os.path.basename(filename))[0].endswith('_ios'):
    return True
  if 'ios' in filename.split(os.path.sep):
    return True
  return False


def _FilePathSlashesToCpp(filename):
  return filename.replace('\\', '/')


def _CppImplementation(filename):
  return '\n#include "' + _FilePathSlashesToCpp(_RemoveTestSuffix(filename)) \
    + '.h"\n'


def _ObjCppImplementation(filename):
  implementation = '\n#import "' + _RemoveTestSuffix(filename) + '.h"\n'
  if not _IsIOSFile(filename):
    return implementation
  implementation += '\n'
  implementation += '#if !defined(__has_feature) || !__has_feature(objc_arc)\n'
  implementation += '#error "This file requires ARC support."\n'
  implementation += '#endif\n'
  return implementation


def _CreateFile(filename):
  contents = _GetHeader(filename) + '\n'

  if filename.endswith('.h'):
    contents += _CppHeader(filename)
  elif filename.endswith('.cc'):
    contents += _CppImplementation(filename)
  elif filename.endswith('.mm'):
    contents += _ObjCppImplementation(filename)

  fd = open(filename, 'wb')
  fd.write(contents)
  fd.close()


def Main():
  files = sys.argv[1:]
  if len(files) < 1:
    print >> sys.stderr, 'Usage: boilerplate.py path/to/file.h path/to/file.cc'
    return 1

  # Perform checks first so that the entire operation is atomic.
  for f in files:
    _, ext = os.path.splitext(f)
    if not ext[1:] in EXTENSIONS_TO_COMMENTS:
      print >> sys.stderr, 'Unknown file type for %s' % f
      return 2

    if os.path.exists(f):
      print >> sys.stderr, 'A file at path %s already exists' % f
      return 2

  for f in files:
    _CreateFile(f)


if __name__ == '__main__':
  sys.exit(Main())