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)