~mozillateam/mozilla-devscripts/mozilla-devscripts

213 by Alexander Sack
- add moz-version-compare helper script and ship it in extra_files; this script
1
#!/usr/bin/python
2
359 by Benjamin Drung
Update and fix my email addresses.
3
# Copyright (c) 2009 Benjamin Drung <bdrung@debian.org>
213 by Alexander Sack
- add moz-version-compare helper script and ship it in extra_files; this script
4
#
5
# Permission is hereby granted, free of charge, to any person obtaining a copy
6
# of this software and associated documentation files (the "Software"), to deal
7
# in the Software without restriction, including without limitation the rights
8
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
# copies of the Software, and to permit persons to whom the Software is
10
# furnished to do so, subject to the following conditions:
11
#
12
# The above copyright notice and this permission notice shall be included in
13
# all copies or substantial portions of the Software.
14
# 
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
# THE SOFTWARE.
22
218.1.1 by Benjamin Drung
rename moz-version-compare to moz-version. To compare two versions you have
23
import getopt
303 by Benjamin Drung
* moz-version:
24
import os
213 by Alexander Sack
- add moz-version-compare helper script and ship it in extra_files; this script
25
import sys
26
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
27
from moz_version import (compare_versions, convert_debian_to_moz_version,
28
                         convert_moz_to_debian_version)
286 by Benjamin Drung
- Move compare_versions from moz-version into separate module
29
218.1.1 by Benjamin Drung
rename moz-version-compare to moz-version. To compare two versions you have
30
# error codes
31
COMMAND_LINE_SYNTAX_ERROR = 2
32
INVALID_COMPARATOR = 3
33
EMPTY_VERSION_STRING = 4
34
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
35
COMPARATORS = ("lt", "le", "eq", "ne", "ge", "gt")
36
37
def moz_version_compare(version1, comparator, version2, silent=False,
38
                        verbose=False):
39
    """Return true if the expression version1 comparator version2 is valid,
40
       otherwise false"""
41
    if comparator not in COMPARATORS:
42
        if not silent:
43
            print >> sys.stderr, "E: The comparator " + comparator + \
44
                                 " is not valid. It should one of " + \
45
                                 ", ".join(COMPARATORS) + "."
46
        sys.exit(INVALID_COMPARATOR)
47
48
    if version1.strip() == "" or version2.strip() == "":
49
        if not silent:
50
            print >> sys.stderr, "E: At least one version string is empty."
51
        sys.exit(EMPTY_VERSION_STRING)
52
53
    if verbose:
54
        symbol = {"lt": "<", "le": "<=", "eq": "=", "ne": "!=",
55
                  "ge": ">=", "gt": ">"}
56
        print "I: Comparing %s %s %s." % \
57
              (version1, symbol[comparator], version2)
58
59
    if comparator == "lt":
60
        return compare_versions(version1, version2, verbose) < 0
61
    elif comparator == "le":
62
        return compare_versions(version1, version2, verbose) <= 0
63
    elif comparator == "eq":
64
        return compare_versions(version1, version2, verbose) == 0
65
    elif comparator == "ne":
66
        return compare_versions(version1, version2, verbose) != 0
67
    elif comparator == "ge":
68
        return compare_versions(version1, version2, verbose) >= 0
69
    elif comparator == "gt":
70
        return compare_versions(version1, version2, verbose) > 0
213 by Alexander Sack
- add moz-version-compare helper script and ship it in extra_files; this script
71
218.1.1 by Benjamin Drung
rename moz-version-compare to moz-version. To compare two versions you have
72
73
def usage(output):
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
74
    name = os.path.basename(sys.argv[0])
75
    print >> output, """Usage: %s [options] action
303 by Benjamin Drung
* moz-version:
76
77
Actions:
78
  -c, --compare version1 comparator version2
79
                           compare both Mozilla version numbers
80
                           comparator must be one of %s
81
  -d, --to-deb version     converts Mozilla into a Debian upstream version
82
  -m, --to-moz version     converts Debian into a Mozilla version
83
84
Options:
85
  -h, --help               display this help and exit
86
  -s, --silent             do not print anything and die silent on errors
87
  -v, --verbose            print more information
88
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
89
See %s(1) for more info.""" % (name, ", ".join(COMPARATORS), name)
90
91
92
def main():
93
    try:
94
        long_opts = ["compare", "help", "silent", "to-deb", "to-moz", "verbose"]
95
        opts, args = getopt.gnu_getopt(sys.argv[1:], "cdhmsv", long_opts)
96
    except getopt.GetoptError, e:
97
        # print help information and exit:
98
        print >> sys.stderr, str(e)
99
        usage(sys.stderr)
100
        sys.exit(COMMAND_LINE_SYNTAX_ERROR)
101
102
    actions = set()
103
    silent = False
104
    verbose = False
105
106
    for o, _ in opts:
107
        if o in ("-c", "--compare"):
108
            actions.add("compare")
109
        elif o in ("-d", "--to-deb"):
110
            actions.add("to-deb")
111
        elif o in ("-h", "--help"):
112
            usage(sys.stdout)
113
            sys.exit()
114
        elif o in ("-m", "--to-moz"):
115
            actions.add("to-moz")
116
        elif o in ("-s", "--silent"):
117
            silent = True
118
        elif o in ("-v", "--verbose"):
119
            verbose = True
120
        else:
121
            assert False, "unhandled option"
122
123
    if len(actions) != 1:
124
        if not silent:
125
            print >> sys.stderr, "E: You must specify an action."
126
            usage(sys.stderr)
127
        sys.exit(COMMAND_LINE_SYNTAX_ERROR)
128
129
    action = actions.pop()
130
131
    if action == "compare":
132
        if len(args) != 3:
133
            if not silent:
134
                usage(sys.stderr)
135
            sys.exit(COMMAND_LINE_SYNTAX_ERROR)
136
        if moz_version_compare(args[0], args[1], args[2], silent, verbose):
137
            if verbose:
138
                print "I: Compare expression true."
139
            sys.exit(0)
140
        else:
141
            if verbose:
142
                print "I: Compare expression false."
143
            sys.exit(1)
144
    elif action == "to-deb":
145
        if len(args) != 1:
146
            if not silent:
147
                print >> sys.stderr, "E: The action --to-deb takes exactly " + \
148
                                     "one argument."
149
            sys.exit(COMMAND_LINE_SYNTAX_ERROR)
150
        print convert_moz_to_debian_version(args[0], 0, verbose)
151
    elif action == "to-moz":
152
        if len(args) != 1:
153
            if not silent:
154
                print >> sys.stderr, "E: The action --to-moz takes exactly " + \
155
                                     "one argument."
156
            sys.exit(COMMAND_LINE_SYNTAX_ERROR)
157
        print convert_debian_to_moz_version(args[0], verbose)
218.1.1 by Benjamin Drung
rename moz-version-compare to moz-version. To compare two versions you have
158
213 by Alexander Sack
- add moz-version-compare helper script and ship it in extra_files; this script
159
if __name__ == "__main__":
355 by Benjamin Drung
Convert tabs to spaces in Python scripts and make pylint happier.
160
    main()