Esempio n. 1
0
    def __init__(self, buck_bin_dir, buck_project, buck_reporter):
        self.buck_dir = platform_path(os.path.dirname(buck_bin_dir))

        super(BuckRepo, self).__init__(buck_project, buck_reporter)

        dot_git = os.path.join(self.buck_dir, ".git")
        self.is_git = (os.path.exists(dot_git) and os.path.isdir(dot_git)
                       and which("git") and sys.platform != "cygwin")
        self._is_buck_repo_dirty_override = os.environ.get(
            "BUCK_REPOSITORY_DIRTY")
        if not self._fake_buck_version:
            # self._fake_buck_version has been set previously through BuckTool when the environment
            # variable BUCK_FAKE_VERSION is set.
            # If the environement variable is not set, we'll use the content of .fakebuckversion
            # at the root of the repository if it exists.
            fake_buck_version_file_path = os.path.join(self.buck_dir,
                                                       ".fakebuckversion")
            if os.path.exists(fake_buck_version_file_path):
                with open(
                        fake_buck_version_file_path) as fake_buck_version_file:
                    self._fake_buck_version = fake_buck_version_file.read(
                    ).strip()
                    logging.info(
                        "Using fake buck version (via .fakebuckversion): {}".
                        format(self._fake_buck_version))
Esempio n. 2
0
 def test_java_home_for_wrong_version_ignored(self):
     os.environ["JAVA_HOME"] = (
         "/Library/Java/JavaVirtualMachines/jdk-" +
         str(JAVA_VERSION_THAT_OBVIOUSLY_CANT_EXIST_LOCALLY + 1) +
         ".jdk/Contents/Home")
     self.assertEquals(
         get_java_path(
             JAVA_VERSION_THAT_OBVIOUSLY_CANT_EXIST_LOCALLY).lower(),
         which("java").lower(),
     )
Esempio n. 3
0
def setup_watchman_watch():
    with Tracing("BuckTool._setup_watchman_watch"):
        if not which("watchman"):
            message = textwrap.dedent("""\
                Watchman not found, please install when using buckd.
                See https://github.com/facebook/watchman for details.""")
            if sys.platform == "darwin":
                message += "\n(brew install watchman on OS X)"
            # Bail if watchman isn't installed as we know java's
            # FileSystemWatcher will take too long to process events.
            raise BuckToolException(message)

        logging.debug("Using watchman.")
Esempio n. 4
0
def is_vcs(dirpath):  # type: (str) -> bool
    dot_hg = os.path.join(dirpath, ".hg")
    if which("hg") and sys.platform != "cygwin":
        if os.path.exists(dot_hg) and os.path.isdir(dot_hg):
            return True
        try:
            with open(os.devnull, "w") as devnull:
                output = check_output(["hg", "root"],
                                      cwd=dirpath,
                                      stderr=devnull).decode("utf-8")
            return os.path.abspath(output.strip()) == os.path.abspath(dirpath)
        except CalledProcessError:
            pass
    return False
Esempio n. 5
0
def is_vcs(dirpath):  # type: (str) -> bool
    dot_git = os.path.join(dirpath, ".git")
    if which("git") and sys.platform != "cygwin":
        if os.path.exists(dot_git) and os.path.isdir(dot_git):
            return True
        try:
            with open(os.devnull, "w") as devnull:
                output = check_output(
                    ["git", "rev-parse", "--is-inside-work-tree"],
                    cwd=dirpath,
                    stderr=devnull,
                ).decode("utf-8")
            return output.strip() == "true"
        except CalledProcessError:
            pass
    return False
Esempio n. 6
0
def get_java_path(required_java_version):
    java_home_path = os.getenv("JAVA_HOME")
    if java_home_path:
        # Though we try to respect JAVA_HOME, if the path looks like the wrong version of Java, try
        # to use a known location of the JDK for the right version instead.
        suspected_java_version = _get_suspected_java_version_from_java_path(
            java_home_path)
        if suspected_java_version and suspected_java_version != required_java_version:
            message = (
                'Warning: JAVA_HOME is set to "{}", which looks like a Java {} path, '
                + "but Buck requires Java {}.").format(java_home_path,
                                                       suspected_java_version,
                                                       required_java_version)
            if os.getenv("BUCK_RESPECT_JAVA_HOME") != "1":
                message += " Ignoring JAVA_HOME. Set BUCK_RESPECT_JAVA_HOME to 1 to disable this behavior."
                java_home_path = None
            logging.warning(message)
    if java_home_path is None:
        # Default to a known location of the JDK for the right version of Java, regardless of what
        # version of Java is on the PATH.
        java_base_path = _get_known_java_path_for_version(
            required_java_version)
        java_path = None
        if java_base_path:
            java_path = _get_java_exec(java_base_path)
            if not os.path.isfile(java_path):
                java_path = None
        if not java_path:
            java_path = which("java")
        if java_path is None:
            raise BuckToolException("Could not find Java executable. \
Make sure it is on PATH or JAVA_HOME is set.")
    else:
        java_path = _get_java_exec(java_home_path)
        if not os.path.isfile(java_path):
            message = textwrap.dedent("""
            Could not find Java executable under JAVA_HOME at: '{}'.
            Please make sure your JAVA_HOME environment variable is set correctly.
            Then restart buck (buck kill) and try again.
            """).format(java_path)
            raise BuckToolException(message)
    return java_path
