From e51aa4990ca758fe0fbc8de0d6507551ee4cf4bc Mon Sep 17 00:00:00 2001 From: rzaitov Date: Tue, 12 Nov 2013 15:36:58 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D0=B8=D1=84=D0=B8=D1=86?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BB=20=D0=BA=D0=BE=D0=BC=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D1=83=20=D0=B2=D0=BE=D1=81=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=B7?= =?UTF-8?q?=20=D0=B1=D1=8D=D0=BA=D0=B0=D0=BF=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RestoreBackupCommandBuilder.py | 4 +-- .../BaseBackupCommand/BaseBackupCommand.py | 5 ++- .../BaseBackupCommand/CreateBackupCommand.py | 4 +-- .../BaseBackupCommand/RestoreBackupCommand.py | 34 +++++++++++++++++-- .../ParserBackup/CreateBackupParser.py | 2 +- .../parsers/ParserBackup/ParserBackupBase.py | 6 ---- .../ParserBackup/RestoreBackupParser.py | 3 +- 7 files changed, 39 insertions(+), 19 deletions(-) diff --git a/scripts/TouchinBuild/CommandBuilders/RestoreBackupCommandBuilder.py b/scripts/TouchinBuild/CommandBuilders/RestoreBackupCommandBuilder.py index 00f328d..0f391a1 100644 --- a/scripts/TouchinBuild/CommandBuilders/RestoreBackupCommandBuilder.py +++ b/scripts/TouchinBuild/CommandBuilders/RestoreBackupCommandBuilder.py @@ -18,7 +18,7 @@ class RestoreBackupCommandBuilder: assert line is not None parser = RestoreBackupParser() - folderPath = parser.parseLine(line) + parser.parseLine(line) - command = RestoreBackupCommand(folderPath) + command = RestoreBackupCommand() return command diff --git a/scripts/TouchinBuild/commands/BaseBackupCommand/BaseBackupCommand.py b/scripts/TouchinBuild/commands/BaseBackupCommand/BaseBackupCommand.py index 74dcb22..95a4eb7 100644 --- a/scripts/TouchinBuild/commands/BaseBackupCommand/BaseBackupCommand.py +++ b/scripts/TouchinBuild/commands/BaseBackupCommand/BaseBackupCommand.py @@ -12,16 +12,15 @@ class BaseBackupCommand: self.srcAbsDirPath = self.getAbsSrc() self.backupDirAbsPath = self.getAbsDst() - self.backupIgnore = ['.git', '.gitignore', '.DS_Store'] + self.backupIgnore = ['.git', '.gitignore', '.DS_Store', 'backup'] def getAbsSrc(self): return self.getAbs(self.folderPath) def getAbsDst(self): absFolderPath = self.getAbs(self.folderPath) - absParentDir = os.path.dirname(absFolderPath) - backupAbsPath = os.path.join(absParentDir, 'backup') + backupAbsPath = os.path.join(absFolderPath, 'backup') return backupAbsPath diff --git a/scripts/TouchinBuild/commands/BaseBackupCommand/CreateBackupCommand.py b/scripts/TouchinBuild/commands/BaseBackupCommand/CreateBackupCommand.py index 0498a0b..32b76a9 100644 --- a/scripts/TouchinBuild/commands/BaseBackupCommand/CreateBackupCommand.py +++ b/scripts/TouchinBuild/commands/BaseBackupCommand/CreateBackupCommand.py @@ -11,10 +11,10 @@ class CreateBackupCommand(BaseBackupCommand): if os.path.exists(self.backupDirAbsPath): raise Exception('folder: {0} already exists'.format(self.backupDirAbsPath)) - os.mkdir(self.backupDirAbsPath) - dirContent = os.listdir(self.srcAbsDirPath) + os.mkdir(self.backupDirAbsPath) + for fileOrDir in dirContent: if fileOrDir not in self.backupIgnore: self.copyFileOrDirectoryToBackupFolder(fileOrDir) diff --git a/scripts/TouchinBuild/commands/BaseBackupCommand/RestoreBackupCommand.py b/scripts/TouchinBuild/commands/BaseBackupCommand/RestoreBackupCommand.py index 9ec126c..08bddf5 100644 --- a/scripts/TouchinBuild/commands/BaseBackupCommand/RestoreBackupCommand.py +++ b/scripts/TouchinBuild/commands/BaseBackupCommand/RestoreBackupCommand.py @@ -8,6 +8,34 @@ class RestoreBackupCommand(BaseBackupCommand): BaseBackupCommand.__init__(self) def execute(self): - if os.path.exists(self.backupDirAbsPath): - shutil.rmtree(self.srcAbsDirPath, ignore_errors=True) - shutil.copytree(self.backupDirAbsPath, self.srcAbsDirPath, symlinks=False) + if not os.path.exists(self.backupDirAbsPath): + raise Exception('backup folder: {0} not exists'.format(self.backupDirAbsPath)) + + srcDirContent = os.listdir(self.srcAbsDirPath) + for fileOrDir in srcDirContent: + if fileOrDir not in self.backupIgnore: + self.removeFileOrDirectory(fileOrDir) + + backupDirContent = os.listdir(self.backupDirAbsPath) + for fileOrDir in backupDirContent: + self.copyFileOrDirectoryFromBackupFolder(fileOrDir) + + def removeFileOrDirectory(self, fileOrDirName): + + srcAbsPath = os.path.join(self.srcAbsDirPath, fileOrDirName) + + if os.path.isdir(srcAbsPath): + shutil.rmtree(srcAbsPath) + else: + os.remove(srcAbsPath) + + def copyFileOrDirectoryFromBackupFolder(self, fileOrDirName): + assert fileOrDirName is not None + + srcAbsPath = os.path.join(self.srcAbsDirPath, fileOrDirName) + fileInBackupFolderAbsPath = os.path.join(self.backupDirAbsPath, fileOrDirName) + + if os.path.isdir(srcAbsPath): + shutil.copytree(fileInBackupFolderAbsPath, srcAbsPath) + else: + shutil.copy(fileInBackupFolderAbsPath, srcAbsPath) diff --git a/scripts/TouchinBuild/parsers/ParserBackup/CreateBackupParser.py b/scripts/TouchinBuild/parsers/ParserBackup/CreateBackupParser.py index f42ccae..de31ee3 100644 --- a/scripts/TouchinBuild/parsers/ParserBackup/CreateBackupParser.py +++ b/scripts/TouchinBuild/parsers/ParserBackup/CreateBackupParser.py @@ -10,7 +10,7 @@ class CreateBackupParser(ParserBackupBase): def getMatchInfo(self, line): assert line is not None - regexpSource = self.startsWith('create') + self.spaceEndsWith('backup') + regexpSource = self.startsWith('create') + self.endsWith('backup') regexp = re.compile(regexpSource, re.UNICODE) match = regexp.match(line) diff --git a/scripts/TouchinBuild/parsers/ParserBackup/ParserBackupBase.py b/scripts/TouchinBuild/parsers/ParserBackup/ParserBackupBase.py index e80ba1b..05a560c 100644 --- a/scripts/TouchinBuild/parsers/ParserBackup/ParserBackupBase.py +++ b/scripts/TouchinBuild/parsers/ParserBackup/ParserBackupBase.py @@ -4,7 +4,6 @@ from parsers.LineParser import LineParser class ParserBackupBase(LineParser): def __init__(self): LineParser.__init__(self) - self.folderName = None def parseLine(self, line): assert line is not None @@ -15,11 +14,6 @@ class ParserBackupBase(LineParser): self._guardMatch(match, line, regexpSource) - folderName = match.group('folder') - self.folderName = folderName - - return self.folderName - def getMatchInfo(self, line): return None, None diff --git a/scripts/TouchinBuild/parsers/ParserBackup/RestoreBackupParser.py b/scripts/TouchinBuild/parsers/ParserBackup/RestoreBackupParser.py index 54285a6..3f1ca81 100644 --- a/scripts/TouchinBuild/parsers/ParserBackup/RestoreBackupParser.py +++ b/scripts/TouchinBuild/parsers/ParserBackup/RestoreBackupParser.py @@ -10,8 +10,7 @@ class RestoreBackupParser(ParserBackupBase): def getMatchInfo(self, line): assert line is not None - folderNameRegexp = r"'(?P[^']+)'$" - regexpSource = self.startsWith('restore') + self.than('from') + self.than('backup') + folderNameRegexp + regexpSource = self.startsWith('restore') + self.than('from') + self.endsWith('backup') regexp = re.compile(regexpSource, re.UNICODE) match = regexp.match(line)