Home | History | Annotate | Download | only in targetprep
      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.tradefed.targetprep;
     17 
     18 import com.android.tradefed.build.IBuildInfo;
     19 import com.android.tradefed.config.Option;
     20 import com.android.tradefed.config.OptionClass;
     21 import com.android.tradefed.device.DeviceNotAvailableException;
     22 import com.android.tradefed.device.ITestDevice;
     23 import com.android.tradefed.log.LogUtil.CLog;
     24 import com.android.tradefed.util.FileUtil;
     25 
     26 import java.io.File;
     27 import java.io.IOException;
     28 
     29 /**
     30  * An {@link ITargetPreparer} that writes build info meta data into a specified file.
     31  *
     32  * <p>The file is written in simple key-value pair format; each line of the file has:<br>
     33  * <code>key=value</code><br>
     34  * where <code>key</code> is a meta data field from {@link IBuildInfo}
     35  *
     36  * <p>Currently, only build id is written.
     37  */
     38 @OptionClass(alias = "build-info-recorder")
     39 public class BuildInfoRecorder extends BaseTargetPreparer {
     40 
     41     @Option(name = "build-info-file", description = "when specified, build info will be written "
     42             + "into the file specified. Any existing file will be overwritten.")
     43     private File mBuildInfoFile = null;
     44 
     45     /*
     46      * {@inheritDoc}
     47      */
     48     @Override
     49     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
     50             BuildError, DeviceNotAvailableException {
     51         if (mBuildInfoFile != null) {
     52             try {
     53                 String alias = buildInfo.getBuildAttributes().get("build_alias");
     54                 if (alias == null) {
     55                     alias = buildInfo.getBuildId();
     56                 }
     57                 FileUtil.writeToFile(String.format("%s=%s\n%s=%s\n",
     58                         "build_id", buildInfo.getBuildId(),
     59                         "build_alias", alias),
     60                         mBuildInfoFile);
     61             } catch (IOException ioe) {
     62                 CLog.e("Exception while writing build info into %s",
     63                         mBuildInfoFile.getAbsolutePath());
     64                 CLog.e(ioe);
     65             }
     66         }
     67     }
     68 
     69 }
     70