summaryrefslogtreecommitdiffstats
path: root/src/tools/models/buildtool/sbeGitUtility.py
blob: fba1bb17cb03e99b1848f65a2d7930c029ad2040 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/python
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/tools/models/buildtool/sbeGitUtility.py $
#
# OpenPOWER sbe Project
#
# Contributors Listed Below - COPYRIGHT 2017
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG
###########################################################
#    @file     sbeGitUtility.py
#    @brief    Git Utilility Module to support building SBE image
#
#    @author   George Keishing
#
#    @date     Nov 23, 2015
#    ----------------------------------------------------
#    @version  Developer      Date       Description
#    ----------------------------------------------------
#      1.0     gkeishin     11/12/15     Initial create
###########################################################

#-------------------------
# Imports
#-------------------------
import os
import sys
import time
import shutil
import os.path
import subprocess
from subprocess import Popen, PIPE
from os.path import expanduser    # for getting $HOME PATH
# Local imports
import sbeConstants as sbeConstants

##########################################################################
# Function : utilGitShowCommitFiles
#
# @param   i_commitId   : user supplied command line input commit id
# @param   i_ppe_path   : user supplied command line input path
# 
# @brief   Check the files associated with this commit id
#
##########################################################################
def utilGitShowCommitFiles(i_commitId, i_ppe_path):

    if i_commitId == "None":
        return sbeConstants.SUCCESS_EXIT

    if i_ppe_path == "None":
        l_home_ppe_path = expanduser("~") + "/" +sbeConstants.GIT_PPE_REPO_SB
    else:
        l_home_ppe_path = i_ppe_path + "/" +sbeConstants.GIT_PPE_REPO_SB

    os.chdir(l_home_ppe_path)
    print "   --> ", l_home_ppe_path

    # Quickly check if this commit id is valid or not
    is_valid = "git cat-file -t " + i_commitId
    p_valid = subprocess.Popen(is_valid, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)
    for line in p_valid.stdout:
            line = line.strip()
            if line == "commit":
                print "  * This Commit Validated "
            else:
                #There is an error - command has exited with non-zero code
                return sbeConstants.ERROR_GIT_CHANGEID_FAILED

    # Git command to list only the files associatede with this commit
    l_gitCmd='git show --pretty="format:" --name-only  ' + i_commitId
    print "  Executing : ", l_gitCmd

    p_out = subprocess.Popen(l_gitCmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True)
    for line in p_out.stdout:
        line = line.strip()
        print "   " ,line

    return sbeConstants.SUCCESS_EXIT

##########################################################################
# Function : utilGitShowCommitFiles
#
# @param   i_commitId   : user supplied command line input commit id
# @param   i_ppe_path   : user supplied command line input path
#
# @brief   Check the files associated with this commit id
#
##########################################################################
def utilGitCommitCheckout(i_commitId, i_ppe_path):

    if i_commitId == "None":
        return sbeConstants.SUCCESS_EXIT

    if i_ppe_path == "None":
        l_home_ppe_path = expanduser("~") + "/" +sbeConstants.GIT_PPE_REPO_SB
    else:
        l_home_ppe_path = i_ppe_path + "/" +sbeConstants.GIT_PPE_REPO_SB

    os.chdir(l_home_ppe_path)
    print "   --> ", l_home_ppe_path

    # Git command to list only the files associatede with this commit
    l_gitCmd='git checkout ' + i_commitId + ' -b test_build'
    print "  Executing : ", l_gitCmd
    p_out = subprocess.Popen(l_gitCmd, stdout=subprocess.PIPE, shell=True)

    # Allow time to sink in the checkout
    time.sleep(10)

    l_gitbranch='git branch'
    p_checkout_out = subprocess.Popen(l_gitbranch, stdout=subprocess.PIPE, shell=True)
    for line in p_checkout_out.stdout:
        line = line.strip()
        print "   " ,line

    # Allow time to switch to new branch
    time.sleep(3)

    return sbeConstants.SUCCESS_EXIT

##########################################################################
# Function : utilGetChangeID
#
# @param   i_num   : user supplied command line input commit id
#
# @brief   Get the git log id based on the  changed ID
#
##########################################################################
def utilGetChangeID(i_num):
    print "  commit",i_num
    l_found = False
    #data = []

    # execute the command
    l_cmd = 'git log ' + i_num

    p = subprocess.Popen(l_cmd, stdout=subprocess.PIPE, shell=True)
    #(output, err) = p.communicate()

    while p.poll() is None:
        line = p.stdout.readline()
        line = line.strip()
        if not line: continue

        if i_num in line:
            #print "Commit id marker found"
            l_found=True
            continue

        if 'Change-Id:' in line:
            if l_found:
                #print " ", line
                #data.append(line)
                break

    # Should n't come out without data
    if l_found:
        return line
    else:
        return sbeConstants.ERROR_PREP_CHANGE_FAILED

##########################################################################
# Function : utilGetCommitID
#
# @param   i_num   : user supplied command line input commit id
#
# @brief   Get the git log id based on the  changed ID
#
##########################################################################
def utilGetCommitID(i_num):
    print "  Change-Id: ",i_num
    l_found = False
    #data = []

    # execute the command
    l_cmd = 'git log'
    p = subprocess.Popen(l_cmd, stdout=subprocess.PIPE, shell=True)

    while p.poll() is None:
        line = p.stdout.readline()
        line = line.strip()
        if not line:
            continue

        # Saved off the previous entry which is commit id
        if line.startswith('commit'):
           l_saved_commit_id=line

        if line.startswith('Change-Id:'):
            # Check if the Id is found
            if i_num in line:
                l_found=True
                #print " ", l_saved_commit_id
                break
        else:
            continue

    # Should n't come out without data
    if l_found:
        return l_saved_commit_id
    else:
        return sbeConstants.ERROR_PREP_COM_FAILED


OpenPOWER on IntegriCloud