File: release-queue.py

package info (click to toggle)
lava-server 2016.12-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 28,728 kB
  • sloc: python: 34,417; sh: 902; makefile: 298; xml: 154
file content (134 lines) | stat: -rwxr-xr-x 4,628 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  release-queue.py
#
#  Copyright 2014 Linaro Limited
#  Author: Neil Williams <neil.williams@linaro.org>
#
#  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; either version 3 of the License, or
#  (at your option) any later version.
#
#  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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

import argparse
import re
import subprocess
import time


change_id_pattern = re.compile("\s+Change-Id: (\w+)")
commit_pattern = re.compile("commit (.+)")
author_pattern = re.compile("author ([^>]+>) (\w+) ((\+|-)\w\w\w\w)")


class Commit(object):
    def __init__(self, commit_id, change_id):
        self.commit_id = commit_id
        self.change_id = change_id

        self.obj = subprocess.check_output(['git', 'cat-file', '-p', self.commit_id]).decode('utf-8')
        self.hash = subprocess.check_output(['git', 'rev-parse', '--short', self.commit_id]).decode('utf-8').strip()
        break_next_time = False

        for line in self.obj.split('\n'):
            if break_next_time:
                self.message = line
                break

            if line == '':
                break_next_time = True
            elif line[0:6] == 'author':
                m = author_pattern.match(line)
                if m:
                    self.author = m.group(1)
                    self.time = m.group(2)

    def get_time(self):
        t = time.gmtime(float(self.time))
        return "%02d/%02d/%d %02d:%02d" % (t.tm_mon, t.tm_mday, t.tm_year, t.tm_hour, t.tm_min)

    def render(self):
        return "%s %s (%s %s): %s" % (self.get_time(), self.hash, self.commit_id, self.change_id, self.message)


def get_change_ids(branch):
    results = []

    subprocess.check_output(["git", "checkout", branch], stderr=subprocess.STDOUT)
    lines = subprocess.check_output(["git", "log"]).decode('utf-8')
    for line in lines.split('\n'):
        if "Change-Id" in line:
            m = change_id_pattern.match(line)
            results.append(m.group(1))
    return results


def main():
    # Add the argument parse
    parser = argparse.ArgumentParser(description='Show the missing commits in release branch')
    parser.add_argument('-c', '--changelog', dest='changelog', action='store_true',
                        default=False, help='Print the changelog')
    parser.add_argument('-b', '--branch', dest='branch',
                        default='release', help='branch to compare with base branch')
    parser.add_argument('-s', '--staging', dest='staging', action='store_true',
                        default=False, help='compare branch against staging')
    args = parser.parse_args()

    # Check the current working directory
    try:
        subprocess.check_call(["git", "rev-parse"])
    except subprocess.CalledProcessError:
        print("Ensure this script is run from the git working copy.")
        return 1

    master_branch = 'staging' if args.staging else 'master'
    # Get all change ids between master_branch and release
    master = get_change_ids(master_branch)
    release = get_change_ids(args.branch)

    diff = list(set(master) - set(release))
    diff.sort()

    # Go back to master
    subprocess.check_output(["git", "checkout", master_branch], stderr=subprocess.STDOUT)
    lines = subprocess.check_output(["git", "log"]).decode('utf-8')

    # List the missing commits
    current_hash = ''
    commits = []
    for change in diff:
        for line in lines.split('\n'):
            # Get the commit hash
            if "commit " in line:
                m = commit_pattern.match(line)
                if m:
                    current_hash = m.group(1)
            # Match the Change-id
            if "Change-Id" in line:
                m = change_id_pattern.match(line)
                if change == m.group(1):
                    commits.append(Commit(current_hash, change))
                    break

    commits.sort(key=lambda x: x.time, reverse=True)
    if args.changelog:
        for c in commits:
            print("%s %s" % (c.hash, c.message))
    else:
        for c in commits:
            print(c.render())

    return 0


if __name__ == '__main__':
    main()