/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome

« back to all changes in this revision

Viewing changes to lib/stdhome/vcs/bzr.py

  • Committer: Tim Marston
  • Date: 2014-02-12 21:51:08 UTC
  • Revision ID: tim@ed.am-20140212215108-stk5z0nlvgpi4oa8
added bzr as a vcs backend; finished init command; implemented deployment

Show diffs side-by-side

added added

removed removed

 
1
# bzr.py
 
2
#
 
3
# Copyright (C) 2014 Tim Marston <tim@edm.am>
 
4
#
 
5
# This file is part of stdhome (hereafter referred to as "this program").
 
6
# See http://ed.am/dev/stdhome for more information.
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
 
 
22
import subprocess, os, re, shutil
 
23
from subprocess import Popen
 
24
import stdhome.the as the
 
25
import StringIO
 
26
 
 
27
 
 
28
class VcsBzr:
 
29
 
 
30
 
 
31
        def __init__( self, dir ):
 
32
                """Init class
 
33
 
 
34
                @param dir the fully-qualified directory to work in.
 
35
                """
 
36
                self.dir = dir
 
37
 
 
38
 
 
39
        def init( self ):
 
40
                """Create a new, empty branch
 
41
                """
 
42
 
 
43
                # the directory shouldn't exist
 
44
                os.mkdir( self.dir )
 
45
 
 
46
                # bzr init
 
47
                p = Popen( [ 'bzr', 'init', '.' ], cwd = self.dir,
 
48
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
49
                output = p.communicate()[ 0 ]
 
50
                if p.returncode > 0:
 
51
 
 
52
                        # attempt to clean-up dir
 
53
                        try:
 
54
                                shutil.rmtree( self.dir )
 
55
                        except OSError:
 
56
                                pass
 
57
 
 
58
                        raise the.program.FatalError( 'bzr init failed', output )
 
59
 
 
60
 
 
61
        def checkout( self, url ):
 
62
                """Checkout a new copy of a remote branch.
 
63
 
 
64
                @param url the remote repository URL
 
65
                """
 
66
 
 
67
                # the directory shouldn't exist
 
68
                os.mkdir( self.dir )
 
69
 
 
70
                # bzr co
 
71
                p = Popen( [ 'bzr', 'co', url, '.' ], cwd = self.dir,
 
72
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
73
                output = p.communicate()[ 0 ]
 
74
                if p.returncode > 0:
 
75
 
 
76
                        # attempt to clean-up dir
 
77
                        try:
 
78
                                shutil.rmtree( self.dir )
 
79
                        except OSError:
 
80
                                pass
 
81
 
 
82
                        raise the.program.FatalError( 'bzr checkout failed', output )
 
83
 
 
84
 
 
85
        def revert( self ):
 
86
                """Revert the branch so that there are no outstanding changes or unknown files.
 
87
                """
 
88
 
 
89
                # bzr revert
 
90
                p = Popen( [ 'bzr', 'revert', '--no-backup' ], cwd = self.dir,
 
91
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
92
                output = p.communicate()[ 0 ]
 
93
                if p.returncode > 0:
 
94
                        raise the.program.FatalError( 'bzr revert failed', output )
 
95
 
 
96
                # bzr st
 
97
                p = Popen( [ 'bzr', 'st' ], cwd = self.dir,
 
98
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
99
                output = p.communicate()[ 0 ]
 
100
                if p.returncode > 0:
 
101
                        raise the.program.FatalError( 'bzr status failed', output )
 
102
                files = self.parse_file_blocks( output )
 
103
 
 
104
                # remove unknown files
 
105
                if 'unknown' in files:
 
106
                        for file in files[ 'unknown' ]:
 
107
                                full_file = os.path.join( self.dir, file )
 
108
                                if os.path.isfile( full_file ):
 
109
                                        os.unlink( full_file )
 
110
                                elif os.full_file.isdir( full_file ):
 
111
                                        shutil.rmtree( full_file )
 
112
                                else:
 
113
                                        raise RuntimeError( 'exotic file in repo: %s' % file )
 
114
 
 
115
 
 
116
        def update( self ):
 
117
                """Update the branch, pulling down any upstream changes and merging them.
 
118
                """
 
119
 
 
120
                # bzr update
 
121
                p = Popen( [ 'bzr', 'update' ], cwd = self.dir,
 
122
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
123
                output = p.communicate()[ 0 ]
 
124
                if p.returncode > 0:
 
125
                        raise the.program.FatalError( 'bzr update failed', output )
 
126
 
 
127
 
 
128
        def has_changes( self ):
 
129
                """Check if the branch has any local modifications.
 
130
                """
 
131
 
 
132
                # bzr status
 
133
                p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
 
134
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
135
                output = p.communicate()[ 0 ]
 
136
                if p.returncode > 0:
 
137
                        raise the.program.FatalError( 'bzr status failed', output )
 
138
                files = self.parse_file_blocks( output )
 
139
                return True if len( files ) else False
 
140
 
 
141
 
 
142
        def get_conflicts( self ):
 
143
                """Return a list of files that have conflicts.
 
144
                """
 
145
 
 
146
                # bzr status
 
147
                p = Popen( [ 'bzr', 'status', '--no-pending' ], cwd = self.dir,
 
148
                                   stdout = subprocess.PIPE, stderr = subprocess.STDOUT )
 
149
                output = p.communicate()[ 0 ]
 
150
                if p.returncode > 0:
 
151
                        raise the.program.FatalError( 'bzr status failed', output )
 
152
                files = self.parse_file_blocks( output )
 
153
                return files['conflicts'] if 'conflicts' in files else None
 
154
 
 
155
 
 
156
        def parse_file_blocks( self, output ):
 
157
                res = dict()
 
158
                current = None
 
159
                buf = StringIO.StringIO( output )
 
160
                for line in buf:
 
161
                        matches = re.search( '^([a-z ]+):$', line, re.I )
 
162
                        if matches:
 
163
                                current = matches.group( 1 )
 
164
                                continue
 
165
                        if current:
 
166
                                matches = re.search( '^  ([^ ].*)$', line )
 
167
                                if matches:
 
168
                                        if not current in res:
 
169
                                                res[ current ] = list()
 
170
                                        res[ current ].append( matches.group( 1 ) )
 
171
                                        continue
 
172
                        if re.search( '^[0-9]+ shelf exists', line ): continue
 
173
                        if re.search( '^working tree is out of date', line ): continue
 
174
                        raise self.ParseError( "unrecognised line: %s" % line )
 
175
                return res
 
176
 
 
177
 
 
178
        class ParseError( Exception ):
 
179
                pass