Home | History | Annotate | Download | only in targetprep
      1 /*
      2  * Copyright (C) 2016 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.targetprep;
     18 
     19 import com.android.tradefed.build.IBuildInfo;
     20 import com.android.tradefed.config.Option;
     21 import com.android.tradefed.device.DeviceNotAvailableException;
     22 import com.android.tradefed.device.ITestDevice;
     23 import com.android.tradefed.log.ITestLogger;
     24 import com.android.tradefed.log.LogUtil.CLog;
     25 import com.android.tradefed.result.FileInputStreamSource;
     26 import com.android.tradefed.result.ITestLoggerReceiver;
     27 import com.android.tradefed.result.InputStreamSource;
     28 import com.android.tradefed.result.LogDataType;
     29 import com.android.tradefed.util.FileUtil;
     30 import com.android.tradefed.util.StreamUtil;
     31 import com.android.tradefed.util.ZipUtil;
     32 
     33 import java.io.File;
     34 import java.io.IOException;
     35 import java.util.ArrayList;
     36 import java.util.List;
     37 
     38 /**
     39  * A {@link ITargetCleaner} that pulls directories off device, compresses and saves it into logging
     40  * backend.
     41  */
     42 public class FolderSaver extends BaseTargetPreparer implements ITargetCleaner, ITestLoggerReceiver {
     43 
     44     @Option(name = "device-path", description = "Location of directory on device to be pulled and "
     45             + "logged, may be repeated.")
     46     private List<String> mDevicePaths = new ArrayList<>();
     47 
     48     private ITestLogger mTestLogger;
     49 
     50     /**
     51      * {@inheritDoc}
     52      */
     53     @Override
     54     public void setUp(ITestDevice device, IBuildInfo buildInfo)
     55             throws TargetSetupError, BuildError, DeviceNotAvailableException {
     56         // no-op
     57     }
     58 
     59     /**
     60      * {@inheritDoc}
     61      */
     62     @Override
     63     public void setTestLogger(ITestLogger testLogger) {
     64         mTestLogger = testLogger;
     65     }
     66 
     67     /**
     68      * {@inheritDoc}
     69      */
     70     @Override
     71     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
     72             throws DeviceNotAvailableException {
     73         if (e instanceof DeviceNotAvailableException) {
     74             CLog.i("Device %s not available, skipping.", device.getSerialNumber());
     75             return;
     76         }
     77         if (mDevicePaths.isEmpty()) {
     78             CLog.i("No device path provided, skipping.");
     79             return;
     80         }
     81         for (String path : mDevicePaths) {
     82             File tempDir = null;
     83             try {
     84                 tempDir = FileUtil.createTempDir("tf-pulled-dir");
     85                 if (!device.pullDir(path, tempDir)) {
     86                     CLog.w("Failed to pull directory %s from device %s",
     87                             path, device.getSerialNumber());
     88                 } else {
     89                     File zip = null;
     90                     try {
     91                         zip = ZipUtil.createZip(tempDir);
     92                         InputStreamSource s = null;
     93                         try {
     94                             s = new FileInputStreamSource(zip);
     95                             mTestLogger.testLog(path, LogDataType.ZIP, s);
     96                         } finally {
     97                             StreamUtil.cancel(s);
     98                         }
     99                     } finally {
    100                         FileUtil.deleteFile(zip);
    101                     }
    102                 }
    103             } catch (IOException ioe) {
    104                 throw new RuntimeException("exception while saving device directory", ioe);
    105             } finally {
    106                 FileUtil.recursiveDelete(tempDir);
    107             }
    108         }
    109     }
    110 }
    111