| #!/usr/bin/env python3 |
| """Rust Action Builder |
| # |
| # Licensed to the Apache Software Foundation (ASF) under one or more |
| # contributor license agreements. See the NOTICE file distributed with |
| # this work for additional information regarding copyright ownership. |
| # The ASF licenses this file to You under the Apache License, Version 2.0 |
| # (the "License"); you may not use this file except in compliance with |
| # the License. You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| # |
| """ |
| |
| from __future__ import print_function |
| import os, sys, codecs, subprocess |
| from os.path import abspath, exists, dirname |
| import tempfile |
| |
| ## utils |
| # write a file creating intermediate directories |
| def write_file(file, body): |
| os.makedirs(dirname(file), mode=0o755, exist_ok=True) |
| with open(file, mode="w", encoding="utf-8") as f: |
| f.write(body) |
| |
| # copy a file eventually replacing a substring |
| def copy_replace(src, dst, match=None, replacement=""): |
| with codecs.open(src, 'r', 'utf-8') as s: |
| body = s.read() |
| if match: |
| body = body.replace(match, replacement) |
| write_file(dst, body) |
| |
| ## cargo |
| cargo_action = """[package] |
| name = "actions" |
| version = "0.1.0" |
| |
| [dependencies] |
| serde_json = "1.0" |
| """ |
| |
| cargo_actionloop = """[package] |
| name = "action_loop" |
| version = "0.1.0" |
| authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"] |
| |
| [dependencies] |
| serde_json = "1.0" |
| libc = "0.2.49" |
| actions = { path = "../actions" } |
| """ |
| |
| cargo_workspace = """ |
| [workspace] |
| |
| members = [ |
| "action_loop", |
| "actions", |
| ] |
| """ |
| |
| def build(): |
| pass |
| |
| def sources(main, src_dir, tgt_dir, launcher): |
| src_file = abspath("%s/exec" % src_dir) |
| |
| # move exec in the right place |
| if exists(src_file): |
| os.makedirs(src_dir+"/src", mode=0o755, exist_ok=True) |
| copy_replace(src_file, src_dir+"/src/exec__.rs") |
| |
| # add a cargo.toml if needed |
| cargo_action_file = src_dir+"/Cargo.toml" |
| if not exists(cargo_action_file): |
| write_file(cargo_action_file, cargo_action) |
| |
| # write the boilerplate in a temp dir |
| os.makedirs("/tmp/src", mode=0o755, exist_ok=True) |
| tmp_dir = tempfile.mkdtemp(prefix='/tmp/src/') |
| copy_replace(launcher, tmp_dir+"/action_loop/src/main.rs", |
| "use actions::main as actionMain;", |
| "use actions::%s as actionMain;" % main ) |
| write_file(tmp_dir+"/action_loop/Cargo.toml", cargo_actionloop) |
| write_file(tmp_dir+"/Cargo.toml", cargo_workspace) |
| os.rename(src_dir, tmp_dir+"/actions") |
| return tmp_dir |
| |
| if __name__ == '__main__': |
| if len(sys.argv) < 4: |
| sys.stdout.write("usage: <main-function> <source-dir> <target-dir>\n") |
| sys.stdout.flush() |
| sys.exit(1) |
| dir = sources(sys.argv[1], abspath(sys.argv[2]), abspath(sys.argv[3]), abspath(sys.argv[0]+".launcher.rs")) |
| print(dir) |
| sys.stdout.flush() |
| sys.stderr.flush() |