Esempio n. 7
0
    def launch_buck(self, build_id, client_cwd, java_path, argv):
        with Tracing("BuckTool.launch_buck"):
            with JvmCrashLogger(self, self._buck_project.root):
                self._reporter.build_id = build_id

                try:
                    repository = self._get_repository()
                    self._reporter.repository = repository
                except Exception as e:
                    # _get_repository() is only for reporting,
                    # so skip on error
                    logging.warning("Failed to get repo name: " + str(e))

                if (self._command_line.command == "clean"
                        and not self._command_line.is_help()):
                    self.kill_buckd()

                buck_version_uid = self._get_buck_version_uid()
                self._reporter.buck_version = buck_version_uid

                if self._command_line.is_version():
                    print("buck version {}".format(buck_version_uid))
                    return ExitCodeCallable(ExitCode.SUCCESS)

                use_buckd = self._use_buckd
                if not use_buckd:
                    logging.warning("Not using buckd because NO_BUCKD is set.")
                    self._reporter.no_buckd_reason = "explicit"

                if use_buckd and self._command_line.is_help():
                    use_buckd = False
                    self._reporter.no_buckd_reason = "help"

                if use_buckd:
                    has_watchman = bool(which("watchman"))
                    if not has_watchman:
                        use_buckd = False
                        self._reporter.no_buckd_reason = "watchman"
                        logging.warning(
                            "Not using buckd because watchman isn't installed."
                        )

                if use_buckd:
                    with exclusive_lock(
                            self._buck_project.get_section_lock_path(
                                "buckd_start_stop"),
                            wait=True,
                    ):
                        need_start = True
                        running_version = self._buck_project.get_running_buckd_version(
                        )
                        running_jvm_args = (
                            self._buck_project.get_running_buckd_jvm_args())
                        jvm_args = self._get_java_args(buck_version_uid)
                        if running_version is None:
                            logging.info("Starting new Buck daemon...")
                        elif running_version != buck_version_uid:
                            logging.info(
                                "Restarting Buck daemon because Buck version has "
                                "changed...")
                        elif not self._is_buckd_running():
                            logging.info(
                                "Unable to connect to Buck daemon, restarting it..."
                            )
                        elif jvm_args != running_jvm_args:
                            logging.info(
                                "Restarting Buck daemon because JVM args have "
                                "changed...")
                        else:
                            need_start = False

                        if need_start:
                            self.kill_buckd()
                            if not self.launch_buckd(
                                    java_path,
                                    jvm_args,
                                    buck_version_uid=buck_version_uid):
                                use_buckd = False
                                self._reporter.no_buckd_reason = "daemon_failure"
                                logging.warning(
                                    "Not using buckd because daemon failed to start."
                                )

                env = self._environ_for_buck()
                env["BUCK_BUILD_ID"] = build_id
                env["BUCK_CLIENT_PWD"] = client_cwd

                self._reporter.is_buckd = use_buckd
                run_fn = (self._run_with_nailgun
                          if use_buckd else self._run_without_nailgun)

                self._unpack_modules()

                exit_code_callable = self._execute_command_and_maybe_run_target(
                    run_fn, java_path, env, argv)

                # Most shells return process termination with signal as
                # 128 + N, where N is the signal. However Python's subprocess
                # call returns them as negative numbers. Buck binary protocol
                # uses shell's convention, so convert
                if exit_code_callable.exit_code < 0:
                    exit_code_callable.exit_code = 128 + (
                        -1 * exit_code_callable.exit_code)
                return exit_code_callable
Esempio n. 8
0
 def test_without_java_home(self):
     self.assertEquals(
         get_java_path(
             JAVA_VERSION_THAT_OBVIOUSLY_CANT_EXIST_LOCALLY).lower(),
         which("java").lower(),
     )