rodič
e59a4511d6
revize
327d0ae56a
@ -0,0 +1,161 @@
|
||||
# vim: fileencoding=utf-8
|
||||
#
|
||||
from .base_format import Format
|
||||
from calculate.utils.package import (
|
||||
PackageAtomParser,
|
||||
PackageNotFound,
|
||||
PackageAtomError
|
||||
)
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import List
|
||||
import os
|
||||
|
||||
|
||||
class WorldFormat(Format):
|
||||
FORMAT = 'world'
|
||||
EXECUTABLE = False
|
||||
|
||||
_initialized = False
|
||||
|
||||
comment_symbol = '#'
|
||||
|
||||
def __init__(self, document_text: str,
|
||||
template_path: str,
|
||||
ignore_comments: bool = False,
|
||||
add_header: bool = False,
|
||||
already_changed: bool = False,
|
||||
chroot_path="/",
|
||||
**kwargs):
|
||||
processing_methods = []
|
||||
self._atom_parser = PackageAtomParser(chroot_path=chroot_path)
|
||||
|
||||
super().__init__(processing_methods)
|
||||
self._template_name = os.path.basename(template_path)
|
||||
self._action_symbols = ["!!", "!"]
|
||||
self.warnings = []
|
||||
|
||||
if add_header and not ignore_comments:
|
||||
self.header, document_text = self._get_header_and_document_text(
|
||||
document_text,
|
||||
template_path,
|
||||
already_changed=already_changed)
|
||||
else:
|
||||
self.header = ''
|
||||
|
||||
document_text = document_text.strip()
|
||||
if document_text == '':
|
||||
self._document_dictionary = OrderedDict()
|
||||
else:
|
||||
document_lines = self._get_list_of_logic_lines(document_text)
|
||||
self._document_dictionary = self._lines_to_dictionary(
|
||||
document_lines)
|
||||
|
||||
def _lines_to_dictionary(self, document_lines: List[str]) -> OrderedDict:
|
||||
output = OrderedDict()
|
||||
for line in document_lines:
|
||||
action = None
|
||||
for action_symbol in self._action_symbols:
|
||||
if line.startswith(action_symbol):
|
||||
action = action_symbol
|
||||
line = line[len(action_symbol):]
|
||||
break
|
||||
|
||||
name_only = line.split(':')[0]
|
||||
if "/" not in name_only:
|
||||
line = f"{self._template_name}/{line}"
|
||||
|
||||
atom_dict = self._atom_parser.parse_atom_name(line)
|
||||
atom_dict['package_atom'] = (
|
||||
f'{atom_dict["category"]}/{atom_dict["name"]}')
|
||||
if action == "!!":
|
||||
action = "!"
|
||||
print("WITH EXISTANCE CHECK")
|
||||
try:
|
||||
self._atom_parser._check_package_existance(atom_dict)
|
||||
print("PACKAGE WAS FOUND")
|
||||
except (PackageNotFound, PackageAtomError):
|
||||
print("PACKAGE WAS NOT FOUND")
|
||||
self.warnings.append(f"package '{line}' not found")
|
||||
|
||||
levels = [atom_dict["category"], atom_dict["name"]]
|
||||
level = output
|
||||
for next_level in levels:
|
||||
if next_level not in level:
|
||||
level[next_level] = OrderedDict()
|
||||
level = level[next_level]
|
||||
level[atom_dict["slot"]] = action
|
||||
return output
|
||||
|
||||
def join_template(self, template):
|
||||
self._document_dictionary = self._change_categories(
|
||||
template._document_dictionary,
|
||||
self._document_dictionary)
|
||||
|
||||
def _change_categories(self, template: OrderedDict,
|
||||
original: OrderedDict) -> OrderedDict:
|
||||
for category, names in template.items():
|
||||
if category in original:
|
||||
original_names = original[category]
|
||||
else:
|
||||
original_names = OrderedDict()
|
||||
changed_names = self._change_names(names, original_names)
|
||||
|
||||
if not changed_names:
|
||||
original.pop(category)
|
||||
else:
|
||||
original[category] = changed_names
|
||||
|
||||
return original
|
||||
|
||||
def _change_names(self, template_names: OrderedDict,
|
||||
original_names: OrderedDict) -> OrderedDict:
|
||||
for name, slots in template_names.items():
|
||||
if name in original_names:
|
||||
original_slots = original_names[name]
|
||||
else:
|
||||
original_slots = OrderedDict()
|
||||
changed_slots = self._change_slots(slots, original_slots)
|
||||
|
||||
if not changed_slots:
|
||||
original_names.pop(name)
|
||||
else:
|
||||
original_names[name] = changed_slots
|
||||
|
||||
return original_names
|
||||
|
||||
def _change_slots(self, template_slots: OrderedDict,
|
||||
original_slots: OrderedDict) -> OrderedDict:
|
||||
for slot, action in template_slots.items():
|
||||
if slot == "*":
|
||||
if action == "!":
|
||||
original_slots.clear()
|
||||
continue
|
||||
else:
|
||||
if action == "!" and slot in original_slots:
|
||||
original_slots.pop(slot)
|
||||
continue
|
||||
if action is None and slot not in original_slots:
|
||||
original_slots[slot] = None
|
||||
continue
|
||||
|
||||
return original_slots
|
||||
|
||||
def _get_document_text(self) -> str:
|
||||
lines = []
|
||||
categories = sorted(self._document_dictionary.keys())
|
||||
for category in categories:
|
||||
names_dict = self._document_dictionary[category]
|
||||
names = sorted(names_dict.keys())
|
||||
for name in names:
|
||||
slots = sorted(names_dict[name].keys())
|
||||
for slot in slots:
|
||||
slot = '' if slot is None else f":{slot}"
|
||||
lines.append(f"{category}/{name}{slot}")
|
||||
|
||||
lines.append('')
|
||||
return "\n".join(lines)
|
||||
|
||||
@property
|
||||
def document_text(self) -> str:
|
||||
return "{}{}".format(self.header, self._get_document_text())
|
@ -0,0 +1,199 @@
|
||||
import pytest
|
||||
from collections import OrderedDict
|
||||
from calculate.templates.format.world_format import WorldFormat
|
||||
import os
|
||||
|
||||
|
||||
CHROOT_PATH = os.path.join(os.getcwd(),
|
||||
"tests/templates/format/testfiles/world")
|
||||
|
||||
|
||||
@pytest.mark.formats
|
||||
@pytest.mark.world
|
||||
def test_parsing_document():
|
||||
original_text = """www-client/chromium:unstable
|
||||
dev-python/pip
|
||||
dev-util/cmake
|
||||
clementine
|
||||
"""
|
||||
|
||||
original_object = WorldFormat(original_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
result_dict = OrderedDict({"www-client":
|
||||
OrderedDict({"chromium":
|
||||
OrderedDict({"unstable": None})}),
|
||||
"dev-python":
|
||||
OrderedDict({"pip":
|
||||
OrderedDict({None: None})}),
|
||||
"dev-util":
|
||||
OrderedDict({"cmake":
|
||||
OrderedDict({None: None})}),
|
||||
"media-sound":
|
||||
OrderedDict({"clementine":
|
||||
OrderedDict({None: None})})})
|
||||
assert original_object._document_dictionary == result_dict
|
||||
|
||||
|
||||
@pytest.mark.formats
|
||||
@pytest.mark.world
|
||||
def test_parsing_template():
|
||||
template_text = """www-client/chromium:stable
|
||||
!!www-client/chromium:unstable
|
||||
!www-client/chromium
|
||||
dev-util/cmake
|
||||
alsa-utils
|
||||
"""
|
||||
|
||||
template_object = WorldFormat(template_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
result_dict = OrderedDict({"www-client":
|
||||
OrderedDict({"chromium":
|
||||
OrderedDict({"stable": None,
|
||||
"unstable": '!',
|
||||
None: '!'})}),
|
||||
"dev-util":
|
||||
OrderedDict({"cmake":
|
||||
OrderedDict({None: None})}),
|
||||
"media-sound":
|
||||
OrderedDict({"alsa-utils":
|
||||
OrderedDict({None: None})})})
|
||||
assert template_object._document_dictionary == result_dict
|
||||
|
||||
|
||||
@pytest.mark.formats
|
||||
@pytest.mark.world
|
||||
def test_parsing_template_with_warning():
|
||||
template_text = """www-client/chromium:stable
|
||||
!!www-client/chromium:not_exist
|
||||
!www-client/chromium
|
||||
dev-util/cmake
|
||||
alsa-utils
|
||||
"""
|
||||
|
||||
template_object = WorldFormat(template_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
result_dict = OrderedDict({"www-client":
|
||||
OrderedDict({"chromium":
|
||||
OrderedDict({"stable": None,
|
||||
"not_exist": '!',
|
||||
None: '!'})}),
|
||||
"dev-util":
|
||||
OrderedDict({"cmake":
|
||||
OrderedDict({None: None})}),
|
||||
"media-sound":
|
||||
OrderedDict({"alsa-utils":
|
||||
OrderedDict({None: None})})})
|
||||
assert template_object.warnings == [
|
||||
"package 'www-client/chromium:not_exist' not found"]
|
||||
assert template_object._document_dictionary == result_dict
|
||||
|
||||
|
||||
@pytest.mark.formats
|
||||
@pytest.mark.world
|
||||
def test_joining_template():
|
||||
original_text = """www-client/chromium:unstable
|
||||
dev-python/pip
|
||||
dev-util/cmake
|
||||
clementine
|
||||
"""
|
||||
template_text = """www-client/chromium:stable
|
||||
!!www-client/chromium:unstable
|
||||
!www-client/chromium
|
||||
dev-util/cmake
|
||||
alsa-utils
|
||||
"""
|
||||
|
||||
original_object = WorldFormat(original_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
template_object = WorldFormat(template_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
result_dict = OrderedDict({"www-client":
|
||||
OrderedDict({"chromium":
|
||||
OrderedDict({"stable": None})}),
|
||||
"dev-python":
|
||||
OrderedDict({"pip":
|
||||
OrderedDict({None: None})}),
|
||||
"dev-util":
|
||||
OrderedDict({"cmake":
|
||||
OrderedDict({None: None})}),
|
||||
"media-sound":
|
||||
OrderedDict({"clementine":
|
||||
OrderedDict({None: None}),
|
||||
"alsa-utils":
|
||||
OrderedDict({None: None})})})
|
||||
|
||||
original_object.join_template(template_object)
|
||||
assert original_object._document_dictionary == result_dict
|
||||
|
||||
|
||||
@pytest.mark.formats
|
||||
@pytest.mark.world
|
||||
def test_joining_and_get_text_without_header():
|
||||
original_text = """www-client/chromium:unstable
|
||||
dev-python/pip
|
||||
dev-util/cmake
|
||||
clementine
|
||||
"""
|
||||
template_text = """www-client/chromium:stable
|
||||
!!www-client/chromium:unstable
|
||||
!www-client/chromium
|
||||
dev-util/cmake
|
||||
alsa-utils
|
||||
"""
|
||||
result_text = """dev-python/pip
|
||||
dev-util/cmake
|
||||
media-sound/alsa-utils
|
||||
media-sound/clementine
|
||||
www-client/chromium:stable
|
||||
"""
|
||||
|
||||
original_object = WorldFormat(original_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
template_object = WorldFormat(template_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
original_object.join_template(template_object)
|
||||
assert original_object.document_text == result_text
|
||||
|
||||
|
||||
@pytest.mark.formats
|
||||
@pytest.mark.world
|
||||
def test_joining_and_get_text_with_header():
|
||||
original_text = """www-client/chromium:unstable
|
||||
dev-python/pip
|
||||
dev-util/cmake
|
||||
clementine
|
||||
"""
|
||||
template_text = """www-client/chromium:stable
|
||||
!!www-client/chromium:unstable
|
||||
!www-client/chromium
|
||||
dev-util/cmake
|
||||
alsa-utils
|
||||
"""
|
||||
result_text = """#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# /path/to/template/media-sound
|
||||
#-------------------------------------------------------------------------------
|
||||
dev-python/pip
|
||||
dev-util/cmake
|
||||
media-sound/alsa-utils
|
||||
media-sound/clementine
|
||||
www-client/chromium:stable
|
||||
"""
|
||||
|
||||
original_object = WorldFormat(original_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH,
|
||||
add_header=True)
|
||||
template_object = WorldFormat(template_text,
|
||||
"/path/to/template/media-sound",
|
||||
chroot_path=CHROOT_PATH)
|
||||
original_object.join_template(template_object)
|
||||
assert original_object.document_text == result_text
|
@ -0,0 +1 @@
|
||||
0
|
@ -0,0 +1 @@
|
||||
0
|
@ -0,0 +1 @@
|
||||
0
|
@ -0,0 +1 @@
|
||||
0
|
@ -0,0 +1 @@
|
||||
stable
|
@ -0,0 +1 @@
|
||||
unstable
|
Načítá se…
Odkázat v novém úkolu