Randall Bosetti | ee604ea | 2020-07-27 18:47:41 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3.8 |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 2 | # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import os |
| 8 | import stat |
| 9 | import string |
| 10 | import sys |
| 11 | |
Joshua Seaton | 8bc918c | 2019-12-09 04:33:49 +0000 | [diff] [blame] | 12 | |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 13 | def main(): |
Joshua Seaton | 8bc918c | 2019-12-09 04:33:49 +0000 | [diff] [blame] | 14 | parser = argparse.ArgumentParser( |
| 15 | description='Generate a script that invokes a Dart application') |
| 16 | parser.add_argument( |
| 17 | '--wd', |
| 18 | help='Path to the working directory, relative to that of the script', |
| 19 | required=True) |
| 20 | parser.add_argument( |
| 21 | '--out', help='Path to the invocation file to generate', required=True) |
| 22 | parser.add_argument( |
| 23 | '--dart', |
| 24 | help='Path to the Dart binary, relative to the working directory', |
| 25 | required=True) |
| 26 | parser.add_argument( |
| 27 | '--snapshot', |
| 28 | help='Path to the binary snapshot, relative to the working directory', |
| 29 | required=True) |
| 30 | parser.add_argument( |
| 31 | '--args', |
| 32 | help='Command-line arguments to prepend to those passed to the script', |
| 33 | required=False, |
| 34 | default='') |
| 35 | args = parser.parse_args() |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 36 | |
Joshua Seaton | 8bc918c | 2019-12-09 04:33:49 +0000 | [diff] [blame] | 37 | app_file = args.out |
| 38 | app_path = os.path.dirname(app_file) |
| 39 | if not os.path.exists(app_path): |
| 40 | os.makedirs(app_path) |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 41 | |
Joshua Seaton | 8bc918c | 2019-12-09 04:33:49 +0000 | [diff] [blame] | 42 | script_template = string.Template( |
| 43 | '''#!/bin/sh |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 44 | # DO NOT EDIT |
| 45 | # This script is generated by: |
Gary Miguel | 3d25ef4 | 2020-06-16 22:52:06 +0000 | [diff] [blame] | 46 | # //build/dart/gen_dart_test_invocation.py |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 47 | |
| 48 | cd "$$(dirname $$0)/$wd" |
| 49 | |
| 50 | $dart \\ |
| 51 | $snapshot \\ |
Ross Wang | 09f5ce9 | 2019-04-04 23:30:07 +0000 | [diff] [blame] | 52 | $args \\ |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 53 | "$$@" |
| 54 | ''') |
Joshua Seaton | 8bc918c | 2019-12-09 04:33:49 +0000 | [diff] [blame] | 55 | with open(app_file, 'w') as file: |
| 56 | file.write(script_template.substitute(args.__dict__)) |
| 57 | permissions = ( |
| 58 | stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | |
| 59 | stat.S_IWGRP | stat.S_IXGRP | stat.S_IROTH) |
| 60 | os.chmod(app_file, permissions) |
Joshua Seaton | febb023 | 2019-03-13 23:29:23 -0700 | [diff] [blame] | 61 | |
| 62 | |
| 63 | if __name__ == '__main__': |
Joshua Seaton | 8bc918c | 2019-12-09 04:33:49 +0000 | [diff] [blame] | 64 | sys.exit(main()) |