Skip to content

Commit

Permalink
Add distroinfo and initial skeleton structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jpichon committed Jul 29, 2020
1 parent 16df542 commit f821575
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 24 deletions.
4 changes: 3 additions & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ Sphinx==1.8.5
twine==1.14.0

pytest==4.6.5
pytest-runner==5.1
pytest-runner==5.1

Distroinfo>=0.1
30 changes: 30 additions & 0 deletions rtls/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-

"""Parse configuration information."""

from distroinfo import info


def parse_distro_info_path(path):
"""Break distro_info path into repo + file"""
path = path.strip().rsplit("/", 1)
info_repo = path[0]
info_file = path[1]
remote = False

if info_repo.startswith("http"):
remote = True

return info_file, info_repo, remote


def setup_distro_info(distroinfo_repo):
"""Set up distro_info based on path"""
info_file, info_repo, remote = parse_distro_info_path(distroinfo_repo)

if remote:
di = info.DistroInfo(info_file, remote_git_info=info_repo)
else:
di = info.DistroInfo(info_file, local_info=info_repo)

return di.get_info()
Empty file added rtls/lib/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions rtls/lib/compose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env python
"""This module acts as an interface for locating patches in composes"""


class ComposeLocator(object):

def __init__(self, locator, logger):
"""Constructor for ComposeLocator object
:param rtls.Locator locator: A pre-constructed Locator
:param logging.Logger logger: A pre-configured Python Logger object
"""
self.locator = locator
self.logger = logger
14 changes: 14 additions & 0 deletions rtls/lib/container.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env python
"""This module acts as an interface for locating patches in containers"""


class ContainerLocator(object):

def __init__(self, locator, logger):
"""Constructor for ContainerLocator object
:param rtls.Locator locator: A pre-constructed Locator
:param logging.Logger logger: A pre-configured Python Logger object
"""
self.locator = locator
self.logger = logger
14 changes: 14 additions & 0 deletions rtls/lib/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env python
"""This module acts as an interface for locating patches in git"""


class GitLocator(object):

def __init__(self, distro_info, logger):
"""Constructor for GitLocator object
:param dict distro_info: A distro info dictionary
:param logging.Logger logger: A pre-configured Python Logger object
"""
self.distro_info = distro_info
self.logger = logger
14 changes: 14 additions & 0 deletions rtls/lib/rpm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env python
"""This module acts as an interface for locating patches in rpms"""


class RpmLocator(object):

def __init__(self, distro_info, logger):
"""Constructor for RpmLocator object
:param dict distro_info: A distro info dictionary
:param logging.Logger logger: A pre-configured Python Logger object
"""
self.distro_info = distro_info
self.logger = logger
118 changes: 118 additions & 0 deletions rtls/rtls.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,121 @@
# -*- coding: utf-8 -*-

"""Main module."""

import logging

from rtls import config
from rtls.lib.git import GitLocator
from rtls.lib.rpm import RpmLocator
from rtls.lib.compose import ComposeLocator
from rtls.lib.container import ContainerLocator


LOGGER = logging.getLogger("rtls")
LOGGER.setLevel(logging.INFO)


class Locator(object):
"""Provides functions to locate a patch through its life cycle"""

def __init__(self, distroinfo_path='', logger=None):
"""Constructor for Locator object
:param str distroinfo_path: Path to local or remote distroinfo repo
:param logging.Logger logger: A pre-configured Python Logger object
"""
self._git = None
self._rpm = None
self._container = None
self._compose = None

self.distro_info = config.setup_distro_info(distroinfo_path)
self._setup_logger(logger)

def _setup_logger(self, logger):
"""Set up a pre-configured logger or create a new one
:param logging.Logger logger: A pre-configured Python Logger object
"""
if logger:
self.logger = logger
else:
self.logger = logging.getLogger(__name__)

@property
def git(self):
"""Return object to locate a patch in git"""
if not self._git:
self._git = GitLocator(distro_info=self.distro_info,
logger=self.logger)
return self._git

@git.setter
def git(self, new_git):
"""Set up object to interact with git
:param rtls.git.GitLocator new_git: Pre-constructed GitLocator
"""
if not isinstance(new_git, GitLocator):
raise TypeError("Git must be a GitLocator object.")
self._git = new_git

