Generate EO O&M XML metadata.

def main(args=sys.argv[1:]):
    """Generate EO O&M XML metadata."""
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", nargs=1)
    parser.add_argument("--granule-id", dest="granule_id",
        help=(
            "Optional. Specify a granule to export metadata from."
        )
    )
    parser.add_argument("--single-granule", dest="single_granule",
        action="store_true", default=False,
        help=(
            "When only one granule is contained in the package, include product "
            "metadata from this one granule. Fails when more than one granule "
            "is contained."
        )
    )
    parser.add_argument("--out-file", "-f", dest="out_file",
        help=(
            "Specify an output file to write the metadata to. By default, the "
            "XML is printed on stdout."
        )
    )
    parser.add_argument("--resolution", "-r", dest="resolution", default="10",
        help=(
            "Only produce metadata for bands of this resolution (in meters). "
            "Default is 10."
        )
    )

    parsed = parser.parse_args(args)

    try:
        safe_pkg = s2reader.open(parsed.filename[0])
    except IOError, e:
        parser.error('Could not open SAFE package. Error was "%s"' % e)

    granules = safe_pkg.granules

    granule = None
    if parsed.granule_id:
        granule_dict = dict(
            (granule.granule_identifier, granule) for granule in granules
        )
        try:
            granule = granule_dict[parsed.granule_id]
        except KeyError:
            parser.error('No such granule %r' % parsed.granule_id)

    elif parsed.single_granule:
        if len(granules) > 1:
            parser.error('Package contains more than one granule.')

        granule = granules[0]

    params = _get_product_template_params(safe_pkg, parsed.resolution)

    if granule:
        params.update(_get_granule_template_params(granule, parsed.resolution))
        xml_string = EOOM_TEMPLATE_GRANULE.format(**params)
    else:
        xml_string = EOOM_TEMPLATE_PRODUCT.format(**params)

    if parsed.out_file:
        with open(parsed.out_file, "w") as f:
            f.write(xml_string)
    else:
        print(xml_string)