]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/utilsOsType.py
Vendor import of lldb trunk r301441:
[FreeBSD/FreeBSD.git] / scripts / utilsOsType.py
1 """ Utility module to determine the OS Python running on
2
3     --------------------------------------------------------------------------
4     File:         utilsOsType.py
5
6     Overview:       Python module to supply functions and an enumeration to
7                     help determine the platform type, bit size and OS currently
8                     being used.
9     --------------------------------------------------------------------------
10
11 """
12
13 # Python modules:
14 import sys      # Provide system information
15
16 # Third party modules:
17
18 # In-house modules:
19
20 # Instantiations:
21
22 # Enumerations:
23 #-----------------------------------------------------------------------------
24 # Details:  Class to implement a 'C' style enumeration type.
25 # Gotchas:  None.
26 # Authors:  Illya Rudkin 28/11/2013.
27 # Changes:  None.
28 #--
29 if sys.version_info.major >= 3:
30     from enum import Enum
31
32     class EnumOsType(Enum):
33         Unknown = 0
34         Darwin = 1
35         FreeBSD = 2
36         Linux = 3
37         NetBSD = 4
38         Windows = 5
39         kFreeBSD = 6
40 else:
41     class EnumOsType(object):
42         values = ["Unknown",
43                   "Darwin",
44                   "FreeBSD",
45                   "Linux",
46                   "NetBSD",
47                   "Windows",
48                   "kFreeBSD"]
49
50         class __metaclass__(type):
51             #++----------------------------------------------------------------
52             # Details:  Fn acts as an enumeration.
53             # Args:     vName - (R) Enumeration to match.
54             # Returns:  Int - Matching enumeration/index.
55             # Throws:   None.
56             #--
57
58             def __getattr__(cls, vName):
59                 return cls.values.index(vName)
60
61 #++---------------------------------------------------------------------------
62 # Details:  Reverse fast lookup of the values list.
63 # Args:     vI - (R) Index / enumeration.
64 # Returns:  Str - text description matching enumeration.
65 # Throws:   None.
66 #--
67             def name_of(cls, vI):
68                 return EnumOsType.values[vI]
69
70 #-----------------------------------------------------------------------------
71 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
73
74 #++---------------------------------------------------------------------------
75 # Details:  Determine what operating system is currently running on.
76 # Args:     None.
77 # Returns:  EnumOsType - The OS type being used ATM.
78 # Throws:   None.
79 #--
80
81
82 def determine_os_type():
83     eOSType = EnumOsType.Unknown
84
85     strOS = sys.platform
86     if strOS == "darwin":
87         eOSType = EnumOsType.Darwin
88     elif strOS.startswith("freebsd"):
89         eOSType = EnumOsType.FreeBSD
90     elif strOS.startswith("linux"):
91         eOSType = EnumOsType.Linux
92     elif strOS.startswith("netbsd"):
93         eOSType = EnumOsType.NetBSD
94     elif strOS == "win32":
95         eOSType = EnumOsType.Windows
96     elif strOS.startswith("gnukfreebsd"):
97         eOSType = EnumOsType.kFreeBSD
98
99     return eOSType