Skip to content

Commit 450956c

Browse files
Cyanoxygenliushuyu
andcommitted
ab3cfg: simplify entire class to one single function
Co-Authored-By: liushuyu <[email protected]>
1 parent 1a5b456 commit 450956c

File tree

1 file changed

+15
-34
lines changed

1 file changed

+15
-34
lines changed

acbs/ab3cfg.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,20 @@
11
import os
22

3+
from acbs.const import AUTOBUILD_CONF_DIR
34
from acbs import bashvar
5+
from pyparsing import ParseException # type: ignore
46

5-
class AB3Cfg(object):
67

7-
def __init__(self, path) -> None:
8-
'''
9-
Class representing autobuild3 config file.
10-
11-
:param path: path to the ab3cfg.sh file, with the filename
12-
'''
13-
self.cfgpath = path
14-
self.vars = None
15-
self.stage2 = False
16-
try:
17-
with open(self.cfgpath, 'rt') as cfgfile:
18-
self.parse(cfgfile)
19-
except Exception as e:
20-
# You run ACBS then you should have AB3, eh?
21-
raise RuntimeError(f'Autobuild3 config file {self.cfgpath} does not exist\n' +
22-
'Unable to read Autobuild3 config file.') from e
23-
24-
def parse(self, file):
25-
'''
26-
Parse the ab3cfg.sh file.
27-
'''
28-
try:
29-
self.vars = bashvar.eval_bashvar(file.read(), filename=self.cfgpath)
30-
except Exception as e:
31-
raise RuntimeError(f'Error parsing autobuild3 config file: {e}.') from e
32-
33-
def is_in_stage2(self) -> bool:
34-
'''
35-
Return True if ab3 is in stage 2 mode.
36-
'''
37-
if self.vars:
38-
self.stage2 = ((self.vars.get('ABSTAGE2') == '1') or False)
39-
return self.stage2
8+
def is_in_stage2() -> bool:
9+
ab3cfg_path: str = os.path.join(AUTOBUILD_CONF_DIR, 'ab3cfg.sh')
10+
try:
11+
with open(ab3cfg_path) as f:
12+
vars = bashvar.eval_bashvar(f.read(), filename=ab3cfg_path)
13+
stage2_val: str = vars.get('ABSTAGE2')
14+
return True if stage2_val == '1' else False
15+
except OSError as e:
16+
raise RuntimeError(f'Unable to read Autobuild config file {ab3cfg_path}.') from e
17+
except ParseException as e:
18+
raise RuntimeError(f'Error occurred while parsing Autobuild config file {ab3cfg_path}.') from e
19+
except Exception as e:
20+
raise RuntimeError(f'Error occurred while checking whether stage 2 mode is enabled.') from e

0 commit comments

Comments
 (0)