|
1 | 1 | import os
|
2 | 2 |
|
| 3 | +from acbs.const import AUTOBUILD_CONF_DIR |
3 | 4 | from acbs import bashvar
|
| 5 | +from pyparsing import ParseException # type: ignore |
4 | 6 |
|
5 |
| -class AB3Cfg(object): |
6 | 7 |
|
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