aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/python/site-packages/longbow/FileUtil.py
blob: ae3113f652c067b2f236de3c01e3c55fd0fdf08d (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
#! /usr/bin/env python
# Copyright (c) 2017 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#

import sys
import os
import csv
import subprocess

def readFileLines(fileName):
    '''
    Get the entire file into memory as a list of lines.
    '''
    result = None

    with open(fileName, "r") as file:
        result = file.readlines()

    return result

def readFileString(fileName):
    '''
    Get the entire file into memory as a python string.
    '''
    result = None

    if fileName != None and len(fileName) > 0:
        with open (fileName, "r") as file:
            result = file.read()

    return result

def sourceFileNameToName(sourceFileName):
    '''
    Given the path to a source file, return the name without any path components or trailing suffix.
    '''
    name = os.path.basename(sourceFileName)
    return name.split(".")[0]

def canonicalizeFunctionName(functionName):
    '''
    Given a function name that contains the initial '_' character,
    strip it and return a canonicalised form of the same name suitable for a source file.
    '''
    if functionName[0] == "_":
        functionName = functionName[1:]
    return functionName

def isReservedName(functionName):
    '''
    Given a canonicalized name, determine if it is a reserved name according to ISO/IEC 9899:2011 and  ANSI Sec. 4.1.2.1,
    identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
    '''
    if functionName[0] == '_' and functionName[1] == '_':
        return True
    elif functionName[0] == '_' and functionName[1].isupper():
        return True
    return False

def getDarwinTestableFunctions(objectFileName):
    '''
    Retrieve a set of local and global function names within a file.
    '''
    command = [ "/usr/bin/nm", "-gUm", objectFileName ]

    output = subprocess.check_output(command)
    lines = output.splitlines()

    external = []
    internal = []
    for line in lines:
        if line:
            fields = line.split(" ")
            if (len(fields) > 1) and (fields[1] == "(__TEXT,__text)"):
                functionName = canonicalizeFunctionName(fields[3])

                if not isReservedName(functionName):
                    if fields[2] == "external":
                        external.append( ( functionName ) )
                    else:
                        internal.append( ( functionName ) )
                        pass
                pass
            pass
    pass

    external.sort()
    internal.sort()
    return { "Local": internal, "Global" : external }