| #!/usr/bin/env fuchsia-vendored-python |
| # Copyright 2020 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| # Lint as: python3 |
| |
| import argparse |
| import json |
| import os |
| import sys |
| |
| |
| # Generates shebang files for binaries so they can be invoked from the shell. |
| # Generated files look like this: |
| # #!resolve fuchsia-pkg://fuchsia.com/<package-name>#bin/<bin-name> |
| def main(): |
| parser = argparse.ArgumentParser( |
| "Generate shebang files for shell binaries" |
| ) |
| parser.add_argument("--package-name", required=True) |
| parser.add_argument( |
| "--input-distribution-manifest", |
| required=True, |
| type=argparse.FileType("r"), |
| help="Path to a distribution manifest with all shell binaries", |
| ) |
| parser.add_argument( |
| "--output-directory", |
| required=True, |
| help="Path to a directory to store generated shebang files", |
| ) |
| parser.add_argument( |
| "--output-distribution-manifest", |
| required=True, |
| help="Path to write output distribution manifest", |
| ) |
| parser.add_argument( |
| "--depfile", |
| required=True, |
| help="Depfile for listing generated shebang files as additional inputs", |
| ) |
| args = parser.parse_args() |
| |
| input_dist = json.load(args.input_distribution_manifest) |
| output_dist = [] |
| shebang_files = [] |
| |
| for dist in input_dist: |
| dest = dist["destination"] |
| if not dest.startswith("bin/"): |
| continue |
| shebang_file = os.path.join( |
| args.output_directory, "__untraced_shebangs__", dest |
| ) |
| shebang_files.append(shebang_file) |
| os.makedirs(os.path.dirname(shebang_file), exist_ok=True) |
| with open(shebang_file, "w") as f: |
| f.write( |
| f"#!resolve fuchsia-pkg://fuchsia.com/{args.package_name}#{dest}\n" |
| ) |
| output_dist.append(dict(dist, source=shebang_file)) |
| |
| with open( |
| args.output_distribution_manifest, "w" |
| ) as output_distribution_manifest: |
| json.dump( |
| output_dist, |
| output_distribution_manifest, |
| indent=2, |
| sort_keys=True, |
| separators=(",", ": "), |
| ) |
| |
| with open(args.depfile, "w") as depfile: |
| depfile.write( |
| "{}: {}\n".format( |
| args.output_distribution_manifest.name, " ".join(shebang_files) |
| ) |
| ) |
| |
| return 0 |
| |
| |
| if __name__ == "__main__": |
| sys.exit(main()) |