Package: pazel
pazel - generate Bazel BUILD files for PythonRequirementspazelNo requirements. Tested on Python 2.7 and 3.6 on Ubuntu 16.04 and macOS High Sierra.BazelTested on Bazel 0.11.1. All recent versions are expected to work.Installation> git clone https://github.com/tuomasr/pazel.git
> cd pazel
> python setup.py installUsageNOTE:pazeloverwrites any existing BUILD files. Please use version control or have backups of
your current BUILD files before usingpazel.Default usage with BazelThe following example generates all BUILD files for the sample Python project insample_app.
Start from thepazelroot directory to which the repository was cloned.> bazel run //pazel:app -- <pazel_root_dir>/sample_app -r <pazel_root_dir>/sample_app
-c <pazel_root_dir>/sample_app/.pazelrc
Generated BUILD files for <pazel_root_dir>/sample_app.Default usage without BazelStart from thepazelroot directory.> cd sample_app
> pazel
Generated BUILD files for <pazel_install_dir>/sample_app.Testing the generated BUILD filesNow, we can build, test, and run the sample project by running the following invocations in thesample_appdirectory, respectively.> bazel build
> bazel test ...
> bazel run foo:bar3Command-line optionspazel -hshows a summary of the command-line options. Each of them is explained below.By default, BUILD files are generated recursively for the current working directory.
Usepazel <some_path>to generate BUILD file(s) recursively for another directory
or for a single Python file.All imports are assumed to be relative to the current working directory. For example,sample_app/foo/bar2.pyimports fromsample_app/foo/bar1.pyusingfrom foo.bar1 import sample.
Usepazel -r <some_path>to override the path to which the imports are relative.By default,pazeladds rules to install all external Python packages. If your environment has
pre-installed packages for which these rules are not required, then usepazel -p.pazelconfig file.pazelrcis read from the current working directory. Usepazel -c <pazelrc_path>to specify an alternative path.Ignoring rules in existing BUILD filesThe tag# pazel-ignorecausespazelto ignore the rule that immediately follows the tag in an
existing BUILD file. In particular, the tag can be used to skip custom rules thatpazeldoes not
handle.pazelplaces the ignored rules at the bottom of the BUILD file. Seesample_app/foo/BUILDfor an example using the tag.Customizing and extending pazelpazelcan be programmed using a.pazelrcPython file, which is read from the current
working directory or provided explicitly withpazel -c <pazelrc_path>.The user can define variablesHEADERandFOOTERto add custom header and footer to
all BUILD files, respectively. Seesample_app/.pazelrcandsample_app/BUILDfor an example that
adds the samevisibilityto all BUILD files.If some pip package has different install name than import name, then the user
should defineEXTRA_IMPORT_NAME_TO_PIP_NAMEdictionary accordingly.sample_app/.pazelrchas{'yaml': 'pyyaml'}as an example. In addition, the user can specify local packages and their
corresponding Bazel dependencies using theEXTRA_LOCAL_IMPORT_NAME_TO_DEPdictionary.The user can add support for custom Bazel rules by defining a new class implementing theBazelRuleinterface inpazel/bazel_rules.pyand by adding that class toEXTRA_BAZEL_RULESlist in.pazelrc.sample_app/.pazelrcdefines a customPyDoctestRuleclass that identifies all
doctests and generates custompy_doctestBazel rules for them as defined insample_app/custom_rules.bzl.In addition, the user can implement custom rules for mapping Python imports to Bazel dependencies
that are not natively supported. That is achieved by defining a new class implementing theInferenceImportRuleinterface inpazel/import_inference_rules.pyand by adding the class toEXTRA_IMPORT_INFERENCE_RULESlist in.pazelrc.sample_app/.pazelrcdefines a customLocalImportAllInferenceRuleclass that generates the correct Bazel dependencies forfrom X import *type of imports whereXis a local package.BUILD file formattingpazelgenerates BUILD files that are nearly compatible withBuildifier. Buildifier can be
applied onpazel-generated BUILD files to remove the remaining differences, if needed.