aboutsummaryrefslogtreecommitdiffstats
path: root/tools/osdetect.py
blob: fea78cc5d97db44f7ed551e13d35c581174a61c6 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'''
Created on Aug 2, 2011
@author: Keith Robertson
'''

import pprint
import os
import re

class OSTypes:
    """
    Utility class with enumerations for the various OSes to facilitate 
    OS detection.    
    """
    JAVA_OS_NAME_KEY='os.name'
    
    OS_TYPE_LINUX='Linux'
    OS_TYPE_LINUX_PAT=re.compile('^%s$' % OS_TYPE_LINUX, re.I)
    OS_TYPE_WIN='Windows'
    OS_TYPE_WIN_PAT=re.compile('^%s$' % OS_TYPE_WIN, re.I)
    OS_TYPE_AIX='AIX'
    OS_TYPE_AIX_PAT=re.compile('^%s$' % OS_TYPE_AIX, re.I)
    OS_TYPE_MAC='Mac OS'
    OS_TYPE_MAC_PAT=re.compile('^%s$' % OS_TYPE_MAC, re.I)
    OS_TYPE_390='OS/390'
    OS_TYPE_390_PAT=re.compile('^%s$' % OS_TYPE_390, re.I)
    OS_TYPE_HPUX='HP-UX'
    OS_TYPE_HPUX_PAT=re.compile('^%s$' % OS_TYPE_HPUX, re.I)

def printProps():
    try:
        from java.lang import System
        from java.util import Set
        from java.util import Iterator
        
        set = System.getProperties().entrySet()
        it = set.iterator()
        while it.hasNext():
            me = it.next();        
            print "Key (%s) Value(%s)" % (me.getKey(), me.getValue())
    except Exception, e:
        print "ERROR: unable to print Java properties %s" % e

    

def java_detect_os():
    """
    Try to load Java packages.  If successful then we know we are running
    in JYthon.  Use the JRE to determine what type of OS and return the proper 
    policy.
    """
    try:
        from java.lang import System
        
        ostype = System.getProperty(OSTypes.JAVA_OS_NAME_KEY)
        if ostype:
            if OSTypes.OS_TYPE_LINUX_PAT.match(ostype):
                print "Matched %s" % OSTypes.OS_TYPE_LINUX
                # Lots of checks here to determine linux version.
                #return proper policy here
            elif OSTypes.OS_TYPE_WIN_PAT.match(ostype):
                print "Matched %s" % OSTypes.OS_TYPE_WIN
            elif OS_TYPE_AIX_PAT.match(ostype):
                print "Matched %s" % OSTypes.OS_TYPE_AIX
            elif OS_TYPE_MAC_PAT.match(ostype):
                print "Matched %s" % OSTypes.OS_TYPE_MAC
            elif OS_TYPE_390_PAT.match(ostype):
                print "Matched %s" % OSTypes.OS_TYPE_390
            elif OS_TYPE_HPUX_PAT.match(ostype):
                print "Matched %s" % OSTypes.OS_TYPE_HPUX
            else:
                raise Exception("Unsupported OS type of %s." % ostype)                                  
        else:
            raise Exception("Unable to get %s from JRE's system properties." % OSTypes.JAVA_OS_NAME_KEY)            
    except Exception, e:
        print "WARN: unable to print Java properties %s" % e
        
def native_detect_os():
    print "here" 
        
    

if __name__ == '__main__':
    #printProps()
    detectOS()
    
    pass