brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Bazel Authors. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Utilities for the @bazel_tools//tools/python package. |
| 16 | |
| 17 | This file does not access any Python-rules-specific logic, and is therefore |
| 18 | less likely to be broken by Python-related changes. That in turn means this |
| 19 | file is less likely to cause bootstrapping issues. |
| 20 | """ |
| 21 | |
| 22 | def _expand_pyversion_template_impl(ctx): |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 23 | for output, version, strict in [ |
| 24 | (ctx.outputs.out2, "2", "1"), |
| 25 | (ctx.outputs.out3, "3", "1"), |
| 26 | (ctx.outputs.out2_nonstrict, "2", "0"), |
| 27 | (ctx.outputs.out3_nonstrict, "3", "0"), |
| 28 | ]: |
| 29 | if output: |
| 30 | ctx.actions.expand_template( |
| 31 | template = ctx.file.template, |
| 32 | output = output, |
| 33 | substitutions = { |
| 34 | "%VERSION%": version, |
| 35 | "%STRICT%": strict, |
| 36 | }, |
| 37 | is_executable = True, |
| 38 | ) |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 39 | |
| 40 | expand_pyversion_template = rule( |
| 41 | implementation = _expand_pyversion_template_impl, |
| 42 | attrs = { |
| 43 | "template": attr.label( |
| 44 | allow_single_file = True, |
| 45 | doc = "The input template file.", |
| 46 | ), |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 47 | "out2": attr.output(doc = "The Python 2 strict wrapper."), |
| 48 | "out3": attr.output(doc = "The Python 3 strict wrapper."), |
| 49 | "out2_nonstrict": attr.output( |
| 50 | doc = "The Python 2 non-strict wrapper.", |
| 51 | ), |
| 52 | "out3_nonstrict": attr.output( |
| 53 | doc = "The Python 3 non-strict wrapper.", |
| 54 | ), |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 55 | }, |
| 56 | doc = """\ |
brandjon | 052167e | 2019-06-04 16:04:06 -0700 | [diff] [blame] | 57 | Given the pywrapper template file, generates expansions for both versions of |
| 58 | Python and both levels of strictness.""", |
brandjon | 2299445 | 2019-03-26 13:37:31 -0700 | [diff] [blame] | 59 | ) |