Skip to content

Commit

Permalink
Add command to create a default config
Browse files Browse the repository at this point in the history
Adds a "mkconfig" subcommand and helper to dump the default
configuration to a file or to stdout.
  • Loading branch information
JoshuaWatt committed Dec 2, 2019
1 parent ad893e4 commit 4361c02
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ Pyrex is configured using a ini-style configuration file. The location of this
file is specified by the `PYREXCONFFILE` environment variable. This environment
variable must be set before the environment is initialized.

If you do not yet have a config file, you can use the `mkconfig` command to use
the default one and assign the `PYREXCONFFILE` variable in a single command
like so:

```shell
$ export PYREXCONFFILE=`./meta-pyrex/mkconfig ./pyrex.ini`
```

The configuration file is the ini file format supported by Python's
[configparser](https://docs.python.org/3/library/configparser.html) class, with
the following notes:
Expand Down
17 changes: 17 additions & 0 deletions ci/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,23 @@ def test_pyrex_config(self):
)
self.assertEqual(s, "ABCDEFGHI")

def test_pyrex_mkconfig(self):
out_file = os.path.join(self.build_dir, "temp-pyrex.ini")
cmd = [os.path.join(PYREX_ROOT, "pyrex.py"), "mkconfig"]

output = self.assertSubprocess(
cmd + [out_file], capture=True, cwd=self.build_dir
)
self.assertEqual(output, out_file)

output = self.assertSubprocess(cmd, capture=True)
self.assertEqual(output, pyrex.read_default_config(False).rstrip())

with open(out_file, "r") as f:
self.assertEqual(f.read().rstrip(), output)

self.assertSubprocess(cmd + [out_file], cwd=self.build_dir, returncode=1)


class PyrexImageType_oe(PyrexImageType_base):
"""
Expand Down
18 changes: 18 additions & 0 deletions mkconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#! /bin/sh
#
# Copyright 2019 Garmin Ltd. or its subsidiaries
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"$(dirname "$0")/pyrex.py" mkconfig "$@"
37 changes: 37 additions & 0 deletions pyrex.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import hashlib
import json
import tempfile
import contextlib

VERSION = "0.0.4"

Expand Down Expand Up @@ -761,6 +762,25 @@ def config_get(args):
print(val)
return 0

def mkconfig(args):
@contextlib.contextmanager
def get_output_file():
if args.output == "-":
yield sys.stdout
else:
with open(args.output, "w" if args.force else "x") as f:
yield f
print(os.path.abspath(args.output))

try:
with get_output_file() as f:
f.write(read_default_config(False))
except FileExistsError:
sys.stderr.write("Refusing to overwrite existing file '%s'\n" % args.output)
return 1

return 0

subparser_args = {}
if sys.version_info >= (3, 7, 0):
subparser_args["required"] = True
Expand Down Expand Up @@ -827,6 +847,23 @@ def config_get(args):
)
config_get_parser.set_defaults(func=config_get)

mkconfig_parser = subparsers.add_parser(
"mkconfig", help="Create a default Pyrex configuration"
)
mkconfig_parser.add_argument(
"-f",
"--force",
action="store_true",
help="Overwrite destination file if it already exists",
)
mkconfig_parser.add_argument(
"output",
default="-",
nargs="?",
help="Output file. Use '-' for standard out. Default is %(default)s",
)
mkconfig_parser.set_defaults(func=mkconfig)

args = parser.parse_args()

func = getattr(args, "func", None)
Expand Down

0 comments on commit 4361c02

Please sign in to comment.