~linaro-automation/linaro-android-mirror/trunk

« back to all changes in this revision

Viewing changes to linaro_android_mirror/mirrorer.py

  • Committer: Michael Hudson
  • Date: 2011-02-08 03:43:27 UTC
  • Revision ID: michael.hudson@linaro.org-20110208034327-m3jz9xj6pd5jiflh
give up on tdd for the moment and hack a mirrorer that actually updates the mirrors together

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from collections import defaultdict
 
3
import errno
 
4
import os
 
5
 
 
6
from twisted.internet.defer import (
 
7
    DeferredLock,
 
8
    gatherResults,
 
9
    )
 
10
from twisted.internet.utils import getProcessValue
 
11
 
 
12
from linaro_android_mirror.factor_manifests import (
 
13
    make_mirror_manifests,
 
14
    make_slave_manifest,
 
15
    )
 
16
 
 
17
 
 
18
prefix = '/mnt/mirror'
 
19
 
 
20
_directory_locks = defaultdict(DeferredLock)
 
21
 
 
22
 
 
23
def mirror_directory(hostname, manifest):
 
24
    target_directory = os.path.join(prefix, hostname)
 
25
    d = _directory_locks[target_directory]
 
26
    def _mirror():
 
27
        assert os.path.exists(target_directory)
 
28
        manifest_path = os.path.join(target_directory, '.repo/manifest.xml')
 
29
        try:
 
30
            os.unlink(manifest_path)
 
31
        except OSError, e:
 
32
            if e.errno != errno.ENOENT:
 
33
                raise
 
34
        with open(manifest_path, 'w') as manifest_file:
 
35
            manifest_file.write(manifest)
 
36
        print "'running'", '/usr/local/bin/repo', ['sync', '-j8'], 'in', target_directory
 
37
        return 0
 
38
        return getProcessValue(
 
39
            '/usr/local/bin/repo', ['sync', '-j8'], path=target_directory)
 
40
    return d.run(_mirror)
 
41
 
 
42
 
 
43
def mirror(source_manifest):
 
44
    slave_manifest = make_slave_manifest(source_manifest)
 
45
    mirror_manifests = make_mirror_manifests(source_manifest)
 
46
    ds = []
 
47
    for hostname, manifest in mirror_manifests:
 
48
        ds.append(mirror_directory(hostname, manifest))
 
49
    return gatherResults(ds).addCallback(lambda stuff: slave_manifest)