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 package com.android.compatibility.common.tradefed.targetprep;
17 
18 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
19 import com.android.ddmlib.Log.LogLevel;
20 import com.android.tradefed.build.IBuildInfo;
21 import com.android.tradefed.config.Option;
22 import com.android.tradefed.config.OptionClass;
23 import com.android.tradefed.log.LogUtil.CLog;
24 import com.android.tradefed.targetprep.PushFilePreparer;
25 import com.android.tradefed.testtype.IAbi;
26 import com.android.tradefed.testtype.IAbiReceiver;
27 
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 
31 /**
32  * Pushes specified testing artifacts from Compatibility repository.
33  */
34 @OptionClass(alias="file-pusher")
35 public class FilePusher extends PushFilePreparer implements IAbiReceiver {
36 
37     @Option(name = "append-bitness",
38             description = "Append the ABI's bitness to the filename.")
39     private boolean mAppendBitness = false;
40 
41     private CompatibilityBuildHelper mBuildHelper = null;
42 
43     private IAbi mAbi;
44 
getTestsDir(IBuildInfo buildInfo)45     protected File getTestsDir(IBuildInfo buildInfo) throws FileNotFoundException {
46         if (mBuildHelper == null) {
47             mBuildHelper = new CompatibilityBuildHelper(buildInfo);
48         }
49         return mBuildHelper.getTestsDir();
50     }
51 
52     /**
53      * {@inheritDoc}
54      */
55     @Override
setAbi(IAbi abi)56     public void setAbi(IAbi abi) {
57         mAbi = abi;
58     }
59 
60     /**
61      * {@inheritDoc}
62      */
63     @Override
resolveRelativeFilePath(IBuildInfo buildInfo, String fileName)64     public File resolveRelativeFilePath(IBuildInfo buildInfo, String fileName) {
65         try {
66             File f = new File(getTestsDir(buildInfo),
67                     String.format("%s%s", fileName, mAppendBitness ? mAbi.getBitness() : ""));
68             CLog.logAndDisplay(LogLevel.INFO, "Copying from %s", f.getAbsolutePath());
69             return f;
70         } catch (FileNotFoundException e) {
71             e.printStackTrace();
72         }
73         return null;
74     }
75 }
76