initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / python24 / versioning.py
1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org)
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
5 # are met:
6 # 1.  Redistributions of source code must retain the above copyright
7 #     notice, this list of conditions and the following disclaimer.
8 # 2.  Redistributions in binary form must reproduce the above copyright
9 #     notice, this list of conditions and the following disclaimer in the
10 #     documentation and/or other materials provided with the distribution.
11 #
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23 """Supports Python version checking."""
24
25 import logging
26 import sys
27
28
29 _log = logging.getLogger(__name__)
30
31
32 # The minimum Python version the webkitpy package supports.
33 _MINIMUM_SUPPORTED_PYTHON_VERSION = "2.5"
34
35
36 def compare_version(sysmodule=None, target_version=None):
37     """Compare the current Python version with a target version.
38
39     Args:
40       sysmodule: An object with version and version_info data attributes
41                  used to detect the current Python version.  The attributes
42                  should have the same semantics as sys.version and
43                  sys.version_info.  This parameter should only be used
44                  for unit testing.  Defaults to sys.
45       target_version: A string representing the Python version to compare
46                       the current version against.  The string should have
47                       one of the following three forms: 2, 2.5, or 2.5.3.
48                       Defaults to the minimum version that the webkitpy
49                       package supports.
50
51     Returns:
52       A triple of (comparison, current_version, target_version).
53
54       comparison: An integer representing the result of comparing the
55                   current version with the target version.  A positive
56                   number means the current version is greater than the
57                   target, 0 means they are the same, and a negative number
58                   means the current version is less than the target.
59                       This method compares version information only up
60                   to the precision of the given target version.  For
61                   example, if the target version is 2.6 and the current
62                   version is 2.5.3, this method uses 2.5 for the purposes
63                   of comparing with the target.
64       current_version: A string representing the current Python version, for
65                        example 2.5.3.
66       target_version: A string representing the version that the current
67                       version was compared against, for example 2.5.
68
69     """
70     if sysmodule is None:
71         sysmodule = sys
72     if target_version is None:
73         target_version = _MINIMUM_SUPPORTED_PYTHON_VERSION
74
75     # The number of version parts to compare.
76     precision = len(target_version.split("."))
77
78     # We use sys.version_info rather than sys.version since its first
79     # three elements are guaranteed to be integers.
80     current_version_info_to_compare = sysmodule.version_info[:precision]
81     # Convert integers to strings.
82     current_version_info_to_compare = map(str, current_version_info_to_compare)
83     current_version_to_compare = ".".join(current_version_info_to_compare)
84
85     # Compare version strings lexicographically.
86     if current_version_to_compare > target_version:
87         comparison = 1
88     elif current_version_to_compare == target_version:
89         comparison = 0
90     else:
91         comparison = -1
92
93     # The version number portion of the current version string, for
94     # example "2.6.4".
95     current_version = sysmodule.version.split()[0]
96
97     return (comparison, current_version, target_version)
98
99
100 # FIXME: Add a logging level parameter to allow the version message
101 #        to be logged at levels other than WARNING, for example CRITICAL.
102 def check_version(log=None, sysmodule=None, target_version=None):
103     """Check the current Python version against a target version.
104
105     Logs a warning message if the current version is less than the
106     target version.
107
108     Args:
109       log: A logging.logger instance to use when logging the version warning.
110            Defaults to the logger of this module.
111       sysmodule: See the compare_version() docstring.
112       target_version: See the compare_version() docstring.
113
114     Returns:
115       A boolean value of whether the current version is greater than
116       or equal to the target version.
117
118     """
119     if log is None:
120         log = _log
121
122     (comparison, current_version, target_version) = \
123         compare_version(sysmodule, target_version)
124
125     if comparison >= 0:
126         # Then the current version is at least the minimum version.
127         return True
128
129     message = ("WebKit Python scripts do not support your current Python "
130                "version (%s).  The minimum supported version is %s.\n"
131                "  See the following page to upgrade your Python version:\n\n"
132                "    http://trac.webkit.org/wiki/PythonGuidelines\n"
133                % (current_version, target_version))
134     log.warn(message)
135     return False