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 package com.android.compatibility.common.tradefed.result.suite;
17 
18 import com.android.annotations.VisibleForTesting;
19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
20 import com.android.compatibility.common.util.ChecksumReporter;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.invoker.IInvocationContext;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.result.ITestInvocationListener;
25 import com.android.tradefed.util.FileUtil;
26 
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32 
33 /**
34  * Recursively copy all the files from a previous session into the current one if they don't exists
35  * already.
36  */
37 public class PreviousSessionFileCopier implements ITestInvocationListener {
38 
39     private static final List<String> NOT_RETRY_FILES =
40             Arrays.asList(
41                     ChecksumReporter.NAME,
42                     ChecksumReporter.PREV_NAME,
43                     CertificationReportCreator.FAILURE_REPORT_NAME,
44                     CertificationReportCreator.HTLM_REPORT_NAME,
45                     CertificationReportCreator.FAILURE_REPORT_NAME,
46                     CertificationSuiteResultReporter.SUMMARY_FILE,
47                     CertificationChecksumHelper.NAME,
48                     "diffs",
49                     "proto");
50 
51     private CompatibilityBuildHelper mBuildHelper;
52     private File mPreviousSessionDir = null;
53 
54     /** Sets the previous session directory to copy from. */
setPreviousSessionDir(File previousSessionDir)55     public void setPreviousSessionDir(File previousSessionDir) {
56         mPreviousSessionDir = previousSessionDir;
57     }
58 
59     @Override
invocationStarted(IInvocationContext context)60     public void invocationStarted(IInvocationContext context) {
61         if (mBuildHelper == null) {
62             mBuildHelper = createCompatibilityHelper(context.getBuildInfos().get(0));
63         }
64     }
65 
66     @Override
invocationEnded(long elapsedTime)67     public void invocationEnded(long elapsedTime) {
68         if (mPreviousSessionDir == null) {
69             CLog.e("Could not copy previous sesson files.");
70             return;
71         }
72         File resultDir = getResultDirectory();
73         copyRetryFiles(mPreviousSessionDir, resultDir);
74     }
75 
76     @VisibleForTesting
createCompatibilityHelper(IBuildInfo info)77     protected CompatibilityBuildHelper createCompatibilityHelper(IBuildInfo info) {
78         return new CompatibilityBuildHelper(info);
79     }
80 
81     /**
82      * Recursively copy any other files found in the previous session's result directory to the new
83      * result directory, so long as they don't already exist. For example, a "screenshots" directory
84      * generated in a previous session by a passing test will not be generated on retry unless
85      * copied from the old result directory.
86      *
87      * @param oldDir
88      * @param newDir
89      */
copyRetryFiles(File oldDir, File newDir)90     private void copyRetryFiles(File oldDir, File newDir) {
91         File[] oldChildren = oldDir.listFiles();
92         for (File oldChild : oldChildren) {
93             if (NOT_RETRY_FILES.contains(oldChild.getName())) {
94                 continue; // do not copy this file/directory or its children
95             }
96             File newChild = new File(newDir, oldChild.getName());
97             if (!newChild.exists()) {
98                 // If this old file or directory doesn't exist in new dir, simply copy it
99                 try {
100                     CLog.d("Copying %s to new session.", oldChild.getName());
101                     if (oldChild.isDirectory()) {
102                         FileUtil.recursiveCopy(oldChild, newChild);
103                     } else {
104                         FileUtil.copyFile(oldChild, newChild);
105                     }
106                 } catch (IOException e) {
107                     CLog.w("Failed to copy file \"%s\" from previous session", oldChild.getName());
108                 }
109             } else if (oldChild.isDirectory() && newChild.isDirectory()) {
110                 // If both children exist as directories, make sure the children of the old child
111                 // directory exist in the new child directory.
112                 copyRetryFiles(oldChild, newChild);
113             }
114         }
115     }
116 
getResultDirectory()117     private File getResultDirectory() {
118         File resultDir = null;
119         try {
120             resultDir = mBuildHelper.getResultDir();
121             if (resultDir != null) {
122                 resultDir.mkdirs();
123             }
124         } catch (FileNotFoundException e) {
125             throw new RuntimeException(e);
126         }
127         if (resultDir == null) {
128             throw new RuntimeException("Result Directory was not created");
129         }
130         if (!resultDir.exists()) {
131             throw new RuntimeException(
132                     "Result Directory was not created: " + resultDir.getAbsolutePath());
133         }
134         CLog.d("Results Directory: %s", resultDir.getAbsolutePath());
135         return resultDir;
136     }
137 }
138