Mercurial > gcp
annotate gcp @ 24:154fe67bae72
Journal double-entry check + unicode source_path fix + typo
- journal now fail assertion when trying to write log whitout entry created, or when trying to create 2 entries without closing the first one
- unicode source_path are now fixed: there was a decoding error when trying to add an unicode source_path from a second instance of gcp
- small typo in --help page
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 30 Sep 2010 15:49:16 +0800 |
parents | 8603fcb8615a |
children | f82731c70956 |
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 | |
17 | 34 from optparse import OptionParser, OptionGroup #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']) |
15 | 75 const_FILES_DIR = "~/.gcp" |
76 const_JOURNAL_PATH = const_FILES_DIR + "/journal" | |
77 const_SAVED_LIST = const_FILES_DIR + "/saved_list" | |
0 | 78 |
79 | |
80 class DbusObject(dbus.service.Object): | |
81 | |
82 def __init__(self, gcp, bus, path): | |
83 self._gcp = gcp | |
84 dbus.service.Object.__init__(self, bus, path) | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
85 debug(_("Init DbusObject...")) |
0 | 86 self.cb={} |
87 | |
88 @dbus.service.method(const_DBUS_INTERFACE, | |
89 in_signature='', out_signature='s') | |
90 def getVersion(self): | |
91 """Get gcp version | |
92 @return: version as string""" | |
93 return VERSION | |
94 | |
95 @dbus.service.method(const_DBUS_INTERFACE, | |
1 | 96 in_signature='ss', out_signature='bs') |
0 | 97 def addArgs(self, source_path, args): |
98 """Add arguments to gcp as if there were entered on its own command line | |
1 | 99 @param source_path: current working dir to use as base for arguments, as given by os.getcwd() |
100 @param args: serialized (wich pickle) list of strings - without command name -, as given by sys.argv[1:]. | |
101 @return: success (boolean) and error message if any (string)""" | |
102 try: | |
103 args = pickle.loads(str(args)) | |
104 except TypeError, pickle.UnpicklingError: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
105 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
|
106 try: |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
107 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
|
108 except TypeError, pickle.UnpicklingError: |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
109 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
|
110 return self._gcp.parseArguments(args, source_path) |
0 | 111 |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
112 class Journal(): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
113 def __init__(self, path=const_JOURNAL_PATH): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
114 self.journal_path = os.path.expanduser(path) |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
115 self.journal_fd = open(self.journal_path,'w') #TODO: check and maybe save previous journals |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
116 self.__entry_open = False |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
117 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
118 def __del__(self): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
119 self.journal_fd.flush() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
120 self.journal_fd.close() |
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 startFile(self, source_path): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
123 """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
|
124 assert not self.__entry_open |
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
125 self.__entry_open = True |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
126 self.journal_fd.write(source_path+"\n") |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
127 self.journal_fd.flush() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
128 self.success=True |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
129 self.errors=[] |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
130 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
131 def closeFile(self): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
132 """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
|
133 assert self.__entry_open |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
134 if not self.success: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
135 status = "FAILED" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
136 else: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
137 status = "OK" if not self.errors else "PARTIAL" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
138 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
|
139 self.journal_fd.flush() |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
140 self.__entry_open = False |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
141 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
142 def copyFailed(self): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
143 """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
|
144 assert self.__entry_open |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
145 self.success = False |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
146 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
147 def error(self, name): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
148 """Something went wrong""" |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
149 assert self.__entry_open |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
150 self.errors.append(name) |
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 |
0 | 153 class GCP(): |
154 | |
155 def __init__(self): | |
15 | 156 files_dir = os.path.expanduser(const_FILES_DIR) |
157 if not os.path.exists(files_dir): | |
158 os.makedirs(files_dir) | |
0 | 159 try: |
160 sessions_bus = dbus.SessionBus() | |
161 db_object = sessions_bus.get_object(const_DBUS_INTERFACE, | |
162 const_DBUS_PATH) | |
163 self.gcp_main = dbus.Interface(db_object, | |
164 dbus_interface=const_DBUS_INTERFACE) | |
165 self._main_instance = False | |
166 | |
167 except dbus.exceptions.DBusException,e: | |
168 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown': | |
169 self.launchDbusMainInstance() | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
170 debug (_("gcp launched")) |
0 | 171 self._main_instance = True |
3 | 172 self.buffer_size = const_BUFF_SIZE |
12 | 173 self.__launched = False #True when journal is initialised and copy is started |
0 | 174 else: |
175 raise e | |
176 | |
177 def launchDbusMainInstance(self): | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
178 debug (_("Init DBus...")) |
0 | 179 session_bus = dbus.SessionBus() |
180 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus) | |
181 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH) | |
182 | |
183 self.copy_list = [] | |
184 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
|
185 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
|
186 self.bytes_copied = 0 |
0 | 187 |
188 def getFsType(self, path): | |
6 | 189 fs = '' |
190 last_mount_point = '' | |
0 | 191 for mount in self.mounts: |
6 | 192 if path.startswith(mount) and len(mount)>=len(last_mount_point): |
0 | 193 fs = self.mounts[mount] |
6 | 194 last_mount_point = mount |
0 | 195 return fs |
196 | |
197 def __getMountPoints(self): | |
198 """Parse /proc/mounts to get currently mounted devices""" | |
199 #TODO: reparse when a new device is added/a device is removed | |
1 | 200 #(check freedesktop mounting signals) |
0 | 201 ret = {} |
202 try: | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
203 with open("/proc/mounts",'rb') as mounts: |
0 | 204 for line in mounts.readlines(): |
205 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ') | |
206 ret[fs_file] = fs_vfstype | |
207 except: | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
208 error (_("Can't read mounts table")) |
0 | 209 return ret |
210 | |
3 | 211 def __appendToList(self, path, dest_path, options): |
0 | 212 """Add a file to the copy list |
213 @param path: absolute path of file | |
214 @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
|
215 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
|
216 "dest_path":dest_path.decode('utf-8','replace'), |
6 | 217 "fs_type":self.getFsType(dest_path)} ) |
1 | 218 try: |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
219 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
|
220 self.copy_list.insert(0,(path, dest_path, options)) |
1 | 221 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
|
222 error(_("Can't copy %(path)s: %(exception)s") % {'path':path.decode('utf-8','replace'), 'exception':e.strerror}) |
0 | 223 |
1 | 224 |
3 | 225 def __appendDirToList(self, dirpath, dest_path, options): |
0 | 226 """Add recursively directory to the copy list |
227 @param path: absolute path of dir | |
228 @param options: options as return by optparse""" | |
3 | 229 #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
|
230 dest_path = self.__filename_fix(dest_path, options, no_journal=True) |
3 | 231 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
|
232 debug ("Creating directory %s" % dest_path) |
3 | 233 os.makedirs(dest_path) #TODO: check permissions |
234 #TODO: check that dest_path is an accessible dir, | |
235 # and skip file/write error in log if needed | |
0 | 236 try: |
237 for filename in os.listdir(dirpath): | |
238 filepath = os.path.join(dirpath,filename) | |
239 if os.path.isdir(filepath): | |
3 | 240 full_dest_path = os.path.join(dest_path,filename) |
241 self.__appendDirToList(filepath, full_dest_path, options) | |
0 | 242 else: |
3 | 243 self.__appendToList(filepath, dest_path, options) |
0 | 244 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
|
245 error(_("Can't append %(path)s to copy list: %(exception)s") % {'path':filepath.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 'exception':e.strerror}) |
0 | 247 |
248 def __checkArgs(self, options, source_path, args): | |
249 """Check thats args are files, and add them to copy list""" | |
1 | 250 assert(len (args)>=2) |
251 try: | |
3 | 252 dest_path = os.path.normpath(os.path.join(os.path.expanduser(source_path), args.pop())) |
1 | 253 except OSError,e: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
254 error (_("Invalid dest_path: %s"),e) |
1 | 255 |
0 | 256 for path in args: |
1 | 257 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path)) |
0 | 258 if not os.path.exists(abspath): |
11 | 259 warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath.decode('utf-8','replace')) |
0 | 260 else: |
261 if os.path.isdir(abspath): | |
3 | 262 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path)) |
0 | 263 if not options.recursive: |
15 | 264 warning (_('omitting directory "%s"') % abspath.decode('utf-8','replace')) |
0 | 265 else: |
3 | 266 self.__appendDirToList(abspath, full_dest_path, options) |
0 | 267 else: |
3 | 268 self.__appendToList(abspath, dest_path, options) |
0 | 269 |
3 | 270 def __copyNextFile(self): |
271 """Take the last file in the list, and launch the copy using glib io_watch event | |
272 @return: True a file was added, False else""" | |
273 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
|
274 source_file, dest_path, options = self.copy_list.pop() |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
275 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
|
276 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
|
277 filename = os.path.basename(source_file) |
3 | 278 assert(filename) |
6 | 279 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
|
280 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
|
281 warning (_("File [%s] already exists, skipping it !") % dest_file.decode('utf-8','replace')) |
3 | 282 return True |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
283 try: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
284 dest_fd = open(dest_file, 'wb') |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
285 except: |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
286 self.journal.copyFailed() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
287 self.journal.error("can't open dest") |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
288 self.journal.closeFile() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
289 source_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
290 return True |
3 | 291 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
292 gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile, |
13 | 293 (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
|
294 if not self.progress: |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
295 info(_("COPYING %(source)s ==> %(dest)s") % {"source":source_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
|
296 "dest":dest_file.decode('utf-8','replace')}) |
3 | 297 return True |
298 else: | |
299 #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
|
300 if self.progress: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
301 self.__pbar_finish() |
3 | 302 self.loop.quit() |
303 | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
304 def __copyFailed(self, reason, source_fd, dest_fd): |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
305 """Write the failure in the journal and close files descriptors""" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
306 self.journal.copyFailed() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
307 self.journal.error(reason) |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
308 self.journal.closeFile() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
309 source_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
310 dest_fd.close() |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
311 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
312 |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
313 |
3 | 314 def _copyFile(self, source_fd, condition, data): |
315 """Actually copy the file, callback used with io_add_watch | |
316 @param source_fd: file descriptor of the file to copy | |
317 @param condition: condition which launched the callback (glib.IO_IN) | |
318 @param data: tuple with (destination file descriptor, copying options)""" | |
14 | 319 try: |
320 dest_fd,options = data | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
321 |
14 | 322 try: |
323 buff = source_fd.read(self.buffer_size) | |
324 except KeyboardInterrupt: | |
325 raise KeyboardInterrupt | |
326 except: | |
327 self.__copyFailed("can't read source", source_fd, dest_fd) | |
328 return False | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
329 |
14 | 330 try: |
331 dest_fd.write(buff) | |
332 except KeyboardInterrupt: | |
333 raise KeyboardInterrupt | |
334 except: | |
335 self.__copyFailed("can't write to dest", source_fd, dest_fd) | |
336 return False | |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
337 |
14 | 338 self.bytes_copied += len(buff) |
339 if self.progress: | |
340 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
|
341 |
14 | 342 if len(buff) != self.buffer_size: |
343 source_fd.close() | |
344 dest_fd.close() | |
345 self.__post_copy(source_fd.name, dest_fd.name, options) | |
346 self.journal.closeFile() | |
347 return False | |
348 return True | |
349 except KeyboardInterrupt: | |
350 self._userInterruption() | |
3 | 351 |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
352 def __filename_fix(self, filename, options, no_journal=False): |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
353 """Fix filenames incompatibilities/mistake according to options |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
354 @param filename: full path to the file |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
355 @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
|
356 @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
|
357 @return: fixed filename""" |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
358 fixed_filename = filename |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
359 |
6 | 360 if self.getFsType(filename) == 'vfat' and options.fs_fix: |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
361 fixed_filename = filename.replace('\\','_')\ |
6 | 362 .replace(':',';')\ |
363 .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
|
364 .replace('?','_')\ |
6 | 365 .replace('"','\'')\ |
366 .replace('<','[')\ | |
367 .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
|
368 .replace('|','!')\ |
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
369 .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
|
370 |
9
599b84e4ff01
- added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents:
8
diff
changeset
|
371 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
|
372 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
|
373 if fixed_filename != filename and not no_journal: |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
374 self.journal.error('filename fixed') |
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
375 return fixed_filename |
6 | 376 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
377 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
|
378 """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
|
379 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
|
380 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
|
381 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
382 if preserve == 'mode': |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
383 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
|
384 elif preserve == 'ownership': |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
385 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
|
386 elif preserve == 'timestamps': |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
387 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
|
388 except OSError,e: |
7
a110d31482f7
Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents:
6
diff
changeset
|
389 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
|
390 |
10
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
391 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
|
392 """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
|
393 |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
394 if size>=2**50: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
395 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
|
396 elif size>=2**40: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
397 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
|
398 elif size>=2**30: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
399 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
|
400 elif size>=2**20: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
401 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
|
402 elif size>=2**10: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
403 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
|
404 else: |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
405 return _("%i B") % size |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
406 |
00b27fce4677
- Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents:
9
diff
changeset
|
407 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
|
408 """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
|
409 assert(self.progress) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
410 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
411 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
|
412 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
|
413 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
|
414 except AttributeError: |
6 | 415 if not self.bytes_total: |
416 #No progress bar if the files have a null size | |
417 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
|
418 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
|
419 self.pbar.start() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
420 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
|
421 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
422 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
|
423 """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
|
424 assert(self.progress) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
425 try: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
426 self.pbar.finish() |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
427 except AttributeError: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
428 pass |
0 | 429 |
17 | 430 def __sourcesSaving(self,options,args): |
431 """Manage saving/loading/deleting etc of sources files | |
432 @param options: options as parsed from command line | |
433 @param args: args parsed from command line""" | |
434 if options.sources_save or options.sources_load\ | |
435 or options.sources_list or options.sources_full_list\ | |
18 | 436 or options.sources_del or options.sources_replace: |
17 | 437 try: |
438 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd: | |
439 saved_files = pickle.load(saved_fd) | |
440 except: | |
441 saved_files={} | |
442 | |
443 if options.sources_del: | |
444 if not saved_files.has_key(options.sources_del): | |
445 error(_("No saved sources with this name, check existing names with --sources-list")) | |
446 else: | |
447 del saved_files[options.sources_del] | |
448 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd: | |
449 pickle.dump(saved_files,saved_fd) | |
450 if not args: | |
451 exit(0) | |
452 | |
453 | |
454 if options.sources_list or options.sources_full_list: | |
455 info(_('Saved sources:')) | |
456 sources = saved_files.keys() | |
457 sources.sort() | |
458 for source in sources: | |
459 info("\t[%s]" % source) | |
460 if options.sources_full_list: | |
461 for filename in saved_files[source]: | |
462 info("\t\t%s" % filename) | |
463 info("---\n") | |
464 if not args: | |
465 exit(0) | |
466 | |
467 if options.sources_save or options.sources_replace: | |
468 if saved_files.has_key(options.sources_save) and not options.sources_replace: | |
469 error(_("There is already a saved sources with this name, skipping --sources-save")) | |
470 else: | |
471 if len(args)>1: | |
472 saved_files[options.sources_save] = map(os.path.abspath,args[:-1]) | |
473 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd: | |
474 pickle.dump(saved_files,saved_fd) | |
475 | |
476 if options.sources_load: | |
477 if not saved_files.has_key(options.sources_load): | |
478 error(_("No saved sources with this name, check existing names with --sources-list")) | |
479 else: | |
480 saved_args = saved_files[options.sources_load] | |
481 saved_args.reverse() | |
482 for arg in saved_args: | |
483 args.insert(0,arg) | |
484 | |
0 | 485 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()): |
1 | 486 """Parse arguments and add files to queue |
487 @param full_args: list of arguments strings (without program name) | |
488 @param source_path: path from where the arguments come, ad given by os.getcwd() | |
489 @return: a tuple (boolean, message) where the boolean is the success of the arguments | |
490 validation, and message is the error message to print when necessary""" | |
0 | 491 _usage=""" |
492 %prog [options] FILE1 [FILE2 ...] DEST | |
493 | |
494 %prog --help for options list | |
495 """ | |
1 | 496 for idx in range(len(full_args)): |
497 if isinstance(full_args[idx], unicode): | |
498 #We don't want unicode as some filenames can be invalid unicode | |
499 full_args[idx] = full_args[idx].encode('utf-8') | |
500 | |
0 | 501 parser = OptionParser(usage=_usage,version=ABOUT) |
502 | |
503 parser.add_option("-r", "--recursive", action="store_true", default=False, | |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
504 help=_("copy directories recursively")) |
0 | 505 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
506 parser.add_option("-f", "--force", action="store_true", default=False, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
507 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
|
508 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
509 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
510 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
|
511 |
21
8603fcb8615a
--no-unicode-fix commented, and README update
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
512 #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
|
513 # help=_("don't fix name encoding errors")) #TODO |
6 | 514 |
515 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
|
516 help=_("don't fix filesystem name incompatibily")) |
6 | 517 |
3 | 518 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
|
519 help=_("deactivate progress bar")) |
3 | 520 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
521 parser.add_option("-v", "--verbose", action="store_true", default=False, |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
522 help=_("Show what is currently done")) |
17 | 523 |
524 group_saving = OptionGroup(parser, "sources saving") | |
525 | |
526 group_saving.add_option("--sources-save", action="store", | |
527 help=_("Save source arguments")) | |
528 | |
529 group_saving.add_option("--sources-replace", action="store", | |
530 help=_("Save source arguments and replace memory if it already exists")) | |
531 | |
532 group_saving.add_option("--sources-load", action="store", | |
533 help=_("Load source arguments")) | |
534 | |
535 group_saving.add_option("--sources-del", action="store", | |
536 help=_("delete saved sources")) | |
537 | |
538 group_saving.add_option("--sources-list", action="store_true", default=False, | |
539 help=_("List names of saved sources")) | |
540 | |
541 group_saving.add_option("--sources-full-list", action="store_true", default=False, | |
542 help=_("List names of saved sources and files in it")) | |
543 | |
544 parser.add_option_group(group_saving) | |
545 | |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
546 |
0 | 547 (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
|
548 #options check |
3 | 549 if options.progress and not pbar_available: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
550 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
|
551 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
|
552 else: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
553 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
|
554 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
555 if options.verbose: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
556 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
|
557 |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
558 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
|
559 if not preserve.issubset(const_PRESERVE): |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
560 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
|
561 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
|
562 error('- %s' % value) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
563 exit(2) |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
564 else: |
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
565 options.preserve = preserve |
15 | 566 |
17 | 567 self.__sourcesSaving(options, args) |
0 | 568 |
4
9feb82bd91aa
--preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents:
3
diff
changeset
|
569 #if there is an other instance of gcp, we send options to it |
0 | 570 if not self._main_instance: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
571 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT) |
1 | 572 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames |
573 # can have invalid unicode. | |
24
154fe67bae72
Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
574 return self.gcp_main.addArgs(pickle.dumps(os.getcwd()),pickle.dumps(full_args)) |
0 | 575 else: |
1 | 576 if len(args) < 2: |
5
7edbb0e0d9dd
gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents:
4
diff
changeset
|
577 _error_msg = _("Wrong number of arguments") |
1 | 578 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
|
579 debug(_("adding args to gcp: %s") % str(args).decode('utf-8','replace')) |
0 | 580 self.__checkArgs(options, source_path, args) |
12 | 581 if not self.__launched: |
582 self.journal = Journal() | |
583 gobject.idle_add(self.__copyNextFile) | |
584 self.__launched = True | |
1 | 585 return (True,'') |
0 | 586 |
14 | 587 def _userInterruption(self): |
588 info(_("User interruption: good bye")) | |
589 exit(1) | |
590 | |
0 | 591 def go(self): |
1 | 592 """Launch main loop""" |
0 | 593 self.loop = gobject.MainLoop() |
594 try: | |
595 self.loop.run() | |
596 except KeyboardInterrupt: | |
14 | 597 self._userInterruption() |
0 | 598 |
599 | |
600 if __name__ == "__main__": | |
601 gcp = GCP() | |
1 | 602 success,message = gcp.parseArguments() |
603 if not success: | |
604 error(message) | |
605 exit(1) | |
0 | 606 if gcp._main_instance: |
607 gcp.go() | |
608 |