Skip to content

Commit

Permalink
Add --root-dir option to cli #161
Browse files Browse the repository at this point in the history
Also fix the root_dir arg of LinuxDistribution() to be an absolute
path to the root of a filesystem and not a path to /etc

Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
  • Loading branch information
pombredanne committed Nov 13, 2019
1 parent 0f4cb42 commit 3d3f69e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 10 deletions.
33 changes: 25 additions & 8 deletions distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,10 @@ def __init__(self,
uses an unexpected encoding.
"""
self.root_dir = root_dir
self.etc_dir = os.path.join(root_dir, 'etc') \
if root_dir else _UNIXCONFDIR
self.os_release_file = os_release_file or \
os.path.join(self.root_dir or _UNIXCONFDIR, _OS_RELEASE_BASENAME)
os.path.join(self.etc_dir, _OS_RELEASE_BASENAME)
self.distro_release_file = distro_release_file or '' # updated later
self.include_lsb = include_lsb
self.include_uname = include_uname
Expand Down Expand Up @@ -1099,7 +1101,7 @@ def _distro_release_info(self):
return distro_info
else:
try:
basenames = os.listdir(self.root_dir or _UNIXCONFDIR)
basenames = os.listdir(self.etc_dir)
# We sort for repeatability in cases where there are multiple
# distro specific files; e.g. CentOS, Oracle, Enterprise all
# containing `redhat-release` on top of their own.
Expand Down Expand Up @@ -1129,8 +1131,7 @@ def _distro_release_info(self):
continue
match = _DISTRO_RELEASE_BASENAME_PATTERN.match(basename)
if match:
filepath = os.path.join(
self.root_dir or _UNIXCONFDIR, basename)
filepath = os.path.join(self.etc_dir, basename)
distro_info = self._parse_distro_release_file(filepath)
if 'name' in distro_info:
# The name is always present if the pattern matches
Expand Down Expand Up @@ -1206,15 +1207,31 @@ def main():
'-j',
help="Output in machine readable format",
action="store_true")

parser.add_argument(
'--root-dir',
'-r',
type=str,
dest='root_dir',
help="Path to the root filesystem directory (defaults to /)")

args = parser.parse_args()

if args.root_dir:
dist = LinuxDistribution(
include_lsb=False,
include_uname=False,
root_dir=args.root_dir)
else:
dist = _distro

if args.json:
logger.info(json.dumps(info(), indent=4, sort_keys=True))
logger.info(json.dumps(dist.info(), indent=4, sort_keys=True))
else:
logger.info('Name: %s', name(pretty=True))
distribution_version = version(pretty=True)
logger.info('Name: %s', dist.name(pretty=True))
distribution_version = dist.version(pretty=True)
logger.info('Version: %s', distribution_version)
distribution_codename = codename()
distribution_codename = dist.codename()
logger.info('Codename: %s', distribution_codename)


Expand Down
1 change: 1 addition & 0 deletions tests/resources/cli/fedora30/etc/fedora-release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fedora release 30 (Thirty)
19 changes: 19 additions & 0 deletions tests/resources/cli/fedora30/etc/os-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NAME=Fedora
VERSION="30 (Thirty)"
ID=fedora
VERSION_ID=30
VERSION_CODENAME=""
PLATFORM_ID="platform:f30"
PRETTY_NAME="Fedora 30 (Thirty)"
ANSI_COLOR="0;34"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:30"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/30/system-administrators-guide/"
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=30
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=30
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
1 change: 1 addition & 0 deletions tests/resources/cli/fedora30/etc/redhat-release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fedora release 30 (Thirty)
1 change: 1 addition & 0 deletions tests/resources/cli/fedora30/etc/system-release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fedora release 30 (Thirty)
35 changes: 33 additions & 2 deletions tests/test_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def test_cli_for_coverage_yuch(self):
self._parse('distro')
self._parse('distro -j')

def test_cli_can_parse_root_dir_args(self):
root_dir = os.path.join(RESOURCES, 'cli', 'fedora30')
self._parse('distro --root-dir {}'.format(root_dir))

def test_cli(self):
command = [sys.executable, '-m', 'distro']
desired_output = 'Name: ' + distro.name(pretty=True)
Expand All @@ -85,6 +89,33 @@ def test_cli_json(self):
command = [sys.executable, '-m', 'distro', '-j']
assert ast.literal_eval(self._run(command)) == distro.info()

def test_cli_with_root_dir(self):
root_dir = os.path.join(RESOURCES, 'cli', 'fedora30')
command = [sys.executable, '-m', 'distro', '--root-dir', root_dir]
desired_output = 'Name: Fedora 30 (Thirty)\nVersion: 30\nCodename: \n'
assert desired_output == self._run(command)

def test_cli_with_root_dir_as_json(self):
import json
root_dir = os.path.join(RESOURCES, 'cli', 'fedora30')
command = [sys.executable, '-m', 'distro', '-j', '--root-dir', root_dir]
desired_output = '''
{
"codename": "",
"id": "fedora",
"like": "",
"version": "30",
"version_parts": {
"build_number": "",
"major": "30",
"minor": ""
}
}
'''
desired_output = json.loads(desired_output)
results = json.loads(self._run(command))
assert desired_output == results


@pytest.mark.skipif(not IS_LINUX, reason='Irrelevant on non-linux')
class DistroTestCase(object):
Expand Down Expand Up @@ -439,7 +470,7 @@ class TestWithRootDir(TestOSRelease):

def setup_method(self, test_method):
dist = test_method.__name__.split('_')[1]
root_dir = os.path.join(DISTROS_DIR, dist, 'etc')
root_dir = os.path.join(DISTROS_DIR, dist)
self.distro = distro.LinuxDistribution(
os_release_file='',
distro_release_file='non',
Expand Down Expand Up @@ -2075,6 +2106,6 @@ def test_repr(self):
repr_str = repr(distro._distro)
assert "LinuxDistribution" in repr_str
for attr in MODULE_DISTRO.__dict__.keys():
if attr == 'root_dir':
if attr in ('root_dir', 'etc_dir'):
continue
assert attr + '=' in repr_str

0 comments on commit 3d3f69e

Please sign in to comment.