This package allows for the reading and writing of Android.bp files in Python.

Quick Start

from bp import parser, writer

Parse a file

ast = parser.parse(“path/to/Android.bp”)

... perform inspections or modifications on the AST ...

Write back to a string

output = writer.write(ast)

parser.py

It aims to read in all content from a bp file, including comments, into an Abstract Syntax Tree (AST) that later allows for writing a semantically identical file back out. Expressions and comments are captured in such a way that they can be recreated. “a” + “b” is not abbreviated to “ab”, (though this would fit the definition of being semantically identical). The AST allows for further processing, such as lint checks or information retrieval.

The AST does not aim to allow a byte-for-byte identical file to be recreated. In particular, information about indentation and blank lines may be lost when parsing. This is because SystemUI (and Android more generally) has style opinions about these matters that render capturing this information irrelevant.

Effort is made to keep comments aligned*. This means information about a comment's column number is captured.

The tokenize method and Parser class do the main work, though the convenience methods parse and parse_multiple are provided. parse takes in a string path to a bp file, and parse_multiple takes a list of such strings. parse returns a list of AST objects, roughly one for each unique item in the input (comments, blank lines, modules, and variables). parse_multiple returns a dict, mapping input file names to lists of AST objects.

The tokenize function takes in a string - the contents of a bp file - and breaks it down into chunks that the Parser class can understand. This always includes the kind of token - i.e. a string, or a comment marker, or an identifier - and a value indicating the text of the token. For comment tokens, it also contains boolean values indicating if there is other content before or after the comment. Lastly, it will contain the column number that the token starts on.

The Parser class takes the output of tokenize to its constructor. When its parse method is called, it will iterate through the tokens and construct an AST. It returns the AST, or it can be later retrieved through its results property. The Parser class is stateful - multiple calls to parse are idempotent and the results are cached such that no further work is done.

The returned results are a python list of AST top-level objects: BlockComment, EndComment, BlankLine, Variable, VariableAppend, and Module, in the order that they occur in the file. Note that multiple blank lines may be compressed into a single BlankLine.

Variable and VariableAppend may further contain Addition objects, as well as any of the available types: String, Integer, Bool, List, or Map.

Addition is a python list of primitives and variables.

Module and Map are python lists of Property objects, with Module having the additional constraint that it is initialized with a module_type.

Property objects are key-value pairs used in Module and Map.

Property and any of the Primitive types (String, Integer, Bool) additionally contain optional Comment properties for any comments that come directly before them, directly after them, or after the comma that trails them. This allows reconstruction of lines such as:

java_library {
    /* pre key */ name /* post key */: /* pre value */ "a" /* post value */ + "b", // post comma
}

* Not yet fully implemented.

writer.py

The main entry point for this file is write. It takes the AST returned from Parser and turns it into a string that can be saved to a file to create a bp file. It does no linting or correctness checks. The only formatting it performs is ensuring that Module and Map properties are properly indented. Whitespace around various symbols, such as key-value pairs, assignment, addition, and comments is also considered.

locate.py

Utility methods for finding bp files. It currently contains a single function, find_bp_files, that takes in one or more string paths as arguments, and walks through them to find any and all files called “Android.bp”. It returns a list of absolute paths to the files it found, or an empty list if none were found.