#!/usr/bin/python #2006 Bart Cerneels # transfers files and folders beneath the path given as argument to the # first usbdevice detected by obexftp # dependancies: openobex >= 1.3 and obexftp >= 0.20 with python bindings installed # get it at http://openobex.triq.net/ # this script is written for transfering podcasts to a Sony-Ericsson K610i phone and works ok. # Your miles may vary, feel free to improve it. Any changes you wish to share can be send to me by # email. # know issues: # directory that are not top_of_tree but do not contain files are not deleted if empty. from obexftp import * from xml.dom import minidom, Node import sys import os from os.path import join from string import rsplit # Directories are listed in an XML document, parse it and return the folders. def getFolderList(obexc, path): dirlist = obexc.list(path) #create dom document doc = minidom.parseString(dirlist) folderlist = doc.getElementsByTagName("folder-listing")[0].getElementsByTagName("folder") folders = list() for folder in folderlist: folders.append( folder.attributes.get('name').value ) return folders def main(): #open obexclient for USB obexc = client(USB) #connect to USB device 0 obexc.connect("k610i", 0) #use the first arguments as the start and walk down the directory tree top_of_tree = sys.argv[1] for filepath, dirs, files in os.walk(top_of_tree): #this is the place I keep my podcasts currentpath = join('/Memory Stick/Music', rsplit(filepath, sys.argv[1])[1]) print "chpath", join('/Memory Stick/Music', currentpath) print obexc.chpath(currentpath) if len(dirs) > 0: for dirname in dirs: print "mkpath",join(dirname), obexc.mkpath(join(dirname)) print obexc.chpath(currentpath) if len(files) > 0: for file in files: filename = join(filepath,file) print "put_file", filename if obexc.put_file(filename) > 0 : print "success, removing file" print os.remove(filename) #FIXME: delete empty parent directory to, if != top_of_tree if filepath != top_of_tree and len(os.listdir(filepath)) == 0: print filepath, "is empty, deleting" print os.rmdir(filepath) print "=" * 100 pass if __name__ == "__main__": main()