Skip to content

Commit 1408874

Browse files
authored
Merge pull request #228 from stkw0/config
add configuration file support
2 parents a471d37 + ccef4e1 commit 1408874

File tree

7 files changed

+87
-19
lines changed

7 files changed

+87
-19
lines changed

.github/workflows/functional.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
run: |
2929
sudo apt install python3-pip
3030
pip list --verbose
31+
pip install -r requirements.txt
3132
python3 setup.py install
3233
3334
- name: Clone portage

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,21 @@ The command understands the following command line arguments:
135135
--show-options Show currently selected options and defaults
136136
--ccache CCACHE_DIR Path to mount that contains ccache cache
137137
--batch Do not drop into interactive shell
138+
139+
It is also possible to store default values in a toml configuration file at `~/.config/ebuildtester/config.toml`.
140+
The next example shows all the configuration options that are currently available:
141+
142+
.. code-block:: toml
143+
144+
portage_dir = "/var/db/repos/gentoo"
145+
overlay_dir = ["/var/db/repos/guru"]
146+
147+
features = ["sandbox", "usersandbox", "userfetch"]
148+
149+
install_basic_packages = false
150+
docker_command = "docker"
151+
unstable = true
152+
update = true
153+
batch = false
154+
pull = true
155+
rm = true

ebuildtester/config.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import tomli
2+
from appdirs import user_config_dir
3+
4+
5+
class ConfigFile:
6+
def __init__(self):
7+
appname = "ebuildtester"
8+
cfg_dir = user_config_dir(appname)
9+
cfg_file = "{}/config.toml".format(cfg_dir)
10+
11+
self._d = {}
12+
self._cfg = None
13+
try:
14+
with open(cfg_file, mode="rb") as fp:
15+
self._cfg = tomli.load(fp)
16+
except FileNotFoundError:
17+
pass
18+
19+
def _add(self, e, t=None):
20+
if e in self._cfg:
21+
if t and type(self._cfg[e]) is not t:
22+
raise Exception("{} is not of type {}".format(e, t))
23+
self._d[e] = self._cfg[e]
24+
25+
def get_cfg(self):
26+
if self._cfg is None:
27+
return {}
28+
29+
self._add("batch")
30+
self._add("docker_command")
31+
self._add("features", list)
32+
self._add("install_basic_packages")
33+
self._add("manual")
34+
self._add("overlay_dir", list)
35+
self._add("portage_dir")
36+
self._add("pull")
37+
self._add("rm")
38+
self._add("unstable")
39+
self._add("update")
40+
41+
return self._d

ebuildtester/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55

66
from ebuildtester.atom import Atom
7+
from ebuildtester.config import ConfigFile
78
from ebuildtester.docker import Docker, ExecuteFailure
89
from ebuildtester.parse import parse_commandline
910
import ebuildtester.options as options
@@ -12,7 +13,8 @@
1213
def main():
1314
"""The main function."""
1415

15-
options.OPTIONS = parse_commandline(sys.argv[1:])
16+
cfg = ConfigFile()
17+
options.OPTIONS = parse_commandline(sys.argv[1:], cfg.get_cfg())
1618
if len(options.OPTIONS.atom) > 0:
1719
options.set_logfile('ebuildtester-'
1820
+ ':'.join([f'{atom.category}-{atom.package}'

ebuildtester/parse.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ebuildtester.atom import Atom
99

1010

11-
def parse_commandline(args):
11+
def parse_commandline(args, d):
1212
"""Parse the command line."""
1313

1414
parser = argparse.ArgumentParser(
@@ -38,7 +38,7 @@ def parse_commandline(args):
3838
parser.add_argument(
3939
"--portage-dir",
4040
help="The local portage directory",
41-
required=True)
41+
required="portage_dir" not in d)
4242
parser.add_argument(
4343
"--overlay-dir",
4444
help="Add overlay dir (can be used multiple times)",
@@ -145,6 +145,7 @@ def parse_commandline(args):
145145
'--debug',
146146
help='Add some debugging output',
147147
action='store_true')
148+
parser.set_defaults(**d)
148149

149150
if '--complete' in args:
150151
print('Suggesting')

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tomli
2+
appdirs

test/test_parse.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,77 +10,80 @@ def setUp(self):
1010

1111
def test_atom(self):
1212
options = ebuildtester.parse.parse_commandline(
13-
self.args + ["--atom", "=SECTION/ATOM-1.0.0"])
13+
self.args + ["--atom", "=SECTION/ATOM-1.0.0"], {})
1414
self.assertTrue(Atom("=SECTION/ATOM-1.0.0") in options.atom)
1515

1616
def test_binhost(self):
1717
options = ebuildtester.parse.parse_commandline(
18-
self.args + ["--manual", "--binhost", "http://localhost:8080"])
18+
self.args + ["--manual", "--binhost", "http://localhost:8080"], {})
1919
self.assertIn("http://localhost:8080", options.binhost)
2020

2121
def test_live_ebuild(self):
2222
options = ebuildtester.parse.parse_commandline(
23-
self.args + ["--manual", "--live-ebuild"])
23+
self.args + ["--manual", "--live-ebuild"], {})
2424
self.assertTrue(options.live_ebuild)
2525

