|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Rust Action Builder |
| 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 | + |
| 21 | +from __future__ import print_function |
| 22 | +import os, sys, codecs, subprocess |
| 23 | +from os.path import abspath, exists, dirname |
| 24 | +import tempfile |
| 25 | + |
| 26 | +## utils |
| 27 | +# write a file creating intermediate directories |
| 28 | +def write_file(file, body): |
| 29 | + os.makedirs(dirname(file), mode=0o755, exist_ok=True) |
| 30 | + with open(file, mode="w", encoding="utf-8") as f: |
| 31 | + f.write(body) |
| 32 | + |
| 33 | +# copy a file eventually replacing a substring |
| 34 | +def copy_replace(src, dst, match=None, replacement=""): |
| 35 | + with codecs.open(src, 'r', 'utf-8') as s: |
| 36 | + body = s.read() |
| 37 | + if match: |
| 38 | + body = body.replace(match, replacement) |
| 39 | + write_file(dst, body) |
| 40 | + |
| 41 | +## cargo |
| 42 | +cargo_action = """[package] |
| 43 | +name = "actions" |
| 44 | +version = "0.1.0" |
| 45 | +
|
| 46 | +[dependencies] |
| 47 | +serde_json = "1.0" |
| 48 | +""" |
| 49 | + |
| 50 | +cargo_actionloop = """[package] |
| 51 | +name = "action_loop" |
| 52 | +version = "0.1.0" |
| 53 | +authors = ["Roberto Diaz <roberto@theagilemonkeys.com>"] |
| 54 | +
|
| 55 | +[dependencies] |
| 56 | +serde_json = "1.0" |
| 57 | +libc = "0.2.49" |
| 58 | +actions = { path = "../actions" } |
| 59 | +""" |
| 60 | + |
| 61 | +cargo_workspace = """ |
| 62 | +[workspace] |
| 63 | +
|
| 64 | +members = [ |
| 65 | +"action_loop", |
| 66 | +"actions", |
| 67 | +] |
| 68 | +""" |
| 69 | + |
| 70 | +def build(): |
| 71 | + pass |
| 72 | + |
| 73 | +def sources(main, src_dir, tgt_dir, launcher): |
| 74 | + src_file = abspath("%s/exec" % src_dir) |
| 75 | + |
| 76 | + # move exec in the right place |
| 77 | + if exists(src_file): |
| 78 | + os.makedirs(src_dir+"/src", mode=0o755, exist_ok=True) |
| 79 | + copy_replace(src_file, src_dir+"/src/exec__.rs") |
| 80 | + |
| 81 | + # add a cargo.toml if needed |
| 82 | + cargo_action_file = src_dir+"/Cargo.toml" |
| 83 | + if not exists(cargo_action_file): |
| 84 | + write_file(cargo_action_file, cargo_action) |
| 85 | + |
| 86 | + # write the boilerplate in a temp dir |
| 87 | + os.makedirs("/tmp/src", mode=0o755, exist_ok=True) |
| 88 | + tmp_dir = tempfile.mkdtemp(prefix='/tmp/src/') |
| 89 | + copy_replace(launcher, tmp_dir+"/action_loop/src/main.rs", |
| 90 | + "use actions::main as actionMain;", |
| 91 | + "use actions::%s as actionMain;" % main ) |
| 92 | + write_file(tmp_dir+"/action_loop/Cargo.toml", cargo_actionloop) |
| 93 | + write_file(tmp_dir+"/Cargo.toml", cargo_workspace) |
| 94 | + os.rename(src_dir, tmp_dir+"/actions") |
| 95 | + return tmp_dir |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + if len(sys.argv) < 4: |
| 99 | + sys.stdout.write("usage: <main-function> <source-dir> <target-dir>\n") |
| 100 | + sys.stdout.flush() |
| 101 | + sys.exit(1) |
| 102 | + dir = sources(sys.argv[1], abspath(sys.argv[2]), abspath(sys.argv[3]), abspath(sys.argv[0]+".launcher.rs")) |
| 103 | + print(dir) |
| 104 | + sys.stdout.flush() |
| 105 | + sys.stderr.flush() |
0 commit comments