#!/usr/bin/python import os import urllib from lxml import etree def relpath(path, start=os.path.curdir): """Return a relative version of a path""" if not path: raise ValueError, 'no path specified' start_list = (os.path.abspath(start)).split(os.path.sep) path_list = (os.path.abspath(path)).split(os.path.sep) if start_list[0].lower() <> path_list[0].lower(): unc_path, rest = splitunc(path) unc_start, rest = splitunc(start) if (unc_path and not unc_start) or (not unc_path and unc_start): raise ValueError, "Cannot mix UNC and non-UNC paths (%s and %s)" \ % (path, start) else: raise ValueError, 'path is on drive %s, start on drive %s' \ % (path_list[0], start_list[0]) # Work out how much of the filepath is shared by start and path. for i in range(min(len(start_list), len(path_list))): if start_list[i].lower() <> path_list[i].lower(): break else: i+=1 rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:] return os.path.join(*rel_list) os.path.relpath = relpath os.chdir(os.path.expanduser('~/stash/music')) playlists = os.path.expanduser('~/.local/share/rhythmbox/playlists.xml') playlists = etree.parse(playlists) playlist = playlists.findall('.//playlist[@name="phone"]/location') command = ['rsync','-avR','booph.local:stash/music/'] for url in playlist: command.insert(-1,os.path.relpath(urllib.url2pathname(url.text)[7:])) os.spawnvp(os.P_WAIT,'rsync',command)