SyncServer - added required methods

Implemented required methods in LocalDriveHandler
Added progress for LocalDriveHandler
This commit is contained in:
Petr Kalis 2021-03-08 12:52:52 +01:00
parent 887af4c7e3
commit 348cb05182
2 changed files with 95 additions and 16 deletions

View file

@ -71,3 +71,25 @@ class AbstractProvider(metaclass=ABCMeta):
(list)
"""
pass
@abstractmethod
def create_folder(self, folder_path):
"""
Create all nonexistent folders and subfolders in 'path'.
Args:
path (string): absolute path
Returns:
(string) folder id of lowest subfolder from 'path'
"""
pass
@abstractmethod
def get_tree(self):
"""
Creates folder structure for providers which do not provide
tree folder structure (GDrive has no accessible tree structure,
only parents and their parents)
"""
pass

View file

@ -1,6 +1,8 @@
from __future__ import print_function
import os.path
import shutil
import threading
import time
from pype.api import Logger
from .abstract_provider import AbstractProvider
@ -13,29 +15,37 @@ class LocalDriveHandler(AbstractProvider):
def is_active(self):
return True
def upload_file(self, source_path, target_path, overwrite=True):
def upload_file(self, source_path, target_path,
server, collection, file, representation, site,
overwrite=False, direction="Upload"):
"""
Copies file from 'source_path' to 'target_path'
"""
if os.path.exists(source_path):
if overwrite:
shutil.copy(source_path, target_path)
else:
if os.path.exists(target_path):
raise ValueError("File {} exists, set overwrite".
format(target_path))
if not os.path.isfile(source_path):
raise FileNotFoundError("Source file {} doesn't exist."
.format(source_path))
if overwrite:
thread = threading.Thread(target=self._copy,
args=(source_path, target_path))
thread.start()
self._mark_progress(collection, file, representation, server,
site, source_path, target_path, direction)
else:
if os.path.exists(target_path):
raise ValueError("File {} exists, set overwrite".
format(target_path))
def download_file(self, source_path, local_path, overwrite=True):
return os.path.basename(target_path)
def download_file(self, source_path, local_path,
server, collection, file, representation, site,
overwrite=False):
"""
Download a file form 'source_path' to 'local_path'
"""
if os.path.exists(source_path):
if overwrite:
shutil.copy(source_path, local_path)
else:
if os.path.exists(local_path):
raise ValueError("File {} exists, set overwrite".
format(local_path))
return self.upload_file(source_path, local_path,
server, collection, file, representation, site,
overwrite, direction="Download")
def delete_file(self, path):
"""
@ -57,3 +67,50 @@ class LocalDriveHandler(AbstractProvider):
lst.append(os.path.join(dir_path, name))
return lst
def create_folder(self, folder_path):
"""
Creates 'folder_path' on local system
Args:
folder_path (string): absolute path on local (and mounted) disk
Returns:
(string) - sends back folder_path to denote folder(s) was
created
"""
os.makedirs(folder_path, exist_ok=True)
return folder_path
def get_tree(self):
return
def _copy(self, source_path, target_path):
print("copying {}->{}".format(source_path, target_path))
shutil.copy(source_path, target_path)
def _mark_progress(self, collection, file, representation, server, site,
source_path, target_path, direction):
"""
Updates progress field in DB by values 0-1.
Compares file sizes of source and target.
"""
source_file_size = os.path.getsize(source_path)
target_file_size = 0
last_tick = status_val = None
while source_file_size != target_file_size:
if not last_tick or \
time.time() - last_tick >= server.LOG_PROGRESS_SEC:
status_val = target_file_size / source_file_size
last_tick = time.time()
log.debug(direction + "ed %d%%." % int(status_val * 100))
server.update_db(collection=collection,
new_file_id=None,
file=file,
representation=representation,
site=site,
progress=status_val
)
target_file_size = os.path.getsize(target_path)
time.sleep(0.5)