Browse Source

Using of the import_variables function in the variables files is

available now. fixed #49
master
Иванов Денис 5 months ago
parent
commit
dbf841c689
10 changed files with 274 additions and 223 deletions
  1. +2
    -2
      calculate/templates/template_processor.py
  2. +10
    -5
      calculate/variables/loader.py
  3. +46
    -27
      calculate/vars/main/cl/__init__.py
  4. +65
    -64
      calculate/vars/os/gentoo/__init__.py
  5. +53
    -47
      tests/variables/testfiles/variables_1/level/__init__.py
  6. +5
    -5
      tests/variables/testfiles/variables_1/level/level_2/__init__.py
  7. +3
    -3
      tests/variables/testfiles/variables_10/main/__init__.py
  8. +15
    -17
      tests/variables/testfiles/variables_10/main/cl/system/__init__.py
  9. +44
    -34
      tests/variables/testfiles/variables_10/os/__init__.py
  10. +31
    -19
      tests/vars/test_vars.py

+ 2
- 2
calculate/templates/template_processor.py View File

@@ -2764,8 +2764,8 @@ class DirectoryProcessor:
return False
elif parameters.action.startswith('!'):
action_matching = (parameters.action[1:].strip() not in
self.action)
elif parameters.action not in self.action:
self.action)
elif parameters.action not in self.action:
action_matching = False
else:
action_matching = True


+ 10
- 5
calculate/variables/loader.py View File

@@ -245,7 +245,8 @@ class NamespaceIniFiller:
self.current_namespace = None
return

self.current_namespace = self.current_namespace._namespaces[section]
self.current_namespace = self.current_namespace.\
_namespaces[section]

def clear_section(self, sections: list, lineno) -> None:
'''Метод для очистки пространства имен.'''
@@ -310,7 +311,8 @@ class NamespaceIniFiller:
table_name, section))
self.current_namespace = None
return
self.current_namespace = self.current_namespace._namespaces[section]
self.current_namespace = self.current_namespace.\
_namespaces[section]

if table_name not in self.current_namespace._variables:
if not self.modify_only:
@@ -605,10 +607,12 @@ class VariableLoader:
# importlib.import_module('{}.{}'.format(package, file_name))

spec = importlib.util.spec_from_file_location(
'{}.{}'.format(package, file_name),
file_node.path)
'{}.{}'.format(package, file_name),
file_node.path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
if hasattr(module, 'import_variables'):
module.import_variables()

# Обходим остальные директории.
for directory_node in directory_nodes:
@@ -752,7 +756,8 @@ class Datavars:
# ini-файлах.
try:
self.variables_to_save = {target: dict() for target in
self.main.cl.system.env_order if target in
self.main.cl.system.env_order
if target in
self.main.cl.system.env_path}
except VariableNotFoundError:
self.variables_to_save = dict()


+ 46
- 27
calculate/vars/main/cl/__init__.py View File

@@ -1,11 +1,19 @@
import os
from calculate.utils.fs import readFile
from calculate.variables.datavars import Variable, Namespace, Dependence, \
StringType, BooleanType, HashType, Calculate
from calculate.variables.datavars import (
Variable,
Namespace,
StringType,
BooleanType,
HashType,
Calculate
)


def get_ebuild_phase():
return os.environ.get("EBUILD_PHASE", "")


def get_chroot_status():
"""Detect chroot mode by different mountinfo"""
pid = os.getpid()
@@ -19,25 +27,16 @@ def get_chroot_status():
except Exception:
return False


def is_system_boot():
if os.readlink('/proc/self/fd/0') == '/dev/console':
return True
else:
return False

Variable("ebuild_phase", type=StringType,
source=Calculate(get_ebuild_phase))
Variable("chroot_status", type=BooleanType,
source=Calculate(get_chroot_status))
Variable("system_boot_set", type=BooleanType,
source=Calculate(is_system_boot))

# Переменная, в которую будут помещаться пути к текущим шаблонам.
Variable("current_template", type=StringType, source="")

