1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.compatibility.common.tradefed.build;
17 
18 import com.android.compatibility.SuiteInfo;
19 import com.android.tradefed.build.BuildInfo;
20 import com.android.tradefed.build.BuildRetrievalError;
21 import com.android.tradefed.build.DeviceBuildInfo;
22 import com.android.tradefed.build.IBuildInfo;
23 import com.android.tradefed.build.IBuildProvider;
24 import com.android.tradefed.build.IDeviceBuildProvider;
25 import com.android.tradefed.config.Option;
26 import com.android.tradefed.config.OptionClass;
27 import com.android.tradefed.device.DeviceNotAvailableException;
28 import com.android.tradefed.device.ITestDevice;
29 
30 import java.util.regex.Pattern;
31 /**
32  * A simple {@link IBuildProvider} that uses a pre-existing Compatibility install.
33  */
34 @OptionClass(alias="compatibility-build-provider")
35 public class CompatibilityBuildProvider implements IDeviceBuildProvider {
36 
37     private static final Pattern RELEASE_BUILD = Pattern.compile("^[A-Z]{3}\\d{2}[A-Z]{0,1}$");
38 
39     @Option(name="branch", description="build branch name to supply.")
40     private String mBranch = null;
41 
42     @Option(name="use-device-build-info", description="Bootstrap build info from device")
43     private boolean mUseDeviceBuildInfo = false;
44 
45     @Option(name="test-tag", description="test tag name to supply.")
46     private String mTestTag = "cts";
47 
48     /**
49      * {@inheritDoc}
50      */
51     @Override
getBuild()52     public IBuildInfo getBuild() {
53         // Create a blank BuildInfo which will get populated later.
54         String version = SuiteInfo.BUILD_NUMBER;
55         if (version == null) {
56             version = IBuildInfo.UNKNOWN_BUILD_ID;
57         }
58         IBuildInfo ctsBuild = new BuildInfo(version, mTestTag, mTestTag);
59         if (mBranch  != null) {
60             ctsBuild.setBuildBranch(mBranch);
61         }
62 
63         return ctsBuild;
64     }
65 
66     /**
67      * {@inheritDoc}
68      */
69     @Override
getBuild(ITestDevice device)70     public IBuildInfo getBuild(ITestDevice device)
71             throws BuildRetrievalError, DeviceNotAvailableException {
72         if (!mUseDeviceBuildInfo) {
73             // return a regular build info without extracting device attributes into standard
74             // build info fields
75             return getBuild();
76         } else {
77             String buildId = device.getBuildId();
78             String buildFlavor = device.getBuildFlavor();
79             IBuildInfo info = new DeviceBuildInfo(buildId, mTestTag, buildFlavor);
80             if (mBranch == null) {
81                 // if branch is not specified via param, make a pseudo branch name based on platform
82                 // version and product info from device
83                 mBranch = String.format("%s-%s-%s-%s",
84                         device.getProperty("ro.product.brand"),
85                         device.getProperty("ro.product.name"),
86                         device.getProductVariant(),
87                         device.getProperty("ro.build.version.release"));
88             }
89             info.setBuildBranch(mBranch);
90             info.setBuildFlavor(buildFlavor);
91             String buildAlias = device.getBuildAlias();
92             if (RELEASE_BUILD.matcher(buildAlias).matches()) {
93                 info.addBuildAttribute("build_alias", buildAlias);
94             }
95             return info;
96         }
97     }
98 
99     /**
100      * {@inheritDoc}
101      */
102     @Override
buildNotTested(IBuildInfo info)103     public void buildNotTested(IBuildInfo info) {
104         // ignore
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
cleanUp(IBuildInfo info)111     public void cleanUp(IBuildInfo info) {
112         // ignore
113     }
114 }
115