1
2
3
4 import os
5 import re
6 import rpm
7
8
9 try:
10 from dnf import __version__
11 PM_PLUGIN_CONF = '/etc/dnf/plugins/spacewalk.conf'
12 PM_PLUGIN_NAME = 'dnf-plugin-spacewalk'
13 PM_NAME = 'dnf'
14 except ImportError:
15 PM_PLUGIN_CONF = '/etc/yum/pluginconf.d/rhnplugin.conf'
16 PM_PLUGIN_NAME = 'yum-rhn-plugin'
17 PM_NAME = 'yum'
18
36
38 """ Returns positive number if plugin package is installed, otherwise it return 0 """
39 ts = rpm.TransactionSet()
40 headers = ts.dbMatch('providename', PM_PLUGIN_NAME)
41 return headers.count()
42
44 """ Returns true if PM_PLUGIN_CONF is presented """
45 try:
46 os.stat(PM_PLUGIN_CONF)
47 return True
48 except OSError:
49 return False
50
52 """ Create file PM_PLUGIN_CONF, with default values """
53 f = open(PM_PLUGIN_CONF, 'w')
54 f.write("""[main]
55 enabled = 1
56 gpgcheck = 1""")
57 f.close()
58
60 """ Returns True if plugin is enabled
61 Can thrown IOError exception.
62 """
63 f = open(PM_PLUGIN_CONF, 'r')
64 lines = f.readlines()
65 f.close()
66 main_section = False
67 result = False
68 for line in lines:
69 if re.match("^\[.*]", line):
70 if re.match("^\[main]", line):
71 main_section = True
72 else:
73 main_section = False
74 if main_section:
75 m = re.match('^\s*enabled\s*=\s*([0-9])', line)
76 if m:
77 if int(m.group(1)):
78 result = True
79 else:
80 result = False
81 return result
82
84 """ enable plugin by setting enabled=1 in file PM_PLUGIN_CONF
85 Can thrown IOError exception.
86 """
87 f = open(PM_PLUGIN_CONF, 'r')
88 lines = f.readlines()
89 f.close()
90 main_section = False
91 f = open(PM_PLUGIN_CONF, 'w')
92 for line in lines:
93 if re.match("^\[.*]", line):
94 if re.match("^\[main]", line):
95 main_section = True
96 else:
97 main_section = False
98 if main_section:
99 line = re.sub('^(\s*)enabled\s*=.+', r'\1enabled = 1', line)
100 f.write(line)
101 f.close()
102