File: update_api.py

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (141 lines) | stat: -rwxr-xr-x 4,527 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""update_api.py - Update committed Cronet API."""

import argparse
import filecmp
import fileinput
import os
import re
import shutil
import sys
import tempfile

# Filename of dump of current API.
API_FILENAME = os.path.abspath(os.path.join(
    os.path.dirname(__file__), '..', 'android', 'api.txt'))
# Filename of file containing API version number.
API_VERSION_FILENAME = os.path.abspath(os.path.join(
    os.path.dirname(__file__), '..', 'android', 'api_version.txt'))

# Regular expression that catches the beginning of lines that declare classes.
# The first group returned by a match is the class name.
CLASS_RE = re.compile(r'.*class ([^ ]*) .*\{')

# Regular expression that matches a string containing an unnamed class name,
# for example 'Foo$1'.
UNNAMED_CLASS_RE = re.compile(r'.*\$[0-9]')


def generate_api(api_jar, output_filename):
  # Dumps the API in |api_jar| into |outpuf_filename|.

  with open(output_filename, 'w') as output_file:
    output_file.write(
        'DO NOT EDIT THIS FILE, USE update_api.py TO UPDATE IT\n\n')

  # Extract API class files from api_jar.
  temp_dir = tempfile.mkdtemp()
  old_cwd = os.getcwd()
  api_jar_path = os.path.abspath(api_jar)
  os.chdir(temp_dir)
  if os.system('jar xf %s' % api_jar_path):
    print 'ERROR: jar failed on ' + api_jar
    return False
  os.chdir(old_cwd)
  shutil.rmtree(os.path.join(temp_dir, 'META-INF'))

  # Collect names of all API class files
  api_class_files = []
  for root, _, filenames in os.walk(temp_dir):
    api_class_files += [os.path.join(root, f) for f in filenames]
  api_class_files.sort()

  # Dump API class files into |output_filename|
  javap_cmd = ('javap -protected %s >> %s' % (' '.join(api_class_files),
      output_filename)).replace('$', '\\$')
  if os.system(javap_cmd):
    print 'ERROR: javap command failed: ' + javap_cmd
    return False
  shutil.rmtree(temp_dir)

  # Strip out pieces we don't need to compare.
  output_file = fileinput.FileInput(output_filename, inplace=True)
  skip_to_next_class = False
  for line in output_file:
    # Skip 'Compiled from ' lines as they're not part of the API.
    if line.startswith('Compiled from "'):
      continue
    if CLASS_RE.match(line):
      skip_to_next_class = (
          # Skip internal classes, they aren't exposed.
          UNNAMED_CLASS_RE.match(line) or
          # Skip experimental classes, they can be modified.
          'Experimental' in line
      )
    if skip_to_next_class:
      skip_to_next_class = line != '}'
      continue
    sys.stdout.write(line)
  output_file.close()
  return True


def check_up_to_date(api_jar):
  # Returns True if API_FILENAME matches the API exposed by |api_jar|.

  [_, temp_filename] = tempfile.mkstemp()
  if not generate_api(api_jar, temp_filename):
    return False
  ret = filecmp.cmp(API_FILENAME, temp_filename)
  os.remove(temp_filename)
  return ret


def check_api_update(old_api, new_api):
  # Enforce that lines are only added when updating API.
  with open(old_api, 'r') as old_api_file, open(new_api, 'r') as new_api_file:
    for old_line in old_api_file:
      while True:
        new_line = new_api_file.readline()
        if new_line == old_line:
          break
        if not new_line:
          print 'ERROR: This API was modified or removed:'
          print '           ' + old_line
          print '       Cronet API methods and classes cannot be modified.'
          return False
  return True


def main(args):
  parser = argparse.ArgumentParser(description='Update Cronet api.txt.')
  parser.add_argument('--api_jar',
                      help='Path to API jar (i.e. cronet_api.jar)',
                      required=True,
                      metavar='path/to/cronet_api.jar')
  opts = parser.parse_args(args)

  if check_up_to_date(opts.api_jar):
    return True

  [_, temp_filename] = tempfile.mkstemp()
  if (generate_api(opts.api_jar, temp_filename) and
      check_api_update(API_FILENAME, temp_filename)):
    # Update API version number to new version number
    with open(API_VERSION_FILENAME,'r+') as f:
      version = int(f.read())
      f.seek(0)
      f.write(str(version + 1))
    # Update API file to new API
    shutil.move(temp_filename, API_FILENAME)
    return True
  os.remove(temp_filename)
  return False


if __name__ == '__main__':
  sys.exit(0 if main(sys.argv[1:]) else -1)