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 * Add a text file to log directory. 43 * @param outputFileName output file base name. 44 * The actual output name will contain a hash postfix. 45 * @param source text file source 46 */ addOutputFromTextFile(String outputFileName, File source)47 public void addOutputFromTextFile(String outputFileName, File source) { 48 FileInputStreamSource inputSource = new FileInputStreamSource(source); 49 mListener.testLog(outputFileName, LogDataType.TEXT, inputSource); 50 } 51 52 /** 53 * Collect all VTS python runner log output files as a single zip file 54 * @param logDirectory 55 */ ZipVtsRunnerOutputDir(File logDirectory)56 public void ZipVtsRunnerOutputDir(File logDirectory) { 57 try { 58 Set<String> latest = FileUtil.findFiles(logDirectory, "latest"); 59 if (latest.isEmpty()) { 60 CLog.e("Empty python log directory: %s", logDirectory); 61 return; 62 } 63 64 File tmpZip = ZipUtil.createZip( 65 Arrays.asList(new File(latest.iterator().next()).listFiles())); 66 String outputFileName = "module_" + mTestModuleName + "_output_files_" + mAbiName; 67 FileInputStreamSource inputSource = new FileInputStreamSource(tmpZip); 68 mListener.testLog(outputFileName, LogDataType.ZIP, inputSource); 69 tmpZip.delete(); 70 71 } catch (IOException e) { 72 CLog.e("Error processing python module output directory: %s", logDirectory); 73 CLog.e(e); 74 } 75 } 76 77 /** 78 * 79 * @param logFile 80 */ addVtsRunnerOutputFile(File logFile)81 public void addVtsRunnerOutputFile(File logFile) { 82 String fileName = logFile.getName(); 83 String fileNameLower = fileName.toLowerCase(); 84 85 LogDataType type; 86 if (fileNameLower.endsWith(".html")) { 87 type = LogDataType.HTML; 88 } else if (fileNameLower.startsWith("logcat")) { 89 type = LogDataType.LOGCAT; 90 } else if (fileNameLower.startsWith("bugreport") && fileNameLower.endsWith(".zip")) { 91 type = LogDataType.BUGREPORTZ; 92 } else if (fileNameLower.startsWith("bugreport") && fileNameLower.endsWith(".txt")) { 93 type = LogDataType.BUGREPORT; 94 } else if (fileNameLower.endsWith(".txt") || fileNameLower.endsWith(".log")) { 95 type = LogDataType.TEXT; 96 } else if (fileNameLower.endsWith(".zip")) { 97 type = LogDataType.ZIP; 98 } else if (fileNameLower.endsWith(".jpg")) { 99 type = LogDataType.JPEG; 100 } else if (fileNameLower.endsWith(".tar.gz")) { 101 type = LogDataType.TAR_GZ; 102 } else if (fileNameLower.endsWith(".png")) { 103 type = LogDataType.PNG; 104 } else { 105 type = LogDataType.UNKNOWN; 106 } 107 108 String outputFileName = mTestModuleName + "_" + fileName + "_" + mAbiName; 109 FileInputStreamSource inputSource = new FileInputStreamSource(logFile); 110 mListener.testLog(outputFileName, type, inputSource); 111 } 112 113 /** 114 * @param testModuleName 115 */ setTestModuleName(String testModuleName)116 public void setTestModuleName(String testModuleName) { 117 mTestModuleName = testModuleName; 118 } 119 120 /** 121 * @param abiName 122 */ setAbiName(String abiName)123 public void setAbiName(String abiName) { 124 mAbiName = abiName; 125 } 126 }