1/*
2 * Copyright (C) 2010 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
17package vogar;
18
19import com.google.common.collect.ImmutableList;
20import java.io.File;
21import java.io.FileNotFoundException;
22import java.util.Arrays;
23import java.util.List;
24import vogar.commands.Command;
25import vogar.commands.Mkdir;
26import vogar.commands.Rm;
27
28/**
29 * Run tests on the host machine.
30 */
31public final class LocalTarget extends Target {
32
33    private static final ImmutableList<String> TARGET_PROCESS_PREFIX = ImmutableList.of("sh", "-c");
34
35    private final Log log;
36
37    private final Mkdir mkdir;
38
39    private final Rm rm;
40
41    public LocalTarget(Log log, Mkdir mkdir, Rm rm) {
42        this.mkdir = mkdir;
43        this.rm = rm;
44        this.log = log;
45    }
46
47    public static File defaultDeviceDir() {
48        return new File("/tmp/vogar");
49    }
50
51    @Override protected ImmutableList<String> targetProcessPrefix() {
52        return TARGET_PROCESS_PREFIX;
53    }
54
55    @Override public String getDeviceUserName() {
56        throw new UnsupportedOperationException();
57    }
58
59    @Override public void await(File nonEmptyDirectory) {
60    }
61
62    @Override public void rm(File file) {
63        rm.file(file);
64    }
65
66    @Override public List<File> ls(File directory) throws FileNotFoundException {
67        File[] files = directory.listFiles();
68        if (files == null) {
69            throw new FileNotFoundException(directory + " not found.");
70        }
71        return Arrays.asList(files);
72    }
73
74    @Override public void mkdirs(File file) {
75        mkdir.mkdirs(file);
76    }
77
78    @Override public void forwardTcp(int port) {
79        // do nothing
80    }
81
82    @Override public void push(File local, File remote) {
83        if (remote.equals(local)) {
84            return;
85        }
86        // if the user dir exists, cp would copy the files to the wrong place
87        if (remote.exists()) {
88            throw new IllegalStateException();
89        }
90        new Command(log, "cp", "-r", local.toString(), remote.toString()).execute();
91    }
92
93    @Override public void pull(File remote, File local) {
94        new Command(log, "cp", remote.getPath(), local.getPath()).execute();
95    }
96}
97