Adjust /etc/passwd and GATK parameters if current username missing.

    Set Spark user to avoid lookup errors on environments like Docker where
    we run as a user id that is not present in /etc/passwd

    https://stackoverflow.com/questions/45198252/apache-spark-standalone-for-anonymous-uid-without-user-name/45361221#45361221
    https://github.com/jaceklaskowski/mastering-apache-spark-book/blob/master/spark-sparkcontext-creating-instance-internals.adoc#-utilsgetcurrentusername
    https://blog.openshift.com/jupyter-on-openshift-part-6-running-as-an-assigned-user-id/

def fix_missing_spark_user(cl, prog, params):
    """Adjust /etc/passwd and GATK parameters if current username missing.

    Set Spark user to avoid lookup errors on environments like Docker where
    we run as a user id that is not present in /etc/passwd

    https://stackoverflow.com/questions/45198252/apache-spark-standalone-for-anonymous-uid-without-user-name/45361221#45361221
    https://github.com/jaceklaskowski/mastering-apache-spark-book/blob/master/spark-sparkcontext-creating-instance-internals.adoc#-utilsgetcurrentusername
    https://blog.openshift.com/jupyter-on-openshift-part-6-running-as-an-assigned-user-id/
    """
    if prog.find("Spark") >= 0 or "--spark-master" in params:
        user = None
        try:
            user = getpass.getuser()
        except KeyError:
            if os.access("/etc/passwd", os.W_OK):
                with open("/etc/passwd", "a") as out_handle:
                    out_handle.write("sparkanon:x:{uid}:{uid}:sparkanon:/nonexistent:/usr/sbin/nologin\n"
                                        .format(uid=os.getuid()))
                try:
                    user = getpass.getuser()
                except KeyError:
                    pass
        if user:
            cl = "export SPARK_USER=%s && " % (user) + cl
    return cl