@property
def rpm(self):
"""Return object to locate a patch in rpms"""
if not self._rpm:
self._rpm = RpmLocator(distro_info=self.distro_info,
logger=self.logger)
return self._rpm

@rpm.setter
def rpm(self, new_rpm):
"""Set up object to interact with rpms
:param rtls.rpm.RpmLocator new_rpm: Pre-constructed RpmLocator
"""
if not isinstance(new_rpm, RpmLocator):
raise TypeError("Rpm must be a RpmLocator object.")
self._rpm = new_rpm

@property
def container(self):
"""Return object to locate a patch in containers"""
if not self._container:
# TODO: Only send the required config options rather than
# the full Locator
self._container = ContainerLocator(
locator=self, logger=self.logger
)
return self._container

@container.setter
def container(self, new_container):
"""Set up object to interact with containers
:param rtls.container.ContainerLocator new_container:
Pre-constructed ContainerLocator
"""
if not isinstance(new_container, ContainerLocator):
raise TypeError("Container must be a ContainerLocator object.")
self._container = new_container

@property
def compose(self):
"""Return object to locate a patch in composes"""
if not self._compose:
# TODO: Only send the required config options rather than
# the full Locator
self._compose = ComposeLocator(locator=self, logger=self.logger)
return self._compose

@compose.setter
def compose(self, new_compose):
"""Set up object to interact with composes
:param rtls.compose.ComposeLocator new_compose: Pre-constructed
ComposeLocator
"""
if not isinstance(new_compose, ComposeLocator):
raise TypeError("Compose must be a ComposeLocator object.")
self._compose = new_compose
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@
history = history_file.read()

requirements = [
# TODO: put package requirements here
'Distroinfo>=0.1',
]

setup_requirements = [
# TODO(jguiditta): put setup requirements (distutils extensions, etc.) here
'pytest-runner',
]

test_requirements = [
# TODO: put package test requirements here
'pytest>=3',
]

Expand All @@ -47,7 +45,7 @@
'Natural Language :: English',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
test_suite='tests',
tests_require=test_requirements,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python

"""Tests for `rtls.config` package."""

from rtls.config import parse_distro_info_path


def test_parse_distro_info_path():
# Result form: file, path, remote boolean
data = {
"/home/dlrn/di/test.yaml": ("test.yaml", "/home/dlrn/di", False),
"https://example.com/info/info.yml": ("info.yml",
"https://example.com/info",
True),
}

for path, result in data.items():
assert parse_distro_info_path(path) == result
33 changes: 17 additions & 16 deletions tests/test_rtls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

"""Tests for `rtls` package."""

import pytest
from unittest.mock import patch

from rtls.lib.git import GitLocator
from rtls.lib.rpm import RpmLocator
from rtls.lib.compose import ComposeLocator
from rtls.lib.container import ContainerLocator
from rtls.rtls import Locator

from rtls import rtls


@pytest.fixture
def response():
"""Sample pytest fixture.
See more at: http://doc.pytest.org/en/latest/fixture.html
def test_locator_init():
"""
# import requests
# return requests.get('https://github.com/audreyr/cookiecutter-pypackage')


def test_content(response):
"""Sample pytest test function with the pytest fixture as an argument."""
# from bs4 import BeautifulSoup
# assert 'GitHub' in BeautifulSoup(response.content).title.string
GIVEN Locator initialized with a distro info path
WHEN the object is created
THEN the library objects are initialized
"""
with patch('rtls.config.setup_distro_info'):
locator = Locator('https://example.com/git/example-full.yml')
assert type(locator.git) is GitLocator
assert type(locator.rpm) is RpmLocator
assert type(locator.compose) is ComposeLocator
assert type(locator.container) is ContainerLocator
4 changes: 1 addition & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
[tox]
envlist = py35, py36, py37, py38, flake8
envlist = py36, py38, flake8

[travis]
python =
3.8: py38
3.7: py37
3.6: py36
3.5: py35

[testenv:flake8]
basepython = python
Expand Down

0 comments on commit f821575

Please sign in to comment.