aboutsummaryrefslogtreecommitdiff
blob: 85b6a4dfe24133285bab35d7dfceec120c7c4fd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# vim: set sw=4 sts=4 et :
# Copyright: 2008 Gentoo Foundation
# Author(s): Nirbheek Chauhan <nirbheek.chauhan@gmail.com>
# License: GPL-2
#
# Immortal lh!
#

import os, time
import os.path as osp
from .. import const

class Syncer(object):
    """
    Sync stuff
    """

    def __init__(self, uri=const.JOBTAGE_URI, rev='last:1', destdir=const.JOBTAGE_DIR, scheme='bzr'):
        """
        Default is to sync the jobtage tree from the default location.

        @param destdir: Destination directory
        @scheme destdir: string

        @param uri: Uri where to sync from
        @scheme uri: string

        @param rev: Revision of SCM (if applicable), default is latest
        @scheme rev: string

        @param scheme: The URI scheme
        @scheme scheme: string
        """
        if destdir.endswith('/'):
            destdir = destdir[:-1]
        self.destdir = destdir
        if uri.endswith('/'):
            uri = uri[:-1]
        self.uri = uri
        self.rev = rev
        self.scheme = scheme

    def sync(self):
        """
        Sync. self.destdir must not exist if init-ing
        Returns None if unsuccessful, returns True otherwise.
        """
        init_command = ''
        sync_command = ''
        if self.scheme == 'bzr':
            init_command = 'bzr branch -r"%s" "%s" "%s"' % (self.rev, self.uri, self.destdir)
            sync_command = 'bzr pull --overwrite -r"%s" -d "%s" "%s"' % (self.rev, self.destdir, self.uri)
        elif self.scheme == 'bzr-export':
            init_command = 'bzr export -r"%s" "%s" "%s"' % (self.rev, self.destdir, self.uri)
            sync_command = 'rm -rf \"%s\" && %s' % (self.destdir, init_command)
        elif self.scheme == 'rsync':
            init_command = 'rsync -avz --delete "%s" "%s"' % (self.uri, self.destdir)
            sync_command = init_command
        else:
            print "Unknown scheme: %s" % self.scheme
            return None
        result = 0
        if osp.exists(self.destdir):
            if not osp.isdir(self.destdir):
                return None
            print 'Syncing...'+sync_command
            result = os.system(sync_command)
        else:
            if not osp.exists(osp.dirname(self.destdir)):
                # Create parents
                os.makedirs(osp.dirname(self.destdir))
            print 'Initing...'+init_command
            result = os.system(init_command)

        if not result == 0:
            return None
        return True