Skip to content

Commit

Permalink
WIP: First working code and test (TriBITSPub#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlettroscoe committed Jun 15, 2021
1 parent 7a11ddb commit 5634d55
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
62 changes: 62 additions & 0 deletions test/python_utils/lower_case_cmake_UnitTests.py
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()
72 changes: 72 additions & 0 deletions tribits/python_utils/lower_case_cmake.py
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()


0 comments on commit 5634d55

Please sign in to comment.