[project @ start the copier script]
[htsworkflow.git] / uashelper / copier.py
1 import copy
2 import os
3 import re
4 import subprocess
5 import time
6
7 class rsync(object):
8  
9   def __init__(self, pwfile):
10     self.pwfile = pwfile
11     self.cmd = ['/usr/bin/rsync', '--dry-run', '-a', ]
12     self.cmd.append('--password-file=%s' % (pwfile))
13     self.source_base = 'rsync://sequencer@jumpgate.caltech.edu:8730/sequencer/'
14     self.dest_base = '/home/diane/gec/'
15     self.processes = {}
16     self.exit_code = None
17
18   def list(self):
19     """Get a directory listing"""
20     dirs_to_copy = []
21     args = copy.copy(self.cmd)
22     args.append(self.source_base)
23
24     short_process = subprocess.Popen(args, stdout=subprocess.PIPE)
25     direntries = [ x.split() for x in short_process.stdout ]
26     for permissions, size, filedate, filetime, filename in direntries:
27       #print permissions, filename
28       if permissions[0] == 'd':
29         # hey its a directory, the first step to being something we want to 
30         # copy
31         if re.match("[0-9]{6}", filename):
32           # it starts with something that looks like a 6 digit date
33           # aka good enough for me
34           dirs_to_copy.append(filename)
35     return dirs_to_copy
36
37   def create_copy_process(self, dirname):
38     args = copy.copy(self.cmd)
39     args.append(os.path.join(self.source_base, dirname))
40     args.append(self.dest_base)
41     return subprocess.Popen(args)
42  
43   def copy(self):
44     """copy any interesting looking directories over
45     """
46     dirs_to_copy = self.list()
47     for d in dirs_to_copy:
48       process = self.processes.get(d, None)
49       if process is None:
50         # we don't have a process, so make one
51         print "starting rsync", d
52         self.processes[d] = self.create_copy_process(d)
53       else:
54         retcode = process.poll()
55         if retcode is not None:
56            # we finished
57            print "rsync",d,"exited with state", retcode
58            del self.processes[d]
59
60 if __name__ == "__main__":
61   r = rsync('/home/diane/.sequencer')
62   r.copy()
63   while len(r.processes) > 0:
64     print "call..."
65     r.copy()
66     #time.sleep(0.1)