aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tradefed/targetprep/AdditionalFilesInstaller.java
blob: 8e098aef8f75592116b80b921546b23faf364239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tradefed.targetprep;

import com.android.tradefed.build.IBuildInfo;
import com.android.tradefed.build.VersionedFile;
import com.android.tradefed.config.Option;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.log.LogUtil.CLog;

import java.io.File;

/**
 * A {@link ITargetPreparer} that pushes all {@link IBuildInfo#getFiles()} to specific path on
 * device.
 */
public class AdditionalFilesInstaller implements ITargetPreparer, ITargetCleaner {

    // TODO: make this an option
    private final String DEST_PATH = "/data/local/tmp/";

    @Option(name = "uninstall", description = "remove all contents after test completes.")
    private boolean mUninstall = true;

    // TODO: add a filter options to allow selective pushing of files

    /**
     * {@inheritDoc}
     */
    @Override
    public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError,
            BuildError, DeviceNotAvailableException {
        removeFiles(device);
        for (VersionedFile buildFile : buildInfo.getFiles()) {
            File file = buildFile.getFile();
            CLog.d("Examining build file %s", file.getName());
            String remotePath = String.format("%s%s", DEST_PATH, file.getName());
            CLog.d("Pushing %s to %s", file.getName(), remotePath);
            if (!device.pushFile(file, remotePath)) {
                throw new TargetSetupError(String.format("Failed to push %s to %s",
                        file.getName(), remotePath));
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable t)
            throws DeviceNotAvailableException {
        try {
            removeFiles(device);
        } catch (TargetSetupError e) {
            CLog.e(e);
        }

    }

    /**
     * Remove all files from dest path, if --uninstall option is set/
     *
     * @param device the {@link ITestDevice} to use
     * @throws DeviceNotAvailableException if communication was lost with device.
     * @throws TargetSetupError if failed to remove contents after 3 attempts
     */
    private void removeFiles(ITestDevice device) throws DeviceNotAvailableException,
            TargetSetupError {
        if (mUninstall) {
            for (int i=0; i < 3; i++) {
                device.executeShellCommand(String.format("rm -r %s*", DEST_PATH));
                if (!hasContents(device, DEST_PATH)) {
                    return;
                }
            }
            throw new TargetSetupError(String.format("failed to remove files from %s", DEST_PATH));
        }
    }

    /**
     * Check for presence of any files in <var>path</var> on <var>device</var>.
     *
     * @param device the {@link ITestDevice} to use
     * @param path the absolute file system path to check
     * @return true if path has contents, false otherwise
     * @throws DeviceNotAvailableException if communication was lost with device.
     */
    private boolean hasContents(ITestDevice device, String path)
            throws DeviceNotAvailableException {
        return device.executeShellCommand(String.format("ls %s", path)).trim().length() > 0;
    }
}