• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.compatibility.common.tradefed.targetprep;
18 
19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
20 import com.android.compatibility.common.tradefed.util.CollectorUtil;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.config.Option;
23 import com.android.tradefed.device.DeviceNotAvailableException;
24 import com.android.tradefed.device.ITestDevice;
25 import com.android.tradefed.log.LogUtil.CLog;
26 import com.android.tradefed.targetprep.BuildError;
27 import com.android.tradefed.targetprep.ITargetCleaner;
28 import com.android.tradefed.targetprep.TargetSetupError;
29 import com.android.tradefed.util.FileUtil;
30 
31 import java.io.File;
32 import java.io.FileNotFoundException;
33 
34 /**
35  * An {@link ITargetCleaner} that prepares and pulls report logs.
36  */
37 public class ReportLogCollector implements ITargetCleaner {
38 
39     @Option(name= "src-dir", description = "The directory to copy to the results dir")
40     private String mSrcDir;
41 
42     @Option(name = "dest-dir", description = "The directory under the result to store the files")
43     private String mDestDir;
44 
45     @Option(name = "temp-dir", description = "The temp directory containing host-side report logs")
46     private String mTempReportFolder;
47 
48     @Option(name = "device-dir", description = "Create unique directory for each device")
49     private boolean mDeviceDir;
50 
ReportLogCollector()51     public ReportLogCollector() {
52     }
53 
54     @Override
setUp(ITestDevice device, IBuildInfo buildInfo)55     public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
56             BuildError, DeviceNotAvailableException {
57         prepareReportLogContainers(device, buildInfo);
58     }
59 
prepareReportLogContainers(ITestDevice device, IBuildInfo buildInfo)60     private void prepareReportLogContainers(ITestDevice device, IBuildInfo buildInfo) {
61         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(buildInfo);
62         try {
63             File resultDir = buildHelper.getResultDir();
64             if (mDestDir != null) {
65                 resultDir = new File(resultDir, mDestDir);
66             }
67             resultDir.mkdirs();
68             if (!resultDir.isDirectory()) {
69                 CLog.e("%s is not a directory", resultDir.getAbsolutePath());
70                 return;
71             }
72         } catch (FileNotFoundException fnfe) {
73             fnfe.printStackTrace();
74         }
75     }
76 
77     @Override
tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)78     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e) {
79         // Pull report log files from device.
80         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(buildInfo);
81         try {
82             File resultDir = buildHelper.getResultDir();
83             if (mDestDir != null) {
84                 resultDir = new File(resultDir, mDestDir);
85             }
86             resultDir.mkdirs();
87             if (!resultDir.isDirectory()) {
88                 CLog.e("%s is not a directory", resultDir.getAbsolutePath());
89                 return;
90             }
91             String tmpDirName = mTempReportFolder;
92             if (mDeviceDir) {
93                 tmpDirName = tmpDirName.replaceAll("/$", "");
94                 tmpDirName += "-" + device.getSerialNumber();
95             }
96             final File hostReportDir = FileUtil.createNamedTempDir(tmpDirName);
97             if (!hostReportDir.isDirectory()) {
98                 CLog.e("%s is not a directory", hostReportDir.getAbsolutePath());
99                 return;
100             }
101             String resultPath = resultDir.getAbsolutePath();
102             CollectorUtil.pullFromDevice(device, mSrcDir, resultPath);
103             CollectorUtil.pullFromHost(hostReportDir, resultDir);
104             CollectorUtil.reformatRepeatedStreams(resultDir);
105         } catch (Exception exception) {
106             exception.printStackTrace();
107         }
108     }
109 }
110