source: ReferenceDesigns/w3_802.11/python/wlan_exp/version.py

Last change on this file was 6320, checked in by chunter, 5 years ago

1.8.0 release wlan-exp

File size: 7.7 KB
Line 
1# -*- coding: utf-8 -*-
2"""
3------------------------------------------------------------------------------
4Mango 802.11 Reference Design Experiments Framework - Version Utils
5------------------------------------------------------------------------------
6License:   Copyright 2019 Mango Communications, Inc. All rights reserved.
7           Use and distribution subject to terms in LICENSE.txt
8------------------------------------------------------------------------------
9
10This module provides wlan_exp version information and commands.
11
12Functions (see below for more information):
13    wlan_exp_ver()       -- Returns wlan_exp version
14    wlan_exp_ver_check() -- Checks the provided version against the wlan_exp version
15    wlan_exp_ver_str()   -- Returns string of wlan_exp version
16
17Integer constants:
18    WLAN_EXP_MAJOR, WLAN_EXP_MINOR, WLAN_EXP_REVISION, WLAN_EXP_XTRA,
19        WLAN_EXP_RELEASE -- wlan_exp verision constants
20
21"""
22
23import os
24import inspect
25
26
27__all__ = ['wlan_exp_ver', 'wlan_exp_ver_check', 'wlan_exp_ver_str']
28
29
30# Version defines
31WLAN_EXP_MAJOR          = 1
32WLAN_EXP_MINOR          = 8
33WLAN_EXP_REVISION       = 0
34WLAN_EXP_XTRA           = str('')
35WLAN_EXP_RELEASE        = True
36
37# Version string
38version  = "wlan_exp v"
39version += "{0:d}.".format(WLAN_EXP_MAJOR)
40version += "{0:d}.".format(WLAN_EXP_MINOR)
41version += "{0:d} ".format(WLAN_EXP_REVISION)
42version += "{0:s} ".format(WLAN_EXP_XTRA)
43
44
45# Version number
46version_number  = "{0:d}.".format(WLAN_EXP_MAJOR)
47version_number += "{0:d}.".format(WLAN_EXP_MINOR)
48version_number += "{0:d} ".format(WLAN_EXP_REVISION)
49
50
51# Status defines for wlan_ver_check
52WLAN_EXP_VERSION_SAME   = 0
53WLAN_EXP_VERSION_NEWER  = 1
54WLAN_EXP_VERSION_OLDER  = -1
55
56
57#-----------------------------------------------------------------------------
58# Version Exception
59#-----------------------------------------------------------------------------
60
61class VersionError(Exception):
62    """Exception for version errors.
63   
64    Attributes:
65        message -- explanation message of the error
66    """
67    def __init__(self, message):
68        self.message = message
69
70    def __str__(self):
71        msg  = "Version Error:"
72        msg += "    {0} \n".format(self.message)
73        return msg
74       
75# End Class
76
77
78#-----------------------------------------------------------------------------
79# Version Utilities
80#-----------------------------------------------------------------------------
81def wlan_exp_ver():
82    """Returns the version of WlanExp for this package."""   
83    # Print the release message if this is not an official release   
84    if not WLAN_EXP_RELEASE: 
85        print("-" * 60)
86        print("You are running a version of wlan_exp that may not be ")
87        print("compatible with released wlan_exp bitstreams. Please use ")
88        print("at your own risk.")
89        print("-" * 60)
90   
91    return (WLAN_EXP_MAJOR, WLAN_EXP_MINOR, WLAN_EXP_REVISION)
92   
93# End of wlan_exp_ver()
94
95
96def wlan_exp_ver_check(ver_str=None, major=None, minor=None, revision=None,
97                       caller_desc=None):
98    """Checks the version of wlan_exp for this package.
99   
100    This function will print a warning message if the version specified
101    is older than the current version and will raise a VersionError
102    if the version specified is newer than the current version.
103   
104    Args:
105        ver_str  -- Version string returned by wlan_exp_ver_str()
106        major    -- Major release number for wlan_exp
107        minor    -- Minor release number for wlan_exp
108        revision -- Revision release number for wlan_exp
109       
110    The ver_str attribute takes precedence over the major, minor, revsion
111    attributes.
112    """
113    status    = WLAN_EXP_VERSION_SAME
114    print_msg = False
115    raise_ex  = False
116   
117    if not ver_str is None:
118        try:
119            temp = ver_str.split(" ")
120            (major, minor, revision) = temp[0].split(".")
121        except AttributeError:
122            msg  = "ERROR: input parameter ver_str not valid"
123            raise AttributeError(msg)
124
125    # If ver_str was not specified, then major, minor, revision should be defined
126    #   and contain strings.  Need to convert to integers.       
127    try:
128        major    = int(major)
129        minor    = int(minor)
130        revision = int(revision)
131    except ValueError:
132        msg  = "ERROR: input parameters major, minor, revision not valid"
133        raise AttributeError(msg)
134   
135    # Check the provided version vs the current version
136    if (caller_desc is None):
137        msg  = "wlan_exp Version Mismatch: \n"
138        msg += "    Caller is using wlan_exp package version: {0}\n".format(wlan_exp_ver_str(major, minor, revision))
139    else:
140        msg  = "wlan_exp Version Mismatch: \n"
141        msg += "    " + str(caller_desc)
142       
143    msg += "    Current wlan_exp package version: {0}".format(wlan_exp_ver_str())
144
145    # Given there might be changes that break things, always raise an
146    # exception on version mismatch
147    if (major == WLAN_EXP_MAJOR):
148        if (minor == WLAN_EXP_MINOR):
149            if (revision != WLAN_EXP_REVISION):
150                if (revision < WLAN_EXP_REVISION):
151                    msg      += " (newer)\n"
152                    status    = WLAN_EXP_VERSION_NEWER
153                    raise_ex  = True
154                else:
155                    msg      += " (older)\n"
156                    status    = WLAN_EXP_VERSION_OLDER
157                    raise_ex  = True
158        else:
159            if (minor < WLAN_EXP_MINOR):
160                msg      += " (newer)\n"
161                status    = WLAN_EXP_VERSION_NEWER
162                raise_ex  = True
163            else:
164                msg      += " (older)\n"
165                status    = WLAN_EXP_VERSION_OLDER
166                raise_ex  = True
167    else:
168        if (major < WLAN_EXP_MAJOR):
169            msg      += " (newer)\n"
170            status    = WLAN_EXP_VERSION_NEWER
171            raise_ex  = True
172        else:
173            msg      += " (older)\n"
174            status    = WLAN_EXP_VERSION_OLDER
175            raise_ex  = True
176
177    msg += "        ({0})\n".format(__file__)
178
179    if print_msg:
180        print(msg)
181
182    if raise_ex:
183        raise VersionError(msg)
184   
185    return status
186   
187# End def
188
189
190def print_wlan_exp_ver():
191    """Print the wlan_exp Version."""
192    print("wlan_exp v" + wlan_exp_ver_str() + "\n")
193    print("Framework Location:")
194    print(os.path.dirname(
195              os.path.abspath(inspect.getfile(inspect.currentframe()))))
196
197# End def
198
199
200def wlan_exp_ver_str(major=WLAN_EXP_MAJOR, minor=WLAN_EXP_MINOR, 
201                     revision=WLAN_EXP_REVISION, xtra=WLAN_EXP_XTRA):
202    """Return a string of the wlan_exp version.
203   
204    This will raise a VersionError if the arguments are not integers.   
205    """
206    try:
207        msg  = "{0:d}.".format(major)
208        msg += "{0:d}.".format(minor)
209        msg += "{0:d} ".format(revision)
210        msg += "{0:s}".format(xtra)
211    except ValueError:
212        # Set output string to default values so program can continue
213        error  = "WARNING:  Unknown Argument - All arguments should be integers\n"
214        error += "    Setting wlan_exp version string to default."
215        print(error)
216       
217        msg  = "{0:d}.".format(WLAN_EXP_MAJOR)
218        msg += "{0:d}.".format(WLAN_EXP_MINOR)
219        msg += "{0:d} ".format(WLAN_EXP_REVISION)
220        msg += "{0:s}".format(WLAN_EXP_XTRA)
221       
222    return msg
223   
224# End def
225
226
227def wlan_exp_ver_code_to_str(ver_code):
228    """Convert four byte version code with format [x major minor rev] to a string."""
229    ver = int(ver_code)
230    return wlan_exp_ver_str(((ver >> 24) & 0xFF), ((ver >> 16) & 0xFF), ((ver >> 0) & 0xFF))
231
232# End def
Note: See TracBrowser for help on using the repository browser.