class CmdlineParams(object):
"""
Параметры опции загрузки ядра calculate=
"""Параметры опции загрузки ядра calculate=
"""
# названия параметров
Calculate = "calculate"
@@ -56,6 +55,7 @@ class CmdlineParams(object):
Audio = "audio"
Clock = "clock"


def get_cmdline_parameter(paramname):
cmdLine = "/proc/cmdline"
for param in readFile(cmdLine).split(" "):
@@ -64,6 +64,7 @@ def get_cmdline_parameter(paramname):
return value
return None


def get_calculate_cmdline():
names = (
CmdlineParams.Locale,
@@ -77,7 +78,7 @@ def get_calculate_cmdline():
CmdlineParams.Audio,
CmdlineParams.Clock)
# try get timezone from kernel calculate param
params = {x:"" for x in names}
params = {x: "" for x in names}
try:
value = get_cmdline_parameter(CmdlineParams.Calculate)
if value is not None:
@@ -89,27 +90,45 @@ def get_calculate_cmdline():
pass
return params


def get_isoscan_filename():
value = get_cmdline_parameter(CmdlineParams.IsoscanFile)
if value is not None:
return value
return ""


def get_isoscan_fullpath(base_path, filename):
if filename:
return os.path.join(base_path.value, filename.value.lstrip("/"))
return ""

with Namespace('cmdline'):
Variable("calculate", type=HashType.fixed,
source=Calculate(get_calculate_cmdline))
Variable("isoscan_filename", type=StringType,
source=Calculate(get_isoscan_filename))

with Namespace('isoscan'):
Variable("base_path", type=StringType,
source="/run/initramfs/isoscan")
Variable("full_path", type=StringType,
source=Calculate(get_isoscan_fullpath,
'.base_path', 'main.cl.cmdline.isoscan_filename'
))

def import_variables():
Variable("ebuild_phase", type=StringType,
source=Calculate(get_ebuild_phase))

Variable("chroot_status", type=BooleanType,
source=Calculate(get_chroot_status))

Variable("system_boot_set", type=BooleanType,
source=Calculate(is_system_boot))

# Переменная, в которую будут помещаться пути к текущим шаблонам.
Variable("current_template", type=StringType, source="")

with Namespace('cmdline'):
Variable("calculate", type=HashType.fixed,
source=Calculate(get_calculate_cmdline))

Variable("isoscan_filename", type=StringType,
source=Calculate(get_isoscan_filename))

with Namespace('isoscan'):
Variable("base_path", type=StringType,
source="/run/initramfs/isoscan")

Variable("full_path", type=StringType,
source=Calculate(get_isoscan_fullpath,
'.base_path',
'main.cl.cmdline.isoscan_filename'))

+ 65
- 64
calculate/vars/os/gentoo/__init__.py View File

@@ -3,78 +3,79 @@ from calculate.utils.files import stderr_devnull
from calculate.utils.files import read_link, FilesError
from calculate.variables.datavars import Variable, Namespace, Dependence,\
StringType, TableType
'''
gentoo:
make_profile -> string
profile:
path -> string
name -> string
repositories[*]{name, path} -> table
config -> undefined
'''

# Путь до файла, указывающего на активный профиль
Variable('make_profile', type=StringType, source='/etc/portage/make.profile')

# Параметры текущего профиля.
with Namespace('profile'):
def get_profile_link(make_profile):
make_profile_dir = os.path.dirname(make_profile.value)
try:
profile_link = read_link(make_profile.value)
except FilesError:
return ""
def import_variables():
'''
gentoo:
make_profile -> string
profile:
path -> string
name -> string
repositories[*]{name, path} -> table
config -> undefined
'''

if profile_link:
profile_link = os.path.normpath(
os.path.join(make_profile_dir, profile_link))
return profile_link
else:
return ""
# Абсолютный путь до профиля
Variable('path', type=StringType,
source=Dependence('..make_profile', depend=get_profile_link))
# Путь до файла, указывающего на активный профиль
Variable('make_profile', type=StringType,
source='/etc/portage/make.profile')

def get_profile_name(path, repositories):
profile_path = path.value
if not profile_path:
return ""
# Параметры текущего профиля.
with Namespace('profile'):
def get_profile_link(make_profile):
make_profile_dir = os.path.dirname(make_profile.value)
try:
profile_link = read_link(make_profile.value)
except FilesError:
return ""

for repository in repositories.value:
repository_path = repository['path']
repository_name = repository['name']
remove_part = os.path.normpath(os.path.join(repository_path,
"profiles"))
if profile_path.startswith(remove_part):
return "{}:{}".format(repository_name,
profile_path[len(remove_part) + 1:])
return profile_path
# Название профиля
Variable('name', type=StringType,
source=Dependence('.path', '..repositories',
depend=get_profile_name))
if profile_link:
profile_link = os.path.normpath(os.path.join(make_profile_dir,
profile_link))
return profile_link
else:
return ""
# Абсолютный путь до профиля
Variable('path', type=StringType,
source=Dependence('..make_profile', depend=get_profile_link))

def get_profile_name(path, repositories):
profile_path = path.value
if not profile_path:
return ""

def get_repository_table(config):
return [{'name': name,
'path': path}
for path, name in config.value.repositories.location_map.items()]
for repository in repositories.value:
repository_path = repository['path']
repository_name = repository['name']
remove_part = os.path.normpath(os.path.join(repository_path,
"profiles"))
if profile_path.startswith(remove_part):
return "{}:{}".format(repository_name,
profile_path[len(remove_part) + 1:])
return profile_path
# Название профиля
Variable('name', type=StringType,
source=Dependence('.path', '..repositories',
depend=get_profile_name))

def get_repository_table(config):
return [{'name': name,
'path': path}
for path, name
in config.value.repositories.location_map.items()]

# Информация о репозиториях
# name: имя репозитория
# path: полный путь до репозитория
Variable('repositories', type=TableType,
source=Dependence('.config', depend=get_repository_table))
# Информация о репозиториях
# name: имя репозитория
# path: полный путь до репозитория
Variable('repositories', type=TableType,
source=Dependence('.config', depend=get_repository_table))

def get_config_object(chroot_path):
from portage.package.ebuild.config import config
if chroot_path.value == '/':
with stderr_devnull():
return config()

def get_config_object(chroot_path):
from portage.package.ebuild.config import config
if chroot_path.value == '/':
with stderr_devnull():
return config()


# Объект текущей конфигурации Portage
Variable('config', source=Dependence('main.cl_chroot_path',
depend=get_config_object))
# Объект текущей конфигурации Portage
Variable('config', source=Dependence('main.cl_chroot_path',
depend=get_config_object))

+ 53
- 47
tests/variables/testfiles/variables_1/level/__init__.py View File

@@ -1,62 +1,68 @@
from calculate.variables.datavars import Namespace, Variable, Dependence,\
StringType, HashType, TableType,\
ListType, FloatType


Variable('simple', type=StringType, source='simple value')

Variable('use_local_simple', type=StringType,
source=Dependence('.simple',
depend=lambda simple: 'Using {}'.format(
def import_variables():
from calculate.variables.datavars import (
Namespace,
Variable,
Dependence,
StringType,
HashType,
TableType,
ListType,
FloatType
)

Variable('simple', type=StringType, source='simple value')

Variable('use_local_simple', type=StringType,
source=Dependence('.simple',
depend=lambda simple: 'Using {}'.format(
simple.value)))

Variable('use_full_simple', type=StringType,
source=Dependence('level.simple',
depend=lambda simple: 'Using {}'.format(
Variable('use_full_simple', type=StringType,
source=Dependence('level.simple',
depend=lambda simple: 'Using {}'.format(
simple.value)))

Variable('disks', type=ListType,
source=["/dev/sda1", "/dev/sda2", "/dev/sda3"])
Variable('disks', type=ListType,
source=["/dev/sda1", "/dev/sda2", "/dev/sda3"])

Variable('version', type=FloatType, source='1.0')
Variable('version', type=FloatType, source='1.0')

Variable('my_shortname', type=StringType, source='CLD')
Variable('my_shortname', type=StringType, source='CLD')

Variable('linux', type=HashType,
source=Dependence('.version', '.my_shortname',
depend=lambda version, my_shortname:
{'version': version.value,
'shortname': my_shortname.value}))
Variable('linux', type=HashType,
source=Dependence('.version', '.my_shortname',
depend=lambda version, my_shortname:
{'version': version.value,
'shortname': my_shortname.value}))

Variable('shortname_test', type=StringType,
source=Dependence('.linux.shortname',
depend=lambda shortname: '{} test'.format(
Variable('shortname_test', type=StringType,
source=Dependence('.linux.shortname',
depend=lambda shortname: '{} test'.format(
shortname.value)))

Variable('device_list', type=ListType,
source=["/dev/sda", "/dev/sdb"])


def get_device_table(device_list):
map_data = {'/dev/sda': ["hdd", "Samsung SSD"],
'/dev/sdb': ["flash", "Transcend 64GB"],
'/dev/sdc': ["usbhdd", "WD 1TB"]}
default_value = ["hdd", "Unknown"]
print('device_list = {}'.format(device_list.value))
return [{"dev": device,
"type": map_data.get(device, default_value)[0],
"name": map_data.get(device, default_value)[1]}
for device in device_list.value]
Variable('device_list', type=ListType,
source=["/dev/sda", "/dev/sdb"])

def get_device_table(device_list):
map_data = {'/dev/sda': ["hdd", "Samsung SSD"],
'/dev/sdb': ["flash", "Transcend 64GB"],
'/dev/sdc': ["usbhdd", "WD 1TB"]}
default_value = ["hdd", "Unknown"]
print('device_list = {}'.format(device_list.value))
return [{"dev": device,
"type": map_data.get(device, default_value)[0],
"name": map_data.get(device, default_value)[1]}
for device in device_list.value]

Variable('device', type=TableType, source=Dependence('.device_list',
depend=get_device_table))
Variable('device', type=TableType, source=Dependence(
'.device_list',
depend=get_device_table))

Variable('device_child', type=StringType,
source=Dependence('.device',
depend=lambda device: device.value[0]['type']))
Variable('device_child', type=StringType,
source=Dependence('.device',
depend=lambda device: device.value[0]['type']))

with Namespace('level_3'):
Variable('my_var_1', type=StringType, source='testing')
with Namespace('level_3'):
Variable('my_var_1', type=StringType, source='testing')

Variable('my_var_2', type=StringType, source='testing_2')
Variable('my_var_2', type=StringType, source='testing_2')

+ 5
- 5
tests/variables/testfiles/variables_1/level/level_2/__init__.py View File

@@ -1,6 +1,6 @@
from calculate.variables.datavars import Variable, StringType, Calculate
def import_variables():
from calculate.variables.datavars import Variable, StringType, Calculate


Variable('vargetter', type=StringType,
source=Calculate(lambda chroot: '{} test'.format(chroot.value),
'main.chroot'))
Variable('vargetter', type=StringType,
source=Calculate(lambda chroot: '{} test'.format(chroot.value),
'main.chroot'))

+ 3
- 3
tests/variables/testfiles/variables_10/main/__init__.py View File

@@ -1,4 +1,4 @@
from calculate.variables.datavars import Variable, StringType
def import_variables():
from calculate.variables.datavars import Variable, StringType


Variable('chroot', type=StringType.readonly, source='/')
Variable('chroot', type=StringType.readonly, source='/')

+ 15
- 17
tests/variables/testfiles/variables_10/main/cl/system/__init__.py View File

@@ -1,19 +1,17 @@
import os
from calculate.variables.datavars import Variable, ListType, HashType
'''
system:
env_order -> list
env_path -> hash
'''
def import_variables():
'''
system:
env_order -> list
env_path -> hash
'''
import os
from calculate.variables.datavars import Variable, ListType, HashType

TESTFILES_PATH = os.path.join(os.getcwd(), 'tests/variables/testfiles')
# Список мест, где есть calculate.ini файлы.
Variable('env_order', type=ListType, source=['system', 'local'])

TESTFILES_PATH = os.path.join(os.getcwd(), 'tests/variables/testfiles')


# Список мест, где есть calculate.ini файлы.
Variable('env_order', type=ListType, source=['system', 'local'])

# Отображение множества мест, где есть calculate.ini файлы, на пути к ним.
Variable('env_path', type=HashType,
source={'system': os.path.join(TESTFILES_PATH,
'ini_vars/calculate_6.ini')})
# Отображение множества мест, где есть calculate.ini файлы, на пути к ним.
Variable('env_path', type=HashType,
source={'system': os.path.join(TESTFILES_PATH,
'ini_vars/calculate_6.ini')})

+ 44
- 34
tests/variables/testfiles/variables_10/os/__init__.py View File

@@ -1,50 +1,60 @@
from calculate.variables.datavars import Namespace, Variable, Dependence,\
StringType, HashType, TableType,\
ListType, IntegerType, FloatType
def import_variables():
from calculate.variables.datavars import (
Namespace,
Variable,
Dependence,
StringType,
HashType,
TableType,
IntegerType,
FloatType
)

with Namespace('linux'):
Variable('shortname', source='', type=StringType)
with Namespace('linux'):
Variable('shortname', source='', type=StringType)

Variable('ver', source='', type=StringType)
Variable('ver', source='', type=StringType)

Variable('fullname', source='', type=StringType)
Variable('fullname', source='', type=StringType)

Variable('subname', source='', type=StringType)
Variable('subname', source='', type=StringType)

Variable('arch', source='', type=StringType)
Variable('arch', source='', type=StringType)

Variable('test_1', source=12, type=IntegerType)
Variable('test_1', source=12, type=IntegerType)

Variable('test_2', source=1.2, type=FloatType)
Variable('test_2', source=1.2, type=FloatType)

def get_title(subname, fullname, ver):
if subname.value:
return '{} {} {}'.format(fullname.value, subname.value, ver.value)
else:
return '{} {}'.format(fullname.value, ver.value)
Variable('title', type=StringType,
source=Dependence('.subname', '.fullname', '.ver',
depend=get_title))
def get_title(subname, fullname, ver):
if subname.value:
return '{} {} {}'.format(fullname.value,
subname.value,
ver.value)
else:
return '{} {}'.format(fullname.value, ver.value)
Variable('title', type=StringType,
source=Dependence('.subname', '.fullname', '.ver',
depend=get_title))

Variable('hashvar', source={'value1': 'test1',
'value2': 'test2'}, type=HashType)
Variable('hashvar', source={'value1': 'test1',
'value2': 'test2'}, type=HashType)

Variable('hashvar_0', source={'value1': 'test1',
'value2': 'test2'}, type=HashType)
Variable('hashvar_0', source={'value1': 'test1',
'value2': 'test2'}, type=HashType)

Variable('hashvar_1', source={'key1': 'value1',
'key2': 'value2'}, type=HashType)
Variable('hashvar_1', source={'key1': 'value1',
'key2': 'value2'}, type=HashType)

Variable('hashvar_2', source={'id_1': 1349,
'id_2': 1575}, type=HashType)
Variable('hashvar_2', source={'id_1': 1349,
'id_2': 1575}, type=HashType)

Variable('calculate', type=StringType,
source=Dependence('.hashvar_0',
depend=lambda hashvar: "{} {}".format(
Variable('calculate', type=StringType,
source=Dependence('.hashvar_0',
depend=lambda hashvar: "{} {}".format(
hashvar.value['value1'],
hashvar.value['value2'])))

Variable('tablevar', type=TableType, source=[{"dev": "/dev/sdb1",
"mount": "/"},
{"dev": "/dev/sdb2",
"mount": "/var/calculate"}])
Variable('tablevar', type=TableType, source=[{"dev": "/dev/sdb1",
"mount": "/"},
{"dev": "/dev/sdb2",
"mount": "/var/calculate"}])

+ 31
- 19
tests/vars/test_vars.py View File

@@ -7,11 +7,14 @@ import calculate.vars.install.os.func as install_os
import mock
from itertools import chain


class Var:
def __init__(self, value):
self.value = value

@pytest.mark.parametrize('case',

@pytest.mark.parametrize(
'case',
[
{
"name": "failed xdpyinfo",
@@ -21,7 +24,7 @@ class Var:
}, {
"name": "worked xdpyinfo",
"failed": False,
"data": open('tests/vars/xdpyinfo.output','r').read(),
"data": open('tests/vars/xdpyinfo.output', 'r').read(),
"result": "1920x1080"
},
{
@@ -31,15 +34,17 @@ class Var:
"result": ""
},
],
ids=lambda x:x["name"])
ids=lambda x: x["name"])
@pytest.mark.calculate_vars
def test_main_os_x11_resolution(case):
with mock.patch('calculate.vars.main.os.x11.Process.failed') as failed:
with mock.patch('calculate.vars.main.os.x11.Process.read_lines') as read_lines:
with mock.patch('calculate.vars.main.os.x11.Process.read_lines'
) as read_lines:
failed.return_value = case["failed"]
read_lines.return_value = case["data"].split("\n")
assert main_os_x11.get_resolution_by_xdpyinfo() == case["result"]


empty_calculate_cmdhash = {
CmdlineParams.Locale: "",
CmdlineParams.Keymap: "",
@@ -53,10 +58,13 @@ empty_calculate_cmdhash = {
CmdlineParams.Clock: ""
}


def concat_dict(d1, **d2):
return {x:y for x,y in chain(d1.items(),d2.items())}
return {x: y for x, y in chain(d1.items(), d2.items())}

@pytest.mark.parametrize('case',

@pytest.mark.parametrize(
'case',
[
{
"name": "empty cmdline",
@@ -89,7 +97,7 @@ def concat_dict(d1, **d2):
"result": concat_dict(empty_calculate_cmdhash, video="nvidia", resolution="1920x1080")
},
],
ids=lambda x:x["name"])
ids=lambda x: x["name"])
@pytest.mark.calculate_vars
def test_main_cl_cmdline(case):
with mock.patch('calculate.vars.main.cl.readFile') as readFile:
@@ -97,7 +105,8 @@ def test_main_cl_cmdline(case):
assert main_cl.get_calculate_cmdline() == case["result"]


@pytest.mark.parametrize('case',
@pytest.mark.parametrize(
'case',
[
{
"source": "i686",
@@ -112,18 +121,19 @@ def test_main_cl_cmdline(case):
"result": "amd64"
},
],
ids=lambda x:"{}->{}".format(x["source"],x["result"]))
ids=lambda x: "{}->{}".format(x["source"], x["result"]))
@pytest.mark.calculate_vars
def test_main_os_get_arch_gentoo(case):
assert main_os.get_arch_gentoo(Var(case["source"])) == case["result"]


@pytest.mark.parametrize('case',
@pytest.mark.parametrize(
'case',
[
{
"name": "pulseaudio exists",
"exists": True,
"result": ["alsa","pulseaudio"]
"result": ["alsa", "pulseaudio"]
},
{
"name": "pulseaudio not exists",
@@ -131,18 +141,20 @@ def test_main_os_get_arch_gentoo(case):
"result": ["alsa"]
},
],
ids=lambda x:x["name"])
ids=lambda x: x["name"])
@pytest.mark.calculate_vars
def test_install_os_get_available_audio_system(case):
with mock.patch('calculate.vars.install.os.func.PackageAtomParser.is_package_exists') as exists:
exists.return_value = case["exists"]
assert install_os.get_available_audio_system() == case["result"]

@pytest.mark.parametrize('case',

@pytest.mark.parametrize(
'case',
[
{
"name": "default all available",
"available_systems": ["alsa","pulseaudio"],
"available_systems": ["alsa", "pulseaudio"],
"cmdline_audio": "",
"result": "pulseaudio"
},
@@ -154,19 +166,19 @@ def test_install_os_get_available_audio_system(case):
},
{
"name": "all available alsa selected",
"available_systems": ["alsa","pulseaudio"],
"available_systems": ["alsa", "pulseaudio"],
"cmdline_audio": "alsa",
"result": "alsa"
},
{
"name": "all available pulse selected",
"available_systems": ["alsa","pulseaudio"],
"available_systems": ["alsa", "pulseaudio"],
"cmdline_audio": "pulseaudio",
"result": "pulseaudio"
},
{
"name": "all available wrong cmd line",
"available_systems": ["alsa","pulseaudio"],
"available_systems": ["alsa", "pulseaudio"],
"cmdline_audio": "wrong",
"result": "pulseaudio"
},
@@ -177,9 +189,9 @@ def test_install_os_get_available_audio_system(case):
"result": "alsa"
},
],
ids=lambda x:x["name"])
ids=lambda x: x["name"])
@pytest.mark.calculate_vars
def test_install_os_get_available_audio_system(case):
def test_install_os_get_available_audio_system_other(case):
assert install_os.get_audio_selected(
Var(case["available_systems"]),
Var(case["cmdline_audio"])) == case["result"]

Loading…
Cancel
Save