Search in sources :

Example 6 with SuppressForbidden

use of com.facebook.buck.annotations.SuppressForbidden in project buck by facebook.

the class AdbHelper method filterDevices.

/**
   * Returns list of devices that pass the filter. If there is an invalid combination or no
   * devices are left after filtering this function prints an error and returns null.
   */
@Nullable
@VisibleForTesting
@SuppressForbidden
List<IDevice> filterDevices(IDevice[] allDevices) {
    if (allDevices.length == 0) {
        console.printBuildFailure("No devices are found.");
        return null;
    }
    List<IDevice> devices = Lists.newArrayList();
    Optional<Boolean> emulatorsOnly = Optional.empty();
    if (deviceOptions.isEmulatorsOnlyModeEnabled() && options.isMultiInstallModeEnabled()) {
        emulatorsOnly = Optional.empty();
    } else if (deviceOptions.isEmulatorsOnlyModeEnabled()) {
        emulatorsOnly = Optional.of(true);
    } else if (deviceOptions.isRealDevicesOnlyModeEnabled()) {
        emulatorsOnly = Optional.of(false);
    }
    int onlineDevices = 0;
    for (IDevice device : allDevices) {
        boolean passed = false;
        if (device.isOnline()) {
            onlineDevices++;
            boolean serialMatches = true;
            if (deviceOptions.getSerialNumber().isPresent()) {
                serialMatches = device.getSerialNumber().equals(deviceOptions.getSerialNumber().get());
            } else if (context.getEnvironment().containsKey(SERIAL_NUMBER_ENV)) {
                serialMatches = device.getSerialNumber().equals(context.getEnvironment().get(SERIAL_NUMBER_ENV));
            }
            boolean deviceTypeMatches;
            if (emulatorsOnly.isPresent()) {
                // Only devices of specific type are accepted:
                // either real devices only or emulators only.
                deviceTypeMatches = (emulatorsOnly.get() == isEmulator(device));
            } else {
                // All online devices match.
                deviceTypeMatches = true;
            }
            passed = serialMatches && deviceTypeMatches;
        }
        if (passed) {
            devices.add(device);
        }
    }
    // Filtered out all devices.
    if (onlineDevices == 0) {
        console.printBuildFailure("No devices are found.");
        return null;
    }
    if (devices.isEmpty()) {
        console.printBuildFailure(String.format("Found %d connected device(s), but none of them matches specified filter.", onlineDevices));
        return null;
    }
    return devices;
}
Also used : IDevice(com.android.ddmlib.IDevice) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SuppressForbidden(com.facebook.buck.annotations.SuppressForbidden) Nullable(javax.annotation.Nullable)

Example 7 with SuppressForbidden

use of com.facebook.buck.annotations.SuppressForbidden in project buck by facebook.

the class AdbHelper method isDeviceTempWritable.

@VisibleForTesting
@SuppressForbidden
protected boolean isDeviceTempWritable(IDevice device, String name) {
    StringBuilder loggingInfo = new StringBuilder();
    try {
        String output;
        try {
            output = executeCommandWithErrorChecking(device, "ls -l -d /data/local/tmp");
            if (!(// Pattern for Android's "toolbox" version of ls
            output.matches("\\Adrwx....-x +shell +shell.* tmp[\\r\\n]*\\z") || // Pattern for CyanogenMod's busybox version of ls
            output.matches("\\Adrwx....-x +[0-9]+ +shell +shell.* /data/local/tmp[\\r\\n]*\\z"))) {
                loggingInfo.append(String.format(Locale.ENGLISH, "Bad ls output for /data/local/tmp: '%s'\n", output));
            }
            executeCommandWithErrorChecking(device, "echo exo > /data/local/tmp/buck-experiment");
            output = executeCommandWithErrorChecking(device, "cat /data/local/tmp/buck-experiment");
            if (!output.matches("\\Aexo[\\r\\n]*\\z")) {
                loggingInfo.append(String.format(Locale.ENGLISH, "Bad echo/cat output for /data/local/tmp: '%s'\n", output));
            }
            executeCommandWithErrorChecking(device, "rm /data/local/tmp/buck-experiment");
        } catch (CommandFailedException e) {
            loggingInfo.append(String.format(Locale.ENGLISH, "Failed (%d) '%s':\n%s\n", e.exitCode, e.command, e.output));
        }
        if (!loggingInfo.toString().isEmpty()) {
            CollectingOutputReceiver receiver = new CollectingOutputReceiver();
            device.executeShellCommand("getprop", receiver);
            for (String line : com.google.common.base.Splitter.on('\n').split(receiver.getOutput())) {
                if (line.contains("ro.product.model") || line.contains("ro.build.description")) {
                    loggingInfo.append(line).append('\n');
                }
            }
        }
    } catch (AdbCommandRejectedException | ShellCommandUnresponsiveException | TimeoutException | IOException e) {
        console.printBuildFailure(String.format("Failed to test /data/local/tmp on %s.", name));
        e.printStackTrace(console.getStdErr());
        return false;
    }
    String logMessage = loggingInfo.toString();
    if (!logMessage.isEmpty()) {
        StringBuilder fullMessage = new StringBuilder();
        fullMessage.append("============================================================\n");
        fullMessage.append('\n');
        fullMessage.append("HEY! LISTEN!\n");
        fullMessage.append('\n');
        fullMessage.append("The /data/local/tmp directory on your device isn't fully-functional.\n");
        fullMessage.append("Here's some extra info:\n");
        fullMessage.append(logMessage);
        fullMessage.append("============================================================\n");
        console.getStdErr().println(fullMessage.toString());
    }
    return true;
}
Also used : ShellCommandUnresponsiveException(com.android.ddmlib.ShellCommandUnresponsiveException) CollectingOutputReceiver(com.android.ddmlib.CollectingOutputReceiver) AdbCommandRejectedException(com.android.ddmlib.AdbCommandRejectedException) IOException(java.io.IOException) TimeoutException(com.android.ddmlib.TimeoutException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SuppressForbidden(com.facebook.buck.annotations.SuppressForbidden)

Aggregations

SuppressForbidden (com.facebook.buck.annotations.SuppressForbidden)7 IDevice (com.android.ddmlib.IDevice)5 AdbCommandRejectedException (com.android.ddmlib.AdbCommandRejectedException)3 InstallException (com.android.ddmlib.InstallException)3 ShellCommandUnresponsiveException (com.android.ddmlib.ShellCommandUnresponsiveException)3 TimeoutException (com.android.ddmlib.TimeoutException)3 InterruptionFailedException (com.facebook.buck.util.InterruptionFailedException)3 IOException (java.io.IOException)3 CancellationException (java.util.concurrent.CancellationException)3 ExecutionException (java.util.concurrent.ExecutionException)3 HumanReadableException (com.facebook.buck.util.HumanReadableException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 PrintStream (java.io.PrintStream)2 AndroidDebugBridge (com.android.ddmlib.AndroidDebugBridge)1 CollectingOutputReceiver (com.android.ddmlib.CollectingOutputReceiver)1 InstallEvent (com.facebook.buck.event.InstallEvent)1 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)1 StartActivityEvent (com.facebook.buck.event.StartActivityEvent)1 CommandThreadFactory (com.facebook.buck.log.CommandThreadFactory)1 ExopackageInfo (com.facebook.buck.rules.ExopackageInfo)1