pungi/pungi/scripts/gather_modules.py

65 lines
2.0 KiB
Python
Raw Normal View History

import argparse
import gzip
import os
import typing
from argparse import ArgumentParser
from typing import List
import logging
import yaml
def collect_modules(modules_paths: List[typing.BinaryIO], target_dir: str):
"""
Read given modules.yaml.gz files and export modules
and modulemd files from it.
Returns:
object:
"""
modules_path = os.path.join(target_dir, 'modules')
module_defaults_path = os.path.join(target_dir, 'module_defaults')
os.makedirs(modules_path, exist_ok=True)
os.makedirs(module_defaults_path, exist_ok=True)
for module_file in modules_paths:
data = gzip.decompress(module_file.read())
documents = yaml.load_all(data, Loader=yaml.BaseLoader)
for doc in documents:
if doc['document'] == 'modulemd-defaults':
name = doc['data']['module'] + '.yaml'
path = os.path.join(module_defaults_path, name)
logging.info('Found %s module defaults', name)
else:
name = '%s-%s-%s.%s' % (
doc['data']['name'],
doc['data']['stream'],
doc['data']['version'],
doc['data']['context']
)
path = os.path.join(modules_path, name)
logging.info('Found module %s', name)
if 'artifacts' not in doc['data']:
logging.warning('RPM %s does not have explicit list of artifacts', name)
with open(path, 'w') as f:
yaml.dump(doc, f, default_flow_style=False)
def cli_main():
parser = ArgumentParser()
parser.add_argument(
'-p', '--path', required=True,
type=argparse.FileType('rb'), nargs='+',
help='Path to modules.yaml.gz file. '
'You may pass multiple files by passing -p path1 path2'
)
parser.add_argument('-t', '--target', required=True)
namespace = parser.parse_args()
collect_modules(namespace.path, namespace.target)
if __name__ == '__main__':
cli_main()