Skip to content

local_drive

LocalDriveHandler

Bases: AbstractProvider

Source code in client/ayon_sitesync/providers/local_drive.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
class LocalDriveHandler(AbstractProvider):
    CODE = "local_drive"
    LABEL = "Local drive"

    """ Handles required operations on mounted disks with OS """
    def __init__(self, project_name, site_name, tree=None, presets=None):
        self.presets = None
        self.active = False
        self.project_name = project_name
        self.site_name = site_name
        self._editable_properties = {}

        self.active = self.is_active()

    def is_active(self):
        return True

    def upload_file(
        self,
        source_path,
        target_path,
        server,
        project_name,
        file,
        representation,
        site,
        overwrite=False,
        direction="Upload"
    ):
        """
            Copies file from 'source_path' to '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(
                project_name,
                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))

        return os.path.basename(target_path)

    def download_file(self, source_path, local_path,
                      server, project_name, file, representation, site,
                      overwrite=False):
        """
            Download a file form 'source_path' to 'local_path'
        """
        return self.upload_file(
            source_path,
            local_path,
            server,
            project_name,
            file,
            representation,
            site,
            overwrite,
            direction="Download"
        )

    def delete_file(self, path):
        """
            Deletes a file at 'path'
        """
        if os.path.exists(path):
            os.remove(path)

    def list_folder(self, folder_path):
        """
            Returns list of files and subfolder in a 'folder_path'. Non recurs
        """
        lst = []
        if os.path.isdir(folder_path):
            for (dir_path, dir_names, file_names) in os.walk(folder_path):
                for name in file_names:
                    lst.append(os.path.join(dir_path, name))
                for name in dir_names:
                    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_roots_config(self, anatomy=None):
        """
            Returns root values for path resolving

            Takes value from Anatomy which takes values from Settings
            overridden by Local Settings

        Returns:
            (dict) - {"root": {"root": "/My Drive"}}
                     OR
                     {"root": {"root_ONE": "value", "root_TWO":"value}}
            Format is importing for usage of python's format ** approach
        """
        site_name = self._normalize_site_name(self.site_name)
        if not anatomy:
            anatomy = Anatomy(self.project_name,
                              site_name)

        # TODO cleanup when Anatomy will implement siteRoots method
        roots = anatomy.roots
        root_values = [root.value for root in roots.values()]
        if not all(root_values):
            manager = AddonsManager()
            sitesync_addon = manager.get_enabled_addon("sitesync")
            if not sitesync_addon:
                raise RuntimeError("No SiteSync addon")
            roots = sitesync_addon._get_project_roots_for_site(
                self.project_name, site_name)

        return {'root': roots}

    def get_tree(self):
        return

    def _copy(self, source_path, target_path):
        print("copying {}->{}".format(source_path, target_path))
        try:
            shutil.copy(source_path, target_path)
        except shutil.SameFileError:
            print("same files, skipping")

    def _mark_progress(
        self,
        project_name,
        file,
        repre_status,
        server,
        site_name,
        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
        side = "local"
        if direction == "Upload":
            side = "remote"
        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(
                    project_name=project_name,
                    new_file_id=None,
                    file=file,
                    repre_status=repre_status,
                    site_name=site_name,
                    side=side,
                    progress=status_val
                )
            try:
                target_file_size = os.path.getsize(target_path)
            except FileNotFoundError:
                pass
            time.sleep(0.5)

    def _normalize_site_name(self, site_name):
        """Transform user id to 'local' for Local settings"""
        if site_name != 'studio':
            return 'local'
        return site_name

LABEL = 'Local drive' class-attribute instance-attribute

Handles required operations on mounted disks with OS

create_folder(folder_path)

Creates 'folder_path' on local system

Parameters:

Name Type Description Default
folder_path string

absolute path on local (and mounted) disk

required

Returns:

Type Description

(string) - sends back folder_path to denote folder(s) was created

Source code in client/ayon_sitesync/providers/local_drive.py
112
113
114
115
116
117
118
119
120
121
122
123
124
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

delete_file(path)

Deletes a file at 'path'

Source code in client/ayon_sitesync/providers/local_drive.py
91
92
93
94
95
96
def delete_file(self, path):
    """
        Deletes a file at 'path'
    """
    if os.path.exists(path):
        os.remove(path)

download_file(source_path, local_path, server, project_name, file, representation, site, overwrite=False)

Download a file form 'source_path' to 'local_path'

Source code in client/ayon_sitesync/providers/local_drive.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def download_file(self, source_path, local_path,
                  server, project_name, file, representation, site,
                  overwrite=False):
    """
        Download a file form 'source_path' to 'local_path'
    """
    return self.upload_file(
        source_path,
        local_path,
        server,
        project_name,
        file,
        representation,
        site,
        overwrite,
        direction="Download"
    )

get_roots_config(anatomy=None)

Returns root values for path resolving

Takes value from Anatomy which takes values from Settings
overridden by Local Settings

Returns:

Type Description

(dict) - {"root": {"root": "/My Drive"}} OR {"root": {"root_ONE": "value", "root_TWO":"value}}

Format is importing for usage of python's format ** approach

Source code in client/ayon_sitesync/providers/local_drive.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def get_roots_config(self, anatomy=None):
    """
        Returns root values for path resolving

        Takes value from Anatomy which takes values from Settings
        overridden by Local Settings

    Returns:
        (dict) - {"root": {"root": "/My Drive"}}
                 OR
                 {"root": {"root_ONE": "value", "root_TWO":"value}}
        Format is importing for usage of python's format ** approach
    """
    site_name = self._normalize_site_name(self.site_name)
    if not anatomy:
        anatomy = Anatomy(self.project_name,
                          site_name)

    # TODO cleanup when Anatomy will implement siteRoots method
    roots = anatomy.roots
    root_values = [root.value for root in roots.values()]
    if not all(root_values):
        manager = AddonsManager()
        sitesync_addon = manager.get_enabled_addon("sitesync")
        if not sitesync_addon:
            raise RuntimeError("No SiteSync addon")
        roots = sitesync_addon._get_project_roots_for_site(
            self.project_name, site_name)

    return {'root': roots}

list_folder(folder_path)

Returns list of files and subfolder in a 'folder_path'. Non recurs

Source code in client/ayon_sitesync/providers/local_drive.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def list_folder(self, folder_path):
    """
        Returns list of files and subfolder in a 'folder_path'. Non recurs
    """
    lst = []
    if os.path.isdir(folder_path):
        for (dir_path, dir_names, file_names) in os.walk(folder_path):
            for name in file_names:
                lst.append(os.path.join(dir_path, name))
            for name in dir_names:
                lst.append(os.path.join(dir_path, name))

    return lst

upload_file(source_path, target_path, server, project_name, file, representation, site, overwrite=False, direction='Upload')

Copies file from 'source_path' to 'target_path'

Source code in client/ayon_sitesync/providers/local_drive.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def upload_file(
    self,
    source_path,
    target_path,
    server,
    project_name,
    file,
    representation,
    site,
    overwrite=False,
    direction="Upload"
):
    """
        Copies file from 'source_path' to '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(
            project_name,
            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))

    return os.path.basename(target_path)