annotate gcp @ 15:8253a5ccbe9a

Added sources files saving + misc - source files can be saved to be replayed later - gcp files are now in ~/.gcp (directory is created if needed) - fixed an encoding error in a warning
author Goffi <goffi@goffi.org>
date Tue, 28 Sep 2010 15:42:47 +0800
parents 21b38ed339a4
children b03bd83c1661
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2010 Jérôme Poisson (goffi@goffi.org)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
7
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
12
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
17
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
21
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
22 ### logging ###
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import logging
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from logging import debug, info, error, warning
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
25 logging.basicConfig(level=logging.INFO,
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
26 format='%(message)s')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
27 ###
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
28
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
29 import gettext
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
30 gettext.install('gcp', "i18n", unicode=True)
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
31
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import sys
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 import os,os.path
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from optparse import OptionParser #To be replaced by argparse ASAP
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
35 import cPickle as pickle
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37 import gobject
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 #DBus
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39 import dbus, dbus.glib
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 import dbus.service
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41 import dbus.mainloop.glib
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42 except ImportError,e:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
43 error(_("Error during import"))
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
44 error(_("Please check dependecies:"),e)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
45 exit(2)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
46 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 pbar_available=True
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49 except ImportError, e:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
50 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar'))
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
51 info (_('Progress bar deactivated\n--\n'))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 pbar_available=False
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54 NAME = "gcp (Goffi's copier)"
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55 NAME_SHORT = "gcp"
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 VERSION = '0.1'
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
58 ABOUT = NAME+u" v"+VERSION+u""" (c) Jérôme Poisson (aka Goffi) 2010
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60 ---
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
61 """+NAME+u""" Copyright (C) 2010 Jérôme Poisson
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
62 """ + _(u"""This program comes with ABSOLUTELY NO WARRANTY;
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 This is free software, and you are welcome to redistribute it
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 under certain conditions.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
65 ---
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
66
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
67 This software is an advanced file copier
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
68 Get the latest version at http://www.goffi.org
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
69 """)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71 const_DBUS_INTERFACE = "org.goffi.gcp"
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
72 const_DBUS_PATH = "/org/goffi/gcp"
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
73 const_BUFF_SIZE = 4096
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
74 const_PRESERVE = set(['mode','ownership','timestamps'])
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
75 const_FILES_DIR = "~/.gcp"
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
76 const_JOURNAL_PATH = const_FILES_DIR + "/journal"
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
77 const_SAVED_LIST = const_FILES_DIR + "/saved_list"
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80 class DbusObject(dbus.service.Object):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def __init__(self, gcp, bus, path):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83 self._gcp = gcp
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 dbus.service.Object.__init__(self, bus, path)
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
85 debug(_("Init DbusObject..."))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.cb={}
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
87
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
88 @dbus.service.method(const_DBUS_INTERFACE,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89 in_signature='', out_signature='s')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 def getVersion(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
91 """Get gcp version
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
92 @return: version as string"""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return VERSION
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
94
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @dbus.service.method(const_DBUS_INTERFACE,
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
96 in_signature='ss', out_signature='bs')
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def addArgs(self, source_path, args):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98 """Add arguments to gcp as if there were entered on its own command line
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
99 @param source_path: current working dir to use as base for arguments, as given by os.getcwd()
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
100 @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
101 @return: success (boolean) and error message if any (string)"""
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
102 try:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
103 args = pickle.loads(str(args))
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
104 except TypeError, pickle.UnpicklingError:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
105 return (False, _("INTERNAL ERROR: invalid arguments"))
10
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
106 return self._gcp.parseArguments(args, str(source_path))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
107
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
108 class Journal():
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
109 def __init__(self, path=const_JOURNAL_PATH):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
110 self.journal_path = os.path.expanduser(path)
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
111 self.journal_fd = open(self.journal_path,'w') #TODO: check and maybe save previous journals
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
112
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
113 def __del__(self):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
114 self.journal_fd.flush()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
115 self.journal_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
116
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
117 def startFile(self, source_path):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
118 """Start an entry in the journal"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
119 self.journal_fd.write(source_path+"\n")
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
120 self.journal_fd.flush()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
121 self.success=True
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
122 self.errors=[]
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
123
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
124 def closeFile(self):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
125 """Close the entry in the journal"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
126 if not self.success:
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
127 status = "FAILED"
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
128 else:
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
129 status = "OK" if not self.errors else "PARTIAL"
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
130 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
131 self.journal_fd.flush()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
132
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
133 def copyFailed(self):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
134 """Must be called when something is wrong with the copy itself"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
135 self.success = False
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
136
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
137 def error(self, name):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
138 """Something went wrong"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
139 self.errors.append(name)
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
140
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
141
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 class GCP():
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 def __init__(self):
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
145 files_dir = os.path.expanduser(const_FILES_DIR)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
146 if not os.path.exists(files_dir):
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
147 os.makedirs(files_dir)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149 sessions_bus = dbus.SessionBus()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 db_object = sessions_bus.get_object(const_DBUS_INTERFACE,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151 const_DBUS_PATH)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
152 self.gcp_main = dbus.Interface(db_object,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 dbus_interface=const_DBUS_INTERFACE)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 self._main_instance = False
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
156 except dbus.exceptions.DBusException,e:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
157 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown':
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
158 self.launchDbusMainInstance()
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
159 debug (_("gcp launched"))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
160 self._main_instance = True
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
161 self.buffer_size = const_BUFF_SIZE
12
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
162 self.__launched = False #True when journal is initialised and copy is started
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
163 else:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164 raise e
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
165
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
166 def launchDbusMainInstance(self):
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
167 debug (_("Init DBus..."))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
168 session_bus = dbus.SessionBus()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
169 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
170 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
171
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
172 self.copy_list = []
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
173 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
174 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
175 self.bytes_copied = 0
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
176
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def getFsType(self, path):
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
178 fs = ''
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
179 last_mount_point = ''
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
180 for mount in self.mounts:
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
181 if path.startswith(mount) and len(mount)>=len(last_mount_point):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
182 fs = self.mounts[mount]
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
183 last_mount_point = mount
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184 return fs
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186 def __getMountPoints(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
187 """Parse /proc/mounts to get currently mounted devices"""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
188 #TODO: reparse when a new device is added/a device is removed
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
189 #(check freedesktop mounting signals)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
190 ret = {}
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
191 try:
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
192 with open("/proc/mounts",'rb') as mounts:
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
193 for line in mounts.readlines():
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
194 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195 ret[fs_file] = fs_vfstype
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
196 except:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
197 error (_("Can't read mounts table"))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
198 return ret
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
199
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
200 def __appendToList(self, path, dest_path, options):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
201 """Add a file to the copy list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
202 @param path: absolute path of file
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
203 @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
204 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
205 "dest_path":dest_path.decode('utf-8','replace'),
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
206 "fs_type":self.getFsType(dest_path)} )
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
207 try:
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
208 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
209 self.copy_list.insert(0,(path, dest_path, options))
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
210 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
211 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
212
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
213
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
214 def __appendDirToList(self, dirpath, dest_path, options):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
215 """Add recursively directory to the copy list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
216 @param path: absolute path of dir
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
217 @param options: options as return by optparse"""
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
218 #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
219 dest_path = self.__filename_fix(dest_path, options, no_journal=True)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
220 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
221 debug ("Creating directory %s" % dest_path)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
222 os.makedirs(dest_path) #TODO: check permissions
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
223 #TODO: check that dest_path is an accessible dir,
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
224 # and skip file/write error in log if needed
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
225 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
226 for filename in os.listdir(dirpath):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
227 filepath = os.path.join(dirpath,filename)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
228 if os.path.isdir(filepath):
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
229 full_dest_path = os.path.join(dest_path,filename)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
230 self.__appendDirToList(filepath, full_dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
231 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
232 self.__appendToList(filepath, dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
233 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
234 error(_("Can't append %(path)s to copy list: %(exception)s") % {'path':filepath.decode('utf-8','replace'),
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
235 'exception':e.strerror})
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
236
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
237 def __checkArgs(self, options, source_path, args):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
238 """Check thats args are files, and add them to copy list"""
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
239 assert(len (args)>=2)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
240 try:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
241 dest_path = os.path.normpath(os.path.join(os.path.expanduser(source_path), args.pop()))
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
242 except OSError,e:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
243 error (_("Invalid dest_path: %s"),e)
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
244
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
245 for path in args:
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
246 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
247 if not os.path.exists(abspath):
11
c065a70c3e7b fixed encoding issue for a warning
Goffi <goffi@goffi.org>
parents: 10
diff changeset
248 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
249 else:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
250 if os.path.isdir(abspath):
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
251 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if not options.recursive:
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
253 warning (_('omitting directory "%s"') % abspath.decode('utf-8','replace'))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
254 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
255 self.__appendDirToList(abspath, full_dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
256 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
257 self.__appendToList(abspath, dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
258
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
259 def __copyNextFile(self):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
260 """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
261 @return: True a file was added, False else"""
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
262 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
263 source_file, dest_path, options = self.copy_list.pop()
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
264 self.journal.startFile(source_file)
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
265 source_fd = open(source_file, 'rb')
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
266 filename = os.path.basename(source_file)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
267 assert(filename)
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
268 dest_file = self.__filename_fix(os.path.join(dest_path,filename),options)
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
269 if os.path.exists(dest_file) and not options.force:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
270 warning (_("File [%s] already exists, skipping it !") % dest_file)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
271 return True
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
272 try:
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
273 dest_fd = open(dest_file, 'wb')
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
274 except:
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
275 self.journal.copyFailed()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
276 self.journal.error("can't open dest")
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
277 self.journal.closeFile()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
278 source_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
279 return True
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
280
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
281 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
282 (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
283 if not self.progress:
9
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
284 info(_("COPYING %(source)s ==> %(dest)s") % {"source":source_path.decode('utf-8','replace'),
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
285 "dest":dest_file.decode('utf-8','replace')})
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
286 return True
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
287 else:
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
288 #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
289 if self.progress:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
290 self.__pbar_finish()
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
291 self.loop.quit()
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
292
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
293 def __copyFailed(self, reason, source_fd, dest_fd):
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
294 """Write the failure in the journal and close files descriptors"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
295 self.journal.copyFailed()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
296 self.journal.error(reason)
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
297 self.journal.closeFile()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
298 source_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
299 dest_fd.close()
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
300
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
301
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
302
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
303 def _copyFile(self, source_fd, condition, data):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
304 """Actually copy the file, callback used with io_add_watch
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
305 @param source_fd: file descriptor of the file to copy
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
306 @param condition: condition which launched the callback (glib.IO_IN)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
307 @param data: tuple with (destination file descriptor, copying options)"""
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
308 try:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
309 dest_fd,options = data
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
310
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
311 try:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
312 buff = source_fd.read(self.buffer_size)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
313 except KeyboardInterrupt:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
314 raise KeyboardInterrupt
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
315 except:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
316 self.__copyFailed("can't read source", source_fd, dest_fd)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
317 return False
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
318
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
319 try:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
320 dest_fd.write(buff)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
321 except KeyboardInterrupt:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
322 raise KeyboardInterrupt
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
323 except:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
324 self.__copyFailed("can't write to dest", source_fd, dest_fd)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
325 return False
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
326
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
327 self.bytes_copied += len(buff)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
328 if self.progress:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
329 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
330
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
331 if len(buff) != self.buffer_size:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
332 source_fd.close()
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
333 dest_fd.close()
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
334 self.__post_copy(source_fd.name, dest_fd.name, options)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
335 self.journal.closeFile()
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
336 return False
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
337 return True
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
338 except KeyboardInterrupt:
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
339 self._userInterruption()
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
340
9
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
341 def __filename_fix(self, filename, options, no_journal=False):
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
342 """Fix filenames incompatibilities/mistake according to options
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
343 @param filename: full path to the file
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
344 @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
345 @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
346 @return: fixed filename"""
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
347 fixed_filename = filename
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
348
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
349 if self.getFsType(filename) == 'vfat' and options.fs_fix:
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
350 fixed_filename = filename.replace('\\','_')\
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
351 .replace(':',';')\
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
352 .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
353 .replace('?','_')\
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
354 .replace('"','\'')\
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
355 .replace('<','[')\
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
356 .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
357 .replace('|','!')\
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
358 .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
359
9
599b84e4ff01 - added utf-8 decoding of strings in logs, with 'replace' for badly encoded names
Goffi <goffi@goffi.org>
parents: 8
diff changeset
360 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
361 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
362 if fixed_filename != filename and not no_journal:
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
363 self.journal.error('filename fixed')
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
364 return fixed_filename
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
365
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
366 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
367 """Do post copy traitement (mainly managing --preserve option)"""
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
368 st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime = os.stat(source_file)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
369 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
370 try:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
371 if preserve == 'mode':
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
372 os.chmod(dest_file, st_mode)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
373 elif preserve == 'ownership':
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
374 os.chown(dest_file, st_uid, st_gid)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
375 elif preserve == 'timestamps':
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
376 os.utime(dest_file, (st_atime, st_mtime))
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
377 except OSError,e:
7
a110d31482f7 Journalisation + some additionnal try/except
Goffi <goffi@goffi.org>
parents: 6
diff changeset
378 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
379
10
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
380 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
381 """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
382
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
383 if size>=2**50:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
384 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
385 elif size>=2**40:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
386 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
387 elif size>=2**30:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
388 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
389 elif size>=2**20:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
390 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
391 elif size>=2**10:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
392 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
393 else:
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
394 return _("%i B") % size
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
395
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
396 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
397 """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
398 assert(self.progress)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
399 try:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
400 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
401 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
402 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
403 except AttributeError:
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
404 if not self.bytes_total:
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
405 #No progress bar if the files have a null size
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
406 return
10
00b27fce4677 - Added total size of files to copy in progress bar
Goffi <goffi@goffi.org>
parents: 9
diff changeset
407 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
408 self.pbar.start()
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
409 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
410
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
411 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
412 """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
413 assert(self.progress)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
414 try:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
415 self.pbar.finish()
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
416 except AttributeError:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
417 pass
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
418
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
419 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()):
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
420 """Parse arguments and add files to queue
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
421 @param full_args: list of arguments strings (without program name)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
422 @param source_path: path from where the arguments come, ad given by os.getcwd()
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
423 @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
424 validation, and message is the error message to print when necessary"""
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
425 _usage="""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
426 %prog [options] FILE1 [FILE2 ...] DEST
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
427
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
428 %prog --help for options list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
429 """
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
430 for idx in range(len(full_args)):
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
431 if isinstance(full_args[idx], unicode):
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
432 #We don't want unicode as some filenames can be invalid unicode
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
433 full_args[idx] = full_args[idx].encode('utf-8')
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
434
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
435 parser = OptionParser(usage=_usage,version=ABOUT)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
436
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
437 parser.add_option("-r", "--recursive", action="store_true", default=False,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
438 help=_("copy directories recursively"))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
439
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
440 parser.add_option("-f", "--force", action="store_true", default=False,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
441 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
442
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
443 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
444 help=_("preserve the specified attributes"))
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
445
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
446 parser.add_option("--save", action="store",
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
447 help=_("Save source arguments"))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
448
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
449 parser.add_option("--force-save", action="store",
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
450 help=_("Save source arguments and replace memory if it already exists"))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
451
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
452 parser.add_option("--load", action="store",
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
453 help=_("Load source arguments"))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
454
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
455 parser.add_option("--list", action="store_true", default=False,
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
456 help=_("List names of saved sources"))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
457
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
458 parser.add_option("--full-list", action="store_true", default=False,
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
459 help=_("List names of saved sources and files in it"))
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
460
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
461 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
462 help=_("don't fixe name encoding errors")) #TODO
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
463
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
464 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True,
8
144cb2669f21 removed some forgotten TODO
Goffi <goffi@goffi.org>
parents: 7
diff changeset
465 help=_("don't fixe filesystem name incompatibily"))
6
5f53dc5beec9 vfat names incompatibility fix
Goffi <goffi@goffi.org>
parents: 5
diff changeset
466
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
467 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
468 help=_("deactivate progress bar"))
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
469
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
470 parser.add_option("-v", "--verbose", action="store_true", default=False,
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
471 help=_("Show what is currently done"))
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
472
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
473 (options, args) = parser.parse_args(full_args)
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
474 #options check
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
475 if options.progress and not pbar_available:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
476 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
477 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
478 else:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
479 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
480
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
481 if options.verbose:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
482 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
483
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
484 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
485 if not preserve.issubset(const_PRESERVE):
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
486 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
487 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
488 error('- %s' % value)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
489 exit(2)
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
490 else:
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
491 options.preserve = preserve
15
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
492
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
493 if options.save or options.load or options.list or options.full_list:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
494 try:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
495 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
496 saved_files = pickle.load(saved_fd)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
497 except:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
498 saved_files={}
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
499
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
500 if options.list or options.full_list:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
501 info(_('Saved sources:'))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
502 sources = saved_files.keys()
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
503 sources.sort()
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
504 for source in sources:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
505 info("\t[%s]" % source)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
506 if options.full_list:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
507 for filename in saved_files[source]:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
508 info("\t\t%s" % filename)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
509 info("---\n")
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
510 if not args:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
511 exit(0)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
512
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
513 if options.save or options.force_save:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
514 if saved_files.has_key(options.save) and not options.force_save:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
515 error(_("There is already a saved sources with this name, skipping --save"))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
516 else:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
517 if len(args)>1:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
518 saved_files[options.save] = map(os.path.abspath,args[:-1])
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
519 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
520 pickle.dump(saved_files,saved_fd)
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
521
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
522 if options.load:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
523 if not saved_files.has_key(options.load):
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
524 error(_("No saved sources with this name, check existing names with --list"))
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
525 else:
8253a5ccbe9a Added sources files saving + misc
Goffi <goffi@goffi.org>
parents: 14
diff changeset
526 args = saved_files[options.load] + args
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
527
4
9feb82bd91aa --preserve option (same default as for cp) & progress bar are now working.
Goffi <goffi@goffi.org>
parents: 3
diff changeset
528 #if there is an other instance of gcp, we send options to it
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
529 if not self._main_instance:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
530 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
531 #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
532 # can have invalid unicode.
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
533 return self.gcp_main.addArgs(os.getcwd(),pickle.dumps(full_args))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
534 else:
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
535 if len(args) < 2:
5
7edbb0e0d9dd gettext inclusion & French translation
Goffi <goffi@goffi.org>
parents: 4
diff changeset
536 _error_msg = _("Wrong number of arguments")
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
537 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
538 debug(_("adding args to gcp: %s") % str(args).decode('utf-8','replace'))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
539 self.__checkArgs(options, source_path, args)
12
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
540 if not self.__launched:
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
541 self.journal = Journal()
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
542 gobject.idle_add(self.__copyNextFile)
bbe09d5791dd Fixed double initialisation
Goffi <goffi@goffi.org>
parents: 11
diff changeset
543 self.__launched = True
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
544 return (True,'')
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
545
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
546 def _userInterruption(self):
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
547 info(_("User interruption: good bye"))
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
548 exit(1)
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
549
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
550 def go(self):
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
551 """Launch main loop"""
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
552 self.loop = gobject.MainLoop()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
553 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
554 self.loop.run()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
555 except KeyboardInterrupt:
14
21b38ed339a4 Fixed KeyboardInterrupt management
Goffi <goffi@goffi.org>
parents: 13
diff changeset
556 self._userInterruption()
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
557
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
558
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
559 if __name__ == "__main__":
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
560 gcp = GCP()
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
561 success,message = gcp.parseArguments()
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
562 if not success:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
563 error(message)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
564 exit(1)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
565 if gcp._main_instance:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
566 gcp.go()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
567