~dpb/tarmac/ls-fixes

« back to all changes in this revision

Viewing changes to tarmac/branch.py

  • Committer: Jonathan Lange
  • Date: 2012-12-19 15:54:44 UTC
  • mfrom: (414 trunk)
  • mto: This revision was merged to the branch mainline in revision 415.
  • Revision ID: jml@canonical.com-20121219155444-lsi9um5l8gdwo38k
All of the branch changes, with fixed tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
    def __init__(self, lp_branch, config=False, target=None):
39
39
        self.lp_branch = lp_branch
40
 
        self.bzr_branch = bzr_branch.Branch.open(self.lp_branch.bzr_identity)
41
40
        if config:
42
41
            self.config = BranchConfig(lp_branch.bzr_identity, config)
43
42
        else:
45
44
 
46
45
        self.target = target
47
46
        self.logger = logging.getLogger('tarmac')
 
47
        self.temp_tree_dir = None
 
48
 
 
49
    def get_bzr_branch(self):
 
50
        return bzr_branch.Branch.open(self.lp_branch.bzr_identity)
 
51
 
 
52
    # For backwards compatibility
 
53
    bzr_branch = property(get_bzr_branch)
 
54
 
 
55
    def get_tree(self):
 
56
        if self.temp_tree_dir is not None:
 
57
            return WorkingTree.open(self.temp_tree_dir)
 
58
        if os.path.exists(self.config.tree_dir):
 
59
            return WorkingTree.open(self.config.tree_dir)
 
60
 
 
61
    # For backwards compatibility
 
62
    tree = property(get_tree)
48
63
 
49
64
    def __del__(self):
50
65
        """Do some potenetially necessary cleanup during deletion."""
51
 
        try:
52
 
            # If we were using a temp directory, then remove it
 
66
        if self.temp_tree_dir is not None:
53
67
            shutil.rmtree(self.temp_tree_dir)
54
 
        except AttributeError:
55
 
            # Not using a tempdir
56
 
            pass
57
68
 
58
69
    @classmethod
59
70
    def create(cls, lp_branch, config, create_tree=False, target=None):
62
73
            clazz.create_tree()
63
74
        return clazz
64
75
 
 
76
 
65
77
    def create_tree(self):
66
78
        '''Create the dir and working tree.'''
 
79
        bzr_branch = self.get_bzr_branch()
67
80
        try:
68
 
            self.logger.debug(
69
 
                'Using tree in %(tree_dir)s' % {
70
 
                    'tree_dir': self.config.tree_dir})
71
 
            if os.path.exists(self.config.tree_dir):
72
 
                self.tree = WorkingTree.open(self.config.tree_dir)
73
 
            else:
 
81
            tree = self.get_tree()
 
82
            if tree is None:
74
83
                self.logger.debug('Tree does not exist.  Creating dir')
75
84
                # Create the path up to but not including tree_dir if it does
76
85
                # not exist.
77
86
                parent_dir = os.path.dirname(self.config.tree_dir)
78
87
                if not os.path.exists(parent_dir):
79
88
                    os.makedirs(parent_dir)
80
 
                self.tree = self.bzr_branch.create_checkout(
81
 
                    self.config.tree_dir, lightweight=True)
 
89
                tree = bzr_branch.create_checkout(
 
90
                    self.config.tree_dir, lightweight=False)
 
91
            self.logger.debug(
 
92
                'Using tree in %(tree_dir)s' % {
 
93
                    'tree_dir': self.config.tree_dir})
82
94
        except AttributeError:
83
95
            # Store this so we can rmtree later
84
96
            self.temp_tree_dir = tempfile.mkdtemp()
85
97
            self.logger.debug(
86
98
                'Using temp dir at %(tree_dir)s' % {
87
99
                    'tree_dir': self.temp_tree_dir})
88
 
            self.tree = self.bzr_branch.create_checkout(self.temp_tree_dir)
 
100
            tree = bzr_branch.create_checkout(self.temp_tree_dir)
89
101
 
90
102
        self.cleanup()
91
103
 
92
104
    def cleanup(self):
93
105
        '''Remove the working tree from the temp dir.'''
94
 
        assert self.tree
 
106
        tree = self.get_tree()
 
107
        assert tree
95
108
        self.logger.info("Running cleanup in %s." % (
96
109
            self.lp_branch.bzr_identity))
97
 
        self.tree.revert()
 
110
        tree.revert()
98
111
        self.logger.info("Reverted changes in %s." % (
99
112
            self.lp_branch.bzr_identity))
100
 
        for filename in [self.tree.abspath(f) for f in self.unmanaged_files]:
 
113
        for filename in [tree.abspath(f) for f in self.unmanaged_files]:
101
114
            if os.path.isdir(filename) and not os.path.islink(filename):
102
115
                shutil.rmtree(filename)
103
116
            else:
105
118
 
106
119
        self.logger.info("Successfully removed extra files from %s." % (
107
120
            self.lp_branch.bzr_identity))
108
 
        self.tree.update()
 
121
        tree.update()
109
122
 
110
123
    def merge(self, branch, revid=None):
111
124
        '''Merge from another tarmac.branch.Branch instance.'''
112
 
        assert self.tree
113
 
        conflict_list = self.tree.merge_from_branch(
114
 
            branch.bzr_branch, to_revision=revid)
 
125
        tree = self.get_tree()
 
126
        assert tree
 
