Добавил возможность запускать sh комманду из скрипта

This commit is contained in:
rzaitov 2013-10-30 18:23:49 +04:00
parent bdd8b74fba
commit 0174a20c05
8 changed files with 97 additions and 1 deletions

View File

@ -0,0 +1,20 @@
from commands.ShCommand import ShCommand
from parser.ShParser import ShParser
class ShCommandBuilder:
def isShCommand(self, line):
assert line is not None
parser = ShParser()
isValid = parser.isValidLine(line)
return isValid
def getCommandFor(self, line):
parser = ShParser()
cmdText = parser.parseLine(line)
command = ShCommand(cmdText)
return command

View File

@ -0,0 +1,7 @@
from commands.ShCommand import ShCommand
calendarCommand = ShCommand('cal 12 2013')
calendarCommand.execute()
touchCommand = ShCommand('touch ../tmp.txt')
touchCommand.execute()

View File

@ -0,0 +1 @@
__author__ = 'rzaitov'

View File

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
import unittest
from parser.ShParser import ShParser
class TestShParser(unittest.TestCase):
def setUp(self):
self.parser = ShParser()
def test_isValid(self):
line = 'sh 123 ./±~ bla'
isValid = self.parser.isValidLine(line)
self.assertEqual(True, isValid)
def test_isNotValid(self):
line = 'copy 123 ./±~ bla'
isValid = self.parser.isValidLine(line)
self.assertEqual(False, isValid)
def test_Parse(self):
line = 'sh 123 ./±~ bla'
shCmdText = self.parser.parseLine(line)
self.assertEqual('123 ./±~ bla', shCmdText)

View File

@ -0,0 +1,11 @@
from subprocess import call
class ShCommand:
def __init__(self, commandText):
assert commandText is not None
self.__commandText = commandText
def execute(self):
call(self.__commandText, shell=True)

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,22 @@
from parser.LineParser import LineParser
import re
class ShParser(LineParser):
def parseLine(self, line):
assert line
cmdTextRegexp = r'(?P<text>.*)'
regexpSource = self.startsWithKeywordToken('sh') + cmdTextRegexp
regexp = re.compile(regexpSource, re.UNICODE)
match = regexp.match(line)
self._guardMatch(match, line, regexpSource)
cmdText = match.group('text')
return cmdText
def isValidLine(self, line):
assert line is not None
return line.startswith('sh ')

View File

@ -1,3 +1,8 @@
import os
scriptFilePath = os.path.abspath(__file__)
scriptDir = os.path.dirname(scriptFilePath)
os.chdir(scriptDir)
#import ManualTests.csproj_test
#import ManualTests.info_plist_test
#import ManualTests.copy_test
@ -5,4 +10,6 @@
#import ManualTests.delete_backup_test
#import ManualTests.restore_backup_test
import ManualTests.csproj_test
#import ManualTests.csproj_test
import ManualTests.run_sh_command