fc_sort: delete c version, migrate to python version
Test: build aosp_blueline-userdebug, run build-time tests
Change-Id: I9c466cd718602e6068ee31abd6de7dbab84f4949
diff --git a/tests/Android.bp b/tests/Android.bp
index a7d7023..d27f333 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -33,7 +33,7 @@
python_binary_host {
name: "treble_sepolicy_tests",
srcs: [
- "FcSort.py",
+ "fc_sort.py",
"mini_parser.py",
"policy.py",
"treble_sepolicy_tests.py",
@@ -45,7 +45,7 @@
python_binary_host {
name: "sepolicy_tests",
srcs: [
- "FcSort.py",
+ "fc_sort.py",
"policy.py",
"sepolicy_tests.py",
],
@@ -56,7 +56,7 @@
python_binary_host {
name: "searchpolicy",
srcs: [
- "FcSort.py",
+ "fc_sort.py",
"policy.py",
"searchpolicy.py",
],
@@ -72,3 +72,11 @@
],
defaults: ["py2_only"],
}
+
+python_binary_host {
+ name: "fc_sort",
+ srcs: [
+ "fc_sort.py",
+ ],
+ defaults: ["py2_only"],
+}
diff --git a/tests/FcSort.py b/tests/fc_sort.py
similarity index 80%
rename from tests/FcSort.py
rename to tests/fc_sort.py
index 7cf1998..cbb0e5e 100755
--- a/tests/FcSort.py
+++ b/tests/fc_sort.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python
import sys
import os
+import argparse
class FileContextsNode:
path = None
@@ -11,7 +12,8 @@
stemLen = None
strLen = None
Type = None
- def __init__(self, path, fileType, context, meta, stemLen, strLen):
+ line = None
+ def __init__(self, path, fileType, context, meta, stemLen, strLen, line):
self.path = path
self.fileType = fileType
self.context = context
@@ -19,6 +21,7 @@
self.stemLen = stemLen
self.strlen = strLen
self.Type = context.split(":")[2]
+ self.line = line
metaChars = frozenset(['.', '^', '$', '?', '*', '+', '|', '[', '(', '{'])
escapedMetaChars = frozenset(['\.', '\^', '\$', '\?', '\*', '\+', '\|', '\[', '\(', '\{'])
@@ -65,7 +68,7 @@
stemLen = getStemLen(path)
strLen = len(path.replace("\\", ""))
- return FileContextsNode(path, fileType, context, meta, stemLen, strLen)
+ return FileContextsNode(path, fileType, context, meta, stemLen, strLen, line)
def ReadFileContexts(files):
fc = []
@@ -118,8 +121,22 @@
return Fc
-if __name__ == '__main__':
- if len(sys.argv) < 2:
- sys.exit("Usage: fc_sort.py <file_contexts 1> <file_contexts 2> <file_contexts 3>")
+def PrintFc(Fc, out):
+ if not out:
+ f = sys.stdout
+ else:
+ f = open(out, "w")
+ for node in Fc:
+ f.write(node.line + "\n")
- FcSorted = FcSort(sys.argv[1:])
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description="SELinux file_contexts sorting tool.")
+ parser.add_argument("-i", dest="input", help="Path to the file_contexts file(s).", nargs="?", action='append')
+ parser.add_argument("-o", dest="output", help="Path to the output file", nargs=1)
+ args = parser.parse_args()
+ if not args.input:
+ parser.error("Must include path to policy")
+ if not not args.output:
+ args.output = args.output[0]
+
+ PrintFc(FcSort(args.input),args.output)
diff --git a/tests/policy.py b/tests/policy.py
index 90e387f..0f51e2f 100644
--- a/tests/policy.py
+++ b/tests/policy.py
@@ -3,7 +3,7 @@
import os
import sys
import platform
-import FcSort
+import fc_sort
###
# Check whether the regex will match a file path starting with the provided
@@ -413,7 +413,7 @@
self.__FcDict[t] = [rec[0]]
except:
pass
- self.__FcSorted = FcSort.FcSort(FcPaths)
+ self.__FcSorted = fc_sort.FcSort(FcPaths)
# load policy
def __InitPolicy(self, PolicyPath):