1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18import subprocess
19import datetime
20import sys
21
22
23def print_e(*args, **kwargs):
24    print(*args, file=sys.stderr, **kwargs)
25
26
27def getJetpadReleaseInfo(date):
28    try:
29        rawJetpadReleaseOutput = subprocess.check_output('span sql /span/global/androidx-jetpad:prod_instance \"SELECT GroupId, ArtifactId, ReleaseVersion, PreviousReleaseSHA, ReleaseSHA, Path, RequireSameVersionGroupBuild, ReleaseBuildId, ReleaseBranch FROM LibraryReleases WHERE ReleaseDate = %s\"' % date, shell=True)
30    except subprocess.CalledProcessError:
31        print_e("FAIL: Failed to get jetpad release info for  %s. "
32                "This likely means you need to run gcert." %  date)
33        return None
34    rawJetpadReleaseOutputLines = rawJetpadReleaseOutput.splitlines()
35    if len(rawJetpadReleaseOutputLines) <= 2:
36        print_e("Error: Date %s returned zero results from Jetpad.  Please check your date" % date)
37        return None
38    jetpadReleaseOutput = iter(rawJetpadReleaseOutputLines)
39    return jetpadReleaseOutput
40
41def getReleaseInfoObject(date, includeAllCommits, jetpadReleaseInfo):
42    releaseDateTime = datetime.datetime.fromtimestamp(float(date)/1000.0)
43    releaseJsonObject = {}
44    releaseJsonObject["releaseDate"] = "%02d-%02d-%s" % (releaseDateTime.month, releaseDateTime.day, releaseDateTime.year)
45    releaseJsonObject["includeAllCommits"] = includeAllCommits
46    releaseJsonObject["modules"] = {}
47    for line in jetpadReleaseInfo:
48        if "androidx" not in line.decode(): continue
49        # Remove all white space and split line based on '|'
50        artifactIdReleaseLine = line.decode().replace(" ", "").split('|')
51        groupId = artifactIdReleaseLine[1]
52        artifactId = artifactIdReleaseLine[2]
53        version = artifactIdReleaseLine[3]
54        fromSHA = artifactIdReleaseLine[4]
55        untilSHA = artifactIdReleaseLine[5]
56        path = artifactIdReleaseLine[6]
57        if path and path[0] == '/': path = path[1:]
58        requiresSameVersion = False
59        if artifactIdReleaseLine[7] == "true":
60            requiresSameVersion = True
61        buildId = artifactIdReleaseLine[8]
62        branch = artifactIdReleaseLine[9]
63        if groupId in releaseJsonObject["modules"]:
64            releaseJsonObject["modules"][groupId].append({
65                "groupId": groupId,
66                "artifactId": artifactId,
67                "version": version,
68                "fromSHA": fromSHA,
69                "untilSHA": untilSHA,
70                "requiresSameVersion": requiresSameVersion,
71                "path": path,
72                "buildId": buildId,
73                "branch": branch,
74            })
75        else:
76            releaseJsonObject["modules"][groupId] = [{
77                "groupId": groupId,
78                "artifactId": artifactId,
79                "version": version,
80                "fromSHA": fromSHA,
81                "untilSHA": untilSHA,
82                "requiresSameVersion": requiresSameVersion,
83                "path": path,
84                "buildId": buildId,
85                "branch": branch,
86            }]
87    return releaseJsonObject
88
89def getJetpadRelease(date, includeAllCommits):
90    print("Getting the release info from Jetpad...")
91    jetpadReleaseInfo = getJetpadReleaseInfo(date)
92    if not jetpadReleaseInfo:
93        exit(1)
94    print("Successful")
95    return getReleaseInfoObject(date, includeAllCommits, jetpadReleaseInfo)
96
97