#!/usr/bin/env python

from __future__ import print_function

import argparse
import json
import os
import sys


def fatal(msg):
    print(msg, file=sys.stderr)
    sys.exit(1)


def find_swift_files(path):
    for parent, dirs, files in os.walk(path, topdown=True):
        for filename in files:
            if not filename.endswith('.swift'):
                continue
            yield filename


def main(arguments):
    parser = argparse.ArgumentParser(
        description='Generate an output file map for the given directory')
    parser.add_argument('-o', dest='output_dir',
                        help='Directory to which the file map will be emitted')
    parser.add_argument('input_dir', help='a directory of swift files')
    args = parser.parse_args(arguments)

    if not args.output_dir:
        fatal("output directory is required")

    # Create the output directory if it doesn't already exist.
    if not os.path.isdir(args.output_dir):
        os.makedirs(args.output_dir)

    output_path = os.path.join(args.output_dir, 'output.json')

    if not os.path.isdir(args.input_dir):
        fatal("input directory does not exist, or is not a directory")

    swift_files = find_swift_files(args.input_dir)
    if not swift_files:
        fatal("no swift files in the given input directory")

    all_records = {}
    for swift_file in swift_files:
        file_name = os.path.splitext(swift_file)[0]
        all_records['./' + swift_file] = {
            'object': './' + file_name + '.o',
            'swift-dependencies': './' + file_name + '.swiftdeps',
        }
    all_records[""] = {
        'swift-dependencies': './main-buildrecord.swiftdeps'
    }

    with open(output_path, 'w') as f:
        json.dump(all_records, f)


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))

Add a utility for generating simple output file maps
[Incremental] Add a utility for generating simple output file maps
