summaryrefslogtreecommitdiffhomepage
path: root/git_buildpackage.py
blob: d046c7f67beca17d3fa6fbb7e08ab777ec789924 (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
145
146
147
148
149
150
# helper classes for git-buildpackge and friends
# (C) 2006 Guido Guenther <agx@sigxcpu.org>

import subprocess
import sys

class CommandExecFailed(Exception):
    pass

class Command(object):
    verbose=False

    def __init__(self, cmd, args=[]):
        self.cmd=cmd
        self.args=args

    def __run(self, args):
        try:
            if self.verbose:
                print self.cmd, self.args, args
            retcode = subprocess.call([self.cmd]+self.args+args)
            if retcode < 0:
                print >>sys.stderr, "%s was terminated by signal %d" % (self.cmd,  -retcode)
            elif retcode > 0:
                print >>sys.stderr, "%s returned %d" % (self.cmd,  retcode)
        except OSError, e:
            print >>sys.stderr, "Execution failed:", e
            retcode=1
        if retcode:
            print >>sys.stderr,self.run_error
        return retcode

    def __call__(self, args=[]):
        if self.__run(args):
            raise CommandExecFailed


class UnpackTGZ(Command):
    def __init__(self, tgz, dir):
        self.tgz=tgz
        self.dir=dir
        Command.__init__(self, 'tar', [ '-C', dir, '-zxf', tgz ])
        self.run_error="Couldn't unpack %s" % (self.tgz,)


class RemoveTree(Command):
    "Remove a whole directory tree"
    def __init__(self, tree):
        self.tree=tree
        Command.__init__(self, 'rm', [ '-rf', tree ])
        self.run_error="Couldn't remove %s" % (self.tree,)


class Dch(Command):
    def __init__(self, version, msg):
        args=['-v', version]
        if msg:
            args.append(msg)
        Command.__init__(self, 'dch', args)
        self.run_error="Dch failed."


class DpkgSourceExtract(Command):
    def __init__(self):
        Command.__init__(self, 'dpkg-source', ['-x'])
    
    def __call__(self, dsc, output_dir):
        self.run_error="Couldn't extract %s" % (dsc,)
        Command.__call__(self, [dsc, output_dir])


class GitLoadDirs(Command):
    def __init__(self):
        Command.__init__(self, 'git_load_dirs')

    def __call__(self, dir, log=''):
        self.dir=dir
        self.run_error="Couldn't import %s" % self.dir
        args=[ [],['-L', log] ] [len(log) > 0]
        Command.__call__(self, args+[dir])


class GitCommand(Command):
    "Mother/Father of all git commands"
    def __init__(self, cmd, args=[]):
        Command.__init__(self, 'git-'+cmd, args)


class GitInitDB(GitCommand):
    def __init__(self):
        GitCommand.__init__(self,'init-db')
        self.run_error="Couldn't init git repository"


class GitShowBranch(GitCommand):
    def __init__(self):
        GitCommand.__init__(self,'branch')
        self.run_error="Couldn't list branches"


class GitBranch(GitCommand):
    def __init__(self):
        GitCommand.__init__(self,'branch')

    def __call__(self, branch):
        self.run_error="Couldn't create branch %s" % (branch,)
        GitCommand.__call__(self, [branch])


class GitCheckoutBranch(GitCommand):
    def __init__(self, branch):
        GitCommand.__init__(self,'checkout', [branch])
        self.branch=branch
        self.run_error="Couldn't switch to %s branch" % self.branch


class GitPull(GitCommand):
    def __init__(self, repo, branch):
        GitCommand.__init__(self,'pull', [repo, branch]) 
        self.run_error="Couldn't pull %s to %s" % (branch, repo)


class GitTag(GitCommand):
    def __init__(self):
        GitCommand.__init__(self,'tag') 

    def __call__(self, tag):
        self.run_error="Couldn't tag %s" % (tag,)
        GitCommand.__call__(self, [tag])


class GitAdd(GitCommand):
    """add a lists of files"""
    def __init__(self):
        GitCommand.__init__(self,'add')
        self.run_error="Couldn't add files"


class GitCommitAll(GitCommand):
    """Commit files to the repository"""
    def __init__(self):
        GitCommand.__init__(self,'commit', ['-a'])

    def __call__(self, msg=''):
        args = [ [], ['-m', msg] ][len(msg) > 0]
        self.run_error="Couldn't commit -a %s" % " ".join(args)
        GitCommand.__call__(self, args)


# vim:et:ts=4:sw=4: