-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.py
130 lines (98 loc) · 4.54 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
# // ---------------------------------------------------------------------
# // ------- [cuhHub] Sync Tool
# // ---------------------------------------------------------------------
"""
A tool for syncing all files in a directory from one place to the other.
Repo: https://github.com/cuhHub/cuhHub
Copyright (C) 2024 Cuh4
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# ---- // Imports
import click
import os
# ---- // Classes
class Synchronizer():
"""
A class used to synchronize all files in a directory into another.
"""
def __init__(self, directory: str, destination: str, ignore: list[str] = []):
"""
A class used to synchronize all files in a directory into another.
Args:
directory (str): The directory containing files to synchronize.
destination (str): The destination that all of the synced files will be cloned to.
ignore (list[str]): The files/directories to ignore when synchronizing.
Raises:
ValueError: If the directory does not exist.
"""
if not os.path.exists(directory):
raise ValueError("Directory does not exist.")
self.directory = directory
self.destination = destination
self.ignore = ignore
self.ignore.extend(destination)
def synchronize(self):
"""
Synchronizes all files in a directory from one place to the other.
"""
if os.path.isfile(self.directory):
self._copy_file(self.directory)
return
for root, dirs, files in os.walk(self.directory):
for dir in dirs:
path = os.path.join(root, dir)
if path in self.ignore:
continue
self._copy_file(path)
for file in files:
path = os.path.join(root, file)
if path in self.ignore:
continue
self._copy_file(path)
def _copy_file(self, path: str):
"""
Copies a file from one place to the other.
Args:
path (str): The path of the file to copy.
"""
relative = os.path.relpath(path, self.directory)
target = os.path.join(self.destination, relative) if os.path.isdir(self.directory) else self.destination + os.path.basename(path)
if os.path.isdir(path):
os.makedirs(target, exist_ok = True)
else:
os.makedirs(os.path.dirname(target), exist_ok = True)
with open(path, "rb") as f:
content = f.read()
with open(target, "wb") as f:
f.write(content)
# ---- // Main
@click.command()
@click.option("--directory", "-d", "-p", "--path", type = str, required = True, help = "The directory containing files/directories to synchronize.")
@click.option("--destination", "-de", type = str, required = True, help = "The destination that all of the synced files/directories will be cloned to.")
@click.option("--ignore", "-ip", type = list, default = [], multiple = True, help = "The files/directories to ignore when synchronizing.")
def sync_tool(directory: str, destination: str, ignore: list[str]):
"""
Clones all files and directories in a directory to a destination directory.
Args:
directory (str): The directory containing files/directories to synchronize.
destination (str): The destination that all of the synced files/directories will be cloned to.
ignore (list[str]): The files/directories to ignore when synchronizing.
"""
ignore = list(ignore) # `ignore` is a tuple for some reason
ignore.extend([__file__])
Synchronizer(
directory = directory,
destination = destination,
ignore = ignore
).synchronize()
click.secho(f"[Done] Sucessfully synced `{directory}` to `{destination}`.", fg = "green", underline = True, bold = True)
if __name__ == "__main__":
sync_tool()