• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Buildgen package version plugin
15
16This parses the list of targets from the yaml build file, and creates
17a custom version string for each language's package.
18
19"""
20
21import re
22
23LANGUAGES = [
24    'core',
25    'cpp',
26    'csharp',
27    'node',
28    'objc',
29    'php',
30    'python',
31    'ruby',
32]
33
34
35class Version:
36
37    def __init__(self, s):
38        self.tag = None
39        if '-' in s:
40            s, self.tag = s.split('-')
41        self.major, self.minor, self.patch = [int(x) for x in s.split('.')]
42
43    def __str__(self):
44        """Version string in a somewhat idiomatic style for most languages"""
45        s = '%d.%d.%d' % (self.major, self.minor, self.patch)
46        if self.tag:
47            s += '-%s' % self.tag
48        return s
49
50    def pep440(self):
51        """Version string in Python PEP440 style"""
52        s = '%d.%d.%d' % (self.major, self.minor, self.patch)
53        if self.tag:
54            # we need to translate from grpc version tags to pep440 version
55            # tags; this code is likely to be a little ad-hoc
56            if self.tag == 'dev':
57                s += '.dev0'
58            elif len(self.tag) >= 3 and self.tag[0:3] == 'pre':
59                s += 'rc%d' % int(self.tag[3:])
60            else:
61                raise Exception(
62                    'Don\'t know how to translate version tag "%s" to pep440' %
63                    self.tag)
64        return s
65
66    def ruby(self):
67        """Version string in Ruby style"""
68        if self.tag:
69            return '%d.%d.%d.%s' % (self.major, self.minor, self.patch,
70                                    self.tag)
71        else:
72            return '%d.%d.%d' % (self.major, self.minor, self.patch)
73
74    def php(self):
75        """Version string for PHP PECL package"""
76        s = '%d.%d.%d' % (self.major, self.minor, self.patch)
77        if self.tag:
78            if self.tag == 'dev':
79                s += 'dev'
80            elif len(self.tag) >= 3 and self.tag[0:3] == 'pre':
81                s += 'RC%d' % int(self.tag[3:])
82            else:
83                raise Exception(
84                    'Don\'t know how to translate version tag "%s" to PECL version'
85                    % self.tag)
86        return s
87
88    def php_stability(self):
89        """stability string for PHP PECL package.xml file"""
90        if self.tag:
91            return 'beta'
92        else:
93            return 'stable'
94
95    def php_composer(self):
96        """Version string for PHP Composer package"""
97        return '%d.%d.%d' % (self.major, self.minor, self.patch)
98
99
100def mako_plugin(dictionary):
101    """Expand version numbers:
102     - for each language, ensure there's a language_version tag in
103       settings (defaulting to the master version tag)
104     - expand version strings to major, minor, patch, and tag
105  """
106
107    settings = dictionary['settings']
108    master_version = Version(settings['version'])
109    settings['version'] = master_version
110    for language in LANGUAGES:
111        version_tag = '%s_version' % language
112        if version_tag in settings:
113            settings[version_tag] = Version(settings[version_tag])
114        else:
115            settings[version_tag] = master_version
116