]>
Commit | Line | Data |
---|---|---|
15ef32ea RP |
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 | |
15ef32ea RP |
8 | import re |
9 | import io | |
a4a53f4b JF |
10 | |
11 | from ifupdown.utils import utils | |
fc5e1735 | 12 | import ifupdown.ifupdownflags as ifupdownflags |
15ef32ea RP |
13 | from ifupdown.iface import * |
14 | from cache import * | |
15ef32ea RP |
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) | |
15ef32ea | 33 | |
15ef32ea RP |
34 | def write_file(self, filename, strexpr): |
35 | try: | |
36 | self.logger.info('writing \'%s\'' %strexpr + | |
37 | ' to file %s' %filename) | |
fc5e1735 | 38 | if ifupdownflags.flags.DRYRUN: |
15ef32ea RP |
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): | |
a193d8d1 | 67 | utils.exec_command('sysctl %s=%s' % (variable, value)) |
15ef32ea RP |
68 | |
69 | def sysctl_get(self, variable): | |
a193d8d1 | 70 | return utils.exec_command('sysctl %s' % variable).split('=')[1].strip() |