]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdownaddons/utilsbase.py
Merge pull request #80 from BarbarossaTM/tunnel-fixes-master
[mirror_ifupdown2.git] / ifupdownaddons / utilsbase.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6
7 import logging
8 import re
9 import io
10
11 from ifupdown.utils import utils
12 import ifupdown.ifupdownflags as ifupdownflags
13 from ifupdown.iface import *
14 from cache import *
15 import time
16 import logging
17
18 def profile(func):
19 def wrap(*args, **kwargs):
20 started_at = time.time()
21 result = func(*args, **kwargs)
22 print str(func)
23 print (time.time() - started_at)
24 return result
25 return wrap
26
27 class utilsBase(object):
28 """ Base class for ifupdown addon utilities """
29
30 def __init__(self, *args, **kargs):
31 modulename = self.__class__.__name__
32 self.logger = logging.getLogger('ifupdown.' + modulename)
33
34 def write_file(self, filename, strexpr):
35 try:
36 self.logger.info('writing \'%s\'' %strexpr +
37 ' to file %s' %filename)
38 if ifupdownflags.flags.DRYRUN:
39 return 0
40 with open(filename, 'w') as f:
41 f.write(strexpr)
42 except IOError, e:
43 self.logger.warn('error writing to file %s'
44 %filename + '(' + str(e) + ')')
45 return -1
46 return 0
47
48 def read_file(self, filename):
49 try:
50 self.logger.debug('reading \'%s\'' %filename)
51 with open(filename, 'r') as f:
52 return f.readlines()
53 except:
54 return None
55 return None
56
57 def read_file_oneline(self, filename):
58 try:
59 self.logger.debug('reading \'%s\'' %filename)
60 with open(filename, 'r') as f:
61 return f.readline().strip('\n')
62 except:
63 return None
64 return None
65
66 def sysctl_set(self, variable, value):
67 utils.exec_command('sysctl %s=%s' % (variable, value))
68
69 def sysctl_get(self, variable):
70 return utils.exec_command('sysctl %s' % variable).split('=')[1].strip()