示例#1
0
  private static void build(Iterable<String> in) throws IOException, InterruptedException {
    final List<String> args = newArrayList(in);
    Collections.sort(args);

    final int threads = Runtime.getRuntime().availableProcessors();

    final ExecutorService ex = Executors.newFixedThreadPool(threads);

    final String base = "base";
    new WithVm("base").createIfNotPresent();

    for (String pkg : args)
      ex.submit(
          () -> {
            final WithVm newVm = new WithVm("fbuild-" + pkg, TimeUnit.MINUTES.toMillis(30));

            final File rbuild = new File("wip-" + pkg + ".rbuild");
            try {
              newVm.cloneFrom(base);
              newVm.start();
              newVm.inTee(rbuild, "apt-get", "-oAPT::Get::Only-Source=true", "source", pkg);
              newVm.inTee(rbuild, "apt-get", "build-dep", "-y", "--force-yes", pkg);
              newVm.inTee(rbuild, "ifdown", "eth0");
              final boolean success =
                  0
                      == newVm.inTee(
                          rbuild, "sh", "-c", "cd " + pkg + "-* && dpkg-buildpackage -us -uc");
              newVm.stopNow();
              if (success) {
                rbuild.renameTo(new File("success-" + pkg + ".rbuild"));
                newVm.destroy();
                System.out.println("success: " + pkg);
              } else {
                rbuild.renameTo(new File("failure-" + pkg + ".rbuild"));
                System.out.println("failure: " + pkg);
              }
            } catch (Exception e) {
              rbuild.renameTo(new File("error-" + pkg + ".rbuild"));
              System.err.println("build error: " + pkg);
              e.printStackTrace();
              newVm.stopNow();
            }
            return null;
          });

    ex.shutdown();
    ex.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
  }
示例#2
0
  private void checkAndLaunchUpdate() {
    Log.i(LOG_FILE_NAME, "Checking for an update");

    int statusCode = 8; // UNEXPECTED_ERROR
    File baseUpdateDir = null;
    if (Build.VERSION.SDK_INT >= 8)
      baseUpdateDir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
    else baseUpdateDir = new File(Environment.getExternalStorageDirectory().getPath(), "download");

    File updateDir = new File(new File(baseUpdateDir, "updates"), "0");

    File updateFile = new File(updateDir, "update.apk");
    File statusFile = new File(updateDir, "update.status");

    if (!statusFile.exists() || !readUpdateStatus(statusFile).equals("pending")) return;

    if (!updateFile.exists()) return;

    Log.i(LOG_FILE_NAME, "Update is available!");

    // Launch APK
    File updateFileToRun = new File(updateDir, getPackageName() + "-update.apk");
    try {
      if (updateFile.renameTo(updateFileToRun)) {
        String amCmd =
            "/system/bin/am start -a android.intent.action.VIEW "
                + "-n com.android.packageinstaller/.PackageInstallerActivity -d file://"
                + updateFileToRun.getPath();
        Log.i(LOG_FILE_NAME, amCmd);
        Runtime.getRuntime().exec(amCmd);
        statusCode = 0; // OK
      } else {
        Log.i(LOG_FILE_NAME, "Cannot rename the update file!");
        statusCode = 7; // WRITE_ERROR
      }
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error launching installer to update", e);
    }

    // Update the status file
    String status = statusCode == 0 ? "succeeded\n" : "failed: " + statusCode + "\n";

    OutputStream outStream;
    try {
      byte[] buf = status.getBytes("UTF-8");
      outStream = new FileOutputStream(statusFile);
      outStream.write(buf, 0, buf.length);
      outStream.close();
    } catch (Exception e) {
      Log.i(LOG_FILE_NAME, "error writing status file", e);
    }

    if (statusCode == 0) System.exit(0);
  }