127
        conflict_list = tree.merge_from_branch(
 
128
            branch.get_bzr_branch(), to_revision=revid)
115
129
        if conflict_list:
116
130
            message = u'Conflicts merging branch.'
117
131
            lp_comment = (
124
138
    @property
125
139
    def unmanaged_files(self):
126
140
        """Get the list of ignored and unknown files in the tree."""
127
 
        self.tree.lock_read()
128
 
        unmanaged = [x for x in self.tree.unknowns()]
129
 
        unmanaged.extend([x[0] for x in self.tree.ignored_files()])
130
 
        self.tree.unlock()
 
141
        tree = self.get_tree()
 
142
        assert tree
 
143
        tree.lock_read()
 
144
        unmanaged = [x for x in tree.unknowns()]
 
145
        unmanaged.extend([x[0] for x in tree.ignored_files()])
 
146
        tree.unlock()
131
147
        return unmanaged
132
148
 
133
149
    @property
134
150
    def conflicts(self):
135
151
        '''Print the conflicts.'''
136
 
        assert self.tree.conflicts()
 
152
        tree = self.get_tree()
 
153
        assert tree
137
154
        conflicts = []
138
 
        for conflict in self.tree.conflicts():
 
155
        for conflict in tree.conflicts():
139
156
            conflicts.append(
140
157
                u'%s in %s' % (conflict.typestring, conflict.path))
141
158
        return '\n'.join(conflicts)
142
159
 
143
160
    def commit(self, commit_message, revprops=None, **kwargs):
144
161
        '''Commit changes.'''
 
162
        tree = self.get_tree()
 
163
        assert tree
145
164
        if not revprops:
146
165
            revprops = {}
147
166
 
158
177
                                           'review identity or vote.')
159
178
            revprops['reviews'] = '\n'.join(reviews)
160
179
 
161
 
        self.tree.commit(commit_message, committer='Tarmac',
162
 
                         revprops=revprops, authors=authors)
 
180
        tree.commit(commit_message, committer='Tarmac',
 
181
                    revprops=revprops, authors=authors)
163
182
 
164
183
    @property
165
184
    def landing_candidates(self):
170
189
    def authors(self):
171
190
        author_list = []
172
191
 
 
192
        bzr_branch = self.get_bzr_branch()
173
193
        if self.target:
174
 
            self.bzr_branch.lock_read()
175
 
            self.target.bzr_branch.lock_read()
 
194
            target_bzr_branch = self.target.get_bzr_branch()
 
195
            bzr_branch.lock_read()
 
196
            target_bzr_branch.lock_read()
176
197
 
177
 
            graph = self.bzr_branch.repository.get_graph(
178
 
                self.target.bzr_branch.repository)
 
198
            graph = bzr_branch.repository.get_graph(
 
199
                target_bzr_branch.repository)
179
200
 
180
201
            unique_ids = graph.find_unique_ancestors(
181
 
                self.bzr_branch.last_revision(),
182
 
                [self.target.bzr_branch.last_revision()])
 
202
                bzr_branch.last_revision(),
 
203
                [target_bzr_branch.last_revision()])
183
204
 
184
 
            revs = self.bzr_branch.repository.get_revisions(unique_ids)
 
205
            revs = bzr_branch.repository.get_revisions(unique_ids)
185
206
            for rev in revs:
186
207
                apparent_authors = rev.get_apparent_authors()
187
208
                for author in apparent_authors:
189
210
                    if author not in author_list:
190
211
                        author_list.append(author)
191
212
 
192
 
            self.target.bzr_branch.unlock()
193
 
            self.bzr_branch.unlock()
 
213
            target_bzr_branch.unlock()
 
214
            bzr_branch.unlock()
194
215
        else:
195
 
            last_rev = self.bzr_branch.last_revision()
 
216
            last_rev = bzr_branch.last_revision()
196
217
            if last_rev != 'null:':
197
 
                rev = self.bzr_branch.repository.get_revision(last_rev)
 
218
                rev = bzr_branch.repository.get_revision(last_rev)
198
219
                apparent_authors = rev.get_apparent_authors()
199
220
                author_list.extend(
200
221
                    [a.replace('\n', '') for a in apparent_authors])
206
227
        """Return the list of bugs fixed by the branch."""
207
228
        bugs_list = []
208
229
 
209
 
        self.bzr_branch.lock_read()
210
 
        oldrevid = self.bzr_branch.get_rev_id(self.lp_branch.revision_count)
211
 
        for rev_info in self.bzr_branch.iter_merge_sorted_revisions(
 
230
        bzr_branch = self.get_bzr_branch()
 
231
        bzr_branch.lock_read()
 
232
        oldrevid = bzr_branch.get_rev_id(self.lp_branch.revision_count)
 
233
        for rev_info in bzr_branch.iter_merge_sorted_revisions(
212
234
            stop_revision_id=oldrevid):
213
235
            try:
214
 
                rev = self.bzr_branch.repository.get_revision(rev_info[0])
 
236
                rev = bzr_branch.repository.get_revision(rev_info[0])
215
237
                for bug in rev.iter_bugs():
216
238
                    if bug[0].startswith('https://launchpad.net/bugs/'):
217
239
                        bugs_list.append(bug[0].replace(
219
241
            except NoSuchRevision:
220
242
                continue
221
243
 
222
 
        self.bzr_branch.unlock()
 
244
        bzr_branch.unlock()
223
245
        return bugs_list