annotate gcp @ 58:58091eac8c8e

exit status updated and shown in manpage
author Goffi <goffi@goffi.org>
date Mon, 20 Jun 2011 12:57:15 +0200
parents cce259fe3f35
children 7830d4244fb5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
3
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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>
50
b0c96e4ab9d8 Thomas Preud'homme email address update
Goffi <goffi@goffi.org>
parents: 49
diff changeset
7 (c) 2011 Thomas Preud'homme <robotux@celest.fr>
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 This program is free software: you can redistribute it and/or modify
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10 it under the terms of the GNU General Public License as published by
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11 the Free Software Foundation, either version 3 of the License, or
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12 (at your option) any later version.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 This program is distributed in the hope that it will be useful,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17 GNU General Public License for more details.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21 """
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 ### logging ###
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24 import logging
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 format='%(message)s')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 import sys
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 import os,os.path
17
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
35 from optparse import OptionParser, OptionGroup #To be replaced by argparse ASAP
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
36 import cPickle as pickle
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 import gobject
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39 #DBus
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 import dbus, dbus.glib
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41 import dbus.service
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42 import dbus.mainloop.glib
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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)
58
58091eac8c8e exit status updated and shown in manpage
Goffi <goffi@goffi.org>
parents: 56
diff changeset
46 exit(1)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49 pbar_available=True
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53 pbar_available=False
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55 NAME = "gcp (Goffi's copier)"
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 NAME_SHORT = "gcp"
52
b5c525dfec33 version update
Goffi <goffi@goffi.org>
parents: 50
diff changeset
57 VERSION = '0.1.2'
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
58
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
59 ABOUT = NAME+u" v"+VERSION+u""" (c) Jérôme Poisson (aka Goffi) 2010, 2011
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 This is free software, and you are welcome to redistribute it
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
65 under certain conditions.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
66 ---
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68 This software is an advanced file copier
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
72 const_DBUS_INTERFACE = "org.goffi.gcp"
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
73 const_DBUS_PATH = "/org/goffi/gcp"
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
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
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
76 const_FILES_DIR = "~/.gcp"
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
77 const_JOURNAL_PATH = const_FILES_DIR + "/journal"
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
78 const_SAVED_LIST = const_FILES_DIR + "/saved_list"
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79
25
f82731c70956 Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents: 24
diff changeset
80
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 class DbusObject(dbus.service.Object):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def __init__(self, gcp, bus, path):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 self._gcp = gcp
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.cb={}
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 @dbus.service.method(const_DBUS_INTERFACE,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 in_signature='', out_signature='s')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92 def getVersion(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """Get gcp version
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
94 @return: version as string"""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return VERSION
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
96
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
97 @dbus.service.method(const_DBUS_INTERFACE,
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
98 in_signature='ss', out_signature='bs')
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
99 def addArgs(self, source_dir, args):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 """Add arguments to gcp as if there were entered on its own command line
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
101 @param source_dir: current working dir to use as base for arguments, as given by os.getcwd()
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
102 @param args: serialized (wich pickle) list of strings - without command name -, as given by sys.argv[1:].
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
103 @return: success (boolean) and error message if any (string)"""
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
104 try:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
105 args = pickle.loads(str(args))
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
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:
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
109 source_dir = pickle.loads(str(source_dir))
24
154fe67bae72 Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents: 21
diff changeset
110 except TypeError, pickle.UnpicklingError:
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
111 return (False, _("INTERNAL ERROR: invalid source_dir"))
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
112 return self._gcp.parseArguments(args, source_dir)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
183 class GCP():
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185 def __init__(self):
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
186 files_dir = os.path.expanduser(const_FILES_DIR)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
187 if not os.path.exists(files_dir):
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
188 os.makedirs(files_dir)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
189 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
190 sessions_bus = dbus.SessionBus()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
191 db_object = sessions_bus.get_object(const_DBUS_INTERFACE,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
192 const_DBUS_PATH)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
193 self.gcp_main = dbus.Interface(db_object,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
194 dbus_interface=const_DBUS_INTERFACE)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195 self._main_instance = False
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
196
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
197 except dbus.exceptions.DBusException,e:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
198 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown':
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
199 self.launchDbusMainInstance()
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
200 debug (_("gcp launched"))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
201 self._main_instance = True
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
202 self.buffer_size = const_BUFF_SIZE
12
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
203 self.__launched = False #True when journal is initialised and copy is started
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
204 else:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
205 raise e
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
206
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
207 def launchDbusMainInstance(self):
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
208 debug (_("Init DBus..."))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
209 session_bus = dbus.SessionBus()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
210 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
211 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
212
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
213 self.copy_list = []
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
217
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
218 def getFsType(self, path):
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
219 fs = ''
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
220 last_mount_point = ''
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
221 for mount in self.mounts:
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
222 if path.startswith(mount) and len(mount)>=len(last_mount_point):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
223 fs = self.mounts[mount]
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
224 last_mount_point = mount
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
225 return fs
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
226
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
227 def __getMountPoints(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
228 """Parse /proc/mounts to get currently mounted devices"""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
229 #TODO: reparse when a new device is added/a device is removed
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
230 #(check freedesktop mounting signals)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
231 ret = {}
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
234 for line in mounts.readlines():
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
235 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
236 ret[fs_file] = fs_vfstype
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
237 except:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
238 error (_("Can't read mounts table"))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
239 return ret
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
240
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
241 def __appendToList(self, path, dest_path, options):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
242 """Add a file to the copy list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
243 @param path: absolute path of file
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
247 "fs_type":self.getFsType(dest_path)} )
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
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
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
253
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
254
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
255 def __appendDirToList(self, dirpath, dest_path, options):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
256 """Add recursively directory to the copy list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
257 @param path: absolute path of dir
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
258 @param options: options as return by optparse"""
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
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
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
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
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
263 os.makedirs(dest_path) #TODO: check permissions
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
264 #TODO: check that dest_path is an accessible dir,
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
265 # and skip file/write error in log if needed
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
266 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
267 for filename in os.listdir(dirpath):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
268 filepath = os.path.join(dirpath,filename)
46
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
269 if os.path.islink(filepath) and not options.dereference:
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
270 debug ("Skippink symbolic dir: %s" % filepath.decode('utf-8','replace'))
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
271 continue
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
272 if os.path.isdir(filepath):
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
273 full_dest_path = os.path.join(dest_path,filename)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
274 self.__appendDirToList(filepath, full_dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
275 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
276 self.__appendToList(filepath, dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
285
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
286 def __checkArgs(self, options, source_dir, args):
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
287 """Check thats args are files, and add them to copy list
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
288 @param options: options sets
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
289 @param source_dir: directory where the command was entered
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
290 @parm args: args of the copy"""
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
291 assert(len (args)>=2)
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
292 len_args = len(args)
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
293 try:
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
294 dest_path = os.path.normpath(os.path.join(source_dir, args.pop()))
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
295 except OSError,e:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
296 error (_("Invalid dest_path: %s"),e)
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
297
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
298 for path in args:
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
299 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_dir), path))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
300 if not os.path.exists(abspath):
11
c065a70c3e7b fixed encoding issue for a warning
Goffi <goffi@goffi.org>
parents: 10
diff changeset
301 warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath.decode('utf-8','replace'))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
302 else:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
303 if os.path.isdir(abspath):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
304 if not options.recursive:
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
305 warning (_('omitting directory "%s"') % abspath.decode('utf-8','replace'))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
306 else:
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
307 _basename=os.path.basename(os.path.normpath(path))
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
308 full_dest_path = dest_path if options.directdir else os.path.normpath(os.path.join(dest_path, _basename))
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
309 self.__appendDirToList(abspath, full_dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
310 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
311 self.__appendToList(abspath, dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
312
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
313 def __copyNextFile(self):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
314 """Take the last file in the list, and launch the copy using glib io_watch event
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
315 @return: True a file was added, False else"""
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
316 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
317 source_file, dest_path, options = self.copy_list.pop()
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
318 self.journal.startFile(source_file)
47
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
319 try:
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
320 source_fd = open(source_file, 'rb')
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
321 except:
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
322 self.journal.copyFailed()
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
323 self.journal.error("can't open source")
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
324 self.journal.closeFile()
99da6ef26d26 fixed crash when source file can't be opened
Goffi <goffi@goffi.org>
parents: 46
diff changeset
325 return True
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
326 filename = os.path.basename(source_file)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
327 assert(filename)
28
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
328 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
329 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
330 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
331 self.journal.copyFailed()
95f16ff36308 fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents: 26
diff changeset
332 self.journal.error("already exists")
95f16ff36308 fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents: 26
diff changeset
333 self.journal.closeFile()
95f16ff36308 fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents: 26
diff changeset
334 source_fd.close()
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
335 return True
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
336 try:
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
337 dest_fd = open(dest_file, 'wb')
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
338 except:
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
339 self.journal.copyFailed()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
340 self.journal.error("can't open dest")
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
341 self.journal.closeFile()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
342 source_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
343 return True
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
344
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
345 gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile,
13
894530949e30 lowered copy priority to DEFAULT
Goffi <goffi@goffi.org>
parents: 12
diff changeset
346 (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
347 if not self.progress:
27
95f16ff36308 fixed bad closure when a file already exists
Goffi <goffi@goffi.org>
parents: 26
diff changeset
348 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
349 "dest":dest_file.decode('utf-8','replace')})
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
350 return True
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
351 else:
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
352 #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
353 if self.progress:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
354 self.__pbar_finish()
25
f82731c70956 Error are now shown after the end of the copy
Goffi <goffi@goffi.org>
parents: 24
diff changeset
355 self.journal.showErrors()
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
356 self.loop.quit()
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
357
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
358 def __copyFailed(self, reason, source_fd, dest_fd):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
359 """Write the failure in the journal and close files descriptors"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
360 self.journal.copyFailed()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
361 self.journal.error(reason)
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
362 self.journal.closeFile()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
363 source_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
364 dest_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
365
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
366
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
367
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
368 def _copyFile(self, source_fd, condition, data):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
369 """Actually copy the file, callback used with io_add_watch
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
370 @param source_fd: file descriptor of the file to copy
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
371 @param condition: condition which launched the callback (glib.IO_IN)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
372 @param data: tuple with (destination file descriptor, copying options)"""
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
373 try:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
374 dest_fd,options = data
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
375
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
376 try:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
377 buff = source_fd.read(self.buffer_size)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
378 except KeyboardInterrupt:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
379 raise KeyboardInterrupt
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
380 except:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
381 self.__copyFailed("can't read source", source_fd, dest_fd)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
382 return False
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
383
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
384 try:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
385 dest_fd.write(buff)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
386 except KeyboardInterrupt:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
387 raise KeyboardInterrupt
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
388 except:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
389 self.__copyFailed("can't write to dest", source_fd, dest_fd)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
390 return False
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
391
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
392 self.bytes_copied += len(buff)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
393 if self.progress:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
394 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
395
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
396 if len(buff) != self.buffer_size:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
397 source_fd.close()
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
398 dest_fd.close()
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
399 self.__post_copy(source_fd.name, dest_fd.name, options)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
400 self.journal.closeFile()
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
401 return False
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
402 return True
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
403 except KeyboardInterrupt:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
404 self._userInterruption()
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
405
9
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
406 def __filename_fix(self, filename, options, no_journal=False):
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
407 """Fix filenames incompatibilities/mistake according to options
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
408 @param filename: full path to the file
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
409 @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
410 @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
411 @return: fixed filename"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
412 fixed_filename = filename
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
413
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
414 if self.getFsType(filename) == 'vfat' and options.fs_fix:
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
415 fixed_filename = filename.replace('\\','_')\
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
416 .replace(':',';')\
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
417 .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
418 .replace('?','_')\
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
419 .replace('"','\'')\
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
420 .replace('<','[')\
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
421 .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
422 .replace('|','!')\
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
423 .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
424
9
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
425 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
426 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
427 if fixed_filename != filename and not no_journal:
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
428 self.journal.error('filename fixed')
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
429 return fixed_filename
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
430
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
431 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
432 """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
433 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
434 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
435 try:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
436 if preserve == 'mode':
41
b54e5a4102da os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents: 40
diff changeset
437 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
438 elif preserve == 'ownership':
41
b54e5a4102da os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents: 40
diff changeset
439 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
440 elif preserve == 'timestamps':
41
b54e5a4102da os.stat precision fix
Thomas Preud'homme <thomas.preudhomme@celest.fr>
parents: 40
diff changeset
441 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
442 except OSError,e:
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
443 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
444
10
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
445 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
446 """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
447
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
448 if size>=2**50:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
449 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
450 elif size>=2**40:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
451 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
452 elif size>=2**30:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
453 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
454 elif size>=2**20:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
455 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
456 elif size>=2**10:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
457 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
458 else:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
459 return _("%i B") % size
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
460
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
461 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
462 """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
463 assert(self.progress)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
464 try:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
465 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
466 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
467 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
468 except AttributeError:
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
469 if not self.bytes_total:
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
470 #No progress bar if the files have a null size
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
471 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
472 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
473 self.pbar.start()
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
474 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
475
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
476 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
477 """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
478 assert(self.progress)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
479 try:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
480 self.pbar.finish()
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
481 except AttributeError:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
482 pass
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
483
17
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
484 def __sourcesSaving(self,options,args):
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
485 """Manage saving/loading/deleting etc of sources files
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
486 @param options: options as parsed from command line
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
487 @param args: args parsed from command line"""
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
488 if options.sources_save or options.sources_load\
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
489 or options.sources_list or options.sources_full_list\
18
9d015d756c0f fixed sources-replace
Goffi <goffi@goffi.org>
parents: 17
diff changeset
490 or options.sources_del or options.sources_replace:
17
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
491 try:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
492 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
493 saved_files = pickle.load(saved_fd)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
494 except:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
495 saved_files={}
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
496
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
497 if options.sources_del:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
498 if not saved_files.has_key(options.sources_del):
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
499 error(_("No saved sources with this name, check existing names with --sources-list"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
500 else:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
501 del saved_files[options.sources_del]
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
502 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
503 pickle.dump(saved_files,saved_fd)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
504 if not args:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
505 exit(0)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
506
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
507
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
508 if options.sources_list or options.sources_full_list:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
509 info(_('Saved sources:'))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
510 sources = saved_files.keys()
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
511 sources.sort()
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
512 for source in sources:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
513 info("\t[%s]" % source)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
514 if options.sources_full_list:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
515 for filename in saved_files[source]:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
516 info("\t\t%s" % filename)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
517 info("---\n")
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
518 if not args:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
519 exit(0)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
520
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
521 if options.sources_save or options.sources_replace:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
522 if saved_files.has_key(options.sources_save) and not options.sources_replace:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
523 error(_("There is already a saved sources with this name, skipping --sources-save"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
524 else:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
525 if len(args)>1:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
526 saved_files[options.sources_save] = map(os.path.abspath,args[:-1])
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
527 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
528 pickle.dump(saved_files,saved_fd)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
529
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
530 if options.sources_load:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
531 if not saved_files.has_key(options.sources_load):
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
532 error(_("No saved sources with this name, check existing names with --sources-list"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
533 else:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
534 saved_args = saved_files[options.sources_load]
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
535 saved_args.reverse()
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
536 for arg in saved_args:
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
537 args.insert(0,arg)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
538
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
539 def parseArguments(self, full_args=sys.argv[1:], source_dir = os.getcwd()):
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
540 """Parse arguments and add files to queue
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
541 @param full_args: list of arguments strings (without program name)
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
542 @param source_dir: path from where the arguments come, as given by os.getcwd()
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
543 @return: a tuple (boolean, message) where the boolean is the success of the arguments
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
544 validation, and message is the error message to print when necessary"""
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
545 _usage="""
28
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
546 %prog [options] FILE DEST
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
547 %prog [options] FILE1 [FILE2 ...] DEST-DIR
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
548
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
549 %prog --help for options list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
550 """
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
551 for idx in range(len(full_args)):
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
552 if isinstance(full_args[idx], unicode):
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
553 #We don't want unicode as some filenames can be invalid unicode
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
554 full_args[idx] = full_args[idx].encode('utf-8')
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
555
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
556 parser = OptionParser(usage=_usage,version=ABOUT)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
557
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
558 parser.add_option("-r", "--recursive", action="store_true", default=False,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
559 help=_("copy directories recursively"))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
560
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
561 parser.add_option("-f", "--force", action="store_true", default=False,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
562 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
563
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
564 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
565 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
566
46
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
567 parser.add_option("-L", "--dereference", action="store_true", default=False,
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
568 help=_("always follow symbolic links in sources"))
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
569
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
570 parser.add_option("-P", "--no-dereference", action="store_false", dest='dereference',
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
571 help=_("never follow symbolic links in sources"))
dda2195dc9f4 Symbolic link skipping
Goffi <goffi@goffi.org>
parents: 45
diff changeset
572
21
8603fcb8615a --no-unicode-fix commented, and README update
Goffi <goffi@goffi.org>
parents: 18
diff changeset
573 #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
574 # help=_("don't fix name encoding errors")) #TODO
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
575
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
576 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
577 help=_("don't fix filesystem name incompatibily"))
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
578
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
579 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
580 help=_("deactivate progress bar"))
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
581
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
582 parser.add_option("-v", "--verbose", action="store_true", default=False,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
583 help=_("Show what is currently done"))
17
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
584
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
585 group_saving = OptionGroup(parser, "sources saving")
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
586
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
587 group_saving.add_option("--sources-save", action="store",
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
588 help=_("Save source arguments"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
589
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
590 group_saving.add_option("--sources-replace", action="store",
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
591 help=_("Save source arguments and replace memory if it already exists"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
592
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
593 group_saving.add_option("--sources-load", action="store",
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
594 help=_("Load source arguments"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
595
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
596 group_saving.add_option("--sources-del", action="store",
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
597 help=_("delete saved sources"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
598
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
599 group_saving.add_option("--sources-list", action="store_true", default=False,
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
600 help=_("List names of saved sources"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
601
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
602 group_saving.add_option("--sources-full-list", action="store_true", default=False,
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
603 help=_("List names of saved sources and files in it"))
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
604
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
605 parser.add_option_group(group_saving)
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
606
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
607
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
608 (options, args) = parser.parse_args(full_args)
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
609 options.directdir = False #True only in the special case: we are copying a dir and it doesn't exists
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
610 #options check
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
611 if options.progress and not pbar_available:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
612 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
613 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
614 else:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
615 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
616
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
617 if options.verbose:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
618 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
619
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
620 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
621 if not preserve.issubset(const_PRESERVE):
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
622 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
623 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
624 error('- %s' % value)
58
58091eac8c8e exit status updated and shown in manpage
Goffi <goffi@goffi.org>
parents: 56
diff changeset
625 exit(1)
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
626 else:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
627 options.preserve = preserve
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
628
17
132042c7abb2 Improved sources files management
Goffi <goffi@goffi.org>
parents: 16
diff changeset
629 self.__sourcesSaving(options, args)
28
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
630
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
631 if len(args) == 2: #we check special cases
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
632 src_path = os.path.abspath(os.path.expanduser(args[0]))
28
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
633 dest_path = os.path.abspath(os.path.expanduser(args[1]))
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
634 if os.path.isdir(src_path):
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
635 options.dest_file = None #we are copying a dir, this options is for files only
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
636 if not os.path.exists(dest_path):
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
637 options.directdir = True #dest_dir doesn't exist, it's the directdir special case
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
638 elif not os.path.exists(dest_path) or os.path.isfile(dest_path):
28
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
639 options.dest_file = dest_path
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
640 args[1] = os.path.dirname(dest_path)
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
641 else:
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
642 options.dest_file = None
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
643 else:
548dfc861531 Added 'gcp file dest_file' syntax management
Goffi <goffi@goffi.org>
parents: 27
diff changeset
644 options.dest_file = None
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
645
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
646 #if there is an other instance of gcp, we send options to it
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
647 if not self._main_instance:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
648 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT)
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
649 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
650 # can have invalid unicode.
24
154fe67bae72 Journal double-entry check + unicode source_path fix + typo
Goffi <goffi@goffi.org>
parents: 21
diff changeset
651 return self.gcp_main.addArgs(pickle.dumps(os.getcwd()),pickle.dumps(full_args))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
652 else:
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
653 if len(args) < 2:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
654 _error_msg = _("Wrong number of arguments")
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
655 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
656 debug(_("adding args to gcp: %s") % str(args).decode('utf-8','replace'))
55
98a879663e27 gcp DIR1 DIR2 better handling:
Goffi <goffi@goffi.org>
parents: 52
diff changeset
657 self.__checkArgs(options, source_dir, args)
12
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
658 if not self.__launched:
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
659 self.journal = Journal()
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
660 gobject.idle_add(self.__copyNextFile)
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
661 self.__launched = True
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
662 return (True,'')
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
663
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
664 def _userInterruption(self):
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
665 info(_("User interruption: good bye"))
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
666 exit(1)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
667
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
668 def go(self):
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
669 """Launch main loop"""
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
670 self.loop = gobject.MainLoop()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
671 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
672 self.loop.run()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
673 except KeyboardInterrupt:
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
674 self._userInterruption()
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
675
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
676
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
677 if __name__ == "__main__":
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
678 gcp = GCP()
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
679 success,message = gcp.parseArguments()
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
680 if not success:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
681 error(message)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
682 exit(1)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
683 if gcp._main_instance:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
684 gcp.go()
56
cce259fe3f35 non 0 return code on failure:
Goffi <goffi@goffi.org>
parents: 55
diff changeset
685 if gcp.journal.failed:
cce259fe3f35 non 0 return code on failure:
Goffi <goffi@goffi.org>
parents: 55
diff changeset
686 exit(2)
cce259fe3f35 non 0 return code on failure:
Goffi <goffi@goffi.org>
parents: 55
diff changeset
687 if gcp.journal.partial:
cce259fe3f35 non 0 return code on failure:
Goffi <goffi@goffi.org>
parents: 55
diff changeset
688 exit(1)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
689