From 194d44746597ef9c745d94cd3befb117544e841c Mon Sep 17 00:00:00 2001 From: Nick Coghlan Date: Oct 27 2017 05:54:46 +0000 Subject: Emit generated modulemd on stdout Emitting the generated modulemd on stdout is more useful during development, and the related --output option gives users full control of the name of the resulting file (rather than having to be aware of a fedmod-defined implicit naming convention). --- diff --git a/README.md b/README.md index cdca809..ff56937 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,10 @@ whichever one you think looks better as your starting point. ## Modulemd creation `fedmod rpm2module [RPM NAMES]` creates a modulemd file from the -given package names. Output is written as `.yaml` when a -single package name is given, and `modulemd-output.yaml` otherwise. +given package names and emits it on `stdout`. The YAML metadata can be written +directly to a file instead by passing the ``--output` (or `-o`) option: + + $ fedmod rpm2module -o graphite-web.yaml graphite-web The following metadata is currently used as input to the draft module generation process: diff --git a/src/_fedmod/cli.py b/src/_fedmod/cli.py index 514a0f4..f3ec46d 100644 --- a/src/_fedmod/cli.py +++ b/src/_fedmod/cli.py @@ -29,6 +29,13 @@ class ModtoolsCLI(object): description="Gets package info and dependencies and creates modulemd file." ) parser_rpm2module.add_argument( + "--output", + "-o", + metavar="FILE", + dest="output_fname", + help="Write to FILE instead of stdout" + ) + parser_rpm2module.add_argument( "pkgs", metavar='PKGS', nargs='+', @@ -61,7 +68,7 @@ def run(): logging.basicConfig(level=logging.INFO) if cli.args.cmd_name == 'rpm2module': mg = ModuleGenerator(cli.args.pkgs) - mg.run() + mg.run(cli.args.output_fname) elif cli.args.cmd_name == 'fetch-metadata': _repodata.download_repo_metadata() diff --git a/src/_fedmod/module_generator.py b/src/_fedmod/module_generator.py index 33836db..9ddda30 100644 --- a/src/_fedmod/module_generator.py +++ b/src/_fedmod/module_generator.py @@ -1,5 +1,6 @@ from __future__ import absolute_import +import sys import modulemd import logging import dnf @@ -78,10 +79,9 @@ class ModuleGenerator(object): raise ValueError('No package found in repo') self.pkg = q[0] - def _save_module_md(self): + def _save_module_md(self, output_fname): """ - Function saves modulemd file to the current directory - based on argument name + Function writes modulemd to either stdout or the given filename :return: """ @@ -89,8 +89,11 @@ class ModuleGenerator(object): file_name = self.pkgs[0] + '.yaml' else: file_name = "modulemd-output.yaml" - self.mmd.dump(file_name) - print('Modulemd file is generated here ./%s' % file_name) + if output_fname is not None: + self.mmd.dump(file_name) + print('Generated modulemd file: %r' % output_fname) + else: + print(self.mmd.dumps()) return True def _update_module_md(self): @@ -136,6 +139,7 @@ class ModuleGenerator(object): self.mmd.filter.add_rpm(pkg) # TODO: Emit something for non-empty self.unresolved_build_rpms + # rather than relying solely on the warnings emitted on stderr def _get_build_order(self, pkg): @@ -144,8 +148,8 @@ class ModuleGenerator(object): else: return 0 - def run(self): + def run(self, output_fname): if len(self.pkgs) == 1: self._get_pkg_info() self._update_module_md() - self._save_module_md() + self._save_module_md(output_fname) diff --git a/tests/test_module_generator.py b/tests/test_module_generator.py index e711b91..d8c7aa8 100644 --- a/tests/test_module_generator.py +++ b/tests/test_module_generator.py @@ -8,11 +8,11 @@ def _generate_modulemd(rpms): cmd_input.extend(rpms) cli = ModtoolsCLI(cmd_input) mg = ModuleGenerator(cli.pkgs) - mg.run() if len(rpms) == 1: output_fname = rpms[0] + '.yaml' else: output_fname = 'modulemd-output.yaml' + mg.run(output_fname) return mg, output_fname