-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrgcopy.py
executable file
·356 lines (295 loc) · 10.9 KB
/
rgcopy.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# main.py
import os
directory = os.path.dirname(os.path.realpath(__file__))
import imp
import gi
#gi.require_version('Gtk', '3.0')
#gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk , Gdk , GObject, AppIndicator3, Unity
from commons.copy import Copy, COPY_STATUS_CODE
from gui.gui import Interface as ui
import gettext , locale
import urllib, urlparse
import datetime
STATUS_CODE = {0:'running', 1:'paused'}
SHOW_HIDE_TEXT = {True:'Less', False:'More'}
CONVERSION_RATE = {'b/s':1, 'kB/s':1024, 'MB/s':1048576, 'GB/s':1073741824}
class Handler():
def __init__(self, main):
self.main = main
def on_btn_more_clicked(self, *args):
self.main.ui.scrolledwindow1.set_visible(not self.main.ui.scrolledwindow1.get_visible())
self.main.ui.btn_more.set_label(SHOW_HIDE_TEXT[self.main.ui.scrolledwindow1.get_visible()])
def on_btn_close_clicked(self, button):
self.main.copies[self.main.active_copy].process.kill()
Gtk.main_quit()
def on_btn_pause_clicked(self, button):
if self.main.status == 0:
self.main.copies[self.main.active_copy].status = 2
self.main.ui.btn_pause_resume.set_label('Resume')
self.main.update_ui(copy_paused=True)
self.main.copies[self.main.active_copy].process.pause_resume(self.main.status)
self.main.status = 1
else:
self.main.copies[self.main.active_copy].status = 1
self.main.update_ui(copy_paused=True)
self.main.ui.btn_pause_resume.set_label('Pause')
GObject.idle_add(self.main.manage_copies)
self.main.copies[self.main.active_copy].process.pause_resume(self.main.status)
self.main.status = 0
#TODO: Check event handler
def on_windows_destroy(self, *args):
Gtk.main_quit(*args)
class RGCopy():
def __init__(self):
self.ui = ui()
self.active_copy = None
self.active_dir_copy = -1
self.file_list = []
self.ui.builder.connect_signals(Handler(self))
self.copies_size = 0
self.partial_copies_size = 0
self.lines, self.action = [], 'not_set'#self.get_lines_list()
self.status = 0
self.cont = 0
self.show_state = False
def initialize(self):
self.ui.scrolledwindow1.set_visible(False)
def get_copies_list(self):
cps = []
copy_path = self.get_copy_path()
for line in self.lines:
if not os.path.isdir(line):
copy = Copy(line, copy_path, self, self.action)
copy_size = copy.get_size()
self.copies_size += copy_size
file_name = os.path.basename(line)
parent_dir = os.path.dirname(line)
self.file_list.append((parent_dir, file_name, copy_size))
else:
copy = Copy(line, copy_path, self, self.action, True)
paths = []
files_dic = {}
for path, dirs, files in os.walk(line):
paths.append(path)
files_dic[path] = files
paths.sort()
for path in paths:
files = files_dic[path]
if files:
files.sort()
for fl in files:
full_path = os.path.join(path, fl)
copy_size = os.path.getsize(full_path)#/1024.0
self.copies_size += copy_size
self.file_list.append((path, fl, copy_size))
cps.append(copy)
self.set_treview_model()
return cps
# import os
# statvfs = os.statvfs('/home/foo/bar/baz')
# statvfs.frsize * statvfs.f_blocks # Size of filesystem in bytes
# statvfs.frsize * statvfs.f_bfree # Actual number of free bytes
# statvfs.frsize * statvfs.f_bavail
def get_clipboard_content(self):
cb = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
atom = Gdk.Atom.intern('x-special/gnome-copied-files', False)
content = cb.wait_for_contents(atom)
content_data = content.get_data()
content_data = str(content_data)
if content_data:
split_data = content_data.split('\n')
value1 = split_data[0]
value2 = split_data[1:]
return value1, value2
else:
return None, []
def get_lines_list(self):
lts = []
action, lines = self.get_clipboard_content()
for line in lines:
if line.startswith('file://'):
url = line.split('file://')[1]
line = urllib.url2pathname(url)
elif line.startswith('smb://'):
url = line.split('smb://')[1]
path_array = url.split('/')
server_name = path_array[0]
share_name = path_array[1]
file_path = '/'.join(path_array[2:])
user = get_login_user()
line = urllib.url2pathname('/run/user/%s/gvfs/smb-share:server=%s,share=%s/%s' % (user,server_name,share_name,file_path))
elif line.startswith('sftp://'):
url = line.split('sftp://')[1]
path_array = url.split('/')
server_name = path_array[0]
file_path = '/'.join(path_array[1:])
user = get_login_user()
split_server_name = server_name.split('@')
if len(split_server_name) != 1:
user_name = split_server_name[0]
server_name = split_server_name[1]
line = urllib.url2pathname('/run/user/%s/gvfs/sftp:host=%s,user=%s/%s' % (user,server_name,user_name,file_path))
else:
line = urllib.url2pathname('/run/user/%s/gvfs/sftp:host=%s/%s' % (user,server_name,file_path))
if os.path.exists(line):
lts.append(line)
return lts, action
def convert_bytes(self, bytes):
if bytes >= 1099511627776:
terabytes = bytes / 1099511627776.0
size = '%.2f TB' % terabytes
elif bytes >= 1073741824:
gigabytes = bytes / 1073741824.0
size = '%.2f GB' % gigabytes
elif bytes >= 1048576:
megabytes = bytes / 1048576.0
size = '%.2f MB' % megabytes
elif bytes >= 1024:
kilobytes = bytes / 1024.0
size = '%.2f KB' % kilobytes
else:
size = '%.2f B' % bytes
return size
def set_treview_model(self):
for fl in self.file_list:
self.ui.listStore.append([fl[1], self.convert_bytes(fl[2]), COPY_STATUS_CODE[0], fl[0]])
def get_copy_path(self):
try:
dir_to_open = ""
selected = os.environ["NAUTILUS_SCRIPT_SELECTED_URIS"].split("\n")[:-1]
#Note getting SELECTED_URIS rather than SELECTED_FILE_PATHS as later
#is not set when ~/Desktop and ~/.Trash selected??
if len(selected) == 1:
uri_bits = urlparse.urlparse(urllib.unquote(selected[0]))
if uri_bits[0] == "file":
dir_to_open = uri_bits[2]
elif uri_bits[0] == "x-nautilus-desktop":
if uri_bits[2] == "///trash":
dir_to_open = home_dir + '/.Trash'
elif uri_bits[2] == "///home":
dir_to_open = home_dir + '/Desktop'
if not os.path.isdir(dir_to_open):
dir_to_open = ""
if not dir_to_open: #we didn't select 1 directory so open current dir
current_uri = os.environ["NAUTILUS_SCRIPT_CURRENT_URI"]
uri_bits = urlparse.urlparse(urllib.unquote(current_uri))
if uri_bits[0] == "file":
dir_to_open = uri_bits[2]
elif uri_bits[0] == "x-nautilus-desktop":
dir_to_open = home_dir + '/Desktop'
elif uri_bits[0] == "trash":
dir_to_open = home_dir + '/.Trash'
except Exception, e:
return home_dir
#return '/home/reisy/Escritorio/RGCopy_prueba'
return dir_to_open
def show(self):
def maximize(attr1, attr2):
self.ui.rgcopy.present()
APPNAME = "RGCopy"
ICON = '/opt/RGCopy/icons/32x32/stallion.png'
self.ai = AppIndicator3.Indicator.new(APPNAME, ICON, AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
self.ai.set_menu(self.ui.tray_menu)
#self.ai.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.launcher = Unity.LauncherEntry.get_for_desktop_id ("RGCopy.desktop")
self.ui.rgcopy.set_title('RGCopy')
self.ui.rgcopy.show_all()
self.initialize()
#GObject.threads_init()
Gtk.main()
def on_show_copy_activate(self):
self.ui.rgcopy.set_visible(True)
self.ui.rgcopy.show_all()
def run_process(self):
self.copies = self.get_copies_list()
GObject.idle_add(self.manage_copies)
def manage_copies(self):
if self.status == 1:
return False
if self.active_copy is not None:
if self.copies[self.active_copy].process.done():
self.copies[self.active_copy].process.update_copy({'percent':100, 'copied_size':self.file_list[self.active_dir_copy][2]})
self.update_ui()
self.copies[self.active_copy].status = 3
if len(self.copies) == (self.active_copy + 1):
self.update_ui(True)
Gtk.main_quit()
else:
self.active_copy += 1
self.copies[self.active_copy].status = 1
self.copies[self.active_copy].process.start()
return True
else:
return True
else:
self.active_copy = 0
self.copies[self.active_copy].status = 1
self.copies[self.active_copy].process.start()
return True
def update_ui(self, copy_done=None, file_list=None, copy_paused=False):
def get_pb_all_files_values(updated_value):
if self.copies_size != 0:
return int(((self.partial_copies_size + updated_value) * 100.0) / self.copies_size)
return 1
def convert_to_bytes(rate):
if rate:
mu = rate[-4:]
value = rate[:-4]
return float(value) * CONVERSION_RATE[mu]
return None
def get_total_eta(rate):
b_p_s = convert_to_bytes(rate)
if b_p_s:
left = self.copies_size - (self.partial_copies_size + copy.copied_size)
seconds = left / b_p_s
eta = str(datetime.timedelta(seconds=round(seconds)))
return eta
return '--:--:--'
copy = self.copies[self.active_copy]
if file_list:
try:
for fl in file_list:
self.active_dir_copy += 1
if self.active_dir_copy > 0:
#Adding file size to partial list
self.partial_copies_size += self.file_list[self.active_dir_copy - 1][2]
#Set to copied last file and set to running current
self.ui.listStore[self.active_dir_copy - 1][2] = COPY_STATUS_CODE[3]
self.ui.listStore[self.active_dir_copy][2] = COPY_STATUS_CODE[1]
#Set file name and dest
self.ui.lbl_file_name.set_text(self.format_string(fl))
self.ui.lbl_target.set_text(self.format_string(copy.dest))
else:
#Set file name and dest
self.ui.lbl_file_name.set_text(self.format_string(fl))
self.ui.lbl_target.set_text(self.format_string(copy.dest))
#Set to running current
self.ui.listStore[self.active_dir_copy][2] = COPY_STATUS_CODE[1]
except Exception, e:
pass
if copy_done:
self.ui.listStore[self.active_dir_copy][2] = COPY_STATUS_CODE[3]
if copy_paused:
self.ui.listStore[self.active_dir_copy][2] = COPY_STATUS_CODE[copy.status]
#pb_single_file
self.ui.pb_single_file.set_fraction(float(copy.percent) / 100)
#pb_all_files
fraction = get_pb_all_files_values(copy.copied_size)
self.ui.pb_all_files.set_fraction(float(fraction) / 100)
#Windows Title
self.ui.rgcopy.set_title('Copy: %s (%s)-RGCopy' % (str(fraction) + '%', copy.speed_rate))
#Progress Bar Labels
self.ui.lbl_file_size.set_text('%s of %s' % (self.convert_bytes(self.partial_copies_size + copy.copied_size),
self.convert_bytes(self.copies_size)))
self.ui.lbl_single_file_eta.set_text(copy.ETA)
self.ui.lbl_total_file_count.set_text('%s of %s' % (self.active_dir_copy + 1, len(self.file_list)))
self.ui.lbl_total_files_eta.set_text(get_total_eta(copy.speed_rate))
def format_string(self, string):
return string if len(string) <= 55 else '...%s' % string[len(string) - 55:]
if __name__ == '__main__':
main = RGCopy()
main.show()