From 348cb051827cbea9427ac3ce99e23b72947067e4 Mon Sep 17 00:00:00 2001 From: Petr Kalis Date: Mon, 8 Mar 2021 12:52:52 +0100 Subject: [PATCH] SyncServer - added required methods Implemented required methods in LocalDriveHandler Added progress for LocalDriveHandler --- .../providers/abstract_provider.py | 22 +++++ .../sync_server/providers/local_drive.py | 89 +++++++++++++++---- 2 files changed, 95 insertions(+), 16 deletions(-) diff --git a/pype/modules/sync_server/providers/abstract_provider.py b/pype/modules/sync_server/providers/abstract_provider.py index 9130a06d94..56928e93d8 100644 --- a/pype/modules/sync_server/providers/abstract_provider.py +++ b/pype/modules/sync_server/providers/abstract_provider.py @@ -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 diff --git a/pype/modules/sync_server/providers/local_drive.py b/pype/modules/sync_server/providers/local_drive.py index 4d16b8b930..8d816b008a 100644 --- a/pype/modules/sync_server/providers/local_drive.py +++ b/pype/modules/sync_server/providers/local_drive.py @@ -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)