#!/usr/bin/env python
"""
This is a Jenkins script that is supposed to be run after the
GithubPullRequestBuilder plugin. It will determine what files has changed in
the PR being tested, and set up several environmental variables accordingly.

In particular, the following files may be created in the `target_folder`:
    H2O_RUN_PY_TESTS
    H2O_RUN_R_TESTS
    H2O_RUN_JAVA_TESTS
    H2O_RUN_JS_TESTS
Presence / absence of each of these files indicates whether the corresponding
group of tests should be run.

We may extend the set of these flags in the future.


"""
from __future__ import print_function
import os
import subprocess

target_folder = "../h2o-py/build/tmp"
environment = {"py": False, "r": False, "java": False, "js": False}



def mark_all_flags_true():
    for k in environment:
        environment[k] = True


def mark_flag_true(flag):
    environment[flag] = True


def error(msg):
    print()
    print("WARNING: %s" % msg)
    print("         All tests will be scheduled to run.")
    print()
    mark_all_flags_true()


def get_list_of_modified_files(source_branch, target_branch):
    print("> git merge-base origin/%s origin/%s" % (source_branch, target_branch))
    out1 = subprocess.check_output(["git", "merge-base", source_branch, target_branch]).decode().rstrip()
    print("> git diff --name-only origin/%s %s" % (source_branch, out1))
    out2 = subprocess.check_output(["git", "diff", "--name-only", source_branch, out1]).decode().rstrip()
    return out2.split("\n")


def run():
    source_branch = os.environ.get("ghprbSourceBranch")
    if not source_branch:
        return error("Environment variable ghprbSourceBranch not set")

    target_branch = os.environ.get("ghprbTargetBranch")
    if not target_branch:
        return error("Environment variable ghprbTargetBranch not set")

    try:
        files_changed = get_list_of_modified_files(source_branch, target_branch)
    except Exception as e:
        return error("%r when trying to retrieve the list of changed files" % e)

    for fname in files_changed:
        if fname.startswith("h2o-py/") or fname == "h2o-bindings/bin/gen_python.py":
            mark_flag_true("py")
        elif fname.startswith("h2o-r/") or fname == "h2o-bindings/bin/gen_R.py":
            mark_flag_true("r")
        elif fname.endswith(".md"):
            pass  # Changes in .md files don't require any jobs to be run
        else:
            mark_all_flags_true()
            break


def create_files(folder):
    abs_folder = os.path.join(os.path.dirname(__file__), folder)
    if not os.path.exists(abs_folder):
        os.makedirs(abs_folder)

    print()
    for key, value in environment.items():
        target_file = os.path.join(abs_folder, "H2O_RUN_%s_TESTS" % key.upper())
        if value:
            with open(target_file, "w"): pass
        else:
            if os.path.exists(target_file):
                os.remove(target_file)
        print("H2O_RUN_%s_TESTS = %s" % (key.upper(), str(value).lower()))
    print()


if __name__ == "__main__":
    run()
    create_files(target_folder)

@@ -1060,32 +1060,40 @@
 _modified_files(
+remote, 
 source_branch, t
@@ -1139,32 +1139,32 @@
 ase 
-origin/%25s origin/%25s%22 %25 (
+%25s/%25s %25s/%25s%22 %25 (remote, 
 sour
@@ -1165,32 +1165,40 @@
 , source_branch,
+ remote,
  target_branch))
@@ -1255,29 +1255,68 @@
 -base%22, 
-source_branch
+%22%25s/%25s%22 %25 (remote, source_branch), %22%25s/%25s%22 %25 (remote
 , target
@@ -1322,16 +1322,17 @@
 t_branch
+)
 %5D).decod
@@ -1382,14 +1382,10 @@
 nly 
-origin
+%25s
 /%25s 
@@ -1391,16 +1391,24 @@
  %25s%22 %25 (
+remote, 
 source_b
@@ -1486,16 +1486,35 @@
 e-only%22,
+ %22%25s/%25s%22 %25 (remote,
  source_
@@ -1515,24 +1515,25 @@
 ource_branch
+)
 , out1%5D).dec
@@ -1892,24 +1892,46 @@
  not set%22)%0A%0A
+    remote = %22origin%22%0A
     try:%0A   
@@ -1978,16 +1978,24 @@
 d_files(
+remote, 
 source_b

Use remote reference to compare branches.