Using Sets on files
Problem: I had files from a directory I had on one machine to a directory on another. I didn’t care about subdirectories (there are none) nor did I care about files on the remote machine not being on the local side. What I did care about was not overwriting the files that were already on the local machine, even if they were on the remote side. Note that this is quite different than Unison, which checks time stamps, size, etc. I really just wanted to check filenames and move them around if need-be.
I used the almighty sets package. I actually made three Set objects: one for files I didn’t want to move at all, one of all the files on the remote site, and one of all the files on the local side. I then did a difference on the local set to the remote set, and then another different from that set to the ignored set. This sounds waay complicated, but it’s really not.
Here is the code. Note that I’m also using the almighty path module, which has become a standard for me.
#!/usr/bin/env python
import sys,os,string
from path import path
from sets import Set
host="foo.com"
rdir="/directory/to/copy/to."
if __name__=='__main__':
ignoreFiles = Set(["list","of","files"])
rfiles = []
for f in os.popen('ssh %s "find %s"' %(host,rdir)):
rfiles.append(path(f.strip()))
rfileSet = Set( [x.basename() for x in rfiles])
lfiles = []
for f in path(".").listdir():
lfiles.append(path(f))
lfileSet = Set( [x.basename() for x in lfiles])
localOnly = lfileSet-rfileSet
localOnly = localOnly-ignoreFiles
copyCmd = 'scp -r "%s" "%s:%s"'
for f in localOnly:
os.system(copyCmd %(f,host,rdir))
