Synchronisation de fichiers

Outil ‘rsync’

Après avoir sué vainement pour synchroniser des répertoires et fichiers contenant des espaces par le biai d’un script bash, c’est tout naturellement que je me suis tourné vers python pour essayer de résoudre mon problème.

Quelques minutes ont suffit pour que mon appel de commande rsync soit opérationnel et utilisable. Cerise sur le gâteau, il est également possible d’utiliser des variables source et destination pour définir des listes de répertoires à synchroniser.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

##
# =============================================================================
# Author : Pascal Rapaz
# Version: 1.0.0
# Date   : 15.04.2010 Creation
#          19.04.2010 Added loop for multiple folders sync
#          22.04.2010 Added zenity to display execution log
#
# License: GPL-2+
#   This package 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 2 of the License, or
#   (at your option) any later version.
#
#   This package 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.
#
#   If you need the GNU General Public License write to:
#     Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#     MA 02110-1301, USA.
#
# (c) 2010, Pascal Rapaz (RapazP - pascal.rapaz@rapazp.ch)
# =============================================================================
##
"""
Use rsync to synchronize files and folder even if there contains spaces.

To sync between a Linux and a Windows system, usually I use these options:
   OPTS="-rvDz"

To sync two Linux systems, usually I use these options:
   OPTS="-av"
"""

from subprocess import Popen, PIPE

# source and destination folders
SRC = ["/mnt/win_share/projets en cours/Spécifications métier/",
       "/mnt/win_share/RP/MPP/projets en cours/Spécifications techniques/"
      ]
DEST = ["/home/user/project/Specifications_metier/",
        "/home/user/project/Specifications_techniques/"
       ]

# rsync options
OPTS = "-rvDz"
FILES = "--include '*.doc' --include '*.xls' --include '*.ppt' --include '*.pdf' " +\
        "--exclude '*.*' --exclude '@rchive'"

GLOBAL_STDOUT = ''
GLOBAL_STDERR = ''

# prepare and launch the command
for i in range(0, len(SRC)):
    cmd = "rsync " + OPTS + " " + FILES + " '" + SRC[i] + "' '" + DEST[i] + "' "
    stdout, stderr = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE).communicate()
    
    GLOBAL_STDOUT += "\n[cmd] " + cmd + "\n\n" + stdout
    GLOBAL_STDERR += stderr
#endFor

# use zenity to open a gtk dialog box that display sdtout and stderr
if (GLOBAL_STDOUT.strip() != '' or
    GLOBAL_STDERR.strip() != ''):
    
    log = "[STDOUT]\n" + GLOBAL_STDOUT
    
    if (GLOBAL_STDERR.strip() != ''):
        log += "\n[STDERR]\n" + GLOBAL_STDERR
    #endIf

    cmd = 'echo "%s" | zenity --height="480" --width="640" --text-info --title="Execution log"' %(log)

    Popen(cmd, shell=True).communicate()
#endIf
This entry was posted in Development, GNU/Linux, Python and tagged , , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Your email address will not be published. Required fields are marked *