From d2723105fbf128e9e1f326f57e1e402ce8248cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A5=D0=B8=D1=80=D0=B5=D1=86=D0=BA=D0=B8=D0=B9=20=D0=9C?= =?UTF-8?q?=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Tue, 8 Dec 2020 12:29:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=80=D0=B0=D0=B7=D0=BB=D0=B8=D1=87=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- calculate/utils/files.py | 12 ++++ calculate/utils/system.py | 53 +++++++++++++++ calculate/vars/install/os/__init__.py | 46 ++++++++----- calculate/vars/main/cl/__init__.py | 6 +- calculate/vars/main/os/__init__.py | 94 +++++++++++++++++++++++++-- calculate/vars/update/__init__.py | 0 calculate/vars/update/cl/__init__.py | 20 ++++++ 7 files changed, 206 insertions(+), 25 deletions(-) create mode 100644 calculate/utils/system.py create mode 100644 calculate/vars/update/__init__.py create mode 100644 calculate/vars/update/cl/__init__.py diff --git a/calculate/utils/files.py b/calculate/utils/files.py index c640b81..4652d58 100644 --- a/calculate/utils/files.py +++ b/calculate/utils/files.py @@ -8,6 +8,7 @@ from glob import glob import os import sys import contextlib +import re class FilesError(Exception): @@ -354,6 +355,17 @@ def read_file(file_path): format(str(error), mod, lineno)) +def grep_file(file_path, regexp, flags=0): + """ + Получить из файла данные по регулярному выражению + """ + data = read_file(file_path) + m = re.search(regexp, data, flags=flags) + if m: + return m.group() + return "" + + def write_file(file_path): '''Функция для открытия и записи файлов. Создает директории на пути к целевому файлу если это необходимо. Возвращает файловый объект.''' diff --git a/calculate/utils/system.py b/calculate/utils/system.py new file mode 100644 index 0000000..2886f86 --- /dev/null +++ b/calculate/utils/system.py @@ -0,0 +1,53 @@ +from calculate.utils.files import grep_file +import os + +class SystemType: + """ + Тип контейнера текущей системы + """ + Uml = "uml" + VServer = "vserver" + OpenVZ = "openvz" + LXC = "lxc" + Docker = "docker" + Xen0 = "xen0" + XenU = "xenU" + Rkt = "rkt" + SystemdNSpawn = "systemd-nspawn" + NotDetected = "" + + @classmethod + def detect_container(cls): + if grep_file("/proc/cpuinfo", "UML"): + return cls.Uml + elif grep_file("/proc/self/status", + "(s_context|VxID):\s*[1-9]"): + return cls.VServer + elif (os.path.exists("/proc/vz/veinfo") + and not os.path.exists("/proc/vz/version")): + return cls.OpenVZ + elif grep_file("/proc/self/status", "envID:\s*[1-9]"): + return cls.OpenVZ + elif grep_file("/proc/1/environ", "container=lxc"): + return cls.LXC + elif grep_file("/proc/1/environ", "container=rkt"): + return cls.Rkt + elif grep_file("/proc/1/environ", "container=systemd-nspawn"): + return cls.SystemdNSpawn + elif os.path.exists("/.dockerenv"): + return cls.Docker + elif grep_file("/proc/1/environ", "container=docker"): + return cls.Docker + return cls.NotDetected + + @classmethod + def detect_vm(cls): + if os.path.exists("/proc/xen"): + if grep_file("/proc/xen/capabilities", "control_d"): + return cls.Xen0 + return cls.XenU + return cls.NotDetected + + @classmethod + def detect(cls): + return cls.detect_container() or cls.detect_vm() diff --git a/calculate/vars/install/os/__init__.py b/calculate/vars/install/os/__init__.py index e0af426..3c761b8 100644 --- a/calculate/vars/install/os/__init__.py +++ b/calculate/vars/install/os/__init__.py @@ -5,21 +5,33 @@ from calculate.vars.main.os.func import get_arch_gentoo from calculate.vars.install.os.func import (get_audio_selected, get_available_audio_system) -with Namespace("arch"): - # Variable("machine", type=StringType, - # source=Calculate(lambda x: x.value, "main.os.arch.machine")) - print('var: machine') - Variable("machine", type=StringType, - source=Copy("main.os.arch.machine")) - Variable("gentoo", type=StringType, - source=Calculate(get_arch_gentoo, ".machine")) -with Namespace("audio"): - Variable("available", type=ListType, - source=Calculate( - get_available_audio_system)) - Variable("selected", type=StringType, - source=Calculate( - get_audio_selected, - ".available", - "main.cl.cmdline.calculate.audio")) +def import_variables(): + with Namespace("arch"): + Variable("machine", type=StringType, + source=Copy("main.os.arch.machine")) + Variable("gentoo", type=StringType, + source=Calculate(get_arch_gentoo, ".machine")) + + with Namespace("audio"): + Variable("available", type=ListType, + source=Calculate( + get_available_audio_system)) + Variable("selected", type=StringType, + source=Calculate( + get_audio_selected, + ".available", + "main.cl.cmdline.calculate.audio")) + + Variable("subsystem", type=StringType, source=Copy("main.os.subsystem")) + + with Namespace("container"): + Variable("type", type=StringType, source=Copy("main.os.container.type")) + + with Namespace("linux"): + Variable("shortname", type=StringType, source=Copy("main.os.linux.shortname")) + Variable("name", type=StringType, source=Copy("main.os.linux.name")) + Variable("subname", type=StringType, source=Copy("main.os.linux.subname")) + Variable("system", type=StringType, source=Copy("main.os.linux.system")) + Variable("ver", type=StringType, source=Copy("main.os.linux.ver")) + Variable("build", type=StringType, source=Copy("main.os.linux.build")) diff --git a/calculate/vars/main/cl/__init__.py b/calculate/vars/main/cl/__init__.py index 6fe9472..b0d9ac7 100644 --- a/calculate/vars/main/cl/__init__.py +++ b/calculate/vars/main/cl/__init__.py @@ -9,7 +9,6 @@ from calculate.variables.datavars import ( Calculate ) - def get_ebuild_phase(): return os.environ.get("EBUILD_PHASE", "") @@ -105,6 +104,11 @@ def get_isoscan_fullpath(base_path, filename): def import_variables(): + Variable("chroot_path", type=StringType, + source=Calculate(lambda x:x.value, "main.cl_chroot_path")) + Variable("root_path", type=StringType, + source=Calculate(lambda x:x.value, "main.cl_root_path")) + Variable("ebuild_phase", type=StringType, source=Calculate(get_ebuild_phase)) diff --git a/calculate/vars/main/os/__init__.py b/calculate/vars/main/os/__init__.py index 5a7824a..d14f814 100644 --- a/calculate/vars/main/os/__init__.py +++ b/calculate/vars/main/os/__init__.py @@ -2,13 +2,93 @@ from calculate.variables.datavars import ( Variable, Namespace, StringType, - Calculate + Calculate, + Static ) -from .func import get_arch_machine, get_arch_gentoo +from calculate.vars.main.os.func import get_arch_machine, get_arch_gentoo +from calculate.utils.system import SystemType -with Namespace("arch"): - Variable("machine", type=StringType, - source=Calculate(get_arch_machine)) +import os +import platform - Variable("gentoo", type=StringType, - source=Calculate(get_arch_gentoo, ".machine")) +dictLinuxName = {"CLD": "Calculate Linux Desktop", + "CLDX": "Calculate Linux Desktop", + "CLDG": "Calculate Linux Desktop", + "CDS": "Calculate Directory Server", + "CLS": "Calculate Linux Scratch", + "CSS": "Calculate Scratch Server", + "CMC": "Calculate Media Center", + "Gentoo": "Gentoo"} + +dictLinuxSubName = {"CLD": "KDE", "CLDX": "XFCE", "CLDG": "GNOME"} + +dictNameSystem = {'CDS': 'server', + 'CLD': 'desktop', + 'CLDG': 'desktop', + 'CLDX': 'desktop', + 'CLS': 'desktop', + 'CMC': 'desktop', + 'CSS': 'server'} + +def dict_value(d, keyname, fallback_value): + return d.value.get(keyname.value, fallback_value.value) + +def detect_other_shortname(systemroot): + """Detect other system. Now only Gentoo.""" + gentoo_file = os.path.join(systemroot.value, "etc/gentoo-release") + if os.path.exists(gentoo_file): + return "Gentoo" + return "Linux" + +def get_version_from_gentoo_files(systemroot, profilefile): + """Get version from gentoo files""" + systemroot = systemroot.value + gentoo_file = os.path.join(systemroot, "etc/gentoo-release") + re_ver = re.compile("^(\d+\.)*\d+$", re.S) + if os.path.exists(gentoo_file): + gentoo_link = self.Get('cl_make_profile') + if os.path.islink(gentoo_link): + vers = filter(re_ver.search, + os.readlink(gentoo_link).split('/')) + if vers: + return vers[-1] + +def get_version_from_uname(): + """Get version from uname""" + re_ver = re.search("^(\d+\.)*\d", platform.release(), re.S) + if re_ver: + return re_ver.group() + +def get_linux_version(systemroot, make_profile): + return "21" + +def import_variables(): + with Namespace("arch"): + Variable("machine", type=StringType, + source=Calculate(get_arch_machine)) + + Variable("gentoo", type=StringType, + source=Calculate(get_arch_gentoo, ".machine")) + + with Namespace("gentoo"): + Variable('make_profile', type=StringType, source=Calculate(lambda x:x.value, + "os.gentoo.make_profile")) + + Variable("subsystem", type=StringType, + source=Calculate(lambda : SystemType.detect())) + + with Namespace("container"): + Variable("type", type=StringType, source="") + + with Namespace("linux"): + Variable("shortname", type=StringType, + source=Calculate(detect_other_shortname, "main.cl.chroot_path")) + Variable("name", type=StringType, + source=Calculate(dict_value, dictLinuxName, ".shortname", Static("Linux"))) + Variable("subname", type=StringType, + source=Calculate(dict_value, dictLinuxSubName, ".shortname", Static(""))) + Variable("system", type=StringType, + source=Calculate(dict_value, dictNameSystem, ".shortname", Static(""))) + Variable("ver", type=StringType, + source=Calculate(get_linux_version, "main.cl.chroot_path", "main.os.gentoo.make_profile")) + Variable("build", type=StringType, source="") diff --git a/calculate/vars/update/__init__.py b/calculate/vars/update/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/calculate/vars/update/cl/__init__.py b/calculate/vars/update/cl/__init__.py new file mode 100644 index 0000000..915998d --- /dev/null +++ b/calculate/vars/update/cl/__init__.py @@ -0,0 +1,20 @@ +from calculate.variables.datavars import ( + Variable, + Namespace, + StringType, + Calculate, + Static, + TableType + ) + +def get_repository_table(): + return [ + {'name':'gentoo', + 'url': 'git://git.calculate-linux.org/calculate/gentoo-overlay.git'}, + {'name':'calculate', + 'url': 'git://git.calculate-linux.org/calculate/calculate-overlay.git'}, + ] + +def import_variables(): + Variable('repositories', type=TableType, + source=Calculate(get_repository_table))