Skip to content

Commit 4361c02

Browse files
committed
Add command to create a default config
Adds a "mkconfig" subcommand and helper to dump the default configuration to a file or to stdout.
1 parent ad893e4 commit 4361c02

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ Pyrex is configured using a ini-style configuration file. The location of this
129129
file is specified by the `PYREXCONFFILE` environment variable. This environment
130130
variable must be set before the environment is initialized.
131131

132+
If you do not yet have a config file, you can use the `mkconfig` command to use
133+
the default one and assign the `PYREXCONFFILE` variable in a single command
134+
like so:
135+
136+
```shell
137+
$ export PYREXCONFFILE=`./meta-pyrex/mkconfig ./pyrex.ini`
138+
```
139+
132140
The configuration file is the ini file format supported by Python's
133141
[configparser](https://docs.python.org/3/library/configparser.html) class, with
134142
the following notes:

ci/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,23 @@ def test_pyrex_config(self):
689689
)
690690
self.assertEqual(s, "ABCDEFGHI")
691691

692+
def test_pyrex_mkconfig(self):
693+
out_file = os.path.join(self.build_dir, "temp-pyrex.ini")
694+
cmd = [os.path.join(PYREX_ROOT, "pyrex.py"), "mkconfig"]
695+
696+
output = self.assertSubprocess(
697+
cmd + [out_file], capture=True, cwd=self.build_dir
698+
)
699+
self.assertEqual(output, out_file)
700+
701+
output = self.assertSubprocess(cmd, capture=True)
702+
self.assertEqual(output, pyrex.read_default_config(False).rstrip())
703+
704+
with open(out_file, "r") as f:
705+
self.assertEqual(f.read().rstrip(), output)
706+
707+
self.assertSubprocess(cmd + [out_file], cwd=self.build_dir, returncode=1)
708+
692709

693710
class PyrexImageType_oe(PyrexImageType_base):
694711
"""

mkconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#! /bin/sh
2+
#
3+
# Copyright 2019 Garmin Ltd. or its subsidiaries
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
"$(dirname "$0")/pyrex.py" mkconfig "$@"

pyrex.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import hashlib
3131
import json
3232
import tempfile
33+
import contextlib
3334

3435
VERSION = "0.0.4"
3536

@@ -761,6 +762,25 @@ def config_get(args):
761762
print(val)
762763
return 0
763764

765+
def mkconfig(args):
766+
@contextlib.contextmanager
767+
def get_output_file():
768+
if args.output == "-":
769+
yield sys.stdout
770+
else:
771+
with open(args.output, "w" if args.force else "x") as f:
772+
yield f
773+
print(os.path.abspath(args.output))
774+
775+
try:
776+
with get_output_file() as f:
777+
f.write(read_default_config(False))
778+
except FileExistsError:
779+
sys.stderr.write("Refusing to overwrite existing file '%s'\n" % args.output)
780+
return 1
781+
782+
return 0
783+
764784
subparser_args = {}
765785
if sys.version_info >= (3, 7, 0):
766786
subparser_args["required"] = True
@@ -827,6 +847,23 @@ def config_get(args):
827847
)
828848
config_get_parser.set_defaults(func=config_get)
829849

850+
mkconfig_parser = subparsers.add_parser(
851+
"mkconfig", help="Create a default Pyrex configuration"
852+
)
853+
mkconfig_parser.add_argument(
854+
"-f",
855+
"--force",
856+
action="store_true",
857+
help="Overwrite destination file if it already exists",
858+
)
859+
mkconfig_parser.add_argument(
860+
"output",
861+
default="-",
862+
nargs="?",
863+
help="Output file. Use '-' for standard out. Default is %(default)s",
864+
)
865+
mkconfig_parser.set_defaults(func=mkconfig)
866+
830867
args = parser.parse_args()
831868

832869
func = getattr(args, "func", None)

0 commit comments

Comments
 (0)