14
14
# See the License for the specific language governing permissions and
15
15
# limitations under the License.
16
16
17
- # Lint as: python2
18
17
"""Update the version numbers in cmake/firebase_unity_version.cmake.
19
18
20
19
To use locally, make sure call following installation first.
24
23
python scripts/update_versions.py --unity_sdk_version=<version number>
25
24
"""
26
25
import os
26
+ import re
27
+ import requests
28
+ from xml .etree import ElementTree
27
29
28
30
from github import Github
29
31
from absl import app
@@ -50,6 +52,126 @@ def get_ios_pod_version_from_cpp():
50
52
result = line .split ()
51
53
return result [- 1 ].strip ("\' " )
52
54
55
+ ########## Android versions update #############################
56
+
57
+ # Android gMaven repository from where we scan available Android packages
58
+ # and their versions
59
+ GMAVEN_MASTER_INDEX = "https://dl.google.com/dl/android/maven2/master-index.xml"
60
+ GMAVEN_GROUP_INDEX = "https://dl.google.com/dl/android/maven2/{0}/group-index.xml"
61
+
62
+ # Regex to match versions with just digits (ignoring things like -alpha, -beta)
63
+ RE_NON_EXPERIMENTAL_VERSION = re .compile ('[0-9.]+$' )
64
+
65
+ def get_latest_maven_versions (ignore_packages = None , allow_experimental = False ):
66
+ """Gets latest versions of android packages from google Maven repository.
67
+
68
+ Args:
69
+ ignore_packages (list[str], optional): Case insensitive list of substrings
70
+ If any of these substrings are present in the android package name, it
71
+ will not be updated.
72
+ Eg: ['Foo', 'bar'] will ignore all android packages that have 'foo' or
73
+ 'bar' in their name. For example, 'my.android.foo.package',
74
+ 'myfoo.android.package'
75
+ allow_experimental (bool): Allow experimental versions.
76
+ Eg: 1.2.3-alpha, 1.2.3-beta, 1.2.3-rc
77
+
78
+ Returns:
79
+ dict: Dictionary of the form {<str>: list(str)} containing full name of
80
+ android package as the key and its list of versions as value.
81
+ """
82
+ if ignore_packages is None :
83
+ ignore_packages = []
84
+
85
+ latest_versions = {}
86
+ response = requests .get (GMAVEN_MASTER_INDEX )
87
+ index_xml = ElementTree .fromstring (response .content )
88
+ for index_child in index_xml :
89
+ group_name = index_child .tag
90
+ group_path = group_name .replace ('.' , '/' )
91
+ response = requests .get (GMAVEN_GROUP_INDEX .format (group_path ))
92
+ group_xml = ElementTree .fromstring (response .content )
93
+ for group_child in group_xml :
94
+ package_name = group_child .tag .replace ('-' , '_' )
95
+ package_full_name = group_name + "." + package_name
96
+ if any (ignore_package .lower ().replace ('-' , '_' ) in package_full_name .lower ()
97
+ for ignore_package in ignore_packages ):
98
+ continue
99
+ versions = group_child .attrib ['versions' ].split (',' )
100
+ if not allow_experimental :
101
+ versions = [version for version in versions
102
+ if re .match (RE_NON_EXPERIMENTAL_VERSION , version ) or '-cppsdk' in version ]
103
+ if versions :
104
+ latest_valid_version = versions [- 1 ]
105
+ latest_versions [package_full_name ] = latest_valid_version
106
+ return latest_versions
107
+
108
+ # Regex to match lines like:
109
+ # 'com.google.firebase:firebase-auth:1.2.3'
110
+ RE_GENERIC_DEPENDENCY_MODULE = re .compile (
111
+ r"(?P<quote>[\'\"])(?P<pkg>[a-zA-Z0-9._-]+:[a-zA-Z0-9._-]+):([0-9.]+)[\'\"]"
112
+ )
113
+
114
+ def modify_dependency_file (dependency_filepath , version_map ):
115
+ """Modify a dependency file to reference the correct module versions.
116
+
117
+ Looks for lines like: 'com.google.firebase:firebase-auth:1.2.3'
118
+ for modules matching the ones in the version map, and modifies them in-place.
119
+
120
+ Args:
121
+ dependency_filename: Relative path to the dependency file to edit.
122
+ version_map: Dictionary of packages to version numbers, e.g. {
123
+ 'com.google.firebase.firebase_auth': '15.0.0' }
124
+ """
125
+ global logfile_lines
126
+ logging .debug ('Reading dependency file: {0}' .format (dependency_filepath ))
127
+ lines = None
128
+ with open (dependency_filepath , "r" ) as dependency_file :
129
+ lines = dependency_file .readlines ()
130
+ if not lines :
131
+ logging .fatal ('Update failed. ' +
132
+ 'Could not read contents from file {0}.' .format (dependency_filepath ))
133
+
134
+ output_lines = []
135
+
136
+ # Replacement function, look up the version number of the given pkg.
137
+ def replace_dependency (m ):
138
+ if not m .group ('pkg' ):
139
+ return m .group (0 )
140
+ pkg = m .group ('pkg' ).replace ('-' , '_' ).replace (':' , '.' )
141
+ if pkg not in version_map :
142
+ return m .group (0 )
143
+ quote_type = m .group ('quote' )
144
+ if not quote_type :
145
+ quote_type = "'"
146
+ return '%s%s:%s%s' % (quote_type , m .group ('pkg' ), version_map [pkg ],
147
+ quote_type )
148
+
149
+ substituted_pairs = []
150
+ to_update = False
151
+ for line in lines :
152
+ substituted_line = re .sub (RE_GENERIC_DEPENDENCY_MODULE , replace_dependency ,
153
+ line )
154
+ output_lines .append (substituted_line )
155
+ if substituted_line != line :
156
+ substituted_pairs .append ((line , substituted_line ))
157
+ to_update = True
158
+ log_match = re .search (RE_GENERIC_DEPENDENCY_MODULE , line )
159
+ log_pkg = log_match .group ('pkg' ).replace ('-' , '_' ).replace (':' , '.' )
160
+
161
+ if to_update :
162
+ print ('Updating contents of {0}' .format (dependency_filepath ))
163
+ for original , substituted in substituted_pairs :
164
+ print ('(-) ' + original + '(+) ' + substituted )
165
+ with open (dependency_filepath , 'w' ) as dependency_file :
166
+ dependency_file .writelines (output_lines )
167
+ print ()
168
+
169
+ def update_android_deps ():
170
+ deps_path = os .path .join (os .getcwd (), "cmake" , "android_dependencies.cmake" )
171
+ latest_android_versions_map = get_latest_maven_versions ()
172
+ modify_dependency_file (deps_path , latest_android_versions_map )
173
+
174
+
53
175
def update_unity_version (unity_sdk_version ):
54
176
version_cmake_path = os .path .join (os .getcwd (), "cmake" , "firebase_unity_version.cmake" )
55
177
replacement = ""
@@ -86,6 +208,7 @@ def main(argv):
86
208
raise app .UsageError ('Please set unity_sdk_version.' )
87
209
88
210
update_unity_version (FLAGS .unity_sdk_version )
211
+ update_android_deps ()
89
212
90
213
if __name__ == '__main__' :
91
214
app .run (main )
0 commit comments