1ffe3c632Sopenharmony_ci#!/usr/bin/env python
2ffe3c632Sopenharmony_ci"""Compatibility tests between last released and the current version.
3ffe3c632Sopenharmony_ci
4ffe3c632Sopenharmony_ciUsage: ./update_compatibility_version.py <MAJOR>.<MINOR>.<MICRO> [<RC version>]
5ffe3c632Sopenharmony_ciExample: ./update_compatibility_version.py 3.7.1
6ffe3c632Sopenharmony_ci"""
7ffe3c632Sopenharmony_ci
8ffe3c632Sopenharmony_cifrom __future__ import print_function
9ffe3c632Sopenharmony_ciimport re
10ffe3c632Sopenharmony_ciimport sys
11ffe3c632Sopenharmony_ci
12ffe3c632Sopenharmony_ciif len(sys.argv) < 2 or len(sys.argv) > 3:
13ffe3c632Sopenharmony_ci  print("""
14ffe3c632Sopenharmony_ci[ERROR] Please specify a version.
15ffe3c632Sopenharmony_ci
16ffe3c632Sopenharmony_ci./update_compatibility_version.py.py <MAJOR>.<MINOR>.<MICRO> [<RC version>]
17ffe3c632Sopenharmony_ci
18ffe3c632Sopenharmony_ciExample:
19ffe3c632Sopenharmony_ci./update_compatibility_version.py.py 3.7.1 2
20ffe3c632Sopenharmony_ci""")
21ffe3c632Sopenharmony_ci  exit(1)
22ffe3c632Sopenharmony_ci
23ffe3c632Sopenharmony_ciNEW_VERSION = sys.argv[1]
24ffe3c632Sopenharmony_ciNEW_VERSION_INFO = NEW_VERSION.split('.')
25ffe3c632Sopenharmony_ciif len(NEW_VERSION_INFO) != 3:
26ffe3c632Sopenharmony_ci  print("""
27ffe3c632Sopenharmony_ci[ERROR] Version must be in the format <MAJOR>.<MINOR>.<MICRO>
28ffe3c632Sopenharmony_ci
29ffe3c632Sopenharmony_ciExample:
30ffe3c632Sopenharmony_ci./update_compatibility_version.py.py 3.7.3
31ffe3c632Sopenharmony_ci""")
32ffe3c632Sopenharmony_ci  exit(1)
33ffe3c632Sopenharmony_ci
34ffe3c632Sopenharmony_ciif len(sys.argv) > 2:
35ffe3c632Sopenharmony_ci  RC_VERSION = int(sys.argv[2])
36ffe3c632Sopenharmony_ci  # Do not update compatibility versions for rc release
37ffe3c632Sopenharmony_ci  if RC_VERSION != 0:
38ffe3c632Sopenharmony_ci    exit(0)
39ffe3c632Sopenharmony_ci
40ffe3c632Sopenharmony_ci
41ffe3c632Sopenharmony_cidef RewriteTextFile(filename, line_rewriter):
42ffe3c632Sopenharmony_ci  lines = open(filename, 'r').readlines()
43ffe3c632Sopenharmony_ci  updated_lines = []
44ffe3c632Sopenharmony_ci  for line in lines:
45ffe3c632Sopenharmony_ci    updated_lines.append(line_rewriter(line))
46ffe3c632Sopenharmony_ci  if lines == updated_lines:
47ffe3c632Sopenharmony_ci    print('%s was not updated. Please double check.' % filename)
48ffe3c632Sopenharmony_ci  f = open(filename, 'w')
49ffe3c632Sopenharmony_ci  f.write(''.join(updated_lines))
50ffe3c632Sopenharmony_ci  f.close()
51ffe3c632Sopenharmony_ci
52ffe3c632Sopenharmony_ci
53ffe3c632Sopenharmony_cidef ReplaceVersion(line):
54ffe3c632Sopenharmony_ci  return re.sub(r'LAST_RELEASED=.*$', 'LAST_RELEASED=%s' % NEW_VERSION, line)
55ffe3c632Sopenharmony_ci
56ffe3c632Sopenharmony_ciRewriteTextFile('tests.sh', ReplaceVersion)
57