comparison 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
comparison
equal deleted inserted replaced
14:21b38ed339a4 15:8253a5ccbe9a
70 70
71 const_DBUS_INTERFACE = "org.goffi.gcp" 71 const_DBUS_INTERFACE = "org.goffi.gcp"
72 const_DBUS_PATH = "/org/goffi/gcp" 72 const_DBUS_PATH = "/org/goffi/gcp"
73 const_BUFF_SIZE = 4096 73 const_BUFF_SIZE = 4096
74 const_PRESERVE = set(['mode','ownership','timestamps']) 74 const_PRESERVE = set(['mode','ownership','timestamps'])
75 const_JOURNAL_PATH = "~/.gcp_journal" 75 const_FILES_DIR = "~/.gcp"
76 const_JOURNAL_PATH = const_FILES_DIR + "/journal"
77 const_SAVED_LIST = const_FILES_DIR + "/saved_list"
76 78
77 79
78 class DbusObject(dbus.service.Object): 80 class DbusObject(dbus.service.Object):
79 81
80 def __init__(self, gcp, bus, path): 82 def __init__(self, gcp, bus, path):
138 140
139 141
140 class GCP(): 142 class GCP():
141 143
142 def __init__(self): 144 def __init__(self):
145 files_dir = os.path.expanduser(const_FILES_DIR)
146 if not os.path.exists(files_dir):
147 os.makedirs(files_dir)
143 try: 148 try:
144 sessions_bus = dbus.SessionBus() 149 sessions_bus = dbus.SessionBus()
145 db_object = sessions_bus.get_object(const_DBUS_INTERFACE, 150 db_object = sessions_bus.get_object(const_DBUS_INTERFACE,
146 const_DBUS_PATH) 151 const_DBUS_PATH)
147 self.gcp_main = dbus.Interface(db_object, 152 self.gcp_main = dbus.Interface(db_object,
243 warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath.decode('utf-8','replace')) 248 warning(_("The path given in arg doesn't exist or is not accessible: %s") % abspath.decode('utf-8','replace'))
244 else: 249 else:
245 if os.path.isdir(abspath): 250 if os.path.isdir(abspath):
246 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path)) 251 full_dest_path = dest_path if os.path.isabs(path) else os.path.normpath(os.path.join(dest_path, path))
247 if not options.recursive: 252 if not options.recursive:
248 warning (_('omitting directory "%s"') % abspath) 253 warning (_('omitting directory "%s"') % abspath.decode('utf-8','replace'))
249 else: 254 else:
250 self.__appendDirToList(abspath, full_dest_path, options) 255 self.__appendDirToList(abspath, full_dest_path, options)
251 else: 256 else:
252 self.__appendToList(abspath, dest_path, options) 257 self.__appendToList(abspath, dest_path, options)
253 258
435 parser.add_option("-f", "--force", action="store_true", default=False, 440 parser.add_option("-f", "--force", action="store_true", default=False,
436 help=_("force overwriting of existing files")) 441 help=_("force overwriting of existing files"))
437 442
438 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps', 443 parser.add_option("--preserve", action="store", default='mode,ownership,timestamps',
439 help=_("preserve the specified attributes")) 444 help=_("preserve the specified attributes"))
445
446 parser.add_option("--save", action="store",
447 help=_("Save source arguments"))
448
449 parser.add_option("--force-save", action="store",
450 help=_("Save source arguments and replace memory if it already exists"))
451
452 parser.add_option("--load", action="store",
453 help=_("Load source arguments"))
454
455 parser.add_option("--list", action="store_true", default=False,
456 help=_("List names of saved sources"))
457
458 parser.add_option("--full-list", action="store_true", default=False,
459 help=_("List names of saved sources and files in it"))
440 460
441 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True, 461 parser.add_option("--no-unicode-fix", action="store_false", dest='unicode_fix', default=True,
442 help=_("don't fixe name encoding errors")) #TODO 462 help=_("don't fixe name encoding errors")) #TODO
443 463
444 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True, 464 parser.add_option("--no-fs-fix", action="store_false", dest='fs_fix', default=True,
467 for value in const_PRESERVE: 487 for value in const_PRESERVE:
468 error('- %s' % value) 488 error('- %s' % value)
469 exit(2) 489 exit(2)
470 else: 490 else:
471 options.preserve = preserve 491 options.preserve = preserve
492
493 if options.save or options.load or options.list or options.full_list:
494 try:
495 with open(os.path.expanduser(const_SAVED_LIST),'r') as saved_fd:
496 saved_files = pickle.load(saved_fd)
497 except:
498 saved_files={}
499
500 if options.list or options.full_list:
501 info(_('Saved sources:'))
502 sources = saved_files.keys()
503 sources.sort()
504 for source in sources:
505 info("\t[%s]" % source)
506 if options.full_list:
507 for filename in saved_files[source]:
508 info("\t\t%s" % filename)
509 info("---\n")
510 if not args:
511 exit(0)
512
513 if options.save or options.force_save:
514 if saved_files.has_key(options.save) and not options.force_save:
515 error(_("There is already a saved sources with this name, skipping --save"))
516 else:
517 if len(args)>1:
518 saved_files[options.save] = map(os.path.abspath,args[:-1])
519 with open(os.path.expanduser(const_SAVED_LIST),'w') as saved_fd:
520 pickle.dump(saved_files,saved_fd)
521
522 if options.load:
523 if not saved_files.has_key(options.load):
524 error(_("No saved sources with this name, check existing names with --list"))
525 else:
526 args = saved_files[options.load] + args
472 527
473 #if there is an other instance of gcp, we send options to it 528 #if there is an other instance of gcp, we send options to it
474 if not self._main_instance: 529 if not self._main_instance:
475 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT) 530 info (_("There is already one instance of %s running, pluging to it") % NAME_SHORT)
476 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames 531 #XXX: we have to serialize data as dbus only accept valid unicode, and filenames