Browse Source

replace os.system(cp) with shutil.copyfile

master
myitinos 5 years ago
parent
commit
8c1c0081f0
1 changed files with 3 additions and 4 deletions
  1. +3
    -4
      clone.py

+ 3
- 4
clone.py View File

@ -5,6 +5,7 @@ import logging
import string import string
import hashlib import hashlib
import time import time
import shutil
class Cloner: class Cloner:
@ -34,8 +35,7 @@ class Cloner:
targetFileContent = targetFile.read() targetFileContent = targetFile.read()
realHash = hashlib.md5(targetFileContent).hexdigest() realHash = hashlib.md5(targetFileContent).hexdigest()
if realHash != cloneHashFileContent: if realHash != cloneHashFileContent:
os.system('cp {backup} {real}'.format(
backup=cloneFileName, real=targetFileName))
shutil.copyfile(cloneFileName, targetFileName)
logging.warning( logging.warning(
'{file} failed check, restoring...'.format(file=targetFileName)) '{file} failed check, restoring...'.format(file=targetFileName))
else: else:
@ -43,8 +43,7 @@ class Cloner:
else: else:
logging.warning( logging.warning(
'{file} is a new file, added to backup dir'.format(file=targetFileName)) '{file} is a new file, added to backup dir'.format(file=targetFileName))
os.system('cp {real} {clone}'.format(
real=targetFileName, clone=cloneFileName))
shutil.copyfile(targetFileName, cloneFileName)
with open(cloneHashFileName, 'w') as cloneHashFile, open(targetFileName, 'rb') as targetFile: with open(cloneHashFileName, 'w') as cloneHashFile, open(targetFileName, 'rb') as targetFile:
targetFileContent = targetFile.read() targetFileContent = targetFile.read()
cloneHashFile.write(hashlib.md5(targetFileContent).hexdigest()) cloneHashFile.write(hashlib.md5(targetFileContent).hexdigest())

Loading…
Cancel
Save