Commit e5dd87c4 authored by Chris Mumford's avatar Chris Mumford Committed by Commit Bot

sqlite: Corrected formatting of Python scripts.

Corrected indent to be four spaces IAW the style guide. Doing this
makes all Python scripts uniformaly formatted in //third_party/sqlite/scripts.

Bug: None
Change-Id: I476654c330bf957e0ad65d46c44df53e1eacdf2d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2150236Reviewed-by: default avatarDarwin Huang <huangdarwin@chromium.org>
Commit-Queue: Chris Mumford <cmumford@google.com>
Cr-Commit-Position: refs/heads/master@{#759702}
parent e13e0b1b
# TODO(cmumford): Format older files in a future CL.
extract_sqlite_api.py
extract_sqlite_api_unittest.py
......@@ -3,7 +3,6 @@
# Copyright 2018 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.
'''
Parses SQLite source code and produces renaming macros for its exported symbols.
......@@ -18,10 +17,12 @@ For example, the following renaming macro is produced for sqlite3_initialize().
import re
import sys
class ExtractError(ValueError):
def __init__(self, message):
self.message = message
def ExtractLineTuples(string):
'''Returns a list of lines, with start/end whitespace stripped.
......@@ -31,14 +32,15 @@ def ExtractLineTuples(string):
stripped_lines = [line.strip() for line in raw_lines]
return list(enumerate(stripped_lines, start=1))
def ExtractPreprocessorDirectives(lines):
'''Extracts preprocessor directives from lines of C code.
Each input line should be a tuple of (line number, string).
Returns a list of preprocessor directives, and a list of C code lines with the
preprocessor directives removed. The returned code lines are a subset of the
input tuples.
Returns a list of preprocessor directives, and a list of C code lines with
the preprocessor directives removed. The returned code lines are a subset
of the input tuples.
'''
code_lines = []
directives = []
......@@ -71,11 +73,12 @@ def ExtractPreprocessorDirectives(lines):
# Regular expression used to parse a macro definition.
DEFINITION_RE = re.compile(r'^\#\s*define\s+(\w+)(\s|$)')
def ExtractDefineMacroName(line):
'''Extracts the macro name from a non-function preprocessor definition.
Returns None if the preprocessor line is not a preprocessor macro definition.
Macro functions are not considered preprocessor definitions.
Returns None if the preprocessor line is not a preprocessor macro
definition. Macro functions are not considered preprocessor definitions.
'''
match = DEFINITION_RE.match(line)
if match is None:
......@@ -86,12 +89,15 @@ def ExtractDefineMacroName(line):
# Matches C++-style // single-line comments.
SINGLE_LINE_COMMENT_RE = re.compile(r'//.*$')
# Matches C-style /* multi-line comments */.
MULTI_LINE_COMMENT_RE = re.compile(r'/\*.*?\*/', flags=re.MULTILINE|re.DOTALL)
MULTI_LINE_COMMENT_RE = re.compile(
r'/\*.*?\*/', flags=re.MULTILINE | re.DOTALL)
def RemoveLineComments(line):
'''Returns the given C code line with comments removed.
This handles both C-style /* comments */ and C++-style // comments, but cannot
tackle C-style comments that extend over multiple lines.
This handles both C-style /* comments */ and C++-style // comments, but
cannot tackle C-style comments that extend over multiple lines.
'''
return SINGLE_LINE_COMMENT_RE.sub('', MULTI_LINE_COMMENT_RE.sub('', line))
......@@ -118,6 +124,7 @@ def RemoveComments(code_tuples):
# Splits a line of C code into statement pieces.
STATEMENT_BREAK_RE = re.compile(r'[;{}]')
def ToStatementTuples(code_tuples):
'''Converts C code lines into statements.
......@@ -136,8 +143,8 @@ def ToStatementTuples(code_tuples):
for piece in pieces[:-1]: # The last piece is an unfinished statement.
if current_statement != '':
current_statement = current_statement + '\n' + piece
statements.append(
(current_start, line_number, current_statement.strip()))
statements.append((current_start, line_number,
current_statement.strip()))
current_statement = ''
else:
statements.append((line_number, line_number, piece.strip()))
......@@ -159,12 +166,7 @@ WHITESPACE_RE = re.compile(r'\s+')
# them before incorporating them into exported symbols. We can avoid matching
# curly braces because we do not support enum, struct, or union, and we only
# need to consider declarations involving typedef names and primitive types.
UNSUPPORTED_KEYWORDS = set([
'enum',
'struct',
'union',
'typedef'
])
UNSUPPORTED_KEYWORDS = set(['enum', 'struct', 'union', 'typedef'])
# Type qualifiers that we can skip over.
#
......@@ -198,11 +200,12 @@ COMPOSITE_TYPE_SPECIFIERS = set([
# Matches an identifier.
IDENTIFIER_RE = re.compile(r'^[a-zA-Z_0-9]+$')
def ExtractApiExport(macro_names, api_export_macro, statement):
'''Extracts the symbol name from a statement exporting a function.
Returns None if the statement does not export a symbol. Throws ExtractError if
the parser cannot understand the statement.
Returns None if the statement does not export a symbol. Throws ExtractError
if the parser cannot understand the statement.
'''
# See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf, section 6.7
# for how to parse C declarations. Note that a declaration is a number of
......@@ -253,7 +256,8 @@ def ExtractApiExport(macro_names, api_export_macro, statement):
if word in COMPOSITE_TYPE_SPECIFIERS:
if seen_simple_type:
raise ExtractError('Mixed simple (struct_name) and composite (int) types')
raise ExtractError(
'Mixed simple (struct_name) and composite (int) types')
seen_composite_type = True
continue
......@@ -314,9 +318,11 @@ def ProcessSource(api_export_macro, symbol_prefix, header_line, footer_line,
symbol_name = ExtractApiExport(macro_names, api_export_macro, line)
if symbol_name:
output_lines.append(
ExportedSymbolLine(symbol_prefix, symbol_name, statement_tuple))
ExportedSymbolLine(symbol_prefix, symbol_name,
statement_tuple))
except ExtractError as exception:
output_lines.append(ExportedExceptionLine(exception, statement_tuple))
output_lines.append(
ExportedExceptionLine(exception, statement_tuple))
output_lines.sort()
return [header_line] + output_lines + [footer_line]
......@@ -334,7 +340,8 @@ def ProcessSourceFile(api_export_macro, symbol_prefix, header_line,
with open(output_file, 'w') as f:
f.write('\n'.join(output_lines))
header_line='''// Copyright 2018 The Chromium Authors. All rights reserved.
header_line = '''// Copyright 2018 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.
......@@ -344,11 +351,15 @@ header_line='''// Copyright 2018 The Chromium Authors. All rights reserved.
#define THIRD_PARTY_SQLITE_AMALGAMATION_RENAME_EXPORTS_H_
'''
footer_line ='''
footer_line = '''
#endif // THIRD_PARTY_SQLITE_AMALGAMATION_RENAME_EXPORTS_H_
'''
if __name__ == '__main__':
ProcessSourceFile(api_export_macro='SQLITE_API', symbol_prefix='chrome_',
header_line=header_line, footer_line=footer_line,
input_file=sys.argv[1], output_file=sys.argv[2])
ProcessSourceFile(
api_export_macro='SQLITE_API',
symbol_prefix='chrome_',
header_line=header_line,
footer_line=footer_line,
input_file=sys.argv[1],
output_file=sys.argv[2])
......@@ -15,12 +15,15 @@ import sys
import tempfile
import unittest
class ExtractSqliteApiUnittest(unittest.TestCase):
def setUp(self):
self.test_root = tempfile.mkdtemp()
source_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'extract_sqlite_api.py')
self.extractor = SourceFileLoader('extract_api', source_path).load_module()
os.path.dirname(os.path.realpath(__file__)),
'extract_sqlite_api.py')
self.extractor = SourceFileLoader('extract_api',
source_path).load_module()
def tearDown(self):
if self.test_root:
......@@ -29,13 +32,13 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
def testExtractLineTuples(self):
golden = [(1, 'Line1'), (2, ''), (3, 'Line 2'), (4, 'Line3'), (5, '')]
text_with_newline = "Line1\n\nLine 2 \nLine3\n"
self.assertEqual(self.extractor.ExtractLineTuples(text_with_newline),
golden)
self.assertEqual(
self.extractor.ExtractLineTuples(text_with_newline), golden)
golden = [(1, 'Line1'), (2, ''), (3, 'Line 2'), (4, 'Line3')]
text_without_newline = "Line1\n\nLine 2 \nLine3"
self.assertEqual(self.extractor.ExtractLineTuples(text_without_newline),
golden)
self.assertEqual(
self.extractor.ExtractLineTuples(text_without_newline), golden)
def testExtractPreprocessorDirectives(self):
lines = [
......@@ -50,7 +53,8 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
(9, 'void code() { }'),
]
directives, code_lines = self.extractor.ExtractPreprocessorDirectives(lines)
directives, code_lines = self.extractor.ExtractPreprocessorDirectives(
lines)
self.assertEqual(directives, [
'#define DIRECTIVE 1',
'#define MULTILINE \nMORE_MULTILINE_DIRECTIVE\nEND_MULTILINE_DIRECTIVE',
......@@ -65,43 +69,42 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
def testExtractDefineMacroName(self):
self.assertEqual(
'SQLITE_API', self.extractor.ExtractDefineMacroName(
'#define SQLITE_API 1'))
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#define SQLITE_API 1'))
self.assertEqual(
'SQLITE_API', self.extractor.ExtractDefineMacroName(
'#define SQLITE_API'))
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#define SQLITE_API'))
self.assertEqual(
'SQLITE_API', self.extractor.ExtractDefineMacroName(
'#define SQLITE_API\n1'))
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#define SQLITE_API\n1'))
self.assertEqual(
'SQLITE_API', self.extractor.ExtractDefineMacroName(
'SQLITE_API',
self.extractor.ExtractDefineMacroName(
'# define SQLITE_API 1'))
self.assertEqual(
'SQLITE_API', self.extractor.ExtractDefineMacroName(
'#\tdefine\tSQLITE_API\t1'))
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#\tdefine\tSQLITE_API\t1'))
self.assertEqual(
None, self.extractor.ExtractDefineMacroName(
' #define SQLITE_API 1'))
None,
self.extractor.ExtractDefineMacroName(' #define SQLITE_API 1'))
self.assertEqual(
None, self.extractor.ExtractDefineMacroName(
' #define SQLITE_API() 1'))
None,
self.extractor.ExtractDefineMacroName(' #define SQLITE_API() 1'))
self.assertEqual(None, self.extractor.ExtractDefineMacroName(''))
def testRemoveLineComments(self):
self.assertEqual(
'word;', self.extractor.RemoveLineComments('word;'))
self.assertEqual(
'', self.extractor.RemoveLineComments(''))
self.assertEqual(
'', self.extractor.RemoveLineComments('// comment'))
self.assertEqual(
'', self.extractor.RemoveLineComments('/* comment */'))
self.assertEqual(
'word;', self.extractor.RemoveLineComments('wo/*comment*/rd;'))
self.assertEqual('word;', self.extractor.RemoveLineComments('word;'))
self.assertEqual('', self.extractor.RemoveLineComments(''))
self.assertEqual('', self.extractor.RemoveLineComments('// comment'))
self.assertEqual('',
self.extractor.RemoveLineComments('/* comment */'))
self.assertEqual('word;',
self.extractor.RemoveLineComments('wo/*comment*/rd;'))
self.assertEqual(
'word;*/', self.extractor.RemoveLineComments('wo/*comment*/rd;*/'))
self.assertEqual(
'word;*/', self.extractor.RemoveLineComments('wo/*/*comment*/rd;*/'))
'word;*/',
self.extractor.RemoveLineComments('wo/*/*comment*/rd;*/'))
self.assertEqual(
'word;', self.extractor.RemoveLineComments('wo/*comm//ent*/rd;'))
......@@ -112,12 +115,15 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
(3, '/**'),
(4, 'Spec text'),
(5, '**/ spec_code();'),
(6, 'late_code(); /* with comment */ more_late_code(); /* late comment'),
(6,
'late_code(); /* with comment */ more_late_code(); /* late comment'
),
(7, 'ends here // C++ trap */ code(); // /* C trap'),
(8, 'last_code();'),
]
self.assertEqual(self.extractor.RemoveComments(lines), [
self.assertEqual(
self.extractor.RemoveComments(lines), [
(1, 'code();'),
(2, 'more_code(); more_code();'),
(3, ''),
......@@ -128,19 +134,14 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
])
def testToStatementTuples(self):
lines = [
(1, 'void function();'),
(2, 'int main('),
lines = [(1, 'void function();'), (2, 'int main('),
(3, ' int argc, char* argv) {'),
(4, ' statement1; statement2;'),
(5, '}'),
(6, 'stat'),
(7, 'ement4; statement5; sta'),
(8, 'tem'),
(9, 'ent6; statement7;')
]
(4, ' statement1; statement2;'), (5, '}'), (6, 'stat'),
(7, 'ement4; statement5; sta'), (8, 'tem'),
(9, 'ent6; statement7;')]
self.assertEqual(self.extractor.ToStatementTuples(lines), [
self.assertEqual(
self.extractor.ToStatementTuples(lines), [
(1, 1, 'void function()'),
(2, 3, 'int main(\n int argc, char* argv)'),
(4, 4, 'statement1'),
......@@ -155,8 +156,8 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
def testExtractApiExport(self):
self.assertEqual(
'sqlite3_init',
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API void sqlite3_init()'))
self.extractor.ExtractApiExport(set(), 'SQLITE_API',
'SQLITE_API void sqlite3_init()'))
self.assertEqual(
'sqlite3_sleep',
self.extractor.ExtractApiExport(
......@@ -170,18 +171,21 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
'sqlite3rbu_temp_size',
self.extractor.ExtractApiExport(
set(), 'SQLITE_API',
'SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu)'))
'SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu)'
))
self.assertEqual(
'sqlite3_expired',
self.extractor.ExtractApiExport(
set(['SQLITE_DEPRECATED']), 'SQLITE_API',
'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*)'))
'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*)'
))
# SQLite's header actually #defines double (in some cases).
self.assertEqual(
'sqlite3_column_double',
self.extractor.ExtractApiExport(
set(['double']), 'SQLITE_API',
'SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol)'))
'SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol)'
))
self.assertEqual(
'sqlite3_temp_directory',
self.extractor.ExtractApiExport(
......@@ -201,7 +205,8 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
with self.assertRaisesRegex(self.extractor.ExtractError,
'Mixed simple .* and composite'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API void int sqlite3_sleep(int ms)')
set(), 'SQLITE_API',
'SQLITE_API void int sqlite3_sleep(int ms)')
with self.assertRaisesRegex(self.extractor.ExtractError,
'Unsupported keyword struct'):
self.extractor.ExtractApiExport(
......@@ -280,11 +285,11 @@ class ExtractSqliteApiUnittest(unittest.TestCase):
output_file = os.path.join(self.test_root, 'macros.h')
with open(input_file, 'w') as f:
f.write(file_content)
self.extractor.ProcessSourceFile(
'SQLITE_API', 'chrome_', '// Header', '// Footer', input_file,
output_file)
self.extractor.ProcessSourceFile('SQLITE_API', 'chrome_', '// Header',
'// Footer', input_file, output_file)
with open(output_file, 'r') as f:
self.assertEqual(f.read(), golden_output)
if __name__ == '__main__':
unittest.main()
......@@ -115,7 +115,7 @@ def _read_flags(file_name, param_name):
config_globals = dict()
with open(file_name) as input_file:
code = compile(input_file.read(), file_name, 'exec')
exec(code, config_globals)
exec (code, config_globals)
return config_globals[param_name]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment