1212import logging
1313import hashlib
1414import asyncio
15- from typing import Any , Dict , List , NamedTuple , Optional , Tuple , TypedDict
15+ from typing import Any , Dict , List , NamedTuple , Optional , Tuple , TypedDict , TYPE_CHECKING
1616
1717import aiohttp
1818import toml
1919
20+ try :
21+ import yaml
22+ YAML_AVAIL = True
23+ except ImportError :
24+ YAML_AVAIL = False
25+
26+ if TYPE_CHECKING and not YAML_AVAIL :
27+ import yaml
28+
2029CRATES_IO = 'https://static.crates.io/crates'
2130CARGO_HOME = 'cargo'
2231CARGO_CRATES = f'{ CARGO_HOME } /vendor'
@@ -413,11 +422,15 @@ def main():
413422 parser = argparse .ArgumentParser ()
414423 parser .add_argument ('cargo_lock' , help = 'Path to the Cargo.lock file' )
415424 parser .add_argument ('-o' , '--output' , required = False , help = 'Where to write generated sources' )
425+ parser .add_argument ('--yaml' , action = 'store_true' , help = 'Output as YAML instead of JSON' )
416426 parser .add_argument ('-t' , '--git-tarballs' , action = 'store_true' , help = 'Download git repos as tarballs' )
417427 parser .add_argument ('-d' , '--debug' , action = 'store_true' )
418428 args = parser .parse_args ()
429+
419430 if args .output is not None :
420431 outfile = args .output
432+ if args .yaml and YAML_AVAIL :
433+ outfile = 'generated-sources.yml'
421434 else :
422435 outfile = 'generated-sources.json'
423436 if args .debug :
@@ -428,8 +441,13 @@ def main():
428441
429442 generated_sources = asyncio .run (generate_sources (load_toml (args .cargo_lock ),
430443 git_tarballs = args .git_tarballs ))
431- with open (outfile , 'w' ) as out :
432- json .dump (generated_sources , out , indent = 4 , sort_keys = False )
444+
445+ if args .yaml and YAML_AVAIL :
446+ with open (outfile , 'w' ) as out :
447+ yaml .dump (generated_sources , out , sort_keys = False )
448+ else :
449+ with open (outfile , 'w' ) as out :
450+ json .dump (generated_sources , out , indent = 4 , sort_keys = False )
433451
434452
435453if __name__ == '__main__' :
0 commit comments