Mercurial > gcp
annotate gcp @ 6:5f53dc5beec9
vfat names incompatibility fix
- bug fixed in getFsType(bad fs returned in most cases)
- removing gettext (_("")) in a debug fonction with filename, as the filename can be with invalid utf-8, and then cause an exception. A better solution can probably be found later.
- No more progress bar when the files have a null sum
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 27 Sep 2010 16:44:25 +0800 |
parents | 7edbb0e0d9dd |
children | a110d31482f7 |
rev | line source |
---|---|
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 | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
25 logging.basicConfig(level=logging.INFO, |
0 | 26 format='%(message)s') |
27 ### | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
28 |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
29 import gettext |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
30 gettext.install('gcp', "i18n", unicode=True) |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
31 |
0 | 32 import sys |
33 import os,os.path | |
34 from optparse import OptionParser #To be replaced by argparse ASAP | |
1 | 35 import cPickle as pickle |
0 | 36 try: |
37 import gobject | |
38 #DBus | |
39 import dbus, dbus.glib | |
40 import dbus.service | |
41 import dbus.mainloop.glib | |
42 except ImportError,e: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
43 error(_("Error during import")) |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
44 error(_("Please check dependecies:"),e) |
0 | 45 exit(2) |
46 try: | |
47 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed | |
48 pbar_available=True | |
49 except ImportError, e: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
50 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
51 info (_('Progress bar deactivated\n--\n')) |
0 | 52 pbar_available=False |
53 | |
54 NAME = "gcp (Goffi's copier)" | |
55 NAME_SHORT = "gcp" | |
56 VERSION = '0.1' | |
57 | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
58 ABOUT = NAME+u" v"+VERSION+u""" (c) Jérôme Poisson (aka Goffi) 2010 |
0 | 59 |
60 --- | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
61 """+NAME+u""" Copyright (C) 2010 Jérôme Poisson |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
62 """ + _(u"""This program comes with ABSOLUTELY NO WARRANTY; |
0 | 63 This is free software, and you are welcome to redistribute it |
64 under certain conditions. | |
65 --- | |
66 | |
67 This software is an advanced file copier | |
68 Get the latest version at http://www.goffi.org | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
69 """) |
0 | 70 |
71 const_DBUS_INTERFACE = "org.goffi.gcp" | |
3 | 72 const_DBUS_PATH = "/org/goffi/gcp" |
73 const_BUFF_SIZE = 4096 | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
74 const_PRESERVE = set(['mode','ownership','timestamps']) |
0 | 75 |
76 | |
77 class DbusObject(dbus.service.Object): | |
78 | |
79 def __init__(self, gcp, bus, path): | |
80 self._gcp = gcp | |
81 dbus.service.Object.__init__(self, bus, path) | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
82 debug(_("Init DbusObject...")) |
0 | 83 self.cb={} |
84 | |
85 @dbus.service.method(const_DBUS_INTERFACE, | |
86 in_signature='', out_signature='s') | |
87 def getVersion(self): | |
88 """Get gcp version | |
89 @return: version as string""" | |
90 return VERSION | |
91 | |
92 @dbus.service.method(const_DBUS_INTERFACE, | |
1 | 93 in_signature='ss', out_signature='bs') |
0 | 94 def addArgs(self, source_path, args): |
95 """Add arguments to gcp as if there were entered on its own command line | |
1 | 96 @param source_path: current working dir to use as base for arguments, as given by os.getcwd() |
97 @param args: serialized (wich pickle) list of strings - without command name -, as given by sys.argv[1:]. | |
98 @return: success (boolean) and error message if any (string)""" | |
99 try: | |
100 args = pickle.loads(str(args)) | |
101 except TypeError, pickle.UnpicklingError: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
102 return (False, _("INTERNAL ERROR: invalid arguments")) |
0 | 103 return self._gcp.parseArguments(args, source_path) |
104 | |
105 class GCP(): | |
106 | |
107 def __init__(self): | |
108 try: | |
109 sessions_bus = dbus.SessionBus() | |
110 db_object = sessions_bus.get_object(const_DBUS_INTERFACE, | |
111 const_DBUS_PATH) | |
112 self.gcp_main = dbus.Interface(db_object, | |
113 dbus_interface=const_DBUS_INTERFACE) | |
114 self._main_instance = False | |
115 | |
116 except dbus.exceptions.DBusException,e: | |
117 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown': | |
118 self.launchDbusMainInstance() | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
119 debug (_("gcp launched")) |
0 | 120 self._main_instance = True |
3 | 121 self.buffer_size = const_BUFF_SIZE |
0 | 122 else: |
123 raise e | |
124 | |
125 def launchDbusMainInstance(self): | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
126 debug (_("Init DBus...")) |
0 | 127 session_bus = dbus.SessionBus() |
128 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus) | |
129 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH) | |
130 | |
131 self.copy_list = [] | |
132 self.mounts = self.__getMountPoints() | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
133 self.bytes_total = 0 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
134 self.bytes_copied = 0 |
0 | 135 |
136 def getFsType(self, path): | |
6 | 137 fs = '' |
138 last_mount_point = '' | |
0 | 139 for mount in self.mounts: |
6 | 140 if path.startswith(mount) and len(mount)>=len(last_mount_point): |
0 | 141 fs = self.mounts[mount] |
6 | 142 last_mount_point = mount |
0 | 143 return fs |
144 | |
145 def __getMountPoints(self): | |
146 """Parse /proc/mounts to get currently mounted devices""" | |
147 #TODO: reparse when a new device is added/a device is removed | |
1 | 148 #(check freedesktop mounting signals) |
0 | 149 ret = {} |
150 try: | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
151 with open("/proc/mounts",'rb') as mounts: |
0 | 152 for line in mounts.readlines(): |
153 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ') | |
154 ret[fs_file] = fs_vfstype | |
155 except: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
156 error (_("Can't read mounts table")) |
0 | 157 return ret |
158 | |
3 | 159 def __appendToList(self, path, dest_path, options): |
0 | 160 """Add a file to the copy list |
161 @param path: absolute path of file | |
162 @param options: options as return by optparse""" | |
6 | 163 debug ("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)" % {"path":path, |
164 "dest_path":dest_path, | |
165 "fs_type":self.getFsType(dest_path)} ) | |
1 | 166 try: |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
167 self.bytes_total+=os.path.getsize(path) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
168 self.copy_list.insert(0,(path, dest_path, options)) |
1 | 169 except OSError,e: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
170 error(_("Can't copy %(path)s: %(exception)s") % {'path':path, 'exception':e.strerror}) |
0 | 171 |
1 | 172 |
3 | 173 def __appendDirToList(self, dirpath, dest_path, options): |
0 | 174 """Add recursively directory to the copy list |
175 @param path: absolute path of dir | |
176 @param options: options as return by optparse""" | |
3 | 177 #We first check that the dest path exists, and create it if needed |
6 | 178 dest_path = self.__filename_fix(dest_path, options) |
3 | 179 if not os.path.exists(dest_path): |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
180 debug (_("Creating directory %s") % dest_path) |
3 | 181 os.makedirs(dest_path) #TODO: check permissions |
182 #TODO: check that dest_path is an accessible dir, | |
183 # and skip file/write error in log if needed | |
0 | 184 try: |
185 for filename in os.listdir(dirpath): | |
186 filepath = os.path.join(dirpath,filename) | |
187 if os.path.isdir(filepath): | |
3 | 188 full_dest_path = os.path.join(dest_path,filename) |
189 self.__appendDirToList(filepath, full_dest_path, options) | |
0 | 190 else: |
3 | 191 self.__appendToList(filepath, dest_path, options) |
0 | 192 except OSError,e: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
193 error(_("Can't copy %(path)s: %(exception)s") % {'path':dirpath, 'exception':e.strerror}) |
0 | 194 |
195 def __checkArgs(self, options, source_path, args): | |
196 """Check thats args are files, and add them to copy list""" | |
1 | 197 assert(len (args)>=2) |
198 try: | |
3 | 199 dest_path = os.path.normpath(os.path.join(os.path.expanduser(source_path), args.pop())) |
1 | 200 except OSError,e: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
201 error (_("Invalid dest_path: %s"),e) |
1 | 202 |
0 | 203 for path in args: |
1 | 204 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path)) |
0 | 205 if not os.path.exists(abspath): |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
206 warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath) |
0 | 207 else: |
208 if os.path.isdir(abspath): | |
3 | 209 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path)) |
0 | 210 if not options.recursive: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
211 warning (_('omitting directory "%s"') % abspath) |
0 | 212 else: |
3 | 213 self.__appendDirToList(abspath, full_dest_path, options) |
0 | 214 else: |
3 | 215 self.__appendToList(abspath, dest_path, options) |
0 | 216 |
3 | 217 def __copyNextFile(self): |
218 """Take the last file in the list, and launch the copy using glib io_watch event | |
219 @return: True a file was added, False else""" | |
220 if self.copy_list: | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
221 source_file, dest_path, options = self.copy_list.pop() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
222 source_fd = open(source_file, 'rb') |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
223 filename = os.path.basename(source_file) |
3 | 224 assert(filename) |
6 | 225 dest_file = self.__filename_fix(os.path.join(dest_path,filename),options) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
226 if os.path.exists(dest_file) and not options.force: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
227 warning (_("File [%s] already exists, skipping it !") % dest_file) |
3 | 228 return True |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
229 dest_fd = open(dest_file, 'wb') |
3 | 230 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
231 gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile, |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
232 (dest_fd, options), priority=gobject.PRIORITY_HIGH) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
233 if not self.progress: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
234 info(_("COPYING %(source)s ==> %(dest)s") % {"source":source_path,"dest":dest_file}) |
3 | 235 return True |
236 else: | |
237 #Nothing left to copy, we quit | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
238 if self.progress: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
239 self.__pbar_finish() |
3 | 240 self.loop.quit() |
241 | |
242 def _copyFile(self, source_fd, condition, data): | |
243 """Actually copy the file, callback used with io_add_watch | |
244 @param source_fd: file descriptor of the file to copy | |
245 @param condition: condition which launched the callback (glib.IO_IN) | |
246 @param data: tuple with (destination file descriptor, copying options)""" | |
247 dest_fd,options = data | |
248 buff = source_fd.read(self.buffer_size) | |
249 dest_fd.write(buff) | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
250 self.bytes_copied += len(buff) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
251 if self.progress: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
252 self.__pbar_update() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
253 |
3 | 254 if len(buff) != self.buffer_size: |
255 source_fd.close() | |
256 dest_fd.close() | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
257 self.__post_copy(source_fd.name, dest_fd.name, options) |
3 | 258 return False |
259 return True | |
260 | |
6 | 261 def __filename_fix(self, filename, options): |
262 if self.getFsType(filename) == 'vfat' and options.fs_fix: | |
263 filename = filename.replace('\\','_')\ | |
264 .replace(':',';')\ | |
265 .replace('*','+')\ | |
266 .replace('?','')\ | |
267 .replace('"','\'')\ | |
268 .replace('<','[')\ | |
269 .replace('>',']')\ | |
270 .replace('|','!') | |
271 return filename | |
272 | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
273 def __post_copy(self, source_file, dest_file, options): |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
274 """Do post copy traitement (mainly managing --preserve option)""" |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
275 st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime = os.stat(source_file) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
276 #TODO: complete log in case of errors |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
277 for preserve in options.preserve: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
278 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
279 if preserve == 'mode': |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
280 os.chmod(dest_file, st_mode) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
281 elif preserve == 'ownership': |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
282 os.chown(dest_file, st_uid, st_gid) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
283 elif preserve == 'timestamps': |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
284 os.utime(dest_file, (st_atime, st_mtime)) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
285 except OSError,e: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
286 pass #TODO: complete log here |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
287 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
288 def __pbar_update(self): |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
289 """Update progress bar position, create the bar if it doesn't exist""" |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
290 assert(self.progress) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
291 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
292 if self.pbar.maxval != self.bytes_total: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
293 self.pbar.maxval = self.bytes_total |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
294 except AttributeError: |
6 | 295 if not self.bytes_total: |
296 #No progress bar if the files have a null size | |
297 return | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
298 self.pbar = ProgressBar(self.bytes_total,[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()]) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
299 self.pbar.start() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
300 self.pbar.update(self.bytes_copied) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
301 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
302 def __pbar_finish(self): |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
303 """Mark the progression as finished""" |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
304 assert(self.progress) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
305 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
306 self.pbar.finish() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
307 except AttributeError: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
308 pass |
0 | 309 |
310 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()): | |
1 | 311 """Parse arguments and add files to queue |
312 @param full_args: list of arguments strings (without program name) | |
313 @param source_path: path from where the arguments come, ad given by os.getcwd() | |
314 @return: a tuple (boolean, message) where the boolean is the success of the arguments | |
315 validation, and message is the error message to print when necessary""" | |
0 | 316 _usage=""" |
317 %prog [options] FILE1 [FILE2 ...] DEST | |
318 | |
319 %prog --help for options list | |
320 """ | |
1 | 321 for idx in range(len(full_args)): |
322 if isinstance(full_args[idx], unicode): | |
323 #We don't want unicode as some filenames can be invalid unicode | |
324 full_args[idx] = full_args[idx].encode('utf-8') | |
325 | |
0 | 326 parser = OptionParser(usage=_usage,version=ABOUT) |
327 | |
328 parser.add_option("-r", "--recursive", action="store_true", default=False, | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
329 help=_("copy directories recursively")) |
0 | 330 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
331 parser.add_option("-f", "--force", action="store_true", default=False, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
332 help=_("force overwriting of existing files")) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
333 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
334 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
335 help=_("preserve the specified attributes")) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
336 |
6 | 337 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
338 help=_("don't fixe name encoding errors")) #TODO |
6 | 339 |
340 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True, | |
341 help=_("don't fixe filesystem name incompatibily")) #TODO | |
342 | |
3 | 343 parser.add_option("--no-progress", action="store_false", dest="progress", default=True, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
344 help=_("deactivate progress bar")) |
3 | 345 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
346 parser.add_option("-v", "--verbose", action="store_true", default=False, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
347 help=_("Show what is currently done")) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
348 |
0 | 349 (options, args) = parser.parse_args(full_args) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
350 #options check |
3 | 351 if options.progress and not pbar_available: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
352 warning (_("Progress bar is not available, deactivating")) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
353 options.progress = self.progress = False |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
354 else: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
355 self.progress = options.progress |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
356 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
357 if options.verbose: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
358 logging.getLogger().setLevel(logging.DEBUG) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
359 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
360 preserve = set(options.preserve.split(',')) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
361 if not preserve.issubset(const_PRESERVE): |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
362 error (_("Invalide --preserve value\nvalid values are:")) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
363 for value in const_PRESERVE: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
364 error('- %s' % value) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
365 exit(2) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
366 else: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
367 options.preserve = preserve |
0 | 368 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
369 #if there is an other instance of gcp, we send options to it |
0 | 370 if not self._main_instance: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
371 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT) |
1 | 372 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames |
373 # can have invalid unicode. | |
374 return self.gcp_main.addArgs(os.getcwd(),pickle.dumps(full_args)) | |
0 | 375 else: |
1 | 376 if len(args) < 2: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
377 _error_msg = _("Wrong number of arguments") |
1 | 378 return (False, _error_msg) |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
379 debug(_("adding args to gcp: %s"),args) |
0 | 380 self.__checkArgs(options, source_path, args) |
3 | 381 gobject.idle_add(self.__copyNextFile) |
1 | 382 return (True,'') |
0 | 383 |
384 def go(self): | |
1 | 385 """Launch main loop""" |
0 | 386 self.loop = gobject.MainLoop() |
387 try: | |
388 self.loop.run() | |
389 except KeyboardInterrupt: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
390 info(_("User interruption: good bye")) |
0 | 391 |
392 | |
393 if __name__ == "__main__": | |
394 gcp = GCP() | |
1 | 395 success,message = gcp.parseArguments() |
396 if not success: | |
397 error(message) | |
398 exit(1) | |
0 | 399 if gcp._main_instance: |
400 gcp.go() | |
401 |