Модифицировал комманду восстановления из бэкапа

This commit is contained in:
rzaitov 2013-11-12 15:36:58 +04:00
parent b02e7b9733
commit e51aa4990c
7 changed files with 39 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -10,8 +10,7 @@ class RestoreBackupParser(ParserBackupBase):
def getMatchInfo(self, line):
assert line is not None
folderNameRegexp = r"'(?P<folder>[^']+)'$"
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)