|
| 1 | +#!/usr/bin/python -u |
| 2 | +"""Golang Action Compiler |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +""" |
| 20 | +from __future__ import print_function |
| 21 | +import os, os.path, sys, re, shutil, subprocess, traceback, codecs |
| 22 | +from os.path import dirname, exists |
| 23 | +from time import sleep |
| 24 | + |
| 25 | +# write a file creating intermediate directories |
| 26 | +def write_file(file, body, executable=False): |
| 27 | + try: os.makedirs(dirname(file), mode=0o755) |
| 28 | + except: pass |
| 29 | + with open(file, mode="wb") as f: |
| 30 | + f.write(body) |
| 31 | + if executable: |
| 32 | + os.chmod(file, 0o755) |
| 33 | + |
| 34 | +# copy a file eventually replacing a substring |
| 35 | +def copy_replace(src, dst, match=None, replacement=""): |
| 36 | + with open(src, 'rb') as s: |
| 37 | + body = s.read() |
| 38 | + if match: |
| 39 | + body = body.replace(match, replacement) |
| 40 | + write_file(dst, body) |
| 41 | + |
| 42 | + |
| 43 | +def sources(launcher, source_dir, main): |
| 44 | + func = main.capitalize() |
| 45 | + has_main = None |
| 46 | + |
| 47 | + # copy the exec to exec.go |
| 48 | + # also check if it has a main in it |
| 49 | + src = "%s/exec" % source_dir |
| 50 | + dst = "%s/exec__.go" % source_dir |
| 51 | + if os.path.isfile(src): |
| 52 | + with codecs.open(src, 'r', 'utf-8') as s: |
| 53 | + with codecs.open(dst, 'w', 'utf-8') as d: |
| 54 | + body = s.read() |
| 55 | + has_main = re.match(r".*package\s+main\W.*func\s+main\s*\(\s*\)", body, flags=re.DOTALL) |
| 56 | + d.write(body) |
| 57 | + |
| 58 | + # copy the launcher fixing the main |
| 59 | + if not has_main: |
| 60 | + dst = "%s/main__.go" % source_dir |
| 61 | + if os.path.isdir("%s/main" % source_dir): |
| 62 | + dst = "%s/main/main__.go" % source_dir |
| 63 | + with codecs.open(dst, 'w', 'utf-8') as d: |
| 64 | + with codecs.open(launcher, 'r', 'utf-8') as e: |
| 65 | + code = e.read() |
| 66 | + code = code.replace("Main", func) |
| 67 | + d.write(code) |
| 68 | + |
| 69 | +def build(source_dir, target_dir): |
| 70 | + # compile... |
| 71 | + source_dir = os.path.abspath(source_dir) |
| 72 | + parent = dirname(source_dir) |
| 73 | + target = os.path.abspath("%s/exec" % target_dir) |
| 74 | + if os.environ.get("__OW_EXECUTION_ENV"): |
| 75 | + write_file("%s.env" % target, str.encode(os.environ["__OW_EXECUTION_ENV"])) |
| 76 | + |
| 77 | + env = { |
| 78 | + "GOROOT": "/usr/local/go", |
| 79 | + "GOPATH": "/home/go", |
| 80 | + "PATH": os.environ["PATH"], |
| 81 | + "GOCACHE": "/tmp", |
| 82 | + "GO111MODULE": "on" |
| 83 | + } |
| 84 | + |
| 85 | + gomod = "%s/go.mod" % source_dir |
| 86 | + with open(os.devnull, "w") as dn: |
| 87 | + if exists(gomod): |
| 88 | + ret = subprocess.call(["go", "mod", "download"], cwd=source_dir, env=env, stderr=dn, stdout=dn) |
| 89 | + if ret != 0: |
| 90 | + print("cannot download modules") |
| 91 | + return |
| 92 | + else: |
| 93 | + ret = subprocess.call(["go", "mod", "init", "exec"], cwd=source_dir, env=env, stdout=dn, stderr=dn) |
| 94 | + if ret != 0: |
| 95 | + print("cannot init modules") |
| 96 | + return |
| 97 | + |
| 98 | + ldflags = "-s -w" |
| 99 | + gobuild = ["go", "build", "-o", target, "-ldflags", ldflags] |
| 100 | + if os.environ.get("__OW_EXECUTION_ENV"): |
| 101 | + ldflags += " -X main.OwExecutionEnv=%s" % os.environ["__OW_EXECUTION_ENV"] |
| 102 | + ret = subprocess.call(gobuild, cwd=source_dir, env=env) |
| 103 | + if ret != 0: |
| 104 | + print("failed", " ".join(gobuild), "\nin", source_dir, "\nenv", env) |
| 105 | + |
| 106 | +def debug(source_dir, target_dir, port): |
| 107 | + source_dir = os.path.abspath(source_dir) |
| 108 | + target = os.path.abspath("%s/exec" % target_dir) |
| 109 | + if os.environ.get("__OW_EXECUTION_ENV"): |
| 110 | + write_file("%s/exec.env" % source_dir, os.environ["__OW_EXECUTION_ENV"]) |
| 111 | + shutil.rmtree(target_dir) |
| 112 | + shutil.move(source_dir, target_dir) |
| 113 | + write_file(target, """#!/bin/bash |
| 114 | +cd "$(dirname $0)" |
| 115 | +export GOCACHE=/tmp |
| 116 | +export PATH=%s |
| 117 | +exec script -q -c '/go/bin/dlv debug --headless --listen=127.0.0.1:%s --continue --accept-multiclient --log-dest /tmp/delve.log' |
| 118 | +""" % (os.environ["PATH"], port) , True) |
| 119 | + |
| 120 | +def main(argv): |
| 121 | + if len(argv) < 4: |
| 122 | + print("usage: <main-file> <source-dir> <target-dir>") |
| 123 | + sys.exit(1) |
| 124 | + |
| 125 | + main = argv[1] |
| 126 | + source_dir = argv[2] |
| 127 | + target_dir = argv[3] |
| 128 | + launcher = dirname(dirname(argv[0]))+"/lib/launcher.go" |
| 129 | + sources(launcher, source_dir, main) |
| 130 | + |
| 131 | + # if the debug port is present and not empty build with debug |
| 132 | + if os.environ.get("__OW_DEBUG_PORT"): |
| 133 | + debug(source_dir, target_dir, os.environ["__OW_DEBUG_PORT"]) |
| 134 | + else: |
| 135 | + build(source_dir, target_dir) |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + main(sys.argv) |
0 commit comments