def _make_run_script()

in community/front-end/ofe/infrastructure_files/gcs_bucket/clusters/ansible_setup/roles/c2_daemon/files/ghpcfe_c2daemon.py [0:0]


def _make_run_script(job_dir, uid, gid, orig_run_script):
    text = orig_run_script.strip()
    script_url = urlparse(text)
    if text.startswith("#!"):  # Inline script
        text = text.replace("\r\n", "\n")  # Convert to UNIX line endings
        job_file = job_dir / "job.sh"
        with job_file.open("w", encoding="utf-8") as fileh:
            fileh.write(text)
            fileh.write("\n")
        job_file.chmod(0o755)
        os.chown(job_file, uid, gid)
        # Return just a call to this file to execute it
        return job_file.as_posix()

    if script_url.scheme in ["gs", "http", "https"]:
        recursive_fetch = script_url.path.endswith("/")
        fname = script_url.path.split("/")[-1] if not recursive_fetch else ""
        if script_url.scheme == "gs":
            fetch = (
                "gsutil "
                f"{'-m cp -r ' if recursive_fetch else ''}"
                f"'{text}' "
                f"'{job_dir.as_posix()}'"
            )
        elif script_url.scheme == "s3":
            fetch = (
                "aws s3 cp "
                f"{'--recursive ' if recursive_fetch else ''}"
                f"'{text}' "
                f"'{job_dir.as_posix()}'"
            )
        elif script_url.scheme in ["http", "https"]:
            if recursive_fetch:
                logger.error("Not Implemented recursive HTTP/HTTPS fetches")
                return None
            fetch = f"curl --silent -O '{text}'"

        if fname:
            extract = f"chmod 755 {fname}"
            execute = f"./{fname}"
            archive = False
            file_path = Path(fname)
            if file_path.suffixes in [
                [".tar", ".gz"],
                [".tar", ".xz"],
                [".tar", ".bz2"],
            ]:
                extract = f"tar xfa {file_path.name}"
                archive = True
            if file_path.suffixes in [[".zip"]]:
                extract = f"unzip {file_path.name}"
                archive = True
            if archive:
                execute = (
                    "# Find and execute most top-level 'run.sh' we can find\n"
                )
                execute += (
                    "$("
                    "find . -maxdepth 3 -name run.sh | "
                    "awk '{print length, $0}' | "
                    "sort -n  | "
                    "cut -d' ' -f2- | "
                    "head -n1"
                    ")"
                )
        return f"""
{fetch}
{extract}
{execute}
"""

    logger.error("Job Script not in a recognized format")
    return None