0
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 gcp: Goffi's CoPier |
|
6 Copyright (C) 2010 Jérôme Poisson (goffi@goffi.org) |
|
7 |
|
8 This program is free software: you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation, either version 3 of the License, or |
|
11 (at your option) any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 """ |
|
21 |
|
22 ### logging ### |
|
23 import logging |
|
24 from logging import debug, info, error, warning |
|
25 logging.basicConfig(level=logging.DEBUG, |
|
26 format='%(message)s') |
|
27 ### |
|
28 import sys |
|
29 import os,os.path |
|
30 from optparse import OptionParser #To be replaced by argparse ASAP |
1
|
31 import cPickle as pickle |
0
|
32 try: |
|
33 import gobject |
|
34 #DBus |
|
35 import dbus, dbus.glib |
|
36 import dbus.service |
|
37 import dbus.mainloop.glib |
|
38 except ImportError,e: |
|
39 error("Error during import") |
|
40 error("Please check dependecies:",e) |
|
41 exit(2) |
|
42 try: |
|
43 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed |
|
44 pbar_available=True |
|
45 except ImportError, e: |
|
46 info ('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar') |
|
47 info ('Progress bar deactivated\n--\n') |
|
48 pbar_available=False |
|
49 |
|
50 NAME = "gcp (Goffi's copier)" |
|
51 NAME_SHORT = "gcp" |
|
52 VERSION = '0.1' |
|
53 |
|
54 ABOUT = NAME+" v"+VERSION+""" (c) Jérôme Poisson (aka Goffi) 2010 |
|
55 |
|
56 --- |
|
57 """+NAME+""" Copyright (C) 2010 Jérôme Poisson |
|
58 This program comes with ABSOLUTELY NO WARRANTY; |
|
59 This is free software, and you are welcome to redistribute it |
|
60 under certain conditions. |
|
61 --- |
|
62 |
|
63 This software is an advanced file copier |
|
64 Get the latest version at http://www.goffi.org |
|
65 """ |
|
66 |
|
67 const_DBUS_INTERFACE = "org.goffi.gcp" |
3
|
68 const_DBUS_PATH = "/org/goffi/gcp" |
|
69 const_BUFF_SIZE = 4096 |
0
|
70 |
|
71 |
|
72 class DbusObject(dbus.service.Object): |
|
73 |
|
74 def __init__(self, gcp, bus, path): |
|
75 self._gcp = gcp |
|
76 dbus.service.Object.__init__(self, bus, path) |
|
77 debug("Init DbusObject...") |
|
78 self.cb={} |
|
79 |
|
80 @dbus.service.method(const_DBUS_INTERFACE, |
|
81 in_signature='', out_signature='s') |
|
82 def getVersion(self): |
|
83 """Get gcp version |
|
84 @return: version as string""" |
|
85 return VERSION |
|
86 |
|
87 @dbus.service.method(const_DBUS_INTERFACE, |
1
|
88 in_signature='ss', out_signature='bs') |
0
|
89 def addArgs(self, source_path, args): |
|
90 """Add arguments to gcp as if there were entered on its own command line |
1
|
91 @param source_path: current working dir to use as base for arguments, as given by os.getcwd() |
|
92 @param args: serialized (wich pickle) list of strings - without command name -, as given by sys.argv[1:]. |
|
93 @return: success (boolean) and error message if any (string)""" |
|
94 try: |
|
95 args = pickle.loads(str(args)) |
|
96 except TypeError, pickle.UnpicklingError: |
|
97 return (False, "INTERNAL ERROR: invalid arguments") |
0
|
98 return self._gcp.parseArguments(args, source_path) |
|
99 |
|
100 class GCP(): |
|
101 |
|
102 def __init__(self): |
|
103 try: |
|
104 sessions_bus = dbus.SessionBus() |
|
105 db_object = sessions_bus.get_object(const_DBUS_INTERFACE, |
|
106 const_DBUS_PATH) |
|
107 self.gcp_main = dbus.Interface(db_object, |
|
108 dbus_interface=const_DBUS_INTERFACE) |
|
109 self._main_instance = False |
|
110 |
|
111 except dbus.exceptions.DBusException,e: |
|
112 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown': |
|
113 self.launchDbusMainInstance() |
|
114 debug ("gcp launched") |
|
115 self._main_instance = True |
3
|
116 self.buffer_size = const_BUFF_SIZE |
0
|
117 else: |
|
118 raise e |
|
119 |
|
120 def launchDbusMainInstance(self): |
|
121 debug ("Init DBus...") |
|
122 session_bus = dbus.SessionBus() |
|
123 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus) |
|
124 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH) |
|
125 |
|
126 self.copy_list = [] |
|
127 self.mounts = self.__getMountPoints() |
1
|
128 self.files_left = 0 |
|
129 self.bytes_left = 0 |
0
|
130 |
|
131 def getFsType(self, path): |
|
132 fs='' |
|
133 for mount in self.mounts: |
2
|
134 if path.startswith(mount) and len(self.mounts[mount])>=len(fs): |
0
|
135 fs = self.mounts[mount] |
|
136 return fs |
|
137 |
|
138 def __getMountPoints(self): |
|
139 """Parse /proc/mounts to get currently mounted devices""" |
|
140 #TODO: reparse when a new device is added/a device is removed |
1
|
141 #(check freedesktop mounting signals) |
0
|
142 ret = {} |
|
143 try: |
|
144 with open("/proc/mounts",'r') as mounts: |
|
145 for line in mounts.readlines(): |
|
146 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ') |
|
147 ret[fs_file] = fs_vfstype |
|
148 except: |
|
149 error ("Can't read mounts table") |
|
150 return ret |
|
151 |
3
|
152 def __appendToList(self, path, dest_path, options): |
0
|
153 """Add a file to the copy list |
|
154 @param path: absolute path of file |
|
155 @param options: options as return by optparse""" |
3
|
156 debug ("Adding to copy list: %s ==> %s (%s)", path, dest_path, self.getFsType(dest_path)) |
1
|
157 try: |
|
158 self.bytes_left+=os.path.getsize(path) |
|
159 self.files_left+=1 |
3
|
160 self.copy_list.append((path, dest_path, options)) |
1
|
161 print "total size:", float(self.bytes_left/1024/1024), "Mb (%i)" % self.files_left |
|
162 except OSError,e: |
|
163 error("Can't copy %(path)s: %(exception)s" % {'path':path, 'exception':e.strerror}) |
0
|
164 |
1
|
165 |
3
|
166 def __appendDirToList(self, dirpath, dest_path, options): |
0
|
167 """Add recursively directory to the copy list |
|
168 @param path: absolute path of dir |
|
169 @param options: options as return by optparse""" |
3
|
170 #We first check that the dest path exists, and create it if needed |
|
171 if not os.path.exists(dest_path): |
|
172 debug ("Creating directory %s" % dest_path) |
|
173 os.makedirs(dest_path) #TODO: check permissions |
|
174 #TODO: check that dest_path is an accessible dir, |
|
175 # and skip file/write error in log if needed |
0
|
176 try: |
|
177 for filename in os.listdir(dirpath): |
|
178 filepath = os.path.join(dirpath,filename) |
|
179 if os.path.isdir(filepath): |
3
|
180 full_dest_path = os.path.join(dest_path,filename) |
|
181 self.__appendDirToList(filepath, full_dest_path, options) |
0
|
182 else: |
3
|
183 self.__appendToList(filepath, dest_path, options) |
0
|
184 except OSError,e: |
|
185 error("Can't copy %(path)s: %(exception)s" % {'path':dirpath, 'exception':e.strerror}) |
|
186 |
|
187 def __checkArgs(self, options, source_path, args): |
|
188 """Check thats args are files, and add them to copy list""" |
1
|
189 assert(len (args)>=2) |
|
190 try: |
3
|
191 dest_path = os.path.normpath(os.path.join(os.path.expanduser(source_path), args.pop())) |
1
|
192 except OSError,e: |
3
|
193 error ("Invalid dest_path: %s",e) |
1
|
194 |
0
|
195 for path in args: |
1
|
196 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path)) |
0
|
197 if not os.path.exists(abspath): |
|
198 warning("The path given in arg doesn't exist or is not accessible: %s",abspath) |
|
199 else: |
|
200 if os.path.isdir(abspath): |
3
|
201 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path)) |
0
|
202 if not options.recursive: |
|
203 warning ('omitting directory "%s"' % abspath) |
|
204 else: |
3
|
205 self.__appendDirToList(abspath, full_dest_path, options) |
0
|
206 else: |
3
|
207 self.__appendToList(abspath, dest_path, options) |
0
|
208 |
3
|
209 def __copyNextFile(self): |
|
210 """Take the last file in the list, and launch the copy using glib io_watch event |
|
211 @return: True a file was added, False else""" |
|
212 if self.copy_list: |
|
213 source_path, dest_path, options = self.copy_list.pop() |
|
214 source_fd = open(source_path, 'r') |
|
215 filename = os.path.basename(source_path) |
|
216 assert(filename) |
|
217 dest_file = os.path.join(dest_path,filename) |
|
218 if os.path.exists(dest_file): |
|
219 warning ("File [%s] already exists, skipping it !" % dest_file) |
|
220 return True |
|
221 dest_fd = open(dest_file, 'w') |
|
222 |
|
223 self.total=0 |
|
224 gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile,(dest_fd,options), priority=gobject.PRIORITY_HIGH) |
|
225 print "** COPYING",source_path,"==>",dest_file |
|
226 return True |
|
227 else: |
|
228 #Nothing left to copy, we quit |
|
229 self.loop.quit() |
|
230 |
|
231 def _copyFile(self, source_fd, condition, data): |
|
232 """Actually copy the file, callback used with io_add_watch |
|
233 @param source_fd: file descriptor of the file to copy |
|
234 @param condition: condition which launched the callback (glib.IO_IN) |
|
235 @param data: tuple with (destination file descriptor, copying options)""" |
|
236 dest_fd,options = data |
|
237 buff = source_fd.read(self.buffer_size) |
|
238 dest_fd.write(buff) |
|
239 self.total += len(buff) |
|
240 sys.stdout.write('%i written\r' % self.total) |
|
241 if len(buff) != self.buffer_size: |
|
242 sys.stdout.write('\n---\n') |
|
243 source_fd.close() |
|
244 dest_fd.close() |
|
245 return False |
|
246 return True |
|
247 |
|
248 |
0
|
249 |
|
250 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()): |
1
|
251 """Parse arguments and add files to queue |
|
252 @param full_args: list of arguments strings (without program name) |
|
253 @param source_path: path from where the arguments come, ad given by os.getcwd() |
|
254 @return: a tuple (boolean, message) where the boolean is the success of the arguments |
|
255 validation, and message is the error message to print when necessary""" |
0
|
256 _usage=""" |
|
257 %prog [options] FILE1 [FILE2 ...] DEST |
|
258 |
|
259 %prog --help for options list |
|
260 """ |
1
|
261 for idx in range(len(full_args)): |
|
262 if isinstance(full_args[idx], unicode): |
|
263 #We don't want unicode as some filenames can be invalid unicode |
|
264 full_args[idx] = full_args[idx].encode('utf-8') |
|
265 |
0
|
266 parser = OptionParser(usage=_usage,version=ABOUT) |
|
267 |
|
268 parser.add_option("-r", "--recursive", action="store_true", default=False, |
|
269 help="copy directories recursively") |
|
270 |
1
|
271 parser.add_option("--no-unicode-fix", action="store_true", default=False, |
|
272 help="don't fixe name encoding errors") #TODO |
3
|
273 |
|
274 parser.add_option("--no-progress", action="store_false", dest="progress", default=True, |
|
275 help="deactivate progress bar") |
|
276 |
0
|
277 (options, args) = parser.parse_args(full_args) |
3
|
278 if options.progress and not pbar_available: |
|
279 warning ("Progress bar is not available, deactivating") |
|
280 options.progress = False |
0
|
281 |
|
282 if not self._main_instance: |
1
|
283 info ("There is already one instance of %s running, pluging to it" % NAME_SHORT) |
|
284 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames |
|
285 # can have invalid unicode. |
|
286 return self.gcp_main.addArgs(os.getcwd(),pickle.dumps(full_args)) |
0
|
287 else: |
1
|
288 if len(args) < 2: |
|
289 _error_msg = "Wrong number of arguments" |
|
290 return (False, _error_msg) |
0
|
291 debug("adding args to gcp: %s",args) |
|
292 self.__checkArgs(options, source_path, args) |
3
|
293 gobject.idle_add(self.__copyNextFile) |
1
|
294 return (True,'') |
0
|
295 |
|
296 def go(self): |
1
|
297 """Launch main loop""" |
0
|
298 self.loop = gobject.MainLoop() |
|
299 try: |
|
300 self.loop.run() |
|
301 except KeyboardInterrupt: |
|
302 info("User interruption: good bye") |
|
303 |
|
304 |
|
305 if __name__ == "__main__": |
|
306 gcp = GCP() |
1
|
307 success,message = gcp.parseArguments() |
|
308 if not success: |
|
309 error(message) |
|
310 exit(1) |
0
|
311 if gcp._main_instance: |
|
312 gcp.go() |
|
313 |