|
29 | 29 | import logging |
30 | 30 | import os |
31 | 31 | import string |
32 | | -from pathlib import Path |
| 32 | +from contextlib import suppress |
33 | 33 | from functools import lru_cache |
| 34 | +from pathlib import Path |
34 | 35 |
|
35 | 36 | from jinja2 import Template |
36 | 37 |
|
37 | 38 | from plainbox.i18n import gettext as _ |
38 | | -from plainbox.impl.decorators import cached_property |
39 | | -from plainbox.impl.decorators import instance_method_lru_cache |
| 39 | +from plainbox.impl.decorators import cached_property, instance_method_lru_cache |
40 | 40 | from plainbox.impl.secure.origin import Origin |
41 | 41 | from plainbox.impl.secure.rfc822 import normalize_rfc822_value |
42 | | -from plainbox.impl.symbol import Symbol |
43 | | -from plainbox.impl.symbol import SymbolDef |
44 | | -from plainbox.impl.symbol import SymbolDefMeta |
45 | | -from plainbox.impl.symbol import SymbolDefNs |
46 | | -from plainbox.impl.unit import concrete_validators |
47 | | -from plainbox.impl.unit import get_accessed_parameters |
48 | | -from plainbox.impl.unit.validators import IFieldValidator |
49 | | -from plainbox.impl.unit.validators import MultiUnitFieldIssue |
50 | | -from plainbox.impl.unit.validators import PresentFieldValidator |
51 | | -from plainbox.impl.unit.validators import UnitFieldIssue |
52 | | -from plainbox.impl.validation import Problem |
53 | | -from plainbox.impl.validation import Severity |
| 42 | +from plainbox.impl.symbol import Symbol, SymbolDef, SymbolDefMeta, SymbolDefNs |
| 43 | +from plainbox.impl.unit import concrete_validators, get_accessed_parameters |
| 44 | +from plainbox.impl.unit.validators import ( |
| 45 | + MultiUnitFieldIssue, |
| 46 | + PresentFieldValidator, |
| 47 | + UnitFieldIssue, |
| 48 | +) |
| 49 | +from plainbox.impl.validation import Problem, Severity |
54 | 50 |
|
55 | 51 | __all__ = ["Unit", "UnitValidator"] |
56 | 52 |
|
|
61 | 57 | @lru_cache(maxsize=None) |
62 | 58 | def on_ubuntucore(): |
63 | 59 | """ |
64 | | - Check if running from on ubuntu core |
| 60 | + Returns `True` when running in a strict snap |
65 | 61 | """ |
66 | 62 | snap = os.getenv("SNAP") |
67 | 63 | if snap: |
68 | 64 | with open(os.path.join(snap, "meta/snap.yaml")) as f: |
69 | | - for l in f.readlines(): |
70 | | - if l == "confinement: classic\n": |
| 65 | + for line in f.readlines(): |
| 66 | + if line == "confinement: classic\n": |
71 | 67 | return False |
72 | 68 | return True |
73 | 69 | return False |
74 | 70 |
|
75 | 71 |
|
| 72 | +@lru_cache(maxsize=None) |
| 73 | +def on_os_ubuntucore() -> bool: |
| 74 | + """ |
| 75 | + Returns `True` if the host OS is Ubuntu Core |
| 76 | + """ |
| 77 | + with suppress(FileNotFoundError): |
| 78 | + # if this path exists, we are in a strict snap, but we may not be on UC |
| 79 | + return ( |
| 80 | + 'NAME="Ubuntu Core"' |
| 81 | + in Path("/var/lib/snapd/hostfs/etc/os-release").read_text() |
| 82 | + ) |
| 83 | + return 'NAME="Ubuntu Core"' in Path("/etc/os-release").read_text() |
| 84 | + |
| 85 | + |
76 | 86 | @lru_cache(maxsize=None) |
77 | 87 | def get_snap_base(): |
78 | 88 | """ |
|
0 commit comments