parent
8959bddcdc
commit
998a495669
@ -1,176 +1,222 @@
|
||||
# vim: fileencoding=utf-8
|
||||
#
|
||||
import os
|
||||
from .base_format import Format
|
||||
from collections import OrderedDict
|
||||
from jinja2 import PackageLoader, Environment
|
||||
from pyparsing import Literal, Regex, Word, nums, alphanums, Optional,\
|
||||
ParseException
|
||||
from pyparsing import Literal, Regex, SkipTo, LineEnd, lineno, LineStart
|
||||
from calculate.utils.package import PackageAtomParser, Package, PackageNotFound
|
||||
from calculate.utils.files import join_paths
|
||||
from pprint import pprint
|
||||
from glob import iglob
|
||||
from fnmatch import fnmatch
|
||||
|
||||
|
||||
ADD, REMOVE, MOVE = range(0, 3)
|
||||
|
||||
|
||||
class ContentsFormat(Format):
|
||||
FORMAT = 'contents'
|
||||
EXECUTABLE = False
|
||||
EXECUTABLE = True
|
||||
|
||||
_initialized = False
|
||||
|
||||
comment_symbol = '#'
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
'''Метод для инициализации парсеров.'''
|
||||
if not cls._initialized:
|
||||
cls._initialize_parser()
|
||||
return super().__new__(cls)
|
||||
return super().__new__(cls)
|
||||
|
||||
def __init__(self, document_text: str,
|
||||
template_path,
|
||||
ignore_comments=False,
|
||||
join_before=False,
|
||||
add_header=False,
|
||||
already_changed=False,
|
||||
parameters=None,
|
||||
template_parser=True):
|
||||
processing_methods = [self._parse_dir_line,
|
||||
self._parse_sym_line,
|
||||
self._parse_obj_line]
|
||||
|
||||
super().__init__(processing_methods)
|
||||
|
||||
self._ignore_comments = ignore_comments
|
||||
self._join_before = join_before
|
||||
|
||||
# флаг для указания режима, в котором работает формат.
|
||||
# Если True => формат работает для наложения шаблонов;
|
||||
# Если False => формат работает для получения словаря, используемого
|
||||
# модулем package.
|
||||
self._template_parser_flag = template_parser
|
||||
|
||||
if not self._initialized:
|
||||
self._initialize_parser()
|
||||
|
||||
if (add_header and not ignore_comments and template_parser
|
||||
and template_path is not None):
|
||||
self.header, document_text = self._get_header_and_document_text(
|
||||
document_text,
|
||||
template_path,
|
||||
already_changed=already_changed)
|
||||
else:
|
||||
self.header = ''
|
||||
ignore_comments=False):
|
||||
self._command_methods = {ADD: self._add_command,
|
||||
REMOVE: self._remove_command,
|
||||
MOVE: self._move_command}
|
||||
self._errors = []
|
||||
|
||||
self._commands = self._parse_template(document_text)
|
||||
self._template_path = template_path
|
||||
self._packages = dict()
|
||||
self._atom_parser = None
|
||||
|
||||
def execute_format(self, target_path, chroot_path='/'):
|
||||
'''Метод для запуска работы формата.'''
|
||||
self._package = dict()
|
||||
self._atom_parser = PackageAtomParser(chroot_path=chroot_path)
|
||||
for command in self._commands:
|
||||
target_package = self._get_package(command['target'], chroot_path)
|
||||
|
||||
if 'source' not in command:
|
||||
source_package = None
|
||||
else:
|
||||
source_package = self._get_package(command['source'],
|
||||
chroot_path)
|
||||
|
||||
self._command_methods[command['action']](target_package,
|
||||
command['path'],
|
||||
command['lineno'],
|
||||
source=source_package)
|
||||
pprint(target_package.contents_dictionary)
|
||||
self._save_changes()
|
||||
|
||||
def _add_command(self, target, glob_path, lineno, source=None):
|
||||
# Если файл уже есть в пакете -- ничего не делаем.
|
||||
glob_path = join_paths(target.chroot_path, glob_path)
|
||||
for file_path in iglob(glob_path):
|
||||
# Если файл уже в пакете -- ничего не делаем.
|
||||
if file_path in target:
|
||||
continue
|
||||
|
||||
# Проверяем существование файла.
|
||||
if not os.path.exists(file_path):
|
||||
error = (f"Format processing error:{lineno}: file "
|
||||
f"'{file_path}' does not exist.")
|
||||
self._errors.append(error)
|
||||
continue
|
||||
|
||||
# Проверяем, не принадлежит ли файл какому-нибудь другому пакету.
|
||||
try:
|
||||
file_package = self._atom_parser.get_file_package(file_path)
|
||||
except PackageNotFound:
|
||||
file_package = None
|
||||
if file_package is not None:
|
||||
error = (f"Format processing error:{lineno}: can not add file"
|
||||
f" '{file_path}' belonging to the package"
|
||||
f" {file_package} into the package '{target}'.")
|
||||
self._errors.append(error)
|
||||
continue
|
||||
|
||||
target.add_file(file_path)
|
||||
|
||||
def _remove_command(self, target, glob_path, lineno, source=None):
|
||||
paths_to_remove = []
|
||||
if glob_path.endswith('/'):
|
||||
glob_path = glob_path[0:-1]
|
||||
|
||||
for file_path in target.contents_dictionary.keys():
|
||||
if fnmatch(file_path, glob_path):
|
||||
paths_to_remove.append(file_path)
|
||||
|
||||
for file_path in paths_to_remove:
|
||||
print(file_path)
|
||||
target.remove_file(file_path)
|
||||
|
||||
def _move_command(self, target, glob_path, lineno, source=None):
|
||||
if source is None:
|
||||
return
|
||||
|
||||
document_text = document_text.strip()
|
||||
if document_text == '':
|
||||
self._document_dictionary = OrderedDict()
|
||||
paths_to_move = []
|
||||
if glob_path.endswith('/'):
|
||||
glob_path = glob_path[0:-1]
|
||||
|
||||
for file_path in source.contents_dictionary.keys():
|
||||
if fnmatch(file_path, glob_path):
|
||||
paths_to_move.append(file_path)
|
||||
|
||||
for file_path in paths_to_move:
|
||||
removed = source.remove_file(file_path)
|
||||
|
||||
for file_path, file_info in removed.items():
|
||||
if file_info['type'] == 'dir':
|
||||
target.add_dir(file_path)
|
||||
elif file_info['type'] == 'obj':
|
||||
target.add_obj(file_path,
|
||||
file_md5=file_info['md5'],
|
||||
mtime=file_info['mtime'])
|
||||
elif file_info['type'] == 'sym':
|
||||
target.add_sym(file_path,
|
||||
target_path=file_info['target'],
|
||||
mtime=file_info['mtime'])
|
||||
target.sort_contents_dictionary()
|
||||
|
||||
def _save_changes(self):
|
||||
for atom_name, package in self._packages.items():
|
||||
print(f"SAVING CHANGES FOR PACKAGE: {package}")
|
||||
print('CONTENTS TEXT:')
|
||||
print(package.render_contents_file())
|
||||
package.write_contents_file()
|
||||
self._packages = {}
|
||||
|
||||
def _get_package(self, atom_name: str, chroot_path: str) -> Package:
|
||||
atom = self._atom_parser.parse_package_parameter(atom_name)
|
||||
if atom not in self._packages:
|
||||
package = Package(atom, chroot_path=chroot_path)
|
||||
self._packages.update({atom: package})
|
||||
else:
|
||||
document_lines = self._get_list_of_logic_lines(document_text)
|
||||
self._lines_to_dictionary(document_lines)
|
||||
package = self._packages[atom]
|
||||
|
||||
return package
|
||||
|
||||
def _parse_template(self, template_text: str) -> list:
|
||||
for tokens, start, end in self.template_parser.scanString(
|
||||
template_text):
|
||||
parse_result = tokens[0]
|
||||
if parse_result is None:
|
||||
continue
|
||||
elif 'error' in parse_result:
|
||||
error = (f'Parse Error:{parse_result["lineno"]}:'
|
||||
f' {parse_result["error"]}')
|
||||
self._errors.append(error)
|
||||
continue
|
||||
yield parse_result
|
||||
|
||||
@classmethod
|
||||
def _initialize_parser(cls):
|
||||
action_symbols = (Literal('!') | Literal('-'))
|
||||
|
||||
sym_keyword = Literal('sym')
|
||||
dir_keyword = Literal('dir')
|
||||
obj_keyword = Literal('obj')
|
||||
|
||||
symlink_arrow = Literal('->')
|
||||
atom = Regex(PackageAtomParser.atom_regex)
|
||||
remove_symbol = Literal('!')
|
||||
path = Regex(r'\S+')
|
||||
|
||||
file_path = Regex(r'\S+')
|
||||
add_parser = atom('target') + path('path') + LineEnd().suppress()
|
||||
add_parser.setParseAction(cls._parse_add)
|
||||
|
||||
time_value = Word(nums)
|
||||
move_parser = (atom('source') + Literal(',').suppress()
|
||||
+ atom('target') + path('path') + LineEnd().suppress())
|
||||
move_parser.setParseAction(cls._parse_move)
|
||||
|
||||
md5 = Word(alphanums)
|
||||
remove_parser = (remove_symbol.suppress() + atom('target')
|
||||
+ path('path') + LineEnd().suppress())
|
||||
remove_parser.setParseAction(cls._parse_remove)
|
||||
|
||||
cls.sym_line = (Optional(action_symbols, default='')('action')
|
||||
+ sym_keyword('type') + file_path('name')
|
||||
+ symlink_arrow.suppress() + file_path('target')
|
||||
+ time_value('time'))
|
||||
empty_line = LineStart().suppress() + LineEnd().suppress()
|
||||
empty_line.setParseAction(cls._parse_skip)
|
||||
|
||||
cls.dir_line = (Optional(action_symbols, default='')('action')
|
||||
+ dir_keyword('type') + file_path('name'))
|
||||
comment = Literal('#') + SkipTo(LineEnd(), include=True)
|
||||
comment.setParseAction(cls._parse_skip)
|
||||
|
||||
cls.obj_line = (Optional(action_symbols, default='')('action')
|
||||
+ obj_keyword('type') + file_path('name')
|
||||
+ md5('md5') + time_value('time'))
|
||||
unexpected = SkipTo(LineEnd(), include=True)
|
||||
unexpected.setParseAction(cls._parse_unexpected)
|
||||
|
||||
cls._initialized = True
|
||||
cls.template_parser = (move_parser | remove_parser | add_parser
|
||||
| empty_line | comment | unexpected)
|
||||
|
||||
def _parse_sym_line(self, line):
|
||||
'''Метод для разбора строк типа sym.'''
|
||||
try:
|
||||
parsing_result = self.sym_line.parseString(line)
|
||||
self._match = True
|
||||
if self._template_parser_flag:
|
||||
output_name = (parsing_result.action, parsing_result.name)
|
||||
output_value = (parsing_result.type,
|
||||
parsing_result.target,
|
||||
parsing_result.time)
|
||||
self._item_to_add = OrderedDict({output_name: [output_value]})
|
||||
else:
|
||||
output_name = parsing_result.name
|
||||
output_value = OrderedDict({'type': parsing_result.type,
|
||||
'target': parsing_result.target,
|
||||
'mtime': parsing_result.time})
|
||||
|
||||
self._item_to_add = OrderedDict({output_name: output_value})
|
||||
self._ready_to_update = True
|
||||
except ParseException:
|
||||
return
|
||||
@classmethod
|
||||
def _parse_add(cls, string, location, parse_result):
|
||||
target = parse_result.asDict()['target']
|
||||
path = parse_result.asDict()['path']
|
||||
return {'action': ADD, 'target': target, 'path': path,
|
||||
'lineno': lineno(location, string)}
|
||||
|
||||
def _parse_dir_line(self, line):
|
||||
'''Метод для разбора строк типа dir.'''
|
||||
try:
|
||||
parsing_result = self.dir_line.parseString(line)
|
||||
self._match = True
|
||||
if self._template_parser_flag:
|
||||
output_name = (parsing_result.action, parsing_result.name)
|
||||
output_value = (parsing_result.type,)
|
||||
self._item_to_add = OrderedDict({output_name: [output_value]})
|
||||
else:
|
||||
output_name = parsing_result.name
|
||||
output_value = OrderedDict({'type': parsing_result.type})
|
||||
@classmethod
|
||||
def _parse_remove(cls, string, location, parse_result):
|
||||
target = parse_result.asDict()['target']
|
||||
path = parse_result.asDict()['path']
|
||||
return {'action': REMOVE, 'target': target, 'path': path,
|
||||
'lineno': lineno(location, string)}
|
||||
|
||||
self._item_to_add = OrderedDict({output_name: output_value})
|
||||
self._ready_to_update = True
|
||||
except ParseException:
|
||||
return
|
||||
@classmethod
|
||||
def _parse_move(cls, string, location, parse_result):
|
||||
source = parse_result.asDict()['source']
|
||||
target = parse_result.asDict()['target']
|
||||
path = parse_result.asDict()['path']
|
||||
return {'action': MOVE, 'source': source,
|
||||
'target': target, 'path': path,
|
||||
'lineno': lineno(location, string)}
|
||||
|
||||
def _parse_obj_line(self, line):
|
||||
'''Метод для разбора строк типа obj.'''
|
||||
try:
|
||||
parsing_result = self.obj_line.parseString(line)
|
||||
self._match = True
|
||||
if self._template_parser_flag:
|
||||
output_name = (parsing_result.action, parsing_result.name)
|
||||
output_value = (parsing_result.type,
|
||||
parsing_result.md5,
|
||||
parsing_result.time)
|
||||
self._item_to_add = OrderedDict({output_name: [output_value]})
|
||||
else:
|
||||
output_name = parsing_result.name
|
||||
output_value = OrderedDict({'type': parsing_result.type,
|
||||
'md5': parsing_result.md5,
|
||||
'mtime': parsing_result.time})
|
||||
self._item_to_add = OrderedDict({output_name: output_value})
|
||||
|
||||
self._ready_to_update = True
|
||||
except ParseException:
|
||||
return
|
||||
@classmethod
|
||||
def _parse_skip(cls, parse_result):
|
||||
return [None]
|
||||
|
||||
@property
|
||||
def document_text(self):
|
||||
'''Метод для получения текста результирующего документа.
|
||||
Представляет собой переопределение соответствующего метода базового
|
||||
класса, посколько в данном случае нужно учитывать режим работы формата.
|
||||
'''
|
||||
file_loader = PackageLoader('calculate.templates.format',
|
||||
self.TEMPLATES_DIRECTORY)
|
||||
formats_environment = Environment(loader=file_loader,
|
||||
trim_blocks=True,
|
||||
lstrip_blocks=True)
|
||||
formats_environment.globals.update(zip=zip)
|
||||
formats_environment.add_extension('jinja2.ext.do')
|
||||
template = formats_environment.get_template(self.FORMAT)
|
||||
document_text = template.render(
|
||||
document_dictionary=self._document_dictionary,
|
||||
template_parser=self._template_parser_flag
|
||||
)
|
||||
return '{}{}'.format(self.header, document_text)
|
||||
@classmethod
|
||||
def _parse_unexpected(cls, string, location, parse_result):
|
||||
result = parse_result[0]
|
||||
if not result:
|
||||
return [None]
|
||||
return {'error': result, 'lineno': lineno(location, string)}
|
||||
|
@ -0,0 +1,14 @@
|
||||
{% for file_name, contents_values in document_dictionary.items() %}
|
||||
{% if contents_values is mapping %}
|
||||
{% set contents_values = (contents_values.values()|list) %}
|
||||
{% else %}
|
||||
{% set contents_values = (contents_values[0]|list) %}
|
||||
{% set file_name = file_name[-1] %}
|
||||
{% endif %}
|
||||
{% if contents_values[0] == 'sym' %}
|
||||
{{ contents_values[0] }} {{ file_name }} -> {{ contents_values[1:]|join(' ') }}
|
||||
{% else %}
|
||||
{{ contents_values[0] }} {{ file_name }}{% if contents_values[1:] %} {{ contents_values[1:]|join(' ') }}{% endif %}
|
||||
|
||||
{% endif %}
|
||||
{% endfor %}
|
@ -0,0 +1,5 @@
|
||||
from calculate.variables.datavars import Variable, TableType
|
||||
|
||||
Variable('build', type=TableType, source=[])
|
||||
|
||||
Variable('unistall', type=TableType, source=[])
|
@ -0,0 +1,82 @@
|
||||
from calculate.utils.package import PackageAtomParser
|
||||
from pyparsing import Regex, Literal, LineEnd, restOfLine, LineStart
|
||||
|
||||
|
||||
ADD, REMOVE, MOVE = range(0, 3)
|
||||
|
||||
|
||||
def parse_add(parse_result):
|
||||
target = parse_result.asDict()['target']
|
||||
path = parse_result.asDict()['path']
|
||||
return {'action': ADD, 'target': target, 'path': path}
|
||||
|
||||
|
||||
def parse_remove(parse_result):
|
||||
target = parse_result.asDict()['target']
|
||||
path = parse_result.asDict()['path']
|
||||
return {'action': REMOVE, 'target': target, 'path': path}
|
||||
|
||||
|
||||
def parse_move(parse_result):
|
||||
source = parse_result.asDict()['source']
|
||||
target = parse_result.asDict()['target']
|
||||
path = parse_result.asDict()['path']
|
||||
return {'action': MOVE, 'source': source, 'target': target, 'path': path}
|
||||
|
||||
|
||||
def parse_skip(parse_result):
|
||||
print('skip:', parse_result)
|
||||
return [None]
|
||||
|
||||
|
||||
def parse_unexpected(parse_result):
|
||||
print('unexpected')
|
||||
result = parse_result[0]
|
||||
if not result:
|
||||
return [None]
|
||||
else:
|
||||
return {'unexpected': result}
|
||||
|
||||
|
||||
atom = Regex(PackageAtomParser.atom_regex)
|
||||
remove_symbol = Literal('!')
|
||||
path = Regex(r'\S+')
|
||||
|
||||
add_parser = atom('target') + path('path') + LineEnd().suppress()
|
||||
add_parser.setParseAction(parse_add)
|
||||
|
||||
move_parser = (atom('source') + Literal(',').suppress() + atom('target')
|
||||
+ path('path') + LineEnd().suppress())
|
||||
move_parser.setParseAction(parse_move)
|
||||
|
||||
remove_parser = (remove_symbol.suppress() + atom('target') + path('path')
|
||||
+ LineEnd().suppress())
|
||||
remove_parser.setParseAction(parse_remove)
|
||||
|
||||
empty_line = LineStart().suppress() + LineEnd().suppress()
|
||||
empty_line.setParseAction(parse_skip)
|
||||
|
||||
comment = Literal('#') + restOfLine + LineEnd().suppress()
|
||||
comment.setParseAction(parse_skip)
|
||||
|
||||
unexpected = restOfLine + LineEnd().suppress()
|
||||
unexpected.setParseAction(parse_unexpected)
|
||||
|
||||
|
||||
template_parser = (move_parser | remove_parser | add_parser | comment
|
||||
| empty_line | unexpected)
|
||||
|
||||
text = '''dev-lang/python-3.6[abi_x86_64,ssl] /usr/dir/file
|
||||
|
||||
# comment
|
||||
!sys-fs/udisks-1.0.5-r1 /usr/bin/file
|
||||
|
||||
ll
|
||||
dev-lang/python-3.6[abi_x86_64,ssl], sys-fs/udisks-1.0.5-r1 /usr/bin/file
|
||||
|
||||
'''
|
||||
|
||||
for tokens, start, end in template_parser.scanString(text):
|
||||
result = tokens[0]
|
||||
if result is not None:
|
||||
print(result)
|
@ -1,180 +1,235 @@
|
||||
import os
|
||||
import pytest
|
||||
import shutil
|
||||
from collections import OrderedDict
|
||||
from calculate.templates.format.contents_format import ContentsFormat
|
||||
from calculate.utils.package import PackageAtomName, Version, Package
|
||||
|
||||
|
||||
TESTFILES_PATH = os.path.join(os.getcwd(),
|
||||
'tests/templates/format/testfiles/contents')
|
||||
|
||||
|
||||
@pytest.mark.contents
|
||||
class TestParsingMethods:
|
||||
def test_if_input_document_contains_a_few_lines_without_any_action_symbols__the_initialised_object_contains_correct_dictionary(self):
|
||||
document_text = '''
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
class TestExecutingFormat:
|
||||
def test_copy_testfiles(self):
|
||||
entries = os.scandir(TESTFILES_PATH)
|
||||
for root_entry in entries:
|
||||
if root_entry.path.endswith('.backup'):
|
||||
shutil.copytree(root_entry.path,
|
||||
root_entry.path.rstrip('.backup'),
|
||||
symlinks=True)
|
||||
|
||||
def test_if_template_of_the_contents_format_contains_some_files_to_add_directory_to_add_and_link_to_add_and_some_files_is_listed_using_glob_symbols__all_files_links_and_directories_are_added_to_the_target_package(self):
|
||||
chroot_path = os.path.join(TESTFILES_PATH, 'root_0')
|
||||
test_package_name = PackageAtomName(
|
||||
{'pkg_path':
|
||||
os.path.join(
|
||||
chroot_path,
|
||||
'var/db/pkg/test-category/test-package-1.0'),
|
||||
'version': Version('1.0')})
|
||||
|
||||
result = OrderedDict({('', '/usr/bin/vim'):
|
||||
[('obj', '30acc0f256e11c1ecdb1bd80b688d238',
|
||||
'1573538056')],
|
||||
('', '/usr/share/bash-completion/completions/ex'):
|
||||
[('sym', 'vim', '1573538054')],
|
||||
('', '/usr/share/bash-completion/completions'):
|
||||
[('dir',)]})
|
||||
|
||||
contents_object = ContentsFormat(document_text, '/path/to/template')
|
||||
assert result == contents_object._document_dictionary
|
||||
|
||||
def test_if_input_document_contains_a_few_lines_with_some_action_symbols__the_initialised_object_contains_correct_dictionary(self):
|
||||
document_text = '''
|
||||
!obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
-sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
!dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
template_text = '''
|
||||
test-category/test-package /etc/dir_0/*
|
||||
|
||||
result = OrderedDict({('!', '/usr/bin/vim'):
|
||||
[('obj', '30acc0f256e11c1ecdb1bd80b688d238',
|
||||
'1573538056')],
|
||||
('-', '/usr/share/bash-completion/completions/ex'):
|
||||
[('sym', 'vim', '1573538054')],
|
||||
('!', '/usr/share/bash-completion/completions'):
|
||||
[('dir',)]})
|
||||
|
||||
contents_object = ContentsFormat(document_text, '/path/to/template')
|
||||
assert result == contents_object._document_dictionary
|
||||
|
||||
def test_if_template_parser_flag_is_set_False__the_initialized_object_contains_correct_dictionary_for_contents_util_module(self):
|
||||
document_text = '''
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
test-category/test-package /etc/dir_1/file_0
|
||||
|
||||
result = OrderedDict({'/usr/bin/vim':
|
||||
OrderedDict(
|
||||
{'type': 'obj',
|
||||
'md5': '30acc0f256e11c1ecdb1bd80b688d238',
|
||||
'mtime': '1573538056'}),
|
||||
'/usr/share/bash-completion/completions/ex':
|
||||
OrderedDict(
|
||||
{'type': 'sym',
|
||||
'target': 'vim',
|
||||
'mtime': '1573538054'}),
|
||||
'/usr/share/bash-completion/completions':
|
||||
OrderedDict({'type': 'dir'})})
|
||||
|
||||
contents_object = ContentsFormat(document_text, '/path/to/template',
|
||||
template_parser=False)
|
||||
assert result == contents_object._document_dictionary
|
||||
|
||||
def test_joining_documents_1(self):
|
||||
with open('./tests/templates/format/testfiles/contents_original',
|
||||
'r') as original_file:
|
||||
original_text = original_file.read()
|
||||
contents_original_object = ContentsFormat(original_text, '/path/to/template')
|
||||
|
||||
with open('./tests/templates/format/testfiles/contents_template',
|
||||
'r') as template_file:
|
||||
template_text = template_file.read()
|
||||
contents_template_object = ContentsFormat(template_text,
|
||||
'/path/to/template',
|
||||
ignore_comments=True)
|
||||
|
||||
contents_original_object.join_template(contents_template_object)
|
||||
|
||||
with open('./tests/templates/format/testfiles/contents_result',
|
||||
'r') as result_file:
|
||||
result_text = result_file.read()
|
||||
|
||||
assert contents_original_object.document_text == result_text
|
||||
|
||||
def test_if_input_documents_are_an_original_document_without_calculate_header_and_a_template_and_the_add_header_flag_is_set__the_format_object_joins_an_original_document_and_a_template_and_adds_the_calculate_header(self):
|
||||
original_text = '''
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
test-category/test-package /etc/dir_2
|
||||
|
||||
template_text = '''
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
!sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
test-category/test-package /etc/dir_3/link_0
|
||||
'''
|
||||
result_contents = '''dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253
|
||||
obj /etc/dir_0/file_1 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
dir /etc/dir_1
|
||||
obj /etc/dir_1/file_0 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
dir /etc/dir_2
|
||||
dir /etc/dir_3
|
||||
sym /etc/dir_3/link_0 -> ../dir_1/file_0 1601991426
|
||||
'''
|
||||
|
||||
join_result = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# /path/to/template
|
||||
#-------------------------------------------------------------------------------
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
contents_format = ContentsFormat(template_text, '/path/to/template')
|
||||
contents_format.execute_format('target/path', chroot_path=chroot_path)
|
||||
|
||||
original_object = ContentsFormat(original_text, '/path/to/template',
|
||||
add_header=True)
|
||||
template_object = ContentsFormat(template_text, '/path/to/template')
|
||||
original_object.join_template(template_object)
|
||||
assert original_object.document_text == join_result
|
||||
|
||||
def test_if_input_documents_are_an_original_document_with_calculate_header_and_a_template_the_add_header_flag_is_set_and_the_already_changed_flag_is_not_set__the_format_object_removes_calculate_header_from_original_document__joins_an_original_document_and_a_template_and_adds_the_calculate_header(self):
|
||||
original_text = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# /path/to/ancient/template
|
||||
#-------------------------------------------------------------------------------
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
test_package = Package(test_package_name,
|
||||
chroot_path=chroot_path)
|
||||
|
||||
if contents_format._errors:
|
||||
for error in contents_format._errors:
|
||||
print(error)
|
||||
assert False
|
||||
|
||||
assert '/etc/dir_0/file_0' in test_package
|
||||
assert '/etc/dir_0/file_1' in test_package
|
||||
|
||||
assert '/etc/dir_1' in test_package
|
||||
assert '/etc/dir_1/file_0' in test_package
|
||||
|
||||
assert '/etc/dir_2' in test_package
|
||||
|
||||
assert '/etc/dir_3' in test_package
|
||||
assert '/etc/dir_3/link_0' in test_package
|
||||
|
||||
with open(os.path.join(
|
||||
chroot_path,
|
||||
'var/db/pkg/test-category/test-package-1.0/CONTENTS'),
|
||||
'r') as test_contents:
|
||||
assert test_contents.read() == result_contents
|
||||
|
||||
def test_if_template_of_the_contents_format_contains_some_files_to_remove_directory_to_remove_and_link_to_remove_and_some_files_is_listed_using_glob_symbols__all_files_links_and_directories_are_removed_from_the_target_package(self):
|
||||
chroot_path = os.path.join(TESTFILES_PATH, 'root_1')
|
||||
test_package_name = PackageAtomName(
|
||||
{'pkg_path':
|
||||
os.path.join(
|
||||
chroot_path,
|
||||
'var/db/pkg/test-category/test-package-1.0'),
|
||||
'version': Version('1.0')})
|
||||
template_text = '''
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
!sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
!test-category/test-package /etc/dir_0/file_1
|
||||
|
||||
join_result = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# /path/to/template
|
||||
#-------------------------------------------------------------------------------
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
!test-category/test-package /etc/dir_1/*
|
||||
|
||||
!test-category/test-package /etc/dir_2/
|
||||
|
||||
original_object = ContentsFormat(original_text, '/path/to/template',
|
||||
add_header=True)
|
||||
template_object = ContentsFormat(template_text, '/path/to/template')
|
||||
original_object.join_template(template_object)
|
||||
assert original_object.document_text == join_result
|
||||
|
||||
def test_if_input_documents_are_an_original_document_with_calculate_header_and_a_template_the_add_header_flag_is_set_and_the_already_changed_flag_is_set__the_format_object_joins_an_original_document_and_a_template_and_adds_the_calculate_header_with_a_template_paths_from_the_old_header_and_paths_to_a_current_template(self):
|
||||
original_text = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# /path/to/ancient/template
|
||||
#-------------------------------------------------------------------------------
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
!test-category/test-package /etc/dir_3/link_0
|
||||
'''
|
||||
result_contents = '''dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253
|
||||
dir /etc/dir_1
|
||||
dir /etc/dir_3
|
||||
'''
|
||||
|
||||
contents_format = ContentsFormat(template_text, '/path/to/template')
|
||||
contents_format.execute_format('target/path', chroot_path=chroot_path)
|
||||
|
||||
test_package = Package(test_package_name,
|
||||
chroot_path=chroot_path)
|
||||
|
||||
if contents_format._errors:
|
||||
for error in contents_format._errors:
|
||||
print(error)
|
||||
assert False
|
||||
|
||||
assert '/etc/dir_0/file_0' in test_package
|
||||
assert '/etc/dir_0/file_1' not in test_package
|
||||
|
||||
assert '/etc/dir_1' in test_package
|
||||
assert '/etc/dir_1/file_0' not in test_package
|
||||
assert '/etc/dir_1/file_1' not in test_package
|
||||
|
||||
assert '/etc/dir_3' in test_package
|
||||
assert '/etc/dir_3/link_0' not in test_package
|
||||
|
||||
with open(os.path.join(
|
||||
chroot_path,
|
||||
'var/db/pkg/test-category/test-package-1.0/CONTENTS'),
|
||||
'r') as test_contents:
|
||||
assert test_contents.read() == result_contents
|
||||
|
||||
def test_if_template_of_the_contents_format_contains_some_files_to_move_from_a_source_package_to_a_target_package_directory_to_move_and_link_to_move_and_some_files_is_listed_using_glob_symbols__all_files_links_and_directories_are_moved_from_the_source_package_to_the_target_package(self):
|
||||
chroot_path = os.path.join(TESTFILES_PATH, 'root_2')
|
||||
test_package_name = PackageAtomName(
|
||||
{'pkg_path':
|
||||
os.path.join(
|
||||
chroot_path,
|
||||
'var/db/pkg/test-category/test-package-1.0'),
|
||||
'version': Version('1.0')})
|
||||
other_package_name = PackageAtomName(
|
||||
{'pkg_path':
|
||||
os.path.join(
|
||||
chroot_path,
|
||||
'var/db/pkg/test-category/other-package-1.1'),
|
||||
'version': Version('1.1')})
|
||||
other_package = Package(other_package_name,
|
||||
chroot_path=chroot_path)
|
||||
template_text = '''
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
!sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
dir /usr/share/bash-completion/completions
|
||||
'''
|
||||
test-category/test-package, test-category/other-package /etc/dir_0/file_0
|
||||
test-category/other-package, test-category/test-package /etc/dir_0/file_1
|
||||
|
||||
test-category/test-package, test-category/other-package /etc/dir_1/file_[1,3]
|
||||
test-category/other-package, test-category/test-package /etc/dir_1/file_[0,2]
|
||||
|
||||
join_result = '''#-------------------------------------------------------------------------------
|
||||
# Modified by Calculate Utilities 4.0
|
||||
# Processed template files:
|
||||
# /path/to/ancient/template
|
||||
# /path/to/template
|
||||
#-------------------------------------------------------------------------------
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
dir /usr/share/bash-completion/completions
|
||||
test-category/test-package, test-category/other-package /etc/dir_3/
|
||||
test-category/other-package, test-category/test-package /etc/dir_4/
|
||||
|
||||
test-category/test-package, test-category/other-package /etc/dir_5/link_0
|
||||
test-category/other-package, test-category/test-package /etc/dir_5/link_1
|
||||
'''
|
||||
test_result = '''dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_1 c585be6f171462940b44af994a54040d 1593525253
|
||||
dir /etc/dir_1
|
||||
obj /etc/dir_1/file_0 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_2 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_4 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
dir /etc/dir_4
|
||||
obj /etc/dir_4/file_0 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
dir /etc/dir_5
|
||||
sym /etc/dir_5/link_1 -> ../dir_1/file_0 1601991426
|
||||
'''
|
||||
other_result = '''dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253
|
||||
dir /etc/dir_1
|
||||
obj /etc/dir_1/file_1 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_3 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_5 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
dir /etc/dir_3
|
||||
obj /etc/dir_3/file_0 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
dir /etc/dir_5
|
||||
sym /etc/dir_5/link_0 -> ../dir_1/file_0 1601991426
|
||||
'''
|
||||
|
||||
original_object = ContentsFormat(original_text, '/path/to/template',
|
||||
add_header=True, already_changed=True)
|
||||
template_object = ContentsFormat(template_text, '/path/to/template')
|
||||
original_object.join_template(template_object)
|
||||
assert original_object.document_text == join_result
|
||||
contents_format = ContentsFormat(template_text, '/path/to/template')
|
||||
contents_format.execute_format('target/path', chroot_path=chroot_path)
|
||||
|
||||
test_package = Package(test_package_name,
|
||||
chroot_path=chroot_path)
|
||||
other_package = Package(other_package_name,
|
||||
chroot_path=chroot_path)
|
||||
|
||||
assert '/etc/dir_0/file_0' not in test_package
|
||||
assert '/etc/dir_0/file_1' not in other_package
|
||||
assert '/etc/dir_0/file_1' in test_package
|
||||
assert '/etc/dir_0/file_0' in other_package
|
||||
|
||||
assert '/etc/dir_1/file_1' not in test_package
|
||||
assert '/etc/dir_1/file_3' not in test_package
|
||||
assert '/etc/dir_1/file_0' not in other_package
|
||||
assert '/etc/dir_1/file_2' not in other_package
|
||||
|
||||
assert '/etc/dir_1/file_0' in test_package
|
||||
assert '/etc/dir_1/file_2' in test_package
|
||||
assert '/etc/dir_1/file_4' in test_package
|
||||
assert '/etc/dir_1/file_1' in other_package
|
||||
assert '/etc/dir_1/file_3' in other_package
|
||||
assert '/etc/dir_1/file_5' in other_package
|
||||
|
||||
assert '/etc/dir_3' not in test_package
|
||||
assert '/etc/dir_3/file_0' not in test_package
|
||||
assert '/etc/dir_4' not in other_package
|
||||
assert '/etc/dir_4/file_0' not in other_package
|
||||
assert '/etc/dir_4' in test_package
|
||||
assert '/etc/dir_4/file_0' in test_package
|
||||
assert '/etc/dir_3' in other_package
|
||||
assert '/etc/dir_3/file_0' in other_package
|
||||
|
||||
assert '/etc/dir_5/link_0' not in test_package
|
||||
assert '/etc/dir_5/link_1' not in other_package
|
||||
assert '/etc/dir_5/link_1' in test_package
|
||||
assert '/etc/dir_5/link_0' in other_package
|
||||
|
||||
with open(test_package_name.contents_path, 'r') as test_contents:
|
||||
assert test_contents.read() == test_result
|
||||
|
||||
with open(other_package_name.contents_path,
|
||||
'r') as other_contents:
|
||||
assert other_contents.read() == other_result
|
||||
|
||||
def test_remove_testfiles(self):
|
||||
entries = os.scandir('./tests/templates/format/testfiles/contents')
|
||||
for root_entry in entries:
|
||||
if root_entry.path.endswith('.backup'):
|
||||
shutil.rmtree(root_entry.path.rstrip('.backup'))
|
||||
|
@ -0,0 +1 @@
|
||||
A very important information can be found in this valuable file.
|
@ -0,0 +1 @@
|
||||
A very important information can be found in this valuable file.
|
@ -0,0 +1 @@
|
||||
A very important information can be found in this valuable file.
|
@ -0,0 +1 @@
|
||||
A very important information can be found in this valuable file.
|
@ -0,0 +1 @@
|
||||
../dir_1/file_0
|
@ -0,0 +1,3 @@
|
||||
dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253
|
@ -0,0 +1,12 @@
|
||||
dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253
|
||||
obj /etc/dir_0/file_1 c585be6f171462940b44af994a54040d 1593525253
|
||||
dir /etc/dir_1
|
||||
obj /etc/dir_1/file_0 c585be6f171462940b44af994a54040d 1593525253
|
||||
obj /etc/dir_1/file_1 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
dir /etc/dir_2
|
||||
obj /etc/dir_2/file_0 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
obj /etc/dir_2/file_1 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
dir /etc/dir_3
|
||||
sym /etc/dir_3/link_0 -> ../dir_1/file_0 1601991426
|
@ -0,0 +1,11 @@
|
||||
dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_1 c585be6f171462940b44af994a54040d 1593525253
|
||||
dir /etc/dir_1
|
||||
obj /etc/dir_1/file_0 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_2 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_5 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
dir /etc/dir_4
|
||||
obj /etc/dir_4/file_0 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
dir /etc/dir_5
|
||||
sym /etc/dir_5/link_1 -> ../dir_1/file_0 1601991426
|
@ -0,0 +1 @@
|
||||
2
|
@ -0,0 +1,11 @@
|
||||
dir /etc
|
||||
dir /etc/dir_0
|
||||
obj /etc/dir_0/file_0 c585be6f171462940b44af994a54040d 1593525253
|
||||
dir /etc/dir_1
|
||||
obj /etc/dir_1/file_1 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_3 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
obj /etc/dir_1/file_4 b4ff15ea7028891a83392b490d676622 1601987083
|
||||
dir /etc/dir_3
|
||||
obj /etc/dir_3/file_0 b4ff15ea7028891a83392b490d676622 1601982062
|
||||
dir /etc/dir_5
|
||||
sym /etc/dir_5/link_0 -> ../dir_1/file_0 1601991426
|
@ -1,18 +0,0 @@
|
||||
dir /usr
|
||||
dir /usr/bin
|
||||
sym /usr/bin/rview -> vim 1573538053
|
||||
sym /usr/bin/rvim -> vim 1573538053
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/bin/vimdiff -> vim 1573538053
|
||||
dir /usr/share
|
||||
dir /urs/share/dir
|
||||
obj /urs/share/dir/file 4580e5268ddf10175cdd86f87f481d71 1560504013
|
||||
dir /usr/share/applications
|
||||
dir /usr/share/bash-completion
|
||||
dir /usr/share/bash-completion/completions
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
sym /usr/share/bash-completion/completions/rview -> vim 1573538054
|
||||
sym /usr/share/bash-completion/completions/rvim -> vim 1573538054
|
||||
sym /usr/share/bash-completion/completions/vi -> vim 1573538054
|
||||
obj /usr/share/bash-completion/completions/vim 49aa29933e92bb54d84eb6efc5cdd801 1573538054
|
||||
sym /usr/share/bash-completion/completions/vimdiff -> vim 1573538054
|
@ -1,22 +0,0 @@
|
||||
dir /usr
|
||||
dir /usr/bin
|
||||
sym /usr/bin/rview -> vim 1573538053
|
||||
sym /usr/bin/rvim -> vim 1573538053
|
||||
obj /usr/bin/vim 30acc0f256e11c1ecdb1bd80b688d238 1573538056
|
||||
sym /usr/bin/vimdiff -> vim 1573538053
|
||||
dir /usr/share
|
||||
dir /usr/share/applications
|
||||
dir /usr/share/bash-completion
|
||||
dir /usr/share/bash-completion/completions
|
||||
sym /usr/share/bash-completion/completions/ex -> vim 1573538054
|
||||
sym /usr/share/bash-completion/completions/rview -> vim 1573538054
|
||||
sym /usr/share/bash-completion/completions/rvim -> vim 1573538054
|
||||
sym /usr/share/bash-completion/completions/vi -> vi 1573538054
|
||||
obj /usr/share/bash-completion/completions/vim 49aa29933e92bb54d84eb6efc5cdd801 1573538054
|
||||
sym /usr/share/bash-completion/completions/vimdiff -> vim 1573538054
|
||||
dir /etc
|
||||
dir /etc/env.d
|
||||
obj /etc/env.d/99editor 835da024d5f3c7a862934df592cdc9fe 1574417039
|
||||
sym /usr/share/bash-completion/completions/view -> vim 1573538054
|
||||
dir /etc/vim
|
||||
obj /etc/vim/vimrc.local ed7cd3cef611ff49b12093b93fed59be 1574417039
|
@ -1,9 +0,0 @@
|
||||
dir /etc
|
||||
dir /etc/env.d
|
||||
obj /etc/env.d/99editor 835da024d5f3c7a862934df592cdc9fe 1574417039
|
||||
sym /usr/share/bash-completion/completions/vi -> vi 1573538054
|
||||
sym /usr/share/bash-completion/completions/view -> vim 1573538054
|
||||
!dir /urs/share/dir
|
||||
!obj /urs/share/dir/file 4580e5268ddf10175cdd86f87f481d71 1560504013
|
||||
dir /etc/vim
|
||||
obj /etc/vim/vimrc.local ed7cd3cef611ff49b12093b93fed59be 1574417039
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue