mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
Merge pull request #43345 from ribx/develop
better atom extraction from portage version string
This commit is contained in:
commit
a1b871304b
@ -75,9 +75,20 @@ def _porttree():
|
|||||||
|
|
||||||
|
|
||||||
def _p_to_cp(p):
|
def _p_to_cp(p):
|
||||||
ret = _porttree().dbapi.xmatch("match-all", p)
|
try:
|
||||||
if ret:
|
ret = portage.dep_getkey(p)
|
||||||
return portage.cpv_getkey(ret[0])
|
if ret:
|
||||||
|
return ret
|
||||||
|
except portage.exception.InvalidAtom:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
ret = _porttree().dbapi.xmatch('bestmatch-visible', p)
|
||||||
|
if ret:
|
||||||
|
return portage.dep_getkey(ret)
|
||||||
|
except portage.exception.InvalidAtom:
|
||||||
|
pass
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -91,11 +102,14 @@ def _allnodes():
|
|||||||
|
|
||||||
|
|
||||||
def _cpv_to_cp(cpv):
|
def _cpv_to_cp(cpv):
|
||||||
ret = portage.cpv_getkey(cpv)
|
try:
|
||||||
if ret:
|
ret = portage.dep_getkey(cpv)
|
||||||
return ret
|
if ret:
|
||||||
else:
|
return ret
|
||||||
return cpv
|
except portage.exception.InvalidAtom:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return cpv
|
||||||
|
|
||||||
|
|
||||||
def _cpv_to_version(cpv):
|
def _cpv_to_version(cpv):
|
||||||
|
@ -75,6 +75,8 @@ def _get_config_file(conf, atom):
|
|||||||
if parts.cp == '*/*':
|
if parts.cp == '*/*':
|
||||||
# parts.repo will be empty if there is no repo part
|
# parts.repo will be empty if there is no repo part
|
||||||
relative_path = parts.repo or "gentoo"
|
relative_path = parts.repo or "gentoo"
|
||||||
|
elif str(parts.cp).endswith('/*'):
|
||||||
|
relative_path = str(parts.cp).split("/")[0] + "_"
|
||||||
else:
|
else:
|
||||||
relative_path = os.path.join(*[x for x in os.path.split(parts.cp) if x != '*'])
|
relative_path = os.path.join(*[x for x in os.path.split(parts.cp) if x != '*'])
|
||||||
else:
|
else:
|
||||||
@ -92,9 +94,20 @@ def _p_to_cp(p):
|
|||||||
Convert a package name or a DEPEND atom to category/package format.
|
Convert a package name or a DEPEND atom to category/package format.
|
||||||
Raises an exception if program name is ambiguous.
|
Raises an exception if program name is ambiguous.
|
||||||
'''
|
'''
|
||||||
ret = _porttree().dbapi.xmatch("match-all", p)
|
try:
|
||||||
if ret:
|
ret = portage.dep_getkey(p)
|
||||||
return portage.cpv_getkey(ret[0])
|
if ret:
|
||||||
|
return ret
|
||||||
|
except portage.exception.InvalidAtom:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
ret = _porttree().dbapi.xmatch('bestmatch-visible', p)
|
||||||
|
if ret:
|
||||||
|
return portage.dep_getkey(ret)
|
||||||
|
except portage.exception.InvalidAtom:
|
||||||
|
pass
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -188,12 +201,7 @@ def _package_conf_file_to_dir(file_name):
|
|||||||
else:
|
else:
|
||||||
os.rename(path, path + '.tmpbak')
|
os.rename(path, path + '.tmpbak')
|
||||||
os.mkdir(path, 0o755)
|
os.mkdir(path, 0o755)
|
||||||
with salt.utils.files.fopen(path + '.tmpbak') as fh_:
|
os.rename(path + '.tmpbak', os.path.join(path, 'tmp'))
|
||||||
for line in fh_:
|
|
||||||
line = line.strip()
|
|
||||||
if line and not line.startswith('#'):
|
|
||||||
append_to_package_conf(file_name, string=line)
|
|
||||||
os.remove(path + '.tmpbak')
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
os.mkdir(path, 0o755)
|
os.mkdir(path, 0o755)
|
||||||
@ -218,7 +226,7 @@ def _package_conf_ordering(conf, clean=True, keep_backup=False):
|
|||||||
shutil.copy(file_path, file_path + '.bak')
|
shutil.copy(file_path, file_path + '.bak')
|
||||||
backup_files.append(file_path + '.bak')
|
backup_files.append(file_path + '.bak')
|
||||||
|
|
||||||
if cp[0] == '/' or cp.split('/') > 2:
|
if cp[0] == '/' or len(cp.split('/')) > 2:
|
||||||
with salt.utils.files.fopen(file_path) as fp_:
|
with salt.utils.files.fopen(file_path) as fp_:
|
||||||
rearrange.extend(fp_.readlines())
|
rearrange.extend(fp_.readlines())
|
||||||
os.remove(file_path)
|
os.remove(file_path)
|
||||||
|
@ -7,11 +7,14 @@
|
|||||||
'''
|
'''
|
||||||
# Import Python libs
|
# Import Python libs
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
|
import re
|
||||||
|
|
||||||
# Import Salt Testing libs
|
# Import Salt Testing libs
|
||||||
from tests.support.mixins import LoaderModuleMockMixin
|
from tests.support.mixins import LoaderModuleMockMixin
|
||||||
from tests.support.unit import skipIf, TestCase
|
from tests.support.unit import skipIf, TestCase
|
||||||
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, MagicMock, patch
|
||||||
|
from tests.support.paths import TMP
|
||||||
|
import salt.utils.files
|
||||||
|
|
||||||
# Import salt libs
|
# Import salt libs
|
||||||
import salt.modules.portage_config as portage_config
|
import salt.modules.portage_config as portage_config
|
||||||
@ -19,27 +22,101 @@ import salt.modules.portage_config as portage_config
|
|||||||
|
|
||||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||||
class PortageConfigTestCase(TestCase, LoaderModuleMockMixin):
|
class PortageConfigTestCase(TestCase, LoaderModuleMockMixin):
|
||||||
|
|
||||||
class DummyAtom(object):
|
class DummyAtom(object):
|
||||||
def __init__(self, atom):
|
def __init__(self):
|
||||||
self.cp, self.repo = atom.split("::") if "::" in atom else (atom, None)
|
self.cp = None
|
||||||
|
self.repo = None
|
||||||
|
|
||||||
|
def __call__(self, atom, *_, **__):
|
||||||
|
if atom == '#' or isinstance(atom, MagicMock):
|
||||||
|
self.repo = None
|
||||||
|
self.cp = None
|
||||||
|
return self
|
||||||
|
|
||||||
|
# extract (and remove) repo
|
||||||
|
atom, self.repo = atom.split('::') if '::' in atom else (atom, None)
|
||||||
|
|
||||||
|
# remove '>, >=, <=, =, ~' etc.
|
||||||
|
atom = re.sub(r'[<>~+=]', '', atom)
|
||||||
|
# remove slots
|
||||||
|
atom = re.sub(r':[0-9][^:]*', '', atom)
|
||||||
|
# remove version
|
||||||
|
atom = re.sub(r'-[0-9][\.0-9]*', '', atom)
|
||||||
|
|
||||||
|
self.cp = atom
|
||||||
|
return self
|
||||||
|
|
||||||
def setup_loader_modules(self):
|
def setup_loader_modules(self):
|
||||||
self.portage = MagicMock()
|
try:
|
||||||
self.addCleanup(delattr, self, 'portage')
|
import portage
|
||||||
return {portage_config: {'portage': self.portage}}
|
return {}
|
||||||
|
except ImportError:
|
||||||
|
dummy_atom = self.DummyAtom()
|
||||||
|
self.portage = MagicMock()
|
||||||
|
self.portage.dep.Atom = MagicMock(side_effect=dummy_atom)
|
||||||
|
self.portage.dep_getkey = MagicMock(side_effect=lambda x: dummy_atom(x).cp)
|
||||||
|
self.portage.exception.InvalidAtom = Exception
|
||||||
|
self.addCleanup(delattr, self, 'portage')
|
||||||
|
return {portage_config: {'portage': self.portage}}
|
||||||
|
|
||||||
def test_get_config_file_wildcards(self):
|
def test_get_config_file_wildcards(self):
|
||||||
pairs = [
|
pairs = [
|
||||||
('*/*::repo', '/etc/portage/package.mask/repo'),
|
('*/*::repo', '/etc/portage/package.mask/repo'),
|
||||||
('*/pkg::repo', '/etc/portage/package.mask/pkg'),
|
('*/pkg::repo', '/etc/portage/package.mask/pkg'),
|
||||||
('cat/*', '/etc/portage/package.mask/cat'),
|
('cat/*', '/etc/portage/package.mask/cat_'),
|
||||||
('cat/pkg', '/etc/portage/package.mask/cat/pkg'),
|
('cat/pkg', '/etc/portage/package.mask/cat/pkg'),
|
||||||
('cat/pkg::repo', '/etc/portage/package.mask/cat/pkg'),
|
('cat/pkg::repo', '/etc/portage/package.mask/cat/pkg'),
|
||||||
]
|
]
|
||||||
|
|
||||||
for (atom, expected) in pairs:
|
for (atom, expected) in pairs:
|
||||||
dummy_atom = self.DummyAtom(atom)
|
self.assertEqual(portage_config._get_config_file('mask', atom), expected)
|
||||||
self.portage.dep.Atom = MagicMock(return_value=dummy_atom)
|
|
||||||
with patch.object(portage_config, '_p_to_cp', MagicMock(return_value=dummy_atom.cp)):
|
def test_enforce_nice_config(self):
|
||||||
self.assertEqual(portage_config._get_config_file('mask', atom), expected)
|
atoms = [
|
||||||
|
('*/*::repo', 'repo'),
|
||||||
|
('*/pkg1::repo', 'pkg1'),
|
||||||
|
('cat/*', 'cat_'),
|
||||||
|
('cat/pkg2', 'cat/pkg2'),
|
||||||
|
('cat/pkg3::repo', 'cat/pkg3'),
|
||||||
|
('<cat/pkg4-0.0.0.0', 'cat/pkg4'),
|
||||||
|
('>cat/pkg5-0.0.0.0:0', 'cat/pkg5'),
|
||||||
|
('>cat/pkg6-0.0.0.0:0::repo', 'cat/pkg6'),
|
||||||
|
('<=cat/pkg7-0.0.0.0', 'cat/pkg7'),
|
||||||
|
('=cat/pkg8-0.0.0.0', 'cat/pkg8'),
|
||||||
|
]
|
||||||
|
|
||||||
|
supported = [
|
||||||
|
('accept_keywords', ['~amd64']),
|
||||||
|
('env', ['glibc.conf']),
|
||||||
|
('license', ['LICENCE1', 'LICENCE2']),
|
||||||
|
('mask', ['']),
|
||||||
|
('properties', ['* -interactive']),
|
||||||
|
('unmask', ['']),
|
||||||
|
('use', ['apple', '-banana', 'ananas', 'orange']),
|
||||||
|
]
|
||||||
|
|
||||||
|
base_path = TMP + '/package.{0}'
|
||||||
|
|
||||||
|
def make_line(atom, addition):
|
||||||
|
return atom + (' ' + addition if addition != '' else '') + '\n'
|
||||||
|
|
||||||
|
for typ, additions in supported:
|
||||||
|
path = base_path.format(typ)
|
||||||
|
with salt.utils.files.fopen(path, 'a') as fh:
|
||||||
|
for atom, _ in atoms:
|
||||||
|
for addition in additions:
|
||||||
|
line = make_line(atom, addition)
|
||||||
|
fh.write('# comment for: ' + line)
|
||||||
|
fh.write(line)
|
||||||
|
|
||||||
|
with patch.object(portage_config, 'BASE_PATH', base_path):
|
||||||
|
with patch.object(portage_config, '_merge_flags', lambda l1, l2, _: list(set(l1 + l2))):
|
||||||
|
portage_config.enforce_nice_config()
|
||||||
|
|
||||||
|
for typ, additions in supported:
|
||||||
|
for atom, file_name in atoms:
|
||||||
|
with salt.utils.files.fopen(base_path.format(typ) + "/" + file_name, 'r') as fh:
|
||||||
|
for line in fh:
|
||||||
|
self.assertTrue(atom in line, msg="'{}' not in '{}'".format(addition, line))
|
||||||
|
for addition in additions:
|
||||||
|
self.assertTrue(addition in line, msg="'{}' not in '{}'".format(addition, line))
|
||||||
|
Loading…
Reference in New Issue
Block a user