summaryrefslogtreecommitdiffstats
path: root/modules/darcs.py
blob: 552e9b53f43be82c84a3c39383121a6fb3d49da5 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Fedora Developer Shell
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors: Yaakov M. Nemoy <ynemoy@redhat.com>
#

from __future__ import with_statement

import re

from contextlib import contextmanager
from os import getcwd
from os.path import join
from subprocess import Popen, PIPE

from base.util import pwd, log, rm, log_file, copy, move
from modules.revisioncontrol import RevisionControl

hash_re = re.compile(r'hash=\'(\w|-|.*?)\'', re.MULTILINE)
date_re = re.compile(r'date=\'(\d*?)\'', re.MULTILINE)

class Darcs(RevisionControl):
    _type = 'darcs'
    def load_dir(self, dir):
        super(RevisionControl, self).load_dir(dir)
    
    @property
    def vc_url(self):
        return self.cfg['vc_url']

    def source_dir(self, *args):
        return self.cfg['source']

    @contextmanager
    def src_dir(self, *args):
        with src(*args):
            with pwd(self.cfg['source']):
                yield

    @contextmanager
    def src(self, *args):
        if args:
            old_src = self.cfg['source']
            self.set_cur_to(*args)
        yield
        if args:
            rm(self.cfg['source'])
            self.cfg['source'] = old_src
            self.set_current_head()


    def get(self, src, tgt, *args):
        with pwd(self.dir):
            self.cfg['source'] = tgt
            with log_file('darcs.log') as darcs_out:
                p = Popen(['darcs', 'get'] + list(args) + [src, tgt],
                          stdout = darcs_out, stderr = darcs_out)
                log.info('darcs get %s %s, please wait....' % (src, tgt))
                p.wait()
            self.set_current_head()

    def checkout(self, tgt, url, *args):
        self.cfg['vc_url'] = url
        self.get(url, tgt, *args)

    def set_current_head(self):
        log.debug(getcwd())
        with pwd(self.cfg['source']):
            p = Popen(['darcs', 'changes', '--xml-output', '--last=1'],
                      stdout = PIPE, stderr = PIPE)
            change = p.communicate()[0]
            hash = hash_re.search(change).groups()[0]
            date = date_re.search(change).groups()[0]
            self.cfg['head'] = (hash, date)

    def set_cur_to(self, *args):
        cur_src_dir = self.cfg['source']
        new_src_dir = cur_src_dir + '_tmp'
        self.get(cur_src_dir, new_src_dir, *args)

    def set_cur_to_patch(self, hash):
        self.set_cur_to('--to-match', 'hash ' + hash)

    def set_cur_to_tag(self, tag):
        self.set_cur_to('--tag', tag)

    @property
    def date(self):
        return self.cfg['head'][1]

    @property
    def hash(self):
        return self.cfg['head'][0]

    def set_cur_to_patch(self, hash):
        self.set_cur_to('--to-match', 'hash ' + hash)

    def set_cur_to_tag(self, tag):
        self.set_cur_to('--tag', tag)

    @contextmanager
    def patch(self, hash):
        with self.src('--to-match', 'hash ' + hash):
            yield

    @contextmanager
    def tag(self, tag):
        with self.src('--tag', tag):
            yield

    
    def setup_sourceball(self):
        log.debug('someone set us up the bomb')
        with pwd(self.dir):
            with pwd(self.source_dir()):
                name = self.pkg_name
                ver = self.ver()
                date = self.date
                full_name = '%s-%s.%sdarcs' % (name, ver, date)
                log.debug('full name is ' + full_name)
                with log_file('darcs.log') as darcs_out:
                    log.debug('we get signal')
                    p = Popen(['darcs', 'dist', '-d', full_name],
                              stdout=darcs_out, stderr=darcs_out)
                    log.info('generating tarball %s.tar.gz, please wait...' 
                             % full_name)
                    p.wait()
            sourceball = full_name + '.tar.gz'
            move(join(self.source_dir(), sourceball), sourceball)
            self.cfg['sourceball'] = sourceball
        
__all__ = ['Darcs']