1#!/usr/bin/env python3
2
3#     Copyright 2018 Couchbase, Inc
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17#   Test to check that KV-engine tests have a define only-once guard, e.g.
18#   they contain a #pragma once directive or contain a #ifndef, #define
19#   and #endif wrapper
20
21from __future__ import print_function
22import os
23import sys
24import argparse
25
26
27def check_for_only_def_once(file):
28    """
29    Function to check for the presence of #pragma once or a #ifndef guard in a
30    file
31    :param file: file to search for #pragma once or (#ifndef X, #define X and
32    #endif)
33    :return: returns true if the correct macros are found or false if not
34    """
35    header_file = open(file, "r")
36    # re-set the seek location to the beginning of the file as read()
37    # will have made it point to the end
38    header_file.seek(0)
39    for line in header_file:
40        # if there is a #pragma once then were good
41        if line == str("#pragma once\n"):
42            return True
43    return False
44
45
46def find_and_check_headers_in_dir(top_level_dir, exclusions):
47    """
48    Function to iterate though all the header files in a directory and its
49    sub-directories
50    """
51    # if check_for_only_def_once() ever returns false then this will be set to
52    # false to indicate a failure
53    test_pass = True
54
55    for root, dirs, files in os.walk(top_level_dir):
56        for current_file in files:
57            full_path = os.path.join(root, current_file)
58            if current_file.endswith(".h") and not (full_path in exclusions):
59                if not check_for_only_def_once(full_path):
60                    print("TEST FAIL - Header file found without "
61                          "#pragma once\" or \"#ifndef\" wrapper: " +
62                          full_path, file=sys.stderr)
63                    test_pass = False
64    return test_pass
65
66
67# create argparser so the user can specify which files to exclude if needed
68argParser = argparse.ArgumentParser()
69argParser.add_argument('--rootdir',
70                       metavar='/Users/user/source/couchbase/kv_engine',
71                       type=str, nargs=1,
72                       help='Directory to check header files in, defaults to '
73                            'the current working directory.',
74                       default=[os.getcwd()])
75argParser.add_argument('--exclude',
76                       metavar='engines/ep/src/tasks.def.h',
77                       type=str, nargs='+',
78                       help='List of files to exclude from checking, ' +
79                            'defined by their path within --rootdir or ' +
80                            'if --rootdir is not specified their path within' +
81                            ' the working directory.',
82                       default=[])
83
84args = argParser.parse_args()
85
86# get the grand-parent dir of the file which should be the kv_engine directory
87dirToSearchForHeaders = args.rootdir[0]
88
89if not os.path.isabs(dirToSearchForHeaders):
90    dirToSearchForHeaders = os.path.abspath(dirToSearchForHeaders)
91dirToSearchForHeaders = os.path.normpath(dirToSearchForHeaders)
92
93listOfExclusions = args.exclude
94# fully expand exclusion file paths
95listOfExclusions = [
96    os.path.normpath(
97        os.path.abspath(os.path.join(dirToSearchForHeaders, path)))
98    for path in listOfExclusions]
99
100if find_and_check_headers_in_dir(dirToSearchForHeaders, listOfExclusions):
101    exit(0)  # exit with 0 for pass
102else:
103    exit(1)  # exit with a general error code if there was a test failure
104