~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/jobs/ingest.py

  • Committer: Tarmac
  • Author(s): Aaron Bentley
  • Date: 2013-04-08 19:27:00 UTC
  • mfrom: (187.2.20 revisions-access)
  • Revision ID: tarmac-20130408192700-3w89hqewu4b1cdy9
[r=sinzui][bug=][author=abentley] Include revisions in API

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
from base64 import b64encode
 
5
import calendar
5
6
import contextlib
6
 
from datetime import datetime
 
7
from datetime import (
 
8
    datetime,
 
9
    timedelta,
 
10
)
7
11
import json
8
12
import logging
9
13
import os
13
17
import urllib2
14
18
 
15
19
from bzrlib.branch import Branch
16
 
from bzrlib.revisionspec import RevisionSpec
 
20
from bzrlib.revision import NULL_REVISION
17
21
from bzrlib.transport import get_transport
18
22
import yaml
19
23
 
176
180
 
177
181
    name = 'changelog'
178
182
 
179
 
    def _rev_info(self, r, branch):
 
183
    def __init__(self, limit=10, since=None):
 
184
        self.limit = limit
 
185
        self.since = since
 
186
 
 
187
    def setup(self):
 
188
        super(ChangelogIngestJob, self).setup()
 
189
        if self.since is None:
 
190
            days_of_revisions = settings.get('days_of_revisions')
 
191
            if days_of_revisions is not None:
 
192
                cutoff = datetime.utcnow() - timedelta(int(days_of_revisions))
 
193
                self.since = calendar.timegm(cutoff.timetuple())
 
194
 
 
195
    @staticmethod
 
196
    def _rev_info(r, branch):
180
197
        d = {
 
198
            'authors': r.get_apparent_authors(),
181
199
            "revno": branch.revision_id_to_revno(r.revision_id),
182
200
            "committer": r.committer,
183
201
            "created": r.timestamp,
187
205
 
188
206
    def run(self, charm_data):
189
207
        branch_dir = charm_data["branch_dir"]
190
 
        transport = get_transport(branch_dir)
191
 
        branch = Branch.open_from_transport(transport)
192
 
 
193
 
        # We only want the last 10 changes, in descending order.
194
 
        _, cur_rev_id = branch.last_revision_info()
195
 
        spec = RevisionSpec.from_string("revno:-10")
196
 
        last_rev_id = spec.as_revision_id(branch)
197
 
 
 
208
        charm_data.update(self.get_changes(branch_dir))
 
209
 
 
210
    def get_changes(self, branch_dir):
 
211
        charm_data = {}
 
212
        branch = Branch.open(branch_dir)
198
213
        branch.lock_read()
199
214
        try:
200
 
            graph = branch.repository.get_graph()
201
 
            revs = list(
202
 
                graph.iter_lefthand_ancestry(cur_rev_id, (last_rev_id,)))
203
 
            if not cur_rev_id in revs:
204
 
                revs.insert(0, cur_rev_id)
 
215
            revisions = self.get_revisions(branch)
 
216
            charm_data["changes"] = changes = []
 
217
            for r in revisions:
 
218
                changes.append(self._rev_info(r, branch))
 
219
            if len(revisions) == 0:
 
220
                last_change = None
 
221
                first_change = None
 
222
            else:
 
223
                last_change = changes[0]
 
224
                first = branch.repository.get_revision(branch.get_rev_id(1))
 
225
                first_change = self._rev_info(first, branch)
 
226
            charm_data.update({
 
227
                'last_change': last_change,
 
228
                'first_change': first_change,
 
229
            })
 
230
            return charm_data
205
231
        finally:
206
232
            branch.unlock()
207
 
        revisions = map(branch.repository.get_revision, revs)
208
 
 
209
 
        charm_data["changes"] = changes = []
210
 
 
211
 
        for r in revisions:
212
 
            changes.append(self._rev_info(r, branch))
213
 
        charm_data["last_change"] = changes[0]
214
 
        charm_data["first_change"] = self._rev_info(
215
 
            branch.repository.get_revision(
216
 
                RevisionSpec.from_string("revno:1").as_revision_id(branch)),
217
 
            branch)
 
233
 
 
234
    def get_revisions(self, branch):
 
235
        # We only want the last 10 changes, in descending order.
 
236
        graph = branch.repository.get_graph()
 
237
        cur_rev_id = branch.last_revision()
 
238
        ancestry_iter = graph.iter_lefthand_ancestry(cur_rev_id)
 
239
        revs = []
 
240
        for num, revision_id in enumerate(ancestry_iter):
 
241
            if revision_id == NULL_REVISION:
 
242
                break
 
243
            revision = branch.repository.get_revision(revision_id)
 
244
            if num >= self.limit:
 
245
                if self.since is None or revision.timestamp < self.since:
 
246
                    break
 
247
            revs.append(revision)
 
248
        return revs
218
249
 
219
250
 
220
251
class IndexIngestJob(IngestJob):