-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.py
225 lines (186 loc) · 7.7 KB
/
transfer.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
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
215
216
217
218
219
220
221
222
223
224
225
#! python2
#File: transfer.py
import os
import sys
import shutil
import errno
import stat
from send2trash import send2trash
import transfer_settings
def transfer_stick(direction=None, location=None, duplicate='yes',
overwrite='no', permadelete='no', configuration=None):
# Downloads or uploads from one set of directories to another.
direction_names = ['up', 'down']
opposite_direction_names = {'up': 'down', 'down': 'up'}
# Try different approaches to getting all 4 parameters.
while not (
direction in direction_names
and location in ['prep1', 'prep2', 'work', 'home', 'Lifelogging']
and duplicate in ['yes', 'no']
and overwrite in ['yes', 'no']
and permadelete in ['yes', 'no']
):
# If configuration was not specified,
if configuration == None:
# Try to get configuration from user to specify parameters.
configuration = input('Configuration? (1 - 6): ')
try:
configuration = int(configuration)
except ValueError:
configuration = None
if configuration in transfer_settings.configs.keys():
direction, location, duplicate, overwrite, permadelete = transfer_settings.configs[configuration]
# If no valid configuration, ask user for parameters one at a time.
else:
print 'direction =', direction, ', location =', location, ', duplicate =', duplicate, 'overwrite =', overwrite, 'delete =', delete
print 'Parameters not all entered correctly when script called and no valid configuration selected.'
direction = input('Enter direction (up or down): ')
location = input('Enter location (home or work): ')
duplicate = input('Enter whether to duplicate (yes or no): ')
overwrite = input('Enter overwrite permission (yes or no): ')
permadelete = input('Enter whether to permadelete old files (yes or no): ')
# Construct a list of directories for transfer.
directories = {
direction_name: [transfer_settings.paths[direction_name][location]]
for direction_name in direction_names
}
print ''
print 'Directories to copy: '
print directories
# Get a list of directories to skip transferring, or to target exclusively from parent folder.
skiplist = transfer_settings.skip_array[direction][location]
targetlist = transfer_settings.target_array[opposite_direction_names[direction]][location]
print ''
print 'Items to skip: '
print skiplist
print ''
print 'Items to target exclusively from parent folder: '
print targetlist
# Transfer the files.
print ''
print "Downloading..." if direction == 'down' else "Uploading..."
for i, directory in enumerate(directories[opposite_direction_names[direction]]):
transfer_tree(
dir_src=directory,
dir_dst=directories[direction][i],
skiplist=skiplist,
targetlist=targetlist,
overwrite=overwrite,
duplicate=duplicate,
permadelete=permadelete,
)
print ''
print "Transfer complete."
def handleRemoveReadonly(func, path, exc):
# Handles readonly errors by setting files to readable.
excvalue = exc[1]
if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
func(path)
else:
raise
class ProgressCount:
# Tracks progress of a process operating over a list.
def __init__(self, listlength, milestonesize = 25):
self.listlength = listlength
self.milestonesize = milestonesize
self.progress = 0
self.milestone = 0
def increment(self):
self.progress += 1
progress_percent = (self.progress / self.listlength) * 100
if progress_percent >= self.milestone:
if progress_percent < 100:
print round(progress_percent), '% ... '
else:
print '100%'
self.milestone = self.milestone + self.milestonesize
def transfer_tree(dir_src, dir_dst, skiplist=[], targetlist=[], overwrite='no', duplicate='no',
permadelete='no', symlinks=False, ignore=None):
# Transfers tree and files from dir_src to dir_dst.
# Initialize progress trackers.
counter_copy = ProgressCount(len(os.listdir(dir_src)))
counter_delete = ProgressCount(len(os.listdir(dir_src)))
# Checks for duplicate files and folders at destination. If such exist,
# checks overwrite variable and either deletes originals in prep for
# overwrite or aborts.
print ''
print 'Checking', dir_dst, 'for duplicate files and folders...'
# Construct a list of duplicate paths.
deletelist = []
for item in os.listdir(dir_src):
s = os.path.join(dir_src, item)
d = os.path.join(dir_dst, item)
if (
((len(targetlist) != 0) and not (s in targetlist))
or
(s in skiplist)
or
(not os.path.exists(d))
):
pass
else:
deletelist.append(d)
# Either delete the duplicates or report skip.
if len(deletelist) > 0:
if overwrite == 'yes':
print 'File or folder exists at destination and overwrite set to True.'
print ('Deleting' if permadelete=='no' else 'Permadeleting'), len(deletelist), 'files and/or folders.'
counter_overwrite = ProgressCount(len(deletelist))
for item in deletelist:
if permadelete == 'yes':
if os.path.isdir(item):
shutil.rmtree(item, ignore_errors=False, onerror=handleRemoveReadonly)
else:
os.remove(item)
else:
send2trash(item)
counter_overwrite.increment()
else:
print 'File or folder exists at destination and overwrite set to False.'
print 'Transfer aborted.'
sys.exit()
# Copy files over to destination.
print ''
print 'Copying', dir_src, 'to', dir_dst
for item in os.listdir(dir_src):
s = os.path.join(dir_src, item)
d = os.path.join(dir_dst, item)
if (
((len(targetlist) != 0) and not (s in targetlist))
or
(s in skiplist)
):
pass
else:
if os.path.isdir(s):
shutil.copytree(s, d, symlinks, ignore)
else:
shutil.copy2(s, d)
counter_copy.increment()
# Delete source files.
if duplicate == 'no':
print ''
print ("Deleting original..." if permadelete=='no' else "Permadeleting original...")
for item in os.listdir(dir_src):
s = os.path.join(dir_src, item)
if (
((len(targetlist) != 0) and not (s in targetlist))
or
(s in skiplist)
):
pass
else:
if permadelete == 'yes':
if os.path.isdir(s):
shutil.rmtree(s, ignore_errors=False, onerror=handleRemoveReadonly)
else:
os.remove(s)
else:
send2trash(s)
counter_delete.increment()
if __name__ == "__main__":
#transfer_stick()
key = 2
for config in transfer_settings.config_sets[key]:
transfer_stick(configuration=config)