Skip to content

Commit bad6f0d

Browse files
committed
cargo: Add optional yaml output
Fixes #441
1 parent 57b11b2 commit bad6f0d

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

cargo/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Poetry users can run `poetry install` and skip this.
99
Otherwise install Python 3.8+ with these modules:
1010
- toml
1111
- aiohttp
12+
- (Optional) PyYAML>=6.0.2 for YAML output instead of JSON
1213

1314
Generated manifests are supported by flatpak-builder 1.2.x or newer.
1415

cargo/flatpak-cargo-generator.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,20 @@
1212
import logging
1313
import hashlib
1414
import 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

1717
import aiohttp
1818
import 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+
2029
CRATES_IO = 'https://static.crates.io/crates'
2130
CARGO_HOME = 'cargo'
2231
CARGO_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

435453
if __name__ == '__main__':

cargo/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package-mode = false
55
python = ">=3.8"
66
aiohttp = "^3.9.5"
77
toml = "^0.10.2"
8-
8+
PyYAML = "^6.0.2"
99

1010
[build-system]
1111
requires = ["poetry-core"]

0 commit comments

Comments
 (0)