2626
def test_manual(self):
27-
options = ebuildtester.parse.parse_commandline(self.args + ["--manual"])
27+
options = ebuildtester.parse.parse_commandline(self.args + ["--manual"], {})
2828
self.assertTrue(options.manual)
2929

3030
def test_portage_dir(self):
3131
import sys
3232
if sys.version_info[0] == 2 and sys.version_info[1] == 6:
33-
self.assertRaises(Exception, ebuildtester.parse.parse_commandline())
33+
self.assertRaises(Exception, ebuildtester.parse.parse_commandline({}))
3434
else:
3535
with self.assertRaises(Exception):
36-
options = ebuildtester.parse.parse_commandline()
36+
options = ebuildtester.parse.parse_commandline({})
3737
options = ebuildtester.parse.parse_commandline(
38-
self.args + ["--manual"])
38+
self.args + ["--manual"], {})
3939
self.assertEqual("~/gentoo", options.portage_dir)
4040

4141
def test_overlay_dir(self):
4242
options = ebuildtester.parse.parse_commandline(
43-
self.args + ["--manual", "--overlay-dir", "."])
43+
self.args + ["--manual", "--overlay-dir", "."], {})
4444
self.assertTrue("." in options.overlay_dir)
45+
options = ebuildtester.parse.parse_commandline(
46+
self.args + ["--manual", "--overlay-dir", "."], {"overlay_dir": ["/overlay/path"]})
47+
self.assertTrue("/overlay/path" in options.overlay_dir)
4548

4649
def test_update(self):
4750
options = ebuildtester.parse.parse_commandline(
48-
self.args + ["--manual", "--update"])
51+
self.args + ["--manual", "--update"], {})
4952
self.assertFalse(options.update)
5053

5154
def test_threads(self):
5255
options = ebuildtester.parse.parse_commandline(
53-
self.args + ["--manual", "--threads", "4"])
56+
self.args + ["--manual", "--threads", "4"], {})
5457
self.assertEqual(options.threads, 4)
5558

5659
def test_use(self):
5760
options = ebuildtester.parse.parse_commandline(
58-
self.args + ["--manual", "--use", "a", "b", "c"])
61+
self.args + ["--manual", "--use", "a", "b", "c"], {})
5962
self.assertTrue("a" in options.use)
6063
self.assertTrue("b" in options.use)
6164
self.assertTrue("c" in options.use)
6265

6366
def test_unmask(self):
6467
options = ebuildtester.parse.parse_commandline(
65-
self.args + ["--manual", "--unmask", "ATOM"])
68+
self.args + ["--manual", "--unmask", "ATOM"], {})
6669
self.assertTrue("ATOM" in options.unmask)
6770

6871
def test_gcc_version(self):
6972
options = ebuildtester.parse.parse_commandline(
70-
self.args + ["--manual", "--gcc-version", "VER"])
73+
self.args + ["--manual", "--gcc-version", "VER"], {})
7174
self.assertEqual("VER", options.gcc_version)
7275

7376
def test_python_single_target(self):
7477
options = ebuildtester.parse.parse_commandline(
75-
self.args + ["--manual", "--python-single-target", "-* python3_8"])
78+
self.args + ["--manual", "--python-single-target", "-* python3_8"], {})
7679
self.assertEqual("-* python3_8", options.python_single_target)
7780

7881
def test_python_targets(self):
7982
options = ebuildtester.parse.parse_commandline(
80-
self.args + ["--manual", "--python-targets", "python3_8"])
83+
self.args + ["--manual", "--python-targets", "python3_8"], {})
8184
self.assertEqual("python3_8", options.python_targets)
8285

8386
def test_docker_image(self):
8487
options = ebuildtester.parse.parse_commandline(
85-
self.args + ["--manual"])
88+
self.args + ["--manual"], {})
8689
self.assertEqual(options.docker_image, "gentoo/stage3")

0 commit comments

Comments
 (0)