Package up2date_client :: Module pmPlugin
[hide private]
[frames] | no frames]

Source Code for Module up2date_client.pmPlugin

  1  # Client code for enabling plugin 
  2  # Copyright (c) 2000--2016 Red Hat, Inc. 
  3   
  4  import os 
  5  import re 
  6  import rpm 
  7   
  8  # global variables 
  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   
19 -def pluginEnable():
20 """Enables plugin, may throw IOError""" 21 conf_changed = 0 22 plugin_present = 0 23 if PluginPackagePresent(): 24 plugin_present = 1 25 if PluginConfPresent(): 26 if not PluginEnabled(): 27 enablePlugin() 28 conf_changed = 1 29 else: 30 createDefaultPluginConf() 31 conf_changed = 1 32 elif os.path.exists("/usr/lib/zypp/plugins/services/spacewalk"): 33 """SUSE zypp plugin is installed""" 34 plugin_present = 1 35 return plugin_present, conf_changed
36
37 -def PluginPackagePresent():
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
43 -def PluginConfPresent():
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
51 -def createDefaultPluginConf():
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
59 -def PluginEnabled():
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
83 -def enablePlugin():
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