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, version_str, override_major=None): 38 self.tag = None 39 if '-' in version_str: 40 version_str, self.tag = version_str.split('-') 41 self.major, self.minor, self.patch = [ 42 int(x) for x in version_str.split('.') 43 ] 44 if override_major: 45 self.major = override_major 46 47 def __str__(self): 48 """Version string in a somewhat idiomatic style for most languages""" 49 version_str = '%d.%d.%d' % (self.major, self.minor, self.patch) 50 if self.tag: 51 version_str += '-%s' % self.tag 52 return version_str 53 54 def pep440(self): 55 """Version string in Python PEP440 style""" 56 s = '%d.%d.%d' % (self.major, self.minor, self.patch) 57 if self.tag: 58 # we need to translate from grpc version tags to pep440 version 59 # tags; this code is likely to be a little ad-hoc 60 if self.tag == 'dev': 61 s += '.dev0' 62 elif len(self.tag) >= 3 and self.tag[0:3] == 'pre': 63 s += 'rc%d' % int(self.tag[3:]) 64 else: 65 raise Exception( 66 'Don\'t know how to translate version tag "%s" to pep440' % 67 self.tag) 68 return s 69 70 def ruby(self): 71 """Version string in Ruby style""" 72 if self.tag: 73 return '%d.%d.%d.%s' % (self.major, self.minor, self.patch, 74 self.tag) 75 else: 76 return '%d.%d.%d' % (self.major, self.minor, self.patch) 77 78 def php(self): 79 """Version string for PHP PECL package""" 80 s = '%d.%d.%d' % (self.major, self.minor, self.patch) 81 if self.tag: 82 if self.tag == 'dev': 83 s += 'dev' 84 elif len(self.tag) >= 3 and self.tag[0:3] == 'pre': 85 s += 'RC%d' % int(self.tag[3:]) 86 else: 87 raise Exception( 88 'Don\'t know how to translate version tag "%s" to PECL version' 89 % self.tag) 90 return s 91 92 def php_stability(self): 93 """stability string for PHP PECL package.xml file""" 94 if self.tag: 95 return 'beta' 96 else: 97 return 'stable' 98 99 def php_composer(self): 100 """Version string for PHP Composer package""" 101 return '%d.%d.%d' % (self.major, self.minor, self.patch) 102 103 def php_current_version(self): 104 return '7.2' 105 106 def php_debian_version(self): 107 return 'stretch' 108 109 110def mako_plugin(dictionary): 111 """Expand version numbers: 112 - for each language, ensure there's a language_version tag in 113 settings (defaulting to the master version tag) 114 - expand version strings to major, minor, patch, and tag 115 """ 116 117 settings = dictionary['settings'] 118 version_str = settings['version'] 119 master_version = Version(version_str) 120 settings['version'] = master_version 121 for language in LANGUAGES: 122 version_tag = '%s_version' % language 123 override_major = settings.get('%s_major_version' % language, None) 124 if version_tag in settings: 125 settings[version_tag] = Version(settings[version_tag], 126 override_major=override_major) 127 else: 128 settings[version_tag] = Version(version_str, 129 override_major=override_major) 130 settings['protobuf_major_minor_version'] = ('.'.join( 131 settings['protobuf_version'].split('.')[:2])) 132