Skip to content

Commit

Permalink
Implement --output parameter
Browse files Browse the repository at this point in the history
- Implement --output parameter to specify output file.

- Add input validation for --format parameter.

Signed-off-by: Derek G Foster <[email protected]>
  • Loading branch information
ffoulkes committed Oct 31, 2023
1 parent 388120b commit aa0c159
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
18 changes: 12 additions & 6 deletions docs/scripts/p4cpconfig.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,25 @@ General

``--format=FORMAT``, ``-f FORMAT``
Output file format.

Valid formats are ``cmake``, ``env``, and ``json``.

Defaults to ``cmake`` if no value is specified.

``--load=FILEPATH``, ``-L FILEPATH``
JSON file specifying default configuration settings.

If this parameter is not specified, p4cpconfig will check first
for a file named ``.p4cpconfig`` in the current directory, then for
If no value is specified, p4cpconfig will check first for
a file named ``.p4cpconfig`` in the current directory, then for
a file of the same name in the user's home directory.

May be suppressed by using an *explicitly unspecified* value
(e.g., ``-L-`` or ``--load=None``).

``--output=FILEPATH``, ``-o FILEPATH``
Output file path.

Defaults to standard output if no value is specified.

Paths
-----

Expand Down Expand Up @@ -166,8 +172,8 @@ You can duplicate this behavior by creating a default configuration file:

.. code-block:: bash
./scripts/p4cpconfig.py -L None -f json --target=dpdk \
-O ovs/install -P install > .p4cpconfig
./scripts/p4cpconfig.py -L- -f json --target=dpdk \
-O ovs/install -P install -o .p4cpconfig
The file it generates will be something like this:

Expand All @@ -181,5 +187,5 @@ The file it generates will be something like this:
Notes:

* ``-L None`` keeps p4cpconfig from loading the current defaults.
* ``-L-`` keeps p4cpconfig from loading the current defaults.
* ``-f json`` tells it to create a json file.
41 changes: 33 additions & 8 deletions scripts/p4cpconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ def load_defaults(args):
def process_args(args):
args.variant = None
process_build_type_param(args)
process_format_param(args)
process_output_param(args)
process_target_param(args)
process_path_params(args)
return
Expand All @@ -355,6 +357,25 @@ def process_build_type_param(args):
errcount += 1
return

def process_format_param(args):
global errcount
if args.format in UNSPECIFIED:
logging.error("No output format specified")
errcount += 1
return
format = args.format.lower()
if format in ['cmake', 'env', 'json']:
args.format = format
else:
logging.error("Invalid output format: '%s'", args.format)
errcount += 1
return

def process_output_param(args):
if args.outfile in UNSPECIFIED:
args.outfile = '/dev/stdout'
return

def process_target_param(args):
global errcount
if args.target in UNSPECIFIED:
Expand Down Expand Up @@ -400,10 +421,12 @@ def parse_args():
parser.add_argument('--format', '-f', type=str,
default='cmake',
help='output file format (cmake|json|env)')

parser.add_argument('--load', '-L', dest='default_cfg',
help='default configuration file')
# parser.add_argument('--output', '-o',
# help='output file path')

parser.add_argument('--output', '-o', dest='outfile',
help='output file path')

# 'paths' group
paths = parser.add_argument_group(title='paths')
Expand Down Expand Up @@ -468,11 +491,13 @@ def str2bool(v):
defaults = load_defaults(args)
config = define_config(args, defaults)

if args.format == 'cmake':
config.write_cmake_variables(sys.stdout)
elif args.format == 'env':
config.write_env_variables(sys.stdout)
elif args.format == 'json':
config.write_json_params(sys.stdout)
with open(args.outfile, 'w') as outfile:
if args.format == 'cmake':
config.write_cmake_variables(outfile)
elif args.format == 'env':
config.write_env_variables(outfile)
elif args.format == 'json':
config.write_json_params(outfile)
# end with
# end __main__

0 comments on commit aa0c159

Please sign in to comment.