diff options
Diffstat (limited to 'src/tools/vppapigen/generate_go.py')
-rwxr-xr-x | src/tools/vppapigen/generate_go.py | 154 |
1 files changed, 89 insertions, 65 deletions
diff --git a/src/tools/vppapigen/generate_go.py b/src/tools/vppapigen/generate_go.py index 1fb53c715e2..fa53bc3dca3 100755 --- a/src/tools/vppapigen/generate_go.py +++ b/src/tools/vppapigen/generate_go.py @@ -17,10 +17,12 @@ import sys def get_go_version(go_root): # Returns version of the installed Go - p = subprocess.Popen(["./go", "version"], - cwd=go_root + "/bin", - stdout=subprocess.PIPE, - universal_newlines=True, ) + p = subprocess.Popen( + ["./go", "version"], + cwd=go_root + "/bin", + stdout=subprocess.PIPE, + universal_newlines=True, + ) output, _ = p.communicate() output_fmt = output.replace("go version go", "", 1) @@ -29,10 +31,12 @@ def get_go_version(go_root): # Returns version of the installed binary API generator def get_binapi_gen_version(go_path): - p = subprocess.Popen(["./binapi-generator", "-version"], - cwd=go_path + "/bin", - stdout=subprocess.PIPE, - universal_newlines=True, ) + p = subprocess.Popen( + ["./binapi-generator", "-version"], + cwd=go_path + "/bin", + stdout=subprocess.PIPE, + universal_newlines=True, + ) output, _ = p.communicate() output_fmt = output.replace("govpp", "", 1) @@ -45,11 +49,12 @@ def install_golang(go_root): go_bin = go_root + "/bin/go" if os.path.exists(go_bin) and os.path.isfile(go_bin): - print('Go ' + get_go_version(go_root) + ' is already installed') + print("Go " + get_go_version(go_root) + " is already installed") return - filename = requests.get( - 'https://golang.org/VERSION?m=text').text + ".linux-amd64.tar.gz" + filename = ( + requests.get("https://golang.org/VERSION?m=text").text + ".linux-amd64.tar.gz" + ) url = "https://dl.google.com/go/" + filename print("Go binary not found, installing the latest version...") @@ -61,13 +66,13 @@ def install_golang(go_root): print("Aborting...") exit(1) - go_folders = ['src', 'pkg', 'bin'] + go_folders = ["src", "pkg", "bin"] for f in go_folders: if not os.path.exists(os.path.join(go_root, f)): os.makedirs(os.path.join(go_root, f)) r = requests.get(url) - with open("/tmp/" + filename, 'wb') as f: + with open("/tmp/" + filename, "wb") as f: f.write(r.content) go_tf = tarfile.open("/tmp/" + filename) @@ -78,32 +83,30 @@ def install_golang(go_root): go_tf.close() os.remove("/tmp/" + filename) - print('Go ' + get_go_version(go_root) + ' was installed') + print("Go " + get_go_version(go_root) + " was installed") # Installs latest binary API generator def install_binapi_gen(c, go_root, go_path): - os.environ['GO111MODULE'] = "on" - if (os.path.exists(go_root + "/bin/go") and - os.path.isfile(go_root + "/bin/go")): - p = subprocess.Popen(["./go", "get", - "git.fd.io/govpp.git/cmd/binapi-generator@" + c], - cwd=go_root + "/bin", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, ) + os.environ["GO111MODULE"] = "on" + if os.path.exists(go_root + "/bin/go") and os.path.isfile(go_root + "/bin/go"): + p = subprocess.Popen( + ["./go", "get", "git.fd.io/govpp.git/cmd/binapi-generator@" + c], + cwd=go_root + "/bin", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) _, error = p.communicate() if p.returncode != 0: - print("binapi generator installation failed: %d %s" % - (p.returncode, error)) + print("binapi generator installation failed: %d %s" % (p.returncode, error)) sys.exit(1) bg_ver = get_binapi_gen_version(go_path) - print('Installed binary API generator ' + bg_ver) + print("Installed binary API generator " + bg_ver) # Creates generated bindings using GoVPP binapigen to the target folder -def generate_api(output_dir, vpp_dir, api_list, import_prefix, - no_source, go_path): +def generate_api(output_dir, vpp_dir, api_list, import_prefix, no_source, go_path): json_dir = vpp_dir + "/build-root/install-vpp-native/vpp/share/vpp/api" if not os.path.exists(json_dir): @@ -115,19 +118,20 @@ def generate_api(output_dir, vpp_dir, api_list, import_prefix, if output_dir: cmd += ["--output-dir=" + output_dir] if len(api_list): - print("Following API files were requested by 'GO_API_FILES': " + - str(api_list)) - print("Note that dependency requirements may generate " - "additional API files") + print("Following API files were requested by 'GO_API_FILES': " + str(api_list)) + print("Note that dependency requirements may generate " "additional API files") cmd.append(api_list) if import_prefix: cmd.append("-import-prefix=" + import_prefix) if no_source: cmd.append("-no-source-path-info") - p = subprocess.Popen(cmd, cwd=go_path + "/bin", - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - universal_newlines=True, ) + p = subprocess.Popen( + cmd, + cwd=go_path + "/bin", + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True, + ) out = p.communicate()[1] if p.returncode != 0: @@ -150,50 +154,70 @@ def main(): vpp_dir = root.parent.parent.parent parser = argparse.ArgumentParser() - parser.add_argument("-govpp-commit", "--govpp-commit", - help="GoVPP commit or branch " - "(defaults to v0.3.5-45-g671f16c)", - default="671f16c", # fixed GoVPP version - type=str) - parser.add_argument("-output-dir", "--output-dir", - help="output target directory for generated bindings", - type=str, - default=os.path.join(vpp_dir, "vppbinapi")) - parser.add_argument("-api-files", "--api-files", - help="api files to generate (without commas)", - nargs="+", - type=str, - default=[]) - parser.add_argument("-import-prefix", "--import-prefix", - help="prefix imports in the generated go code", - default="", - type=str) - parser.add_argument("-no-source-path-info", "--no-source-path-info", - help="disable source path info in generated files", - nargs='?', - const=True, - default=True) + parser.add_argument( + "-govpp-commit", + "--govpp-commit", + help="GoVPP commit or branch " "(defaults to v0.3.5-45-g671f16c)", + default="671f16c", # fixed GoVPP version + type=str, + ) + parser.add_argument( + "-output-dir", + "--output-dir", + help="output target directory for generated bindings", + type=str, + default=os.path.join(vpp_dir, "vppbinapi"), + ) + parser.add_argument( + "-api-files", + "--api-files", + help="api files to generate (without commas)", + nargs="+", + type=str, + default=[], + ) + parser.add_argument( + "-import-prefix", + "--import-prefix", + help="prefix imports in the generated go code", + default="", + type=str, + ) + parser.add_argument( + "-no-source-path-info", + "--no-source-path-info", + help="disable source path info in generated files", + nargs="?", + const=True, + default=True, + ) args = parser.parse_args() # go specific environment variables if "GOROOT" in os.environ: - go_root = os.environ['GOROOT'] + go_root = os.environ["GOROOT"] else: go_binary = shutil.which("go") if go_binary != "": go_binary_dir, _ = os.path.split(go_binary) go_root = os.path.join(go_binary_dir, "..") else: - go_root = os.environ['HOME'] + "/.go" + go_root = os.environ["HOME"] + "/.go" if "GOPATH" in os.environ: - go_path = os.environ['GOPATH'] + go_path = os.environ["GOPATH"] else: - go_path = os.environ['HOME'] + "/go" + go_path = os.environ["HOME"] + "/go" install_golang(go_root) install_binapi_gen(args.govpp_commit, go_root, go_path) - generate_api(args.output_dir, str(vpp_dir), args.api_files, - args.import_prefix, args.no_source_path_info, go_path) + generate_api( + args.output_dir, + str(vpp_dir), + args.api_files, + args.import_prefix, + args.no_source_path_info, + go_path, + ) if __name__ == "__main__": |