Remove locks

pull/6/head
Inex Code 2021-12-06 12:00:53 +03:00
parent f68bd88a31
commit 340b50bb0d
1 changed files with 47 additions and 53 deletions

View File

@ -3,7 +3,6 @@ from datetime import datetime
import json import json
import subprocess import subprocess
import os import os
from threading import Lock
from enum import Enum from enum import Enum
import portalocker import portalocker
from selfprivacy_api.utils import ReadUserData from selfprivacy_api.utils import ReadUserData
@ -36,7 +35,6 @@ class ResticController:
""" """
_instance = None _instance = None
_lock = Lock()
_initialized = False _initialized = False
def __new__(cls): def __new__(cls):
@ -110,29 +108,28 @@ class ResticController:
): ):
return return
with self._lock: with subprocess.Popen(
with subprocess.Popen( backup_listing_command,
backup_listing_command, shell=False,
shell=False, stdout=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
stderr=subprocess.STDOUT, ) as backup_listing_process_descriptor:
) as backup_listing_process_descriptor: snapshots_list = backup_listing_process_descriptor.communicate()[0].decode(
snapshots_list = backup_listing_process_descriptor.communicate()[ "utf-8"
0 )
].decode("utf-8") try:
try: starting_index = snapshots_list.find("[")
starting_index = snapshots_list.find("[") json.loads(snapshots_list[starting_index:])
json.loads(snapshots_list[starting_index:]) self.snapshot_list = json.loads(snapshots_list[starting_index:])
self.snapshot_list = json.loads(snapshots_list[starting_index:]) self.state = ResticStates.INITIALIZED
self.state = ResticStates.INITIALIZED except ValueError:
except ValueError: if "Is there a repository at the following location?" in snapshots_list:
if "Is there a repository at the following location?" in snapshots_list: self.state = ResticStates.NOT_INITIALIZED
self.state = ResticStates.NOT_INITIALIZED return
return else:
else: self.state = ResticStates.ERROR
self.state = ResticStates.ERROR self.error_message = snapshots_list
self.error_message = snapshots_list return
return
def initialize_repository(self): def initialize_repository(self):
""" """
@ -144,23 +141,22 @@ class ResticController:
f"rclone:backblaze:{self._repository_name}/sfbackup", f"rclone:backblaze:{self._repository_name}/sfbackup",
"init", "init",
] ]
with self._lock: with subprocess.Popen(
with subprocess.Popen( initialize_repository_command,
initialize_repository_command, shell=False,
shell=False, stdout=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
stderr=subprocess.STDOUT, ) as initialize_repository_process_descriptor:
) as initialize_repository_process_descriptor: msg = initialize_repository_process_descriptor.communicate()[0].decode(
msg = initialize_repository_process_descriptor.communicate()[0].decode( "utf-8"
"utf-8" )
) if initialize_repository_process_descriptor.returncode == 0:
if initialize_repository_process_descriptor.returncode == 0: self.state = ResticStates.INITIALIZED
self.state = ResticStates.INITIALIZED else:
else: self.state = ResticStates.ERROR
self.state = ResticStates.ERROR self.error_message = msg
self.error_message = msg
self.state = ResticStates.INITIALIZED self.state = ResticStates.INITIALIZED
def start_backup(self): def start_backup(self):
""" """
@ -175,17 +171,16 @@ class ResticController:
"backup", "backup",
"/var", "/var",
] ]
with self._lock: with open("/tmp/backup.log", "w", encoding="utf-8") as log_file:
with open("/tmp/backup.log", "w", encoding="utf-8") as log_file: subprocess.Popen(
subprocess.Popen( backup_command,
backup_command, shell=False,
shell=False, stdout=log_file,
stdout=log_file, stderr=subprocess.STDOUT,
stderr=subprocess.STDOUT, )
)
self.state = ResticStates.BACKING_UP self.state = ResticStates.BACKING_UP
self.progress = 0 self.progress = 0
def check_progress(self): def check_progress(self):
""" """
@ -249,7 +244,6 @@ class ResticController:
self.state = ResticStates.RESTORING self.state = ResticStates.RESTORING
with self._lock: subprocess.run(backup_restoration_command, shell=False)
subprocess.run(backup_restoration_command, shell=False)
self.state = ResticStates.INITIALIZED self.state = ResticStates.INITIALIZED