1 /*
2  * Copyright (C) 2018 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.tradefed.util;
18 
19 import com.android.tradefed.log.ITestLogger;
20 import com.android.tradefed.log.LogUtil.CLog;
21 import com.android.tradefed.result.FileInputStreamSource;
22 import com.android.tradefed.result.LogDataType;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Arrays;
26 import java.util.Set;
27 
28 /**
29  * Utility class to add output file to TradeFed log directory.
30  */
31 public class OutputUtil {
32     // Test logger object from test invocation
33     ITestLogger mListener;
34     private String mTestModuleName = null;
35     private String mAbiName = null;
36 
OutputUtil(ITestLogger listener)37     public OutputUtil(ITestLogger listener) {
38         mListener = listener;
39     }
40 
41     /**
42      * Collect all VTS python runner log output files as a single zip file
43      * @param logDirectory
44      */
ZipVtsRunnerOutputDir(File logDirectory)45     public void ZipVtsRunnerOutputDir(File logDirectory) {
46         try {
47             Set<String> latest = FileUtil.findFiles(logDirectory, "latest");
48             if (latest.isEmpty()) {
49                 CLog.e("Empty python log directory: %s", logDirectory);
50                 return;
51             }
52 
53             File tmpZip = ZipUtil.createZip(
54                     Arrays.asList(new File(latest.iterator().next()).listFiles()));
55             String outputFileName = "module_" + mTestModuleName + "_output_files_" + mAbiName;
56             try (FileInputStreamSource inputSource = new FileInputStreamSource(tmpZip, true)) {
57                 mListener.testLog(outputFileName, LogDataType.ZIP, inputSource);
58             }
59         } catch (IOException e) {
60             CLog.e("Error processing python module output directory: %s", logDirectory);
61             CLog.e(e);
62         }
63     }
64 
65     /**
66      * @param testModuleName
67      */
setTestModuleName(String testModuleName)68     public void setTestModuleName(String testModuleName) {
69         mTestModuleName = testModuleName;
70     }
71 
72     /**
73      * @param abiName
74      */
setAbiName(String abiName)75     public void setAbiName(String abiName) {
76         mAbiName = abiName;
77     }
78 }