def commandExecutionCygwin2(command): """ :param command: pass the command line statement :return: returns the output of command being supplied """ log = cl.custom_logger(logging.DEBUG) try: log.info("executing command....") print(command) with Popen([r'C:\cygwin64\bin\bash.exe'], stdin=PIPE, stdout=PIPE, shell=True) as p: p.stdin.write(command.encode()) p.stdin.close() out = p.stdout output = out.read().splitlines() except: output = "" print("error :" + str(p.stderr.errors)) log.error("error with executing command..") finally: p.terminate() p.kill() subprocess._cleanup() return output
def commandExecution(command): """ :param command: pass the command line statement :return: returns the output of command being supplied """ log = cl.custom_logger(logging.DEBUG) try: log.info("executing command....") output = subprocess.check_output(command, shell=True).decode() except subprocess.CalledProcessError as e: output = e.output log.error("error with executing command..") return output
def sortFileContentUnique(file, keyvalue, reverseorder): """ :param file: filename :param keyvalue: :param reverseorder: boolean , True or False :return: """ log = cl.custom_logger(logging.DEBUG) filecontent = readFileContent(file) log.debug("python sorting started...") response = sorted(set(filecontent), key=keyvalue, reverse=reverseorder) log.debug("python sorting completed...") return response
def test_numericSorting(self): """ Verifying reverse sorting with any '--numeric-sort or -n' command :return: """ log = cl.custom_logger(logging.DEBUG) # getting list from unix command sortResultCmd = executecommand.commandExecutionCygwin("sort -n " + numfile) # getting list from python sorted() method for verification sortResultPy = sortingPython.sortFileContent(numfile, int, False) # verifying both the lists TestClassSortingTest.verification(self, sortResultCmd, sortResultPy, log)
def test_uniqueSorting(self): """ Verifying reverse sorting with any '--unique or -u' command :return: """ log = cl.custom_logger(logging.DEBUG) # getting list from unix command sortResultCmd = executecommand.commandExecutionCygwin("sort -u " + file) # getting list from python sorted() method for verification sortResultPy = sortingPython.sortFileContentUnique( file, lambda v: (v.lower(), v[0].isupper()), False) # verifying both the lists TestClassSortingTest.verification(self, sortResultCmd, sortResultPy, log)
def test_simpleSorting(self): """ Verifying default sorting without any explicit command :return: test 1: simple sorting """ log = cl.custom_logger(logging.DEBUG) # getting list from unix command sortResultCmd = executecommand.commandExecutionCygwin("sort " + file) # getting list from python sorted() method for verification sortResultPy = sortingPython.sortFileContent( file, lambda v: (v.lower(), v[0].isupper()), False) # verifying both the lists TestClassSortingTest.verification(self, sortResultCmd, sortResultPy, log)
def sortingCmd(fileToBeRead, *args): """ :param fileToBeRead: file to be read :param args: specific command for sortingCmd :return: returns the sorted output """ arguments = "" log = cl.custom_logger(logging.DEBUG) for arg in args: arguments = arguments + " " + arg command = "sort " + arguments + " " + fileToBeRead log.info("executing sorting command: " + command) # output = commandExecution(command) output = commandExecutionCygwin(command) return output
def commandExecutionCygwin(command): """ :param command: pass the command line statement :return: returns the output of command being supplied """ log = cl.custom_logger(logging.DEBUG) try: log.info("executing command....") print(command) cmd = [r'C:\cygwin64\bin\bash.exe', '-c', '-l', command] output = (subprocess.check_output(cmd, shell=True).decode()).splitlines() except subprocess.CalledProcessError as e: output = e.output log.error("error with executing command..") finally: subprocess._cleanup() return output
def test_generalNumericAndDictionarySorting(self): """ verifying combination of --general-numeric-sort and --dictionary-order :return:Alpha Numeric sorting """ log = cl.custom_logger(logging.DEBUG) # getting list from unix command sortResultCmd = executecommand.commandExecutionCygwin("sort -g " + file + " | sort -d " + file + "") # getting list from python sorted() method for verification sortResultPy = sortingPython.sortFileContent( file, lambda v: (v.lower(), v[0].isupper()), False) # verifying both the lists TestClassSortingTest.verification(self, sortResultCmd, sortResultPy, log)
def readFileContent(file): """ :param file: FileName :return: file content as a list """ log = cl.custom_logger(logging.DEBUG) filecontent = "" if os.path.exists(file): try: log.info("reading the file...") fileobj = open(file, "r") filecontent = fileobj.read().splitlines() except IOError: log.error("error while reading the file: " + file) finally: log.info("closing the file...") fileobj.close() else: log.error("file does not exist...") return filecontent
import unittest, logging from loggerpackage import customLogger as cl from commandexecution import executecommand, sortingPython from Helpers import helpermethods file = "C:/Users/hlalpuriya/Documents/MyPersonalSpace/AllOther/Learning/swarm64/tsk1/DemoFileText.txt" numfile = "C:/Users/hlalpuriya/Documents/MyPersonalSpace/AllOther/Learning/swarm64/tsk1/NumericFileText.txt" log = cl.custom_logger(logging.DEBUG) class TestClassSortingTest(unittest.TestCase): @classmethod def setUpClass(cls): print("sortingCmd test class started..") def setUp(self): print("setup") def test_simpleSorting(self): """ Verifying default sorting without any explicit command :return: test 1: simple sorting """ log = cl.custom_logger(logging.DEBUG) # getting list from unix command sortResultCmd = executecommand.commandExecutionCygwin("sort " + file) # getting list from python sorted() method for verification sortResultPy = sortingPython.sortFileContent(