| # Copyright 2023 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| """Update the tokenizer database. |
| |
| Note: this is not what most developers think of when they see "tokenizer". For |
| details see https://pigweed.dev/pw_tokenizer/. |
| |
| On its face builders using this recipe could create infinite loops but they |
| won't because the changes created by this recipe won't cause changes in the |
| generated database. |
| """ |
| |
| import re |
| |
| import attr |
| from PB.recipes.pigweed.tokendb_updater import InputProperties, TokenPath |
| |
| DEPS = [ |
| 'fuchsia/auto_roller', |
| 'pigweed/build', |
| 'pigweed/checkout', |
| 'pigweed/environment', |
| 'recipe_engine/file', |
| 'recipe_engine/properties', |
| ] |
| |
| PROPERTIES = InputProperties |
| |
| |
| def RunSteps(api, props): # pylint: disable=invalid-name |
| checkout = api.checkout(props.checkout_options) |
| |
| env = api.environment.init(checkout, props.environment_options) |
| build = api.build.create(checkout.root, props.build_options) |
| |
| with env(): |
| api.build(build) |
| |
| for token_path in props.token_paths: |
| # Assume if generated is empty that the build wrote to the committed |
| # path directly. |
| if token_path.generated: |
| api.file.copy( |
| f'copy <build>/{token_path.generated} ' |
| f'<checkout>/{token_path.committed}', |
| build.root.join(token_path.generated), |
| checkout.root.join(token_path.committed), |
| ) |
| |
| if not props.auto_roller_options.remote: |
| props.auto_roller_options.remote = checkout.options.remote |
| |
| change = api.auto_roller.attempt_roll( |
| props.auto_roller_options, |
| repo_dir=checkout.root, |
| commit_message=f'[tokendb] Update for {checkout.revision()[0:15]}', |
| ) |
| |
| return api.auto_roller.raw_result(change) |
| |
| |
| def GenTests(api): # pylint: disable=invalid-name |
| def properties(dry_run=False, **kwargs): |
| props = InputProperties(**kwargs) |
| props.token_paths.extend( |
| [ |
| TokenPath(generated='gen1.csv', committed='tok1.csv'), |
| TokenPath(generated='gen2.csv', committed='tok2.csv'), |
| ] |
| ) |
| props.checkout_options.CopyFrom(api.checkout.git_options()) |
| props.build_options.CopyFrom( |
| api.build.options(ninja_targets=("gen_tokendb",)) |
| ) |
| props.auto_roller_options.CopyFrom( |
| api.auto_roller.Options(dry_run=dry_run) |
| ) |
| return api.properties(props) |
| |
| yield api.test('simple') + properties() + api.auto_roller.success() |
| |
| yield ( |
| api.test('dry-run') |
| + properties(dry_run=True) |
| + api.auto_roller.dry_run_success() |
| ) |