forked from TriBITSPub/TriBITS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: First working code and test (TriBITSPub#274)
- Loading branch information
1 parent
7a11ddb
commit 5634d55
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
################################# | ||
# Unit testing code for gitdist # | ||
################################# | ||
|
||
import sys | ||
import imp | ||
import shutil | ||
|
||
from unittest_helpers import * | ||
|
||
pythonDir = os.path.abspath(GeneralScriptSupport.getScriptBaseDir()) | ||
utilsDir = pythonDir+"/utils" | ||
tribitsDir = os.path.abspath(pythonDir+"/..") | ||
|
||
sys.path = [pythonUtilsDir] + sys.path | ||
|
||
import lower_case_cmake as LCC | ||
|
||
|
||
class test_lower_case_cmake_str(unittest.TestCase): | ||
|
||
def test_upper_1(self): | ||
cmakeCodeStrIn = "\n"+\ | ||
"SET(SOME_VAR some_value)\n"+\ | ||
"\n" | ||
cmakeCodeStrOut_expected = "\n"+\ | ||
"set(SOME_VAR some_value)\n"+\ | ||
"\n" | ||
cmakeCodeStrOut = LCC.lower_case_cmake_str(cmakeCodeStrIn) | ||
self.assertEqual(cmakeCodeStrOut, cmakeCodeStrOut_expected) | ||
|
||
|
||
def test_mixed_1(self): | ||
cmakeCodeStrIn = "\n"+\ | ||
" SET (SOME_VAR some_value)\n"+\ | ||
"\n"+\ | ||
" # Some comment\n"+\ | ||
" Some_Longer_Func(SOME_VAR some_value)\n"+\ | ||
"\n"+\ | ||
" other_functioNS\n"+\ | ||
" (\n"+\ | ||
" SOME_VAR some_value)\n"+\ | ||
"\n" | ||
cmakeCodeStrOut_expected = "\n"+\ | ||
" set (SOME_VAR some_value)\n"+\ | ||
"\n"+\ | ||
" # Some comment\n"+\ | ||
" some_longer_func(SOME_VAR some_value)\n"+\ | ||
"\n"+\ | ||
" other_functions\n"+\ | ||
" (\n"+\ | ||
" SOME_VAR some_value)\n"+\ | ||
"\n" | ||
cmakeCodeStrOut = LCC.lower_case_cmake_str(cmakeCodeStrIn) | ||
self.assertEqual(cmakeCodeStrOut, cmakeCodeStrOut_expected) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
import re | ||
|
||
|
||
# Lower case CMake command calls and macro and function names. | ||
# | ||
def lower_case_cmake_str(cmakeCodeStrIn): | ||
# Regex for a command name in CMake (upper or lower case chars) | ||
p = re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*[\s]*\(', re.DOTALL) | ||
cmakeCodeStrOut = "" | ||
cmakeCodeStrInLen = len(cmakeCodeStrIn) | ||
cmakeCodeStrStartSearchIdx = 0 | ||
while cmakeCodeStrStartSearchIdx < cmakeCodeStrInLen: | ||
m = p.search(cmakeCodeStrIn[cmakeCodeStrStartSearchIdx:]) | ||
if m: | ||
# Get the substr for the match | ||
cmakeCodeStrMatchStartIdx = cmakeCodeStrStartSearchIdx + m.start() | ||
cmakeCodeStrMatchEndIdx = cmakeCodeStrStartSearchIdx + m.end() | ||
commandCallMatchStr = \ | ||
cmakeCodeStrIn[cmakeCodeStrMatchStartIdx:cmakeCodeStrMatchEndIdx] | ||
assert m.group() == commandCallMatchStr | ||
# Lower case the matching command call and update the output string | ||
# since last match | ||
cmakeCodeStrOut += \ | ||
cmakeCodeStrIn[cmakeCodeStrStartSearchIdx:cmakeCodeStrMatchStartIdx] | ||
cmakeCodeStrOut += commandCallMatchStr.lower() | ||
# Update indexes for next search | ||
cmakeCodeStrStartSearchIdx += m.end() | ||
else: | ||
# No more matches so copy the rest of the string | ||
cmakeCodeStrOut += \ | ||
cmakeCodeStrIn[cmakeCodeStrStartSearchIdx:] | ||
break | ||
|
||
return cmakeCodeStrOut | ||
|
||
|
||
|
||
|
||
# | ||
# Helper functions for main() | ||
# | ||
|
||
|
||
usageHelp = r""" | ||
Convert the cmake command calls to lower case and the macro and function names | ||
to lower case in a given file. | ||
""" | ||
|
||
|
||
def getCmndLineOptions(): | ||
from argparse import ArgumentParser, RawDescriptionHelpFormatter | ||
clp = ArgumentParser(description=usageHelp, | ||
formatter_class=RawDescriptionHelpFormatter) | ||
clp.add_argument("file", nargs='?', default="", | ||
help="The file to operate on" ) | ||
options = clp.parse_args(sys.argv[1:]) | ||
return options | ||
|
||
|
||
# | ||
# main() | ||
# | ||
|
||
if __name__ == '__main__': | ||
inOptions = getCmndLineOptions() | ||
|
||
|