Mercurial > gcp
annotate gcp @ 46:dda2195dc9f4
Symbolic link skipping
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 04 Jun 2011 20:55:44 +0200 |
parents | 97384a79043d |
children | 99da6ef26d26 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 gcp: Goffi's CoPier | |
41
b54e5a4102da
os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents:
40
diff
changeset
|
6 Copyright (C) 2010, 2011 Jérôme Poisson <goffi@goffi.org> |
b54e5a4102da
os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents:
40
diff
changeset
|
7 (c) 2011 Thomas Preud'homme <thomas.preudhomme@celest.fr> |
0 | 8 |
9 This program is free software: you can redistribute it and/or modify | |
10 it under the terms of the GNU General Public License as published by | |
11 the Free Software Foundation, either version 3 of the License, or | |
12 (at your option) any later version. | |
13 | |
14 This program is distributed in the hope that it will be useful, | |
15 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 GNU General Public License for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
20 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 """ | |
22 | |
23 ### logging ### | |
24 import logging | |
25 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
|
26 logging.basicConfig(level=logging.INFO, |
0 | 27 format='%(message)s') |
28 ### | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
29 |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
30 import gettext |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
31 gettext.install('gcp', "i18n", unicode=True) |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
32 |
0 | 33 import sys |
34 import os,os.path | |
17 | 35 from optparse import OptionParser, OptionGroup #To be replaced by argparse ASAP |
1 | 36 import cPickle as pickle |
0 | 37 try: |
38 import gobject | |
39 #DBus | |
40 import dbus, dbus.glib | |
41 import dbus.service | |
42 import dbus.mainloop.glib | |
43 except ImportError,e: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
44 error(_("Error during import")) |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
45 error(_("Please check dependecies:"),e) |
0 | 46 exit(2) |
47 try: | |
48 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed | |
49 pbar_available=True | |
50 except ImportError, e: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
51 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
|
52 info (_('Progress bar deactivated\n--\n')) |
0 | 53 pbar_available=False |
54 | |
55 NAME = "gcp (Goffi's copier)" | |
56 NAME_SHORT = "gcp" | |
31 | 57 VERSION = '0.1.1' |
0 | 58 |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
59 ABOUT = NAME+u" v"+VERSION+u""" (c) Jérôme Poisson (aka Goffi) 2010 |
0 | 60 |
61 --- | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
62 """+NAME+u""" Copyright (C) 2010 Jérôme Poisson |
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
63 """ + _(u"""This program comes with ABSOLUTELY NO WARRANTY; |
0 | 64 This is free software, and you are welcome to redistribute it |
65 under certain conditions. | |
66 --- | |
67 | |
68 This software is an advanced file copier | |
69 Get the latest version at http://www.goffi.org | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
70 """) |
0 | 71 |
72 const_DBUS_INTERFACE = "org.goffi.gcp" | |
3 | 73 const_DBUS_PATH = "/org/goffi/gcp" |
74 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
|
75 const_PRESERVE = set(['mode','ownership','timestamps']) |
15 | 76 const_FILES_DIR = "~/.gcp" |
77 const_JOURNAL_PATH = const_FILES_DIR + "/journal" | |
78 const_SAVED_LIST = const_FILES_DIR + "/saved_list" | |
0 | 79 |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
80 |
0 | 81 |
82 class DbusObject(dbus.service.Object): | |
83 | |
84 def __init__(self, gcp, bus, path): | |
85 self._gcp = gcp | |
86 dbus.service.Object.__init__(self, bus, path) | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
87 debug(_("Init DbusObject...")) |
0 | 88 self.cb={} |
89 | |
90 @dbus.service.method(const_DBUS_INTERFACE, | |
91 in_signature='', out_signature='s') | |
92 def getVersion(self): | |
93 """Get gcp version | |
94 @return: version as string""" | |
95 return VERSION | |
96 | |
97 @dbus.service.method(const_DBUS_INTERFACE, | |
1 | 98 in_signature='ss', out_signature='bs') |
0 | 99 def addArgs(self, source_path, args): |
100 """Add arguments to gcp as if there were entered on its own command line | |
1 | 101 @param source_path: current working dir to use as base for arguments, as given by os.getcwd() |
102 @param args: serialized (wich pickle) list of strings - without command name -, as given by sys.argv[1:]. | |
103 @return: success (boolean) and error message if any (string)""" | |
104 try: | |
105 args = pickle.loads(str(args)) | |
106 except TypeError, pickle.UnpicklingError: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
107 return (False, _("INTERNAL ERROR: invalid arguments")) |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
108 try: |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
109 source_path = pickle.loads(str(source_path)) |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
110 except TypeError, pickle.UnpicklingError: |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
111 return (False, _("INTERNAL ERROR: invalid source_path")) |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
112 return self._gcp.parseArguments(args, source_path) |
0 | 113 |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
114 class Journal(): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
115 def __init__(self, path=const_JOURNAL_PATH): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
116 self.journal_path = os.path.expanduser(path) |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
117 self.journal_fd = open(self.journal_path,'w') #TODO: check and maybe save previous journals |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
118 self.__entry_open = None |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
119 self.failed = [] |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
120 self.partial = [] |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
121 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
122 def __del__(self): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
123 self.journal_fd.flush() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
124 self.journal_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
125 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
126 def startFile(self, source_path): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
127 """Start an entry in the journal""" |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
128 assert not self.__entry_open |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
129 self.__entry_open = source_path |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
130 self.journal_fd.write(source_path+"\n") |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
131 self.journal_fd.flush() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
132 self.success=True |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
133 self.errors=[] |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
134 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
135 def closeFile(self): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
136 """Close the entry in the journal""" |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
137 assert self.__entry_open |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
138 if not self.success: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
139 status = "FAILED" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
140 else: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
141 status = "OK" if not self.errors else "PARTIAL" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
142 self.journal_fd.write("%(status)s: %(errors)s\n" % {'status': status, 'errors': ', '.join(self.errors)}) |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
143 self.journal_fd.flush() |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
144 self.__entry_open = None |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
145 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
146 def copyFailed(self): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
147 """Must be called when something is wrong with the copy itself""" |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
148 assert self.__entry_open |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
149 self.success = False |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
150 self.failed.append(self.__entry_open) |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
151 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
152 def error(self, name): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
153 """Something went wrong""" |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
154 assert self.__entry_open |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
155 self.errors.append(name) |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
156 self.partial.append(self.__entry_open) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
157 |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
158 def showErrors(self): |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
159 """Show which files were not successfully copied""" |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
160 failed = set(self.failed) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
161 partial = set(self.partial) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
162 for entry in failed: |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
163 partial.discard(entry) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
164 |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
165 if failed: |
26
d0c00ef2b917
removed color from copy failed error message
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
166 error(_("/!\\ THE FOLLOWING FILES WERE *NOT* SUCCESSFULY COPIED:")) |
d0c00ef2b917
removed color from copy failed error message
Goffi <goffi@goffi.org>
parents:
25
diff
changeset
|
167 #TODO: use logging capability to print all error message in red |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
168 for entry in failed: |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
169 info("\t- %s" % entry) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
170 info ('--\n') |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
171 if partial: |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
172 warning(_("The following files were copied, but some errors happened:")) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
173 for entry in partial: |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
174 info("\t- %s" % entry) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
175 info ('--\n') |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
176 |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
177 if failed or partial: |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
178 info(_("Please check journal: %s") % self.journal_path) |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
179 |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
180 |
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
181 |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
182 |
0 | 183 class GCP(): |
184 | |
185 def __init__(self): | |
15 | 186 files_dir = os.path.expanduser(const_FILES_DIR) |
187 if not os.path.exists(files_dir): | |
188 os.makedirs(files_dir) | |
0 | 189 try: |
190 sessions_bus = dbus.SessionBus() | |
191 db_object = sessions_bus.get_object(const_DBUS_INTERFACE, | |
192 const_DBUS_PATH) | |
193 self.gcp_main = dbus.Interface(db_object, | |
194 dbus_interface=const_DBUS_INTERFACE) | |
195 self._main_instance = False | |
196 | |
197 except dbus.exceptions.DBusException,e: | |
198 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown': | |
199 self.launchDbusMainInstance() | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
200 debug (_("gcp launched")) |
0 | 201 self._main_instance = True |
3 | 202 self.buffer_size = const_BUFF_SIZE |
12 | 203 self.__launched = False #True when journal is initialised and copy is started |
0 | 204 else: |
205 raise e | |
206 | |
207 def launchDbusMainInstance(self): | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
208 debug (_("Init DBus...")) |
0 | 209 session_bus = dbus.SessionBus() |
210 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus) | |
211 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH) | |
212 | |
213 self.copy_list = [] | |
214 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
|
215 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
|
216 self.bytes_copied = 0 |
0 | 217 |
218 def getFsType(self, path): | |
6 | 219 fs = '' |
220 last_mount_point = '' | |
0 | 221 for mount in self.mounts: |
6 | 222 if path.startswith(mount) and len(mount)>=len(last_mount_point): |
0 | 223 fs = self.mounts[mount] |
6 | 224 last_mount_point = mount |
0 | 225 return fs |
226 | |
227 def __getMountPoints(self): | |
228 """Parse /proc/mounts to get currently mounted devices""" | |
229 #TODO: reparse when a new device is added/a device is removed | |
1 | 230 #(check freedesktop mounting signals) |
0 | 231 ret = {} |
232 try: | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
233 with open("/proc/mounts",'rb') as mounts: |
0 | 234 for line in mounts.readlines(): |
235 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ') | |
236 ret[fs_file] = fs_vfstype | |
237 except: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
238 error (_("Can't read mounts table")) |
0 | 239 return ret |
240 | |
3 | 241 def __appendToList(self, path, dest_path, options): |
0 | 242 """Add a file to the copy list |
243 @param path: absolute path of file | |
244 @param options: options as return by optparse""" | |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
245 debug (_("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)") % {"path":path.decode('utf-8','replace'), |
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
246 "dest_path":dest_path.decode('utf-8','replace'), |
6 | 247 "fs_type":self.getFsType(dest_path)} ) |
1 | 248 try: |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
249 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
|
250 self.copy_list.insert(0,(path, dest_path, options)) |
1 | 251 except OSError,e: |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
252 error(_("Can't copy %(path)s: %(exception)s") % {'path':path.decode('utf-8','replace'), 'exception':e.strerror}) |
0 | 253 |
1 | 254 |
3 | 255 def __appendDirToList(self, dirpath, dest_path, options): |
0 | 256 """Add recursively directory to the copy list |
257 @param path: absolute path of dir | |
258 @param options: options as return by optparse""" | |
3 | 259 #We first check that the dest path exists, and create it if needed |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
260 dest_path = self.__filename_fix(dest_path, options, no_journal=True) |
3 | 261 if not os.path.exists(dest_path): |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
262 debug ("Creating directory %s" % dest_path) |
3 | 263 os.makedirs(dest_path) #TODO: check permissions |
264 #TODO: check that dest_path is an accessible dir, | |
265 # and skip file/write error in log if needed | |
0 | 266 try: |
267 for filename in os.listdir(dirpath): | |
268 filepath = os.path.join(dirpath,filename) | |
46 | 269 if os.path.islink(filepath) and not options.dereference: |
270 debug ("Skippink symbolic dir: %s" % filepath.decode('utf-8','replace')) | |
271 continue | |
0 | 272 if os.path.isdir(filepath): |
3 | 273 full_dest_path = os.path.join(dest_path,filename) |
274 self.__appendDirToList(filepath, full_dest_path, options) | |
0 | 275 else: |
3 | 276 self.__appendToList(filepath, dest_path, options) |
0 | 277 except OSError,e: |
45
97384a79043d
fixed crash when a source dir can't be accessed
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
278 try: |
97384a79043d
fixed crash when a source dir can't be accessed
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
279 error(_("Can't append %(path)s to copy list: %(exception)s") % {'path':filepath.decode('utf-8','replace'), |
97384a79043d
fixed crash when a source dir can't be accessed
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
280 'exception':e.strerror}) |
97384a79043d
fixed crash when a source dir can't be accessed
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
281 except NameError: |
97384a79043d
fixed crash when a source dir can't be accessed
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
282 #We can't list the dir |
97384a79043d
fixed crash when a source dir can't be accessed
Goffi <goffi@goffi.org>
parents:
41
diff
changeset
|
283 error(_("Can't access %(dirpath)s: %(exception)s") % {'dirpath':dirpath.decode('utf-8','replace'), |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
284 'exception':e.strerror}) |
0 | 285 |
286 def __checkArgs(self, options, source_path, args): | |
287 """Check thats args are files, and add them to copy list""" | |
1 | 288 assert(len (args)>=2) |
289 try: | |
3 | 290 dest_path = os.path.normpath(os.path.join(os.path.expanduser(source_path), args.pop())) |
1 | 291 except OSError,e: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
292 error (_("Invalid dest_path: %s"),e) |
1 | 293 |
0 | 294 for path in args: |
1 | 295 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path)) |
0 | 296 if not os.path.exists(abspath): |
11 | 297 warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath.decode('utf-8','replace')) |
0 | 298 else: |
299 if os.path.isdir(abspath): | |
3 | 300 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path)) |
0 | 301 if not options.recursive: |
15 | 302 warning (_('omitting directory "%s"') % abspath.decode('utf-8','replace')) |
0 | 303 else: |
3 | 304 self.__appendDirToList(abspath, full_dest_path, options) |
0 | 305 else: |
3 | 306 self.__appendToList(abspath, dest_path, options) |
0 | 307 |
3 | 308 def __copyNextFile(self): |
309 """Take the last file in the list, and launch the copy using glib io_watch event | |
310 @return: True a file was added, False else""" | |
311 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
|
312 source_file, dest_path, options = self.copy_list.pop() |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
313 self.journal.startFile(source_file) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
314 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
|
315 filename = os.path.basename(source_file) |
3 | 316 assert(filename) |
28
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
317 dest_file = self.__filename_fix(options.dest_file,options) if options.dest_file else 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
|
318 if os.path.exists(dest_file) and not options.force: |
16
b03bd83c1661
Fixed yet another utf-8 encoding issue + added a space on progress bar before percentage
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
319 warning (_("File [%s] already exists, skipping it !") % dest_file.decode('utf-8','replace')) |
27
95f16ff36308
fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
320 self.journal.copyFailed() |
95f16ff36308
fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
321 self.journal.error("already exists") |
95f16ff36308
fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
322 self.journal.closeFile() |
95f16ff36308
fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
323 source_fd.close() |
3 | 324 return True |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
325 try: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
326 dest_fd = open(dest_file, 'wb') |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
327 except: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
328 self.journal.copyFailed() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
329 self.journal.error("can't open dest") |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
330 self.journal.closeFile() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
331 source_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
332 return True |
3 | 333 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
334 gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile, |
13 | 335 (dest_fd, options), priority=gobject.PRIORITY_DEFAULT) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
336 if not self.progress: |
27
95f16ff36308
fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents:
26
diff
changeset
|
337 info(_("COPYING %(source)s ==> %(dest)s") % {"source":source_file.decode('utf-8','replace'), |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
338 "dest":dest_file.decode('utf-8','replace')}) |
3 | 339 return True |
340 else: | |
341 #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
|
342 if self.progress: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
343 self.__pbar_finish() |
25
f82731c70956
Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents:
24
diff
changeset
|
344 self.journal.showErrors() |
3 | 345 self.loop.quit() |
346 | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
347 def __copyFailed(self, reason, source_fd, dest_fd): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
348 """Write the failure in the journal and close files descriptors""" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
349 self.journal.copyFailed() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
350 self.journal.error(reason) |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
351 self.journal.closeFile() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
352 source_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
353 dest_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
354 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
355 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
356 |
3 | 357 def _copyFile(self, source_fd, condition, data): |
358 """Actually copy the file, callback used with io_add_watch | |
359 @param source_fd: file descriptor of the file to copy | |
360 @param condition: condition which launched the callback (glib.IO_IN) | |
361 @param data: tuple with (destination file descriptor, copying options)""" | |
14 | 362 try: |
363 dest_fd,options = data | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
364 |
14 | 365 try: |
366 buff = source_fd.read(self.buffer_size) | |
367 except KeyboardInterrupt: | |
368 raise KeyboardInterrupt | |
369 except: | |
370 self.__copyFailed("can't read source", source_fd, dest_fd) | |
371 return False | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
372 |
14 | 373 try: |
374 dest_fd.write(buff) | |
375 except KeyboardInterrupt: | |
376 raise KeyboardInterrupt | |
377 except: | |
378 self.__copyFailed("can't write to dest", source_fd, dest_fd) | |
379 return False | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
380 |
14 | 381 self.bytes_copied += len(buff) |
382 if self.progress: | |
383 self._pbar_update() | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
384 |
14 | 385 if len(buff) != self.buffer_size: |
386 source_fd.close() | |
387 dest_fd.close() | |
388 self.__post_copy(source_fd.name, dest_fd.name, options) | |
389 self.journal.closeFile() | |
390 return False | |
391 return True | |
392 except KeyboardInterrupt: | |
393 self._userInterruption() | |
3 | 394 |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
395 def __filename_fix(self, filename, options, no_journal=False): |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
396 """Fix filenames incompatibilities/mistake according to options |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
397 @param filename: full path to the file |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
398 @param options: options as parsed on command line |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
399 @param no_journal: don't write any entry in journal |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
400 @return: fixed filename""" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
401 fixed_filename = filename |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
402 |
6 | 403 if self.getFsType(filename) == 'vfat' and options.fs_fix: |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
404 fixed_filename = filename.replace('\\','_')\ |
6 | 405 .replace(':',';')\ |
406 .replace('*','+')\ | |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
407 .replace('?','_')\ |
6 | 408 .replace('"','\'')\ |
409 .replace('<','[')\ | |
410 .replace('>',']')\ | |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
411 .replace('|','!')\ |
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
412 .rstrip() #XXX: suffixed spaces cause issues (must check FAT doc for why) |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
413 |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
414 if not fixed_filename: |
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
415 fixed_filename = '_' |
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
416 if fixed_filename != filename and not no_journal: |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
417 self.journal.error('filename fixed') |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
418 return fixed_filename |
6 | 419 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
420 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
|
421 """Do post copy traitement (mainly managing --preserve option)""" |
41
b54e5a4102da
os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents:
40
diff
changeset
|
422 st_file = os.stat(source_file) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
423 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
|
424 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
425 if preserve == 'mode': |
41
b54e5a4102da
os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents:
40
diff
changeset
|
426 os.chmod(dest_file, st_file.st_mode) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
427 elif preserve == 'ownership': |
41
b54e5a4102da
os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents:
40
diff
changeset
|
428 os.chown(dest_file, st_file.st_uid, st_file.st_gid) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
429 elif preserve == 'timestamps': |
41
b54e5a4102da
os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents:
40
diff
changeset
|
430 os.utime(dest_file, (st_file.st_atime, st_file.st_mtime)) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
431 except OSError,e: |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
432 self.journal.error("preserve-"+preserve) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
433 |
10
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
434 def __get_string_size(self, size): |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
435 """Return a nice string representation of a size""" |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
436 |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
437 if size>=2**50: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
438 return _("%.2f PiB") % (float(size)/2**50) |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
439 elif size>=2**40: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
440 return _("%.2f TiB") % (float(size)/2**40) |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
441 elif size>=2**30: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
442 return _("%.2f GiB") % (float(size)/2**30) |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
443 elif size>=2**20: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
444 return _("%.2f MiB") % (float(size)/2**20) |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
445 elif size>=2**10: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
446 return _("%.2f KiB") % (float(size)/2**10) |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
447 else: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
448 return _("%i B") % size |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
449 |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
450 def _pbar_update(self): |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
451 """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
|
452 assert(self.progress) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
453 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
454 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
|
455 self.pbar.maxval = self.bytes_total |
10
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
456 self.pbar.widgets[0] = _("Copying %s") % self.__get_string_size(self.bytes_total) |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
457 except AttributeError: |
6 | 458 if not self.bytes_total: |
459 #No progress bar if the files have a null size | |
460 return | |
16
b03bd83c1661
Fixed yet another utf-8 encoding issue + added a space on progress bar before percentage
Goffi <goffi@goffi.org>
parents:
15
diff
changeset
|
461 self.pbar = ProgressBar(self.bytes_total,[_("Copying %s") % self.__get_string_size(self.bytes_total)," ",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
|
462 self.pbar.start() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
463 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
|
464 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
465 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
|
466 """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
|
467 assert(self.progress) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
468 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
469 self.pbar.finish() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
470 except AttributeError: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
471 pass |
0 | 472 |
17 | 473 def __sourcesSaving(self,options,args): |
474 """Manage saving/loading/deleting etc of sources files | |
475 @param options: options as parsed from command line | |
476 @param args: args parsed from command line""" | |
477 if options.sources_save or options.sources_load\ | |
478 or options.sources_list or options.sources_full_list\ | |
18 | 479 or options.sources_del or options.sources_replace: |
17 | 480 try: |
481 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd: | |
482 saved_files = pickle.load(saved_fd) | |
483 except: | |
484 saved_files={} | |
485 | |
486 if options.sources_del: | |
487 if not saved_files.has_key(options.sources_del): | |
488 error(_("No saved sources with this name, check existing names with --sources-list")) | |
489 else: | |
490 del saved_files[options.sources_del] | |
491 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd: | |
492 pickle.dump(saved_files,saved_fd) | |
493 if not args: | |
494 exit(0) | |
495 | |
496 | |
497 if options.sources_list or options.sources_full_list: | |
498 info(_('Saved sources:')) | |
499 sources = saved_files.keys() | |
500 sources.sort() | |
501 for source in sources: | |
502 info("\t[%s]" % source) | |
503 if options.sources_full_list: | |
504 for filename in saved_files[source]: | |
505 info("\t\t%s" % filename) | |
506 info("---\n") | |
507 if not args: | |
508 exit(0) | |
509 | |
510 if options.sources_save or options.sources_replace: | |
511 if saved_files.has_key(options.sources_save) and not options.sources_replace: | |
512 error(_("There is already a saved sources with this name, skipping --sources-save")) | |
513 else: | |
514 if len(args)>1: | |
515 saved_files[options.sources_save] = map(os.path.abspath,args[:-1]) | |
516 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd: | |
517 pickle.dump(saved_files,saved_fd) | |
518 | |
519 if options.sources_load: | |
520 if not saved_files.has_key(options.sources_load): | |
521 error(_("No saved sources with this name, check existing names with --sources-list")) | |
522 else: | |
523 saved_args = saved_files[options.sources_load] | |
524 saved_args.reverse() | |
525 for arg in saved_args: | |
526 args.insert(0,arg) | |
527 | |
0 | 528 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()): |
1 | 529 """Parse arguments and add files to queue |
530 @param full_args: list of arguments strings (without program name) | |
531 @param source_path: path from where the arguments come, ad given by os.getcwd() | |
532 @return: a tuple (boolean, message) where the boolean is the success of the arguments | |
533 validation, and message is the error message to print when necessary""" | |
0 | 534 _usage=""" |
28
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
535 %prog [options] FILE DEST |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
536 %prog [options] FILE1 [FILE2 ...] DEST-DIR |
0 | 537 |
538 %prog --help for options list | |
539 """ | |
1 | 540 for idx in range(len(full_args)): |
541 if isinstance(full_args[idx], unicode): | |
542 #We don't want unicode as some filenames can be invalid unicode | |
543 full_args[idx] = full_args[idx].encode('utf-8') | |
544 | |
0 | 545 parser = OptionParser(usage=_usage,version=ABOUT) |
546 | |
547 parser.add_option("-r", "--recursive", action="store_true", default=False, | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
548 help=_("copy directories recursively")) |
0 | 549 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
550 parser.add_option("-f", "--force", action="store_true", default=False, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
551 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
|
552 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
553 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
554 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
|
555 |
46 | 556 parser.add_option("-L", "--dereference", action="store_true", default=False, |
557 help=_("always follow symbolic links in sources")) | |
558 | |
559 parser.add_option("-P", "--no-dereference", action="store_false", dest='dereference', | |
560 help=_("never follow symbolic links in sources")) | |
561 | |
21
8603fcb8615a
--no-unicode-fix commented, and README update
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
562 #parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True, |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
563 # help=_("don't fix name encoding errors")) #TODO |
6 | 564 |
565 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True, | |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
566 help=_("don't fix filesystem name incompatibily")) |
6 | 567 |
3 | 568 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
|
569 help=_("deactivate progress bar")) |
3 | 570 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
571 parser.add_option("-v", "--verbose", action="store_true", default=False, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
572 help=_("Show what is currently done")) |
17 | 573 |
574 group_saving = OptionGroup(parser, "sources saving") | |
575 | |
576 group_saving.add_option("--sources-save", action="store", | |
577 help=_("Save source arguments")) | |
578 | |
579 group_saving.add_option("--sources-replace", action="store", | |
580 help=_("Save source arguments and replace memory if it already exists")) | |
581 | |
582 group_saving.add_option("--sources-load", action="store", | |
583 help=_("Load source arguments")) | |
584 | |
585 group_saving.add_option("--sources-del", action="store", | |
586 help=_("delete saved sources")) | |
587 | |
588 group_saving.add_option("--sources-list", action="store_true", default=False, | |
589 help=_("List names of saved sources")) | |
590 | |
591 group_saving.add_option("--sources-full-list", action="store_true", default=False, | |
592 help=_("List names of saved sources and files in it")) | |
593 | |
594 parser.add_option_group(group_saving) | |
595 | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
596 |
0 | 597 (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
|
598 #options check |
3 | 599 if options.progress and not pbar_available: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
600 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
|
601 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
|
602 else: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
603 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
|
604 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
605 if options.verbose: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
606 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
|
607 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
608 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
|
609 if not preserve.issubset(const_PRESERVE): |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
610 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
|
611 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
|
612 error('- %s' % value) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
613 exit(2) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
614 else: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
615 options.preserve = preserve |
15 | 616 |
17 | 617 self.__sourcesSaving(options, args) |
28
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
618 |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
619 if len(args) == 2: |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
620 dest_path = os.path.abspath(os.path.expanduser(args[1])) |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
621 if not os.path.exists(dest_path) or os.path.isfile(dest_path): |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
622 options.dest_file = dest_path |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
623 args[1] = os.path.dirname(dest_path) |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
624 else: |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
625 options.dest_file = None |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
626 else: |
548dfc861531
Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents:
27
diff
changeset
|
627 options.dest_file = None |
0 | 628 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
629 #if there is an other instance of gcp, we send options to it |
0 | 630 if not self._main_instance: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
631 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT) |
1 | 632 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames |
633 # can have invalid unicode. | |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
634 return self.gcp_main.addArgs(pickle.dumps(os.getcwd()),pickle.dumps(full_args)) |
0 | 635 else: |
1 | 636 if len(args) < 2: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
637 _error_msg = _("Wrong number of arguments") |
1 | 638 return (False, _error_msg) |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
639 debug(_("adding args to gcp: %s") % str(args).decode('utf-8','replace')) |
0 | 640 self.__checkArgs(options, source_path, args) |
12 | 641 if not self.__launched: |
642 self.journal = Journal() | |
643 gobject.idle_add(self.__copyNextFile) | |
644 self.__launched = True | |
1 | 645 return (True,'') |
0 | 646 |
14 | 647 def _userInterruption(self): |
648 info(_("User interruption: good bye")) | |
649 exit(1) | |
650 | |
0 | 651 def go(self): |
1 | 652 """Launch main loop""" |
0 | 653 self.loop = gobject.MainLoop() |
654 try: | |
655 self.loop.run() | |
656 except KeyboardInterrupt: | |
14 | 657 self._userInterruption() |
0 | 658 |
659 | |
660 if __name__ == "__main__": | |
661 gcp = GCP() | |
1 | 662 success,message = gcp.parseArguments() |
663 if not success: | |
664 error(message) | |
665 exit(1) | |
0 | 666 if gcp._main_instance: |
667 gcp.go() | |
668 |