aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/python/site-packages/longbow/FileUtil.py
diff options
context:
space:
mode:
Diffstat (limited to 'longbow/src/python/site-packages/longbow/FileUtil.py')
-rwxr-xr-xlongbow/src/python/site-packages/longbow/FileUtil.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/longbow/src/python/site-packages/longbow/FileUtil.py b/longbow/src/python/site-packages/longbow/FileUtil.py
new file mode 100755
index 00000000..ae3113f6
--- /dev/null
+++ b/longbow/src/python/site-packages/longbow/FileUtil.py
@@ -0,0 +1,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 }