File: subprocess.py

package info (click to toggle)
snmpsim 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,536 kB
  • ctags: 461
  • sloc: python: 4,257; makefile: 9
file content (49 lines) | stat: -rw-r--r-- 2,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# SNMP Simulator, http://snmpsim.sourceforge.net
# Managed value variation module
# Get/set managed value by invoking an external program
import sys
import subprocess
from snmpsim import log

def init(**context):
    moduleContext['settings'] = {}
    if context['options']:
        moduleContext['settings'].update(
            dict([x.split(':') for x in context['options'].split(',')])
        )
    if 'shell' not in moduleContext['settings']:
        moduleContext['settings']['shell'] = sys.platform[:3] == 'win'
    else:
        moduleContext['settings']['shell'] = int(moduleContext['settings']['shell'])
 
def variate(oid, tag, value, **context):
    args = [ x\
             .replace('@DATAFILE@', context['dataFile'])\
             .replace('@OID@', str(oid))\
             .replace('@TAG@', tag)\
             .replace('@ORIGOID@', str(context['origOid']))\
             .replace('@ORIGTAG@', str(sum([ x for x in context['origValue'].tagSet[0]])))\
             .replace('@ORIGVALUE@', str(context['origValue']))\
             .replace('@SETFLAG@', str(int(context['setFlag'])))\
             .replace('@NEXTFLAG@', str(int(context['nextFlag'])))\
             .replace('@SUBTREEFLAG@', str(int(context['subtreeFlag'])))\
             for x in value.split(' ') ]

    log.msg('subprocess: executing external process "%s"' % ' '.join(args))

    call = hasattr(subprocess, 'check_output') and subprocess.check_output or \
               hasattr(subprocess, 'check_call') and subprocess.check_call or \
               subprocess.call
    if not hasattr(subprocess, 'check_output'):
        log.msg('subprocess: old Python, expect no output!')

    try:
        return oid, tag, call(
            args, shell=moduleContext['settings']['shell']
        )
    except hasattr(subprocess, 'CalledProcessError') and \
               subprocess.CalledProcessError or Exception:
        log.msg('subprocess: external program execution failed')
        return context['origOid'], tag, context['errorStatus']

def shutdown(**context): pass