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