gcp/gcp

402 lines
16 KiB
Plaintext
Raw Normal View History

2010-08-25 10:11:34 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
gcp: Goffi's CoPier
Copyright (C) 2010 Jérôme Poisson (goffi@goffi.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
### logging ###
import logging
from logging import debug, info, error, warning
logging.basicConfig(level=logging.INFO,
2010-08-25 10:11:34 +02:00
format='%(message)s')
###
2010-09-27 07:09:42 +02:00
import gettext
gettext.install('gcp', "i18n", unicode=True)
2010-08-25 10:11:34 +02:00
import sys
import os,os.path
from optparse import OptionParser #To be replaced by argparse ASAP
2010-08-30 14:15:26 +02:00
import cPickle as pickle
2010-08-25 10:11:34 +02:00
try:
import gobject
#DBus
import dbus, dbus.glib
import dbus.service
import dbus.mainloop.glib
except ImportError,e:
2010-09-27 07:09:42 +02:00
error(_("Error during import"))
error(_("Please check dependecies:"),e)
2010-08-25 10:11:34 +02:00
exit(2)
try:
from progressbar import ProgressBar, Percentage, Bar, ETA, FileTransferSpeed
pbar_available=True
except ImportError, e:
2010-09-27 07:09:42 +02:00
info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar'))
info (_('Progress bar deactivated\n--\n'))
2010-08-25 10:11:34 +02:00
pbar_available=False
NAME = "gcp (Goffi's copier)"
NAME_SHORT = "gcp"
VERSION = '0.1'
2010-09-27 07:09:42 +02:00
ABOUT = NAME+u" v"+VERSION+u""" (c) Jérôme Poisson (aka Goffi) 2010
2010-08-25 10:11:34 +02:00
---
2010-09-27 07:09:42 +02:00
"""+NAME+u""" Copyright (C) 2010 Jérôme Poisson
""" + _(u"""This program comes with ABSOLUTELY NO WARRANTY;
2010-08-25 10:11:34 +02:00
This is free software, and you are welcome to redistribute it
under certain conditions.
---
This software is an advanced file copier
Get the latest version at http://www.goffi.org
2010-09-27 07:09:42 +02:00
""")
2010-08-25 10:11:34 +02:00
const_DBUS_INTERFACE = "org.goffi.gcp"
const_DBUS_PATH = "/org/goffi/gcp"
const_BUFF_SIZE = 4096
const_PRESERVE = set(['mode','ownership','timestamps'])
2010-08-25 10:11:34 +02:00
class DbusObject(dbus.service.Object):
def __init__(self, gcp, bus, path):
self._gcp = gcp
dbus.service.Object.__init__(self, bus, path)
2010-09-27 07:09:42 +02:00
debug(_("Init DbusObject..."))
2010-08-25 10:11:34 +02:00
self.cb={}
@dbus.service.method(const_DBUS_INTERFACE,
in_signature='', out_signature='s')
def getVersion(self):
"""Get gcp version
@return: version as string"""
return VERSION
@dbus.service.method(const_DBUS_INTERFACE,
2010-08-30 14:15:26 +02:00
in_signature='ss', out_signature='bs')
2010-08-25 10:11:34 +02:00
def addArgs(self, source_path, args):
"""Add arguments to gcp as if there were entered on its own command line
2010-08-30 14:15:26 +02:00
@param source_path: current working dir to use as base for arguments, as given by os.getcwd()
@param args: serialized (wich pickle) list of strings - without command name -, as given by sys.argv[1:].
@return: success (boolean) and error message if any (string)"""
try:
args = pickle.loads(str(args))
except TypeError, pickle.UnpicklingError:
2010-09-27 07:09:42 +02:00
return (False, _("INTERNAL ERROR: invalid arguments"))
2010-08-25 10:11:34 +02:00
return self._gcp.parseArguments(args, source_path)
class GCP():
def __init__(self):
try:
sessions_bus = dbus.SessionBus()
db_object = sessions_bus.get_object(const_DBUS_INTERFACE,
const_DBUS_PATH)
self.gcp_main = dbus.Interface(db_object,
dbus_interface=const_DBUS_INTERFACE)
self._main_instance = False
except dbus.exceptions.DBusException,e:
if e._dbus_error_name=='org.freedesktop.DBus.Error.ServiceUnknown':
self.launchDbusMainInstance()
2010-09-27 07:09:42 +02:00
debug (_("gcp launched"))
2010-08-25 10:11:34 +02:00
self._main_instance = True
self.buffer_size = const_BUFF_SIZE
2010-08-25 10:11:34 +02:00
else:
raise e
def launchDbusMainInstance(self):
2010-09-27 07:09:42 +02:00
debug (_("Init DBus..."))
2010-08-25 10:11:34 +02:00
session_bus = dbus.SessionBus()
self.dbus_name = dbus.service.BusName(const_DBUS_INTERFACE, session_bus)
self.dbus_object = DbusObject(self, session_bus, const_DBUS_PATH)
self.copy_list = []
self.mounts = self.__getMountPoints()
self.bytes_total = 0
self.bytes_copied = 0
2010-08-25 10:11:34 +02:00
def getFsType(self, path):
fs = ''
last_mount_point = ''
2010-08-25 10:11:34 +02:00
for mount in self.mounts:
if path.startswith(mount) and len(mount)>=len(last_mount_point):
2010-08-25 10:11:34 +02:00
fs = self.mounts[mount]
last_mount_point = mount
2010-08-25 10:11:34 +02:00
return fs
def __getMountPoints(self):
"""Parse /proc/mounts to get currently mounted devices"""
#TODO: reparse when a new device is added/a device is removed
2010-08-30 14:15:26 +02:00
#(check freedesktop mounting signals)
2010-08-25 10:11:34 +02:00
ret = {}
try:
with open("/proc/mounts",'rb') as mounts:
2010-08-25 10:11:34 +02:00
for line in mounts.readlines():
fs_spec, fs_file, fs_vfstype, fs_mntops, fs_freq, fs_passno = line.split(' ')
ret[fs_file] = fs_vfstype
except:
2010-09-27 07:09:42 +02:00
error (_("Can't read mounts table"))
2010-08-25 10:11:34 +02:00
return ret
def __appendToList(self, path, dest_path, options):
2010-08-25 10:11:34 +02:00
"""Add a file to the copy list
@param path: absolute path of file
@param options: options as return by optparse"""
debug ("Adding to copy list: %(path)s ==> %(dest_path)s (%(fs_type)s)" % {"path":path,
"dest_path":dest_path,
"fs_type":self.getFsType(dest_path)} )
2010-08-30 14:15:26 +02:00
try:
self.bytes_total+=os.path.getsize(path)
self.copy_list.insert(0,(path, dest_path, options))
2010-08-30 14:15:26 +02:00
except OSError,e:
2010-09-27 07:09:42 +02:00
error(_("Can't copy %(path)s: %(exception)s") % {'path':path, 'exception':e.strerror})
2010-08-25 10:11:34 +02:00
2010-08-30 14:15:26 +02:00
def __appendDirToList(self, dirpath, dest_path, options):
2010-08-25 10:11:34 +02:00
"""Add recursively directory to the copy list
@param path: absolute path of dir
@param options: options as return by optparse"""
#We first check that the dest path exists, and create it if needed
dest_path = self.__filename_fix(dest_path, options)
if not os.path.exists(dest_path):
2010-09-27 07:09:42 +02:00
debug (_("Creating directory %s") % dest_path)
os.makedirs(dest_path) #TODO: check permissions
#TODO: check that dest_path is an accessible dir,
# and skip file/write error in log if needed
2010-08-25 10:11:34 +02:00
try:
for filename in os.listdir(dirpath):
filepath = os.path.join(dirpath,filename)
if os.path.isdir(filepath):
full_dest_path = os.path.join(dest_path,filename)
self.__appendDirToList(filepath, full_dest_path, options)
2010-08-25 10:11:34 +02:00
else:
self.__appendToList(filepath, dest_path, options)
2010-08-25 10:11:34 +02:00
except OSError,e:
2010-09-27 07:09:42 +02:00
error(_("Can't copy %(path)s: %(exception)s") % {'path':dirpath, 'exception':e.strerror})
2010-08-25 10:11:34 +02:00
def __checkArgs(self, options, source_path, args):
"""Check thats args are files, and add them to copy list"""
2010-08-30 14:15:26 +02:00
assert(len (args)>=2)
try:
dest_path = os.path.normpath(os.path.join(os.path.expanduser(source_path), args.pop()))
2010-08-30 14:15:26 +02:00
except OSError,e:
2010-09-27 07:09:42 +02:00
error (_("Invalid dest_path: %s"),e)
2010-08-30 14:15:26 +02:00
2010-08-25 10:11:34 +02:00
for path in args:
2010-08-30 14:15:26 +02:00
abspath = os.path.normpath(os.path.join(os.path.expanduser(source_path), path))
2010-08-25 10:11:34 +02:00
if not os.path.exists(abspath):
2010-09-27 07:09:42 +02:00
warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath)
2010-08-25 10:11:34 +02:00
else:
if os.path.isdir(abspath):
full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path))
2010-08-25 10:11:34 +02:00
if not options.recursive:
2010-09-27 07:09:42 +02:00
warning (_('omitting directory "%s"') % abspath)
2010-08-25 10:11:34 +02:00
else:
self.__appendDirToList(abspath, full_dest_path, options)
2010-08-25 10:11:34 +02:00
else:
self.__appendToList(abspath, dest_path, options)
def __copyNextFile(self):
"""Take the last file in the list, and launch the copy using glib io_watch event
@return: True a file was added, False else"""
if self.copy_list:
source_file, dest_path, options = self.copy_list.pop()
source_fd = open(source_file, 'rb')
filename = os.path.basename(source_file)
assert(filename)
dest_file = self.__filename_fix(os.path.join(dest_path,filename),options)
if os.path.exists(dest_file) and not options.force:
2010-09-27 07:09:42 +02:00
warning (_("File [%s] already exists, skipping it !") % dest_file)
return True
dest_fd = open(dest_file, 'wb')
gobject.io_add_watch(source_fd,gobject.IO_IN,self._copyFile,
(dest_fd, options), priority=gobject.PRIORITY_HIGH)
if not self.progress:
2010-09-27 07:09:42 +02:00
info(_("COPYING %(source)s ==> %(dest)s") % {"source":source_path,"dest":dest_file})
return True
else:
#Nothing left to copy, we quit
if self.progress:
self.__pbar_finish()
self.loop.quit()
def _copyFile(self, source_fd, condition, data):
"""Actually copy the file, callback used with io_add_watch
@param source_fd: file descriptor of the file to copy
@param condition: condition which launched the callback (glib.IO_IN)
@param data: tuple with (destination file descriptor, copying options)"""
dest_fd,options = data
buff = source_fd.read(self.buffer_size)
dest_fd.write(buff)
self.bytes_copied += len(buff)
if self.progress:
self.__pbar_update()
if len(buff) != self.buffer_size:
source_fd.close()
dest_fd.close()
self.__post_copy(source_fd.name, dest_fd.name, options)
return False
return True
2010-08-25 10:11:34 +02:00
def __filename_fix(self, filename, options):
if self.getFsType(filename) == 'vfat' and options.fs_fix:
filename = filename.replace('\\','_')\
.replace(':',';')\
.replace('*','+')\
.replace('?','')\
.replace('"','\'')\
.replace('<','[')\
.replace('>',']')\
.replace('|','!')
return filename
def __post_copy(self, source_file, dest_file, options):
"""Do post copy traitement (mainly managing --preserve option)"""
st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime = os.stat(source_file)
#TODO: complete log in case of errors
for preserve in options.preserve:
try:
if preserve == 'mode':
os.chmod(dest_file, st_mode)
elif preserve == 'ownership':
os.chown(dest_file, st_uid, st_gid)
elif preserve == 'timestamps':
os.utime(dest_file, (st_atime, st_mtime))
except OSError,e:
pass #TODO: complete log here
def __pbar_update(self):
"""Update progress bar position, create the bar if it doesn't exist"""
assert(self.progress)
try:
if self.pbar.maxval != self.bytes_total:
self.pbar.maxval = self.bytes_total
except AttributeError:
if not self.bytes_total:
#No progress bar if the files have a null size
return
2010-09-27 07:09:42 +02:00
self.pbar = ProgressBar(self.bytes_total,[_("Progress: "),Percentage()," ",Bar()," ",FileTransferSpeed()," ",ETA()])
self.pbar.start()
self.pbar.update(self.bytes_copied)
def __pbar_finish(self):
"""Mark the progression as finished"""
assert(self.progress)
try:
self.pbar.finish()
except AttributeError:
pass
2010-08-25 10:11:34 +02:00
def parseArguments(self, full_args=sys.argv[1:], source_path = os.getcwd()):
2010-08-30 14:15:26 +02:00
"""Parse arguments and add files to queue
@param full_args: list of arguments strings (without program name)
@param source_path: path from where the arguments come, ad given by os.getcwd()
@return: a tuple (boolean, message) where the boolean is the success of the arguments
validation, and message is the error message to print when necessary"""
2010-08-25 10:11:34 +02:00
_usage="""
%prog [options] FILE1 [FILE2 ...] DEST
%prog --help for options list
"""
2010-08-30 14:15:26 +02:00
for idx in range(len(full_args)):
if isinstance(full_args[idx], unicode):
#We don't want unicode as some filenames can be invalid unicode
full_args[idx] = full_args[idx].encode('utf-8')
2010-08-25 10:11:34 +02:00
parser = OptionParser(usage=_usage,version=ABOUT)
parser.add_option("-r", "--recursive", action="store_true", default=False,
2010-09-27 07:09:42 +02:00
help=_("copy directories recursively"))
2010-08-25 10:11:34 +02:00
parser.add_option("-f", "--force", action="store_true", default=False,
2010-09-27 07:09:42 +02:00
help=_("force overwriting of existing files"))
parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
2010-09-27 07:09:42 +02:00
help=_("preserve the specified attributes"))
parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True,
2010-09-27 07:09:42 +02:00
help=_("don't fixe name encoding errors")) #TODO
parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True,
help=_("don't fixe filesystem name incompatibily")) #TODO
parser.add_option("--no-progress", action="store_false", dest="progress", default=True,
2010-09-27 07:09:42 +02:00
help=_("deactivate progress bar"))
parser.add_option("-v", "--verbose", action="store_true", default=False,
2010-09-27 07:09:42 +02:00
help=_("Show what is currently done"))
2010-08-25 10:11:34 +02:00
(options, args) = parser.parse_args(full_args)
#options check
if options.progress and not pbar_available:
2010-09-27 07:09:42 +02:00
warning (_("Progress bar is not available, deactivating"))
options.progress = self.progress = False
else:
self.progress = options.progress
if options.verbose:
logging.getLogger().setLevel(logging.DEBUG)
preserve = set(options.preserve.split(','))
if not preserve.issubset(const_PRESERVE):
2010-09-27 07:09:42 +02:00
error (_("Invalide --preserve value\nvalid values are:"))
for value in const_PRESERVE:
error('- %s' % value)
exit(2)
else:
options.preserve = preserve
2010-08-25 10:11:34 +02:00
#if there is an other instance of gcp, we send options to it
2010-08-25 10:11:34 +02:00
if not self._main_instance:
2010-09-27 07:09:42 +02:00
info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT)
2010-08-30 14:15:26 +02:00
#XXX: we have to serialize data as dbus only accept valid unicode, and filenames
# can have invalid unicode.
return self.gcp_main.addArgs(os.getcwd(),pickle.dumps(full_args))
2010-08-25 10:11:34 +02:00
else:
2010-08-30 14:15:26 +02:00
if len(args) < 2:
2010-09-27 07:09:42 +02:00
_error_msg = _("Wrong number of arguments")
2010-08-30 14:15:26 +02:00
return (False, _error_msg)
2010-09-27 07:09:42 +02:00
debug(_("adding args to gcp: %s"),args)
2010-08-25 10:11:34 +02:00
self.__checkArgs(options, source_path, args)
gobject.idle_add(self.__copyNextFile)
2010-08-30 14:15:26 +02:00
return (True,'')
2010-08-25 10:11:34 +02:00
def go(self):
2010-08-30 14:15:26 +02:00
"""Launch main loop"""
2010-08-25 10:11:34 +02:00
self.loop = gobject.MainLoop()
try:
self.loop.run()
except KeyboardInterrupt:
2010-09-27 07:09:42 +02:00
info(_("User interruption: good bye"))
2010-08-25 10:11:34 +02:00
if __name__ == "__main__":
gcp = GCP()
2010-08-30 14:15:26 +02:00
success,message = gcp.parseArguments()
if not success:
error(message)
exit(1)
2010-08-25 10:11:34 +02:00
if gcp._main_instance:
gcp.go()