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