aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/add_version_headers
blob: def7d55abfd5242793ec62af3394333478ac98a3 (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
#!/usr/bin/env python
# -----------------------------------------------------------------------------
# add_version_headers
#
# Add version header
#
# Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
# See file LICENSE for more details
# -----------------------------------------------------------------------------

"""\file add_version_headers
\brief Adds the version headers to every file.

Add Version Headers
===================

This file adds the version headers to every file in the directory


Improvements
------------

Add the ability to input command line arguments.
"""

import os
import re
import sys


header = """/* ---------------------------------------------------------------\
-------------
 * {0}
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

"""


class HeaderUpdate(object):
    """Updates the header in all the source and header files in the code"""
    def __init__(self, **kwargs):
        self.match_re = ".*[.]cpp$|.*[.]hpp$"
        self.exclude_re = "^$"
        self.exclude_build = True
        self.exclude_dir = ".*build.*"
        self.match_dir = ""
        self.comment_match = "^\/[*] -+"
        self.starting_dir = os.getcwd()
        for key, value in kwargs.items():
            setattr(self, key, value)

    def writeHeader(self):
        def writeFileOperation(filePath, src):
            if not re.match(self.comment_match, src):
                print(filePath, end=" ")
                with open(filePath, 'w') as src_file_lic:
                    src_file_lic.write(header.format(filePath))
                    src_file_lic.write(src)
                print("-- done")

        self._traverseDir(self.starting_dir, writeFileOperation)

    def updateHeader(self):
        ...

    def removeHeader(self):
        ...

    def _traverseDir(self, path, fileOperation):
        for subdir, dirs, files in os.walk(os.getcwd()):
            if (not re.match(self.exclude_dir, subdir)) and \
               (re.match(self.match_dir, subdir)):
                for fileName in files:
                    if (re.match(self.match_re, fileName)) and \
                       (not re.match(self.exclude_re, fileName)):
                        with open(os.path.join(subdir, fileName), 'r') \
                             as src_file:
                            src = src_file.read()
                        fileOperation(os.path.join(subdir, fileName), src)


def main(argv):
    update = HeaderUpdate(exclude_re="^picopng[.]cpp",
                          exclude_dir=".*build.*|.*google.*",
                          match_dir=".*src.*|.*include.*|.*test.*")
    update.writeHeader()


if __name__ == "__main__":
    main(sys.argv)