annotate gcp @ 3:6a24b5928980

Basic file copying - missing dir are created (without permission check yet) - new options to deactivate progress bar
author Goffi <goffi@goffi.org>
date Sun, 26 Sep 2010 12:00:05 +0800
parents 1b91aa4f3c85
children 9feb82bd91aa
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
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
25 logging.basicConfig(level=logging.DEBUG,
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 ###
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import sys
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import os,os.path
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from optparse import OptionParser #To be replaced by argparse ASAP
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
31 import cPickle as pickle
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
32 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
33 import gobject
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
34 #DBus
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
35 import dbus, dbus.glib
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
36 import dbus.service
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
37 import dbus.mainloop.glib
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
38 except ImportError,e:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
39 error("Error during import")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
40 error("Please check dependecies:",e)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
41 exit(2)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
42 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
43 from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
44 pbar_available=True
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
45 except ImportError, e:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
46 info ('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
47 info ('Progress bar deactivated\n--\n')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
48 pbar_available=False
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
49
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
50 NAME = "gcp (Goffi's copier)"
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
51 NAME_SHORT = "gcp"
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
52 VERSION = '0.1'
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
53
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
54 ABOUT = NAME+" v"+VERSION+""" (c) Jérôme Poisson (aka Goffi) 2010
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
55
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
56 ---
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """+NAME+""" Copyright (C) 2010 Jérôme Poisson
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
58 This program comes with ABSOLUTELY NO WARRANTY;
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
59 This is free software, and you are welcome to redistribute it
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
60 under certain conditions.
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
61 ---
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
62
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
63 This software is an advanced file copier
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
64 Get the latest version at http://www.goffi.org
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 const_DBUS_INTERFACE = "org.goffi.gcp"
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
68 const_DBUS_PATH = "/org/goffi/gcp"
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
69 const_BUFF_SIZE = 4096
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
70
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
71
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
72 class DbusObject(dbus.service.Object):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
73
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def __init__(self, gcp, bus, path):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self._gcp = gcp
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
76 dbus.service.Object.__init__(self, bus, path)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
77 debug("Init DbusObject...")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self.cb={}
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
79
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
80 @dbus.service.method(const_DBUS_INTERFACE,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
81 in_signature='', out_signature='s')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def getVersion(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
83 """Get gcp version
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @return: version as string"""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return VERSION
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
86
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
87 @dbus.service.method(const_DBUS_INTERFACE,
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
88 in_signature='ss', out_signature='bs')
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
89 def addArgs(self, source_path, args):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
90 """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
91 @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
92 @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
93 @return: success (boolean) and error message if any (string)"""
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
94 try:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
95 args = pickle.loads(str(args))
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
96 except TypeError, pickle.UnpicklingError:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
97 return (False, "INTERNAL ERROR: invalid arguments")
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
98 return self._gcp.parseArguments(args, source_path)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
99
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
100 class GCP():
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
101
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def __init__(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
103 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
104 sessions_bus = dbus.SessionBus()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
105 db_object = sessions_bus.get_object(const_DBUS_INTERFACE,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
106 const_DBUS_PATH)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
107 self.gcp_main = dbus.Interface(db_object,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
108 dbus_interface=const_DBUS_INTERFACE)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
109 self._main_instance = False
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
110
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
111 except dbus.exceptions.DBusException,e:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
112 if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown':
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
113 self.launchDbusMainInstance()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
114 debug ("gcp launched")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
115 self._main_instance = True
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
116 self.buffer_size = const_BUFF_SIZE
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
117 else:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
118 raise e
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
119
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
120 def launchDbusMainInstance(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
121 debug ("Init DBus...")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
122 session_bus = dbus.SessionBus()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
123 self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
124 self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
125
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
126 self.copy_list = []
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
127 self.mounts = self.__getMountPoints()
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
128 self.files_left = 0
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
129 self.bytes_left = 0
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
130
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
131 def getFsType(self, path):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
132 fs=''
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
133 for mount in self.mounts:
2
1b91aa4f3c85 getFsType fix
Goffi <goffi@goffi.org>
parents: 1
diff changeset
134 if path.startswith(mount) and len(self.mounts[mount])>=len(fs):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
135 fs = self.mounts[mount]
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
136 return fs
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
137
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
138 def __getMountPoints(self):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
139 """Parse /proc/mounts to get currently mounted devices"""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
140 #TODO: reparse when a new device is added/a device is removed
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
141 #(check freedesktop mounting signals)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
142 ret = {}
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
143 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
144 with open("/proc/mounts",'r') as mounts:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
145 for line in mounts.readlines():
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
146 fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ')
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
147 ret[fs_file] = fs_vfstype
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
148 except:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
149 error ("Can't read mounts table")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return ret
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
151
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
152 def __appendToList(self, path, dest_path, options):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
153 """Add a file to the copy list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
154 @param path: absolute path of file
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
155 @param options: options as return by optparse"""
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
156 debug ("Adding to copy list: %s ==> %s (%s)", path, dest_path, self.getFsType(dest_path))
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
157 try:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
158 self.bytes_left+=os.path.getsize(path)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
159 self.files_left+=1
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
160 self.copy_list.append((path, dest_path, options))
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
161 print "total size:", float(self.bytes_left/1024/1024), "Mb (%i)" % self.files_left
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
162 except OSError,e:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
163 error("Can't copy %(path)s: %(exception)s" % {'path':path, 'exception':e.strerror})
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
164
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
165
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
166 def __appendDirToList(self, dirpath, dest_path, options):
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
167 """Add recursively directory to the copy list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
168 @param path: absolute path of dir
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
169 @param options: options as return by optparse"""
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
170 #We first check that the dest path exists, and create it if needed
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
171 if not os.path.exists(dest_path):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
172 debug ("Creating directory %s" % dest_path)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
173 os.makedirs(dest_path) #TODO: check permissions
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
174 #TODO: check that dest_path is an accessible dir,
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
175 # and skip file/write error in log if needed
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
176 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
177 for filename in os.listdir(dirpath):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
178 filepath = os.path.join(dirpath,filename)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
179 if os.path.isdir(filepath):
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
180 full_dest_path = os.path.join(dest_path,filename)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
181 self.__appendDirToList(filepath, full_dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
182 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
183 self.__appendToList(filepath, dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
184 except OSError,e:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
185 error("Can't copy %(path)s: %(exception)s" % {'path':dirpath, 'exception':e.strerror})
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
186
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def __checkArgs(self, options, source_path, args):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
188 """Check thats args are files, and add them to copy list"""
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
189 assert(len (args)>=2)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
190 try:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
191 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
192 except OSError,e:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
193 error ("Invalid dest_path: %s",e)
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
194
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
195 for path in args:
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
196 abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
197 if not os.path.exists(abspath):
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
198 warning("The path given in arg doesn't exist or is not accessible: %s",abspath)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
199 else:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
200 if os.path.isdir(abspath):
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
201 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
202 if not options.recursive:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
203 warning ('omitting directory "%s"' % abspath)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
204 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
205 self.__appendDirToList(abspath, full_dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
206 else:
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
207 self.__appendToList(abspath, dest_path, options)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
208
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
209 def __copyNextFile(self):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
210 """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
211 @return: True a file was added, False else"""
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
212 if self.copy_list:
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
213 source_path, dest_path, options = self.copy_list.pop()
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
214 source_fd = open(source_path, 'r')
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
215 filename = os.path.basename(source_path)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
216 assert(filename)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
217 dest_file = os.path.join(dest_path,filename)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
218 if os.path.exists(dest_file):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
219 warning ("File [%s] already exists, skipping it !" % dest_file)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
220 return True
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
221 dest_fd = open(dest_file, 'w')
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
222
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
223 self.total=0
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
224 gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile,(dest_fd,options), priority=gobject.PRIORITY_HIGH)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
225 print "** COPYING",source_path,"==>",dest_file
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
226 return True
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
227 else:
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
228 #Nothing left to copy, we quit
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
229 self.loop.quit()
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
230
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
231 def _copyFile(self, source_fd, condition, data):
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
232 """Actually copy the file, callback used with io_add_watch
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
233 @param source_fd: file descriptor of the file to copy
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
234 @param condition: condition which launched the callback (glib.IO_IN)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
235 @param data: tuple with (destination file descriptor, copying options)"""
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
236 dest_fd,options = data
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
237 buff = source_fd.read(self.buffer_size)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
238 dest_fd.write(buff)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
239 self.total += len(buff)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
240 sys.stdout.write('%i written\r' % self.total)
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
241 if len(buff) != self.buffer_size:
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
242 sys.stdout.write('\n---\n')
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
243 source_fd.close()
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
244 dest_fd.close()
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
245 return False
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
246 return True
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
247
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
248
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
249
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
250 def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()):
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
251 """Parse arguments and add files to queue
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
252 @param full_args: list of arguments strings (without program name)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
253 @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
254 @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
255 validation, and message is the error message to print when necessary"""
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
256 _usage="""
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
257 %prog [options] FILE1 [FILE2 ...] DEST
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
258
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
259 %prog --help for options list
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
260 """
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
261 for idx in range(len(full_args)):
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
262 if isinstance(full_args[idx], unicode):
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
263 #We don't want unicode as some filenames can be invalid unicode
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
264 full_args[idx] = full_args[idx].encode('utf-8')
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
265
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
266 parser = OptionParser(usage=_usage,version=ABOUT)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
267
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
268 parser.add_option("-r", "--recursive", action="store_true", default=False,
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
269 help="copy directories recursively")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
270
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
271 parser.add_option("--no-unicode-fix", action="store_true", default=False,
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
272 help="don't fixe name encoding errors") #TODO
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
273
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
274 parser.add_option("--no-progress", action="store_false", dest="progress", default=True,
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
275 help="deactivate progress bar")
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
276
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
277 (options, args) = parser.parse_args(full_args)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
278 if options.progress and not pbar_available:
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
279 warning ("Progress bar is not available, deactivating")
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
280 options.progress = False
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
281
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
282 if not self._main_instance:
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
283 info ("There is already one instance of %s running, pluging to it" % NAME_SHORT)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
284 #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
285 # can have invalid unicode.
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
286 return self.gcp_main.addArgs(os.getcwd(),pickle.dumps(full_args))
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
287 else:
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
288 if len(args) < 2:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
289 _error_msg = "Wrong number of arguments"
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
290 return (False, _error_msg)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
291 debug("adding args to gcp: %s",args)
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
292 self.__checkArgs(options, source_path, args)
3
6a24b5928980 Basic file copying
Goffi <goffi@goffi.org>
parents: 2
diff changeset
293 gobject.idle_add(self.__copyNextFile)
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
294 return (True,'')
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
295
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
296 def go(self):
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
297 """Launch main loop"""
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
298 self.loop = gobject.MainLoop()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
299 try:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
300 self.loop.run()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
301 except KeyboardInterrupt:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
302 info("User interruption: good bye")
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
303
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
304
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
305 if __name__ == "__main__":
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
306 gcp = GCP()
1
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
307 success,message = gcp.parseArguments()
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
308 if not success:
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
309 error(message)
995487813501 Arguments management
Goffi <goffi@goffi.org>
parents: 0
diff changeset
310 exit(1)
0
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
311 if gcp._main_instance:
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
312 gcp.go()
674ce820a4ef initial commit
Goffi <goffi@goffi.org>
parents:
diff changeset
313