Posted by Canx on Thu 5 Nov 16:33 (modification of post by Canx view diff)
report abuse | download | new post
- #!/usr/bin/env python
- #
- # syncusb.py
- # Sincroniza bidireccionalmente un dispositivo extraible al montar y al desmontar.
- # Autor: Canx
- # Requisitos: unison, python-gobject-dev
- # python-daemon(http://pypi.python.org/pypi/python-daemon/)
- # INSTALACION
- # 1.- Situar en el home con permisos de ejecucion.
- # 2.- Agregar en Sistemas->Preferencias->Aplicaciones de Inicio
- # CONFIGURACION
- # uuid_usb -> identificador del dispositivo del tipo XXXX-XXXX (usar lsusb para averiguarlo)
- # origen -> directorio del dispositivo extraible
- # destino -> directorio local
- uuid_usb="5842-B0AA"
- origen="/media/disk/Documentos/"
- destino="/home/canx/Documentos/"
- # FIN CONFIGURACION
- from subprocess import Popen, PIPE, STDOUT
- import os
- import gobject
- import gio
- import daemon
- import syslog
- class SyncPendrive:
- def mount_pre_unmount(self, volume_monitor, drive):
- syslog.syslog("sincronizando al desmontar...")
- self.sincronizar(volume_monitor.get_mounts()[0].get_uuid())
- def mount_added(self, volume_monitor, drive):
- syslog.syslog("sincronizando al montar...")
- self.sincronizar(volume_monitor.get_mounts()[0].get_uuid())
- def sincronizar(self, uuid):
- if uuid == uuid_usb:
- if os.path.exists(origen) and os.path.exists(destino):
- output = Popen(["unison", "-batch", "-perms", "0", origen, destino], stdout=PIPE).communicate()[0]
- syslog.syslog(output)
- syslog.syslog("sincronizacion completada!")
- else:
- syslog.syslog("no existe algun directorio!")
- else:
- syslog.syslog(uuid + " no es nuestro dispositivo!")
- def __init__(self):
- monitor = gio.volume_monitor_get()
- monitor.connect("mount-pre-unmount", self.mount_pre_unmount)
- monitor.connect("mount-added", self.mount_added)
- with daemon.DaemonContext():
- # TODO: Solo arrancar si existe unison
- prueba = SyncPendrive()
- loop = gobject.MainLoop()
- loop.run()
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.