From 04646f334cad09d784e2b68aa4b7639cd7c10b18 Mon Sep 17 00:00:00 2001 From: Benjy Weinberger Date: Wed, 29 Jul 2020 20:35:08 -0700 Subject: [PATCH 01/21] Fix the setup-py part of the example. (#28) Required moving the .proto files to be below the exported target (in the filesystem sense). If we support setting a source root on generated protos in the future, we can modify this further to demo that. --- .gitignore | 4 ++++ helloworld/BUILD | 2 +- helloworld/config.py | 2 +- helloworld/db.sqlite3 | Bin 0 -> 24576 bytes helloworld/util/BUILD | 4 ++-- helloworld/util/config_loader.py | 2 +- helloworld/{protos => util/proto}/BUILD | 2 +- helloworld/{protos => util/proto}/config.proto | 2 +- 8 files changed, 11 insertions(+), 7 deletions(-) create mode 100644 helloworld/db.sqlite3 rename helloworld/{protos => util/proto}/BUILD (71%) rename helloworld/{protos => util/proto}/config.proto (87%) diff --git a/.gitignore b/.gitignore index f7a6370..97b1dfa 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ __pycache__/ # Editors .idea/ +*.iml + +# Local databases. +*.sqlite3 diff --git a/helloworld/BUILD b/helloworld/BUILD index bf588a4..b4b9546 100644 --- a/helloworld/BUILD +++ b/helloworld/BUILD @@ -40,8 +40,8 @@ python_library( sources = ["config.py"], dependencies = [ ":config_file", - "helloworld/protos", "helloworld/util", + "helloworld/util/proto", ], ) diff --git a/helloworld/config.py b/helloworld/config.py index d3cdc96..7d56799 100644 --- a/helloworld/config.py +++ b/helloworld/config.py @@ -1,8 +1,8 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). -from helloworld.protos.config_pb2 import Config from helloworld.util.config_loader import load_config_from_json +from helloworld.util.proto.config_pb2 import Config def load_config() -> Config: diff --git a/helloworld/db.sqlite3 b/helloworld/db.sqlite3 new file mode 100644 index 0000000000000000000000000000000000000000..093e09e0f69421aa6ad3122260f0b78985eac85e GIT binary patch literal 24576 zcmeI)&2rmB00;1uEZdZ6svbx+ZRxPc^)#L|jx5z54HpcU#;xPDsWXt1s#se`RF)h` zE_8qy+%qR0fg8L655R$*`U)_^Yp`-1e^t#4y^tCHJ7Z~A`{CW+Du>-!=i!5v6EXVO z5BgR_4RVnP0{NIyLP(Z>GW>HA&hvq;CkbDe@F=}FCkZ9qYEEYT zn@&tgn{%J`Iyt*=U(6`$>%zf!B;m2km$zofj)%d7Zw~3#`cTy>K$}*#mK*-IMv7<-e`V>_ybe<%M^3@ikq}<)p5|_#cODMJ#gq zZ2FoP;iSlxvQ@fWG>q!4a#c(5;`wRulIeI(}XWTS@T>f2k#7tE?4kdaYcmRR!Ksez~PLbzxGmRMsjh z@=KDEe9A{@A9Ie{DR1T@e2Y3AOHafKmY;|fB*y_009U<00L)TK#`OiuXYL+ zeoiA^mGZfB^M?%P;_>-^QT~I-fAR|g1Rwwb2tWV=5P$##AOHafKmY<~UEq=^5#`4C zq`<=Z|EeI7d~R;dAI`<&^?ycw6tDkZ$iK)x$=~q=0t6rc0SG_<0uX=z1Rwwb2tWV= z|4QIWMk3b=eLrxlAbNI}|A;@NJCoFVX})03S+>i9X23i~>v42dlE~%U(a`4q5OXI8 zi*$P&{V>HFs=6$U0?TGiFC4IL Config: diff --git a/helloworld/protos/BUILD b/helloworld/util/proto/BUILD similarity index 71% rename from helloworld/protos/BUILD rename to helloworld/util/proto/BUILD index 6212f61..3ddf1d9 100644 --- a/helloworld/protos/BUILD +++ b/helloworld/util/proto/BUILD @@ -1,6 +1,6 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). -# `name` defaults to the name of this directory, i.e., `protos`. +# `name` defaults to the name of this directory, i.e., `proto`. # `sources` defaults to ["*.proto"]. protobuf_library() diff --git a/helloworld/protos/config.proto b/helloworld/util/proto/config.proto similarity index 87% rename from helloworld/protos/config.proto rename to helloworld/util/proto/config.proto index 8fd242c..9db68bd 100644 --- a/helloworld/protos/config.proto +++ b/helloworld/util/proto/config.proto @@ -3,7 +3,7 @@ syntax = "proto3"; -package helloworld.protos; +package helloworld.util.proto; message Config { repeated string languages = 1; From 3b199d82da67dde1da16fd5926f8144c3ddeb44b Mon Sep 17 00:00:00 2001 From: Benjy Weinberger Date: Wed, 29 Jul 2020 23:03:51 -0700 Subject: [PATCH 02/21] Delete .sqlite3 file added by accident (#29) --- helloworld/db.sqlite3 | Bin 24576 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 helloworld/db.sqlite3 diff --git a/helloworld/db.sqlite3 b/helloworld/db.sqlite3 deleted file mode 100644 index 093e09e0f69421aa6ad3122260f0b78985eac85e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 24576 zcmeI)&2rmB00;1uEZdZ6svbx+ZRxPc^)#L|jx5z54HpcU#;xPDsWXt1s#se`RF)h` zE_8qy+%qR0fg8L655R$*`U)_^Yp`-1e^t#4y^tCHJ7Z~A`{CW+Du>-!=i!5v6EXVO z5BgR_4RVnP0{NIyLP(Z>GW>HA&hvq;CkbDe@F=}FCkZ9qYEEYT zn@&tgn{%J`Iyt*=U(6`$>%zf!B;m2km$zofj)%d7Zw~3#`cTy>K$}*#mK*-IMv7<-e`V>_ybe<%M^3@ikq}<)p5|_#cODMJ#gq zZ2FoP;iSlxvQ@fWG>q!4a#c(5;`wRulIeI(}XWTS@T>f2k#7tE?4kdaYcmRR!Ksez~PLbzxGmRMsjh z@=KDEe9A{@A9Ie{DR1T@e2Y3AOHafKmY;|fB*y_009U<00L)TK#`OiuXYL+ zeoiA^mGZfB^M?%P;_>-^QT~I-fAR|g1Rwwb2tWV=5P$##AOHafKmY<~UEq=^5#`4C zq`<=Z|EeI7d~R;dAI`<&^?ycw6tDkZ$iK)x$=~q=0t6rc0SG_<0uX=z1Rwwb2tWV= z|4QIWMk3b=eLrxlAbNI}|A;@NJCoFVX})03S+>i9X23i~>v42dlE~%U(a`4q5OXI8 zi*$P&{V>HFs=6$U0?TGiFC4IL Date: Wed, 5 Aug 2020 14:50:28 -0700 Subject: [PATCH 03/21] Upgrade to 1.30.1rc0 (#31) This fixes running `./pants setup-py --args="bdist_wheel" helloworld/util`. --- pants.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pants.toml b/pants.toml index 5b4442e..a06a21e 100644 --- a/pants.toml +++ b/pants.toml @@ -2,7 +2,7 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). [GLOBAL] -pants_version = "1.30.0" +pants_version = "1.30.1rc0" v1 = false # Turn off the v1 execution engine. v2 = true # Enable the v2 execution engine. pantsd = true # Enable the Pants daemon for better performance. From a91a1eb9a76b2c8116d647502bd20adb7ede3992 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Tue, 11 Aug 2020 11:09:30 -0700 Subject: [PATCH 04/21] Update doc URLs to pantsbuild.org (#34) --- README.md | 4 ++-- helloworld/util/BUILD | 2 +- pants.ci.toml | 2 +- pants.toml | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9af10e0..70a353e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # example-python An example repository to demonstrate Python support in Pants. -See [pantsbuild.org](https://www.pantsbuild.org/docs) for much more detailed documentation. +See [www.pantsbuild.org](https://www.pantsbuild.org/docs) for much more detailed documentation. # Running Pants @@ -146,7 +146,7 @@ Try these out in this repo! ## Build an AWS Lambda -(This example only works on Linux because it has an sdist. See https://pants.readme.io/docs/awslambda-python.) +(This example only works on Linux because it has an sdist. See https://www.pantsbuild.org/docs/awslambda-python.) ``` ./pants awslambda helloworld:helloworld-awslambda diff --git a/helloworld/util/BUILD b/helloworld/util/BUILD index 59f523c..38fe165 100644 --- a/helloworld/util/BUILD +++ b/helloworld/util/BUILD @@ -9,7 +9,7 @@ python_library( "//:translate", "helloworld/util/proto", ], - # See https://pants.readme.io/docs/python-setup-py-goal. + # See https://www.pantsbuild.org/docs/python-setup-py-goal. provides = setup_py( name='helloworld.util', version='0.0.1', diff --git a/pants.ci.toml b/pants.ci.toml index ec64bf1..636e727 100644 --- a/pants.ci.toml +++ b/pants.ci.toml @@ -1,4 +1,4 @@ -# See https://pants.readme.io/docs/using-pants-in-ci. +# See https://www.pantsbuild.org/docs/using-pants-in-ci. [GLOBAL] pantsd = false diff --git a/pants.toml b/pants.toml index a06a21e..67943e8 100644 --- a/pants.toml +++ b/pants.toml @@ -24,15 +24,15 @@ backend_packages2.add = [ plugins2 = [] [source] -# The Python source root is the repo root. See https://pants.readme.io/docs/source-roots. +# The Python source root is the repo root. See https://www.pantsbuild.org/docs/source-roots. root_patterns = ["/"] [python-setup] # The default interpreter compatibility for code in this repo. Individual targets can ovverride # this with the `compatibility` field. See -# https://pants.readme.io/docs/python-interpreter-compatibility. +# https://www.pantsbuild.org/docs/python-interpreter-compatibility. interpreter_constraints = [">=3.7"] -# Use a lockfile. See https://pants.readme.io/docs/python-third-party-dependencies. +# Use a lockfile. See https://www.pantsbuild.org/docs/python-third-party-dependencies. requirement_constraints = "constraints.txt" [protoc] From 93810b678ff34ae72d7eb821a5425cb61a841cce Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Tue, 11 Aug 2020 18:34:15 -0700 Subject: [PATCH 05/21] Testing something ignore me. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8c62824..026ce39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ env: global: - PANTS_CONFIG_FILES=pants.ci.toml - os: linux dist: bionic language: python From 50351b5e27ac220c33f6d9984fee3628ba3609d7 Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 12 Aug 2020 10:01:31 -0700 Subject: [PATCH 06/21] travis notifications test. --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.travis.yml b/.travis.yml index 026ce39..43ac725 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,3 +22,12 @@ install: script: - ./pants --changed-since=origin/master lint - ./pants --changed-since=origin/master --changed-include-dependees=transitive test +notifications: + webhooks: + urls: + - https://afff243f88fd.ngrok.io/travis/repo/cosmo/kramer72/ + on_success: always + on_failure: always + on_start: always + on_cancel: always + on_error: always \ No newline at end of file From 57056e956328b7f27d508ad000abfb6cc792472f Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 12 Aug 2020 10:10:45 -0700 Subject: [PATCH 07/21] break code. --- helloworld/awslambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helloworld/awslambda.py b/helloworld/awslambda.py index 161db44..572a813 100644 --- a/helloworld/awslambda.py +++ b/helloworld/awslambda.py @@ -6,6 +6,6 @@ def handler(event, context): - config = load_config() + config = load_co nfig() greeter = Greeter(languages=config.languages, greetings=config.greetings) print(greeter.greet("world")) From 08b80e65630a5f30d11ad506622d21a747d7cc5e Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 12 Aug 2020 10:17:25 -0700 Subject: [PATCH 08/21] Fix code. --- helloworld/awslambda.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helloworld/awslambda.py b/helloworld/awslambda.py index 572a813..161db44 100644 --- a/helloworld/awslambda.py +++ b/helloworld/awslambda.py @@ -6,6 +6,6 @@ def handler(event, context): - config = load_co nfig() + config = load_config() greeter = Greeter(languages=config.languages, greetings=config.greetings) print(greeter.greet("world")) From a5f5869070761ec45dc2f75412f84fcc5d0ee8fc Mon Sep 17 00:00:00 2001 From: Asher Foa Date: Wed, 12 Aug 2020 10:36:20 -0700 Subject: [PATCH 09/21] Revert "test travis notifications, please ignore." --- .travis.yml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 43ac725..8c62824 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ env: global: - PANTS_CONFIG_FILES=pants.ci.toml + os: linux dist: bionic language: python @@ -22,12 +23,3 @@ install: script: - ./pants --changed-since=origin/master lint - ./pants --changed-since=origin/master --changed-include-dependees=transitive test -notifications: - webhooks: - urls: - - https://afff243f88fd.ngrok.io/travis/repo/cosmo/kramer72/ - on_success: always - on_failure: always - on_start: always - on_cancel: always - on_error: always \ No newline at end of file From 5471b261cd07d81f7864f41b98af62069317578d Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Thu, 13 Aug 2020 10:45:53 -0700 Subject: [PATCH 10/21] Enable travis webhooks w/ to toolchain service with this repo. --- .travis.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.travis.yml b/.travis.yml index 8c62824..ed582b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,3 +23,12 @@ install: script: - ./pants --changed-since=origin/master lint - ./pants --changed-since=origin/master --changed-include-dependees=transitive test +notifications: + webhooks: + urls: + - https://staging.webhooks.toolchain.com/travis/repo/toolchainlabs/example-python/ + on_success: always + on_failure: always + on_start: always + on_cancel: always + on_error: always \ No newline at end of file From 5fbdc9a9baa3ac86617896e53b84161558ef3bdb Mon Sep 17 00:00:00 2001 From: Benjy Weinberger Date: Thu, 13 Aug 2020 13:31:42 -0700 Subject: [PATCH 11/21] Make our py3 binary non-zip-safe. (#36) Otherwise PEP420 doesn't work. --- helloworld/BUILD | 3 +++ 1 file changed, 3 insertions(+) diff --git a/helloworld/BUILD b/helloworld/BUILD index b4b9546..7c80285 100644 --- a/helloworld/BUILD +++ b/helloworld/BUILD @@ -10,6 +10,9 @@ python_binary( "//:ansicolors", "helloworld/greet", ], + # In 1.30 the default is True, but pep420 implicit namespaces don't work well with zipped pexes. + # In 2.x the default will be False, and we should soon fix the pep420 issues with zipped pexes anyway. + zip_safe=False ) # Note: Has sdist dependencies, so must be built on Linux. From 50945d8f29835d40e5010a9ec7a898682c6730cb Mon Sep 17 00:00:00 2001 From: "Daniel J. Beutel" Date: Fri, 24 Jul 2020 17:49:05 +0200 Subject: [PATCH 12/21] Update `pants` and `pants.toml` for 2.0 release (#26) --- pants | 2 +- pants.toml | 14 +++++--------- pants_from_sources | 6 +++--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/pants b/pants index ba90e1d..e2217aa 100755 --- a/pants +++ b/pants @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Copyright 2020 Pants project contributors. +# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). # Licensed under the Apache License, Version 2.0 (see LICENSE). # =============================== NOTE =============================== diff --git a/pants.toml b/pants.toml index 67943e8..427e2e4 100644 --- a/pants.toml +++ b/pants.toml @@ -2,15 +2,11 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). [GLOBAL] -pants_version = "1.30.1rc0" -v1 = false # Turn off the v1 execution engine. -v2 = true # Enable the v2 execution engine. +pants_version = "2.0.0.dev9" pantsd = true # Enable the Pants daemon for better performance. -backend_packages = [] # Deregister all v1 backends. - -# List v2 backends here. -backend_packages2.add = [ +# List backends here. +backend_packages.add = [ 'pants.backend.awslambda.python', 'pants.backend.codegen.protobuf.python', 'pants.backend.python', @@ -20,8 +16,8 @@ backend_packages2.add = [ 'pants.backend.python.lint.isort', ] -# List v2 plugins here. -plugins2 = [] +# List plugins here. +plugins = [] [source] # The Python source root is the repo root. See https://www.pantsbuild.org/docs/source-roots. diff --git a/pants_from_sources b/pants_from_sources index 08de322..733aae1 100755 --- a/pants_from_sources +++ b/pants_from_sources @@ -36,10 +36,10 @@ function string_list() { } export PANTS_VERSION="$(cat "${PANTS_SOURCE}/src/python/pants/VERSION")" -export PANTS_PLUGINS2="$(string_list plugins)" +export PANTS_PLUGINS="$(string_list plugins)" export PANTS_PYTHONPATH="+$(string_list pythonpath)" -export PANTS_BACKEND_PACKAGES2="+$(string_list backend_packages)" -export PANTS_ENABLE_PANTSD="${ENABLE_PANTSD}" +export PANTS_BACKEND_PACKAGES="+$(string_list backend_packages)" +export PANTS_PANTSD="${ENABLE_PANTSD}" export no_proxy="*" exec "${PANTS_SOURCE}/pants" "--no-verify-config" "$@" From 099f5c867424dc2b5a51004854e3e79347e92068 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Tue, 11 Aug 2020 10:30:34 -0700 Subject: [PATCH 13/21] Upgrade to 2.0.0.dev7 (#33) Remaining followups: * [ ] Fix `./pants run helloworld/main.py` failing due to import error. Test it in CI. * [ ] Turn on dependency inference. Needs docs update. * [ ] Enable MyPy. * [ ] Possibly change Protobuf structure to show off the new `python_source_root` plugin field. Needs docs update. * [ ] Use `resolve_all_constraints`. Needs docs update. * [ ] Fix all references to readme.io. Fix the `master` branch too. * [ ] Update the `./pants` script. Fix the `master` branch too. --- .travis.yml | 5 +++++ README.md | 2 +- helloworld/util/BUILD | 27 ++++++++++++++++----------- pants.ci.toml | 7 +++++-- pants.toml | 7 +++---- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index ed582b5..4a99e96 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,11 @@ install: script: - ./pants --changed-since=origin/master lint - ./pants --changed-since=origin/master --changed-include-dependees=transitive test + # Smoke test that our release process will work. + - ./pants binary helloworld/main.py helloworld/main_py2.py + - ./pants setup-py --args="bdist_wheel" helloworld/util:dist + - ./pants awslambda helloworld:helloworld-awslambda + notifications: webhooks: urls: diff --git a/README.md b/README.md index 70a353e..969a0e7 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Try these out in this repo! ## Run `setup.py` commands ``` -./pants setup-py --args="bdist_wheel" helloworld/util # Build a wheel. +./pants setup-py --args="bdist_wheel" helloworld/util:dist # Build a wheel. ``` ## Build an AWS Lambda diff --git a/helloworld/util/BUILD b/helloworld/util/BUILD index 38fe165..aea9ea6 100644 --- a/helloworld/util/BUILD +++ b/helloworld/util/BUILD @@ -1,32 +1,37 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). +# See https://pants.readme.io/docs/python-setup-py-goal. +python_distribution( + name="dist", + dependencies=[":util"], + provides=setup_py( + name='helloworld.util', + version='0.0.1', + description='Greeting library utilities.', + ), +) + python_library( # `name` defaults to the name of this directory, i.e., `util`. # `sources` defaults to ['*.py', '!*_test.py', '!test_*.py', '!conftest.py']. - dependencies = [ + dependencies=[ "//:setuptools", "//:translate", "helloworld/util/proto", ], - # See https://www.pantsbuild.org/docs/python-setup-py-goal. - provides = setup_py( - name='helloworld.util', - version='0.0.1', - description='Greeting library utilities.', - ), ) python_tests( - name = 'tests', + name='tests', # `sources` defaults to ['*_test.py', 'test_*.py', 'conftest.py']. - dependencies = [ + dependencies=[ ":config_loader_test_data", ":util", ], ) resources( - name = 'config_loader_test_data', - sources = ['config_loader_test_data.json'], + name='config_loader_test_data', + sources=['config_loader_test_data.json'], ) diff --git a/pants.ci.toml b/pants.ci.toml index 636e727..9a3c672 100644 --- a/pants.ci.toml +++ b/pants.ci.toml @@ -1,4 +1,7 @@ -# See https://www.pantsbuild.org/docs/using-pants-in-ci. +# Copyright 2020 Pants project contributors. +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +# See https://pants.readme.io/docs/using-pants-in-ci. [GLOBAL] pantsd = false @@ -6,7 +9,7 @@ pantsd = false # Limit the maximum number of concurrent processes. Change this # to a number that makes sense for your CI setup, based on # the number of cores/threads. -process_execution_local_parallelism = 4 +process_execution_local_parallelism = 2 [python-setup] # Limit the maximum number of concurrent jobs used to resolve third diff --git a/pants.toml b/pants.toml index 427e2e4..db2d8ed 100644 --- a/pants.toml +++ b/pants.toml @@ -5,7 +5,6 @@ pants_version = "2.0.0.dev9" pantsd = true # Enable the Pants daemon for better performance. -# List backends here. backend_packages.add = [ 'pants.backend.awslambda.python', 'pants.backend.codegen.protobuf.python', @@ -16,9 +15,6 @@ backend_packages.add = [ 'pants.backend.python.lint.isort', ] -# List plugins here. -plugins = [] - [source] # The Python source root is the repo root. See https://www.pantsbuild.org/docs/source-roots. root_patterns = ["/"] @@ -39,3 +35,6 @@ config = ".flake8" [isort] config = [".isort.cfg"] + +[python-infer] +imports = false From 1abf94c8816d1d3fa7e9240c249c00a3cb31b5d7 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Tue, 11 Aug 2020 11:09:41 -0700 Subject: [PATCH 14/21] Update doc URLs to pantsbuild.org (#35) --- helloworld/util/BUILD | 2 +- pants.ci.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/helloworld/util/BUILD b/helloworld/util/BUILD index aea9ea6..e1c97d7 100644 --- a/helloworld/util/BUILD +++ b/helloworld/util/BUILD @@ -1,7 +1,7 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). -# See https://pants.readme.io/docs/python-setup-py-goal. +# See https://www.pantsbuild.org/docs/python-setup-py-goal. python_distribution( name="dist", dependencies=[":util"], diff --git a/pants.ci.toml b/pants.ci.toml index 9a3c672..ea1e6b1 100644 --- a/pants.ci.toml +++ b/pants.ci.toml @@ -1,7 +1,7 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). -# See https://pants.readme.io/docs/using-pants-in-ci. +# See https://www.pantsbuild.org/docs/using-pants-in-ci. [GLOBAL] pantsd = false From 726e5e8d63aae4073c19e87b0913713f0edc1f1d Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Fri, 14 Aug 2020 09:17:54 -0700 Subject: [PATCH 15/21] Upgrade to 2.0.0.dev8 (#37) --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a99e96..4826b0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,10 +21,11 @@ install: - ./pants --version # This will bootstrap Pants script: - - ./pants --changed-since=origin/master lint - - ./pants --changed-since=origin/master --changed-include-dependees=transitive test + - ./pants lint test '**' # Smoke test that our release process will work. - ./pants binary helloworld/main.py helloworld/main_py2.py + - ./pants run helloworld/main.py + - ./pants run helloworld/main_pb2.py - ./pants setup-py --args="bdist_wheel" helloworld/util:dist - ./pants awslambda helloworld:helloworld-awslambda From 631cac367354140879af80ce03bcf8eec87bc413 Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 19 Aug 2020 19:59:35 -0700 Subject: [PATCH 16/21] fix --- helloworld/BUILD | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/helloworld/BUILD b/helloworld/BUILD index 7c80285..43fd798 100644 --- a/helloworld/BUILD +++ b/helloworld/BUILD @@ -10,9 +10,6 @@ python_binary( "//:ansicolors", "helloworld/greet", ], - # In 1.30 the default is True, but pep420 implicit namespaces don't work well with zipped pexes. - # In 2.x the default will be False, and we should soon fix the pep420 issues with zipped pexes anyway. - zip_safe=False ) # Note: Has sdist dependencies, so must be built on Linux. @@ -51,4 +48,4 @@ python_library( resources( name = "config_file", sources = ["config.json"], -) +) \ No newline at end of file From b216341d8b11ae5e32d11fd7af5bd558cd369976 Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 19 Aug 2020 20:02:53 -0700 Subject: [PATCH 17/21] fix2 --- README.md | 2 +- helloworld/BUILD | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 969a0e7..b8a7923 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # example-python An example repository to demonstrate Python support in Pants. -See [www.pantsbuild.org](https://www.pantsbuild.org/docs) for much more detailed documentation. +See [pantsbuild.org](https://www.pantsbuild.org/docs) for much more detailed documentation. # Running Pants diff --git a/helloworld/BUILD b/helloworld/BUILD index 43fd798..b4b9546 100644 --- a/helloworld/BUILD +++ b/helloworld/BUILD @@ -48,4 +48,4 @@ python_library( resources( name = "config_file", sources = ["config.json"], -) \ No newline at end of file +) From c49669be75b7654d8b87a3b9890a5d3a7758ac2e Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 19 Aug 2020 20:08:34 -0700 Subject: [PATCH 18/21] dev8 --- pants.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pants.toml b/pants.toml index db2d8ed..4cd4306 100644 --- a/pants.toml +++ b/pants.toml @@ -2,7 +2,7 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). [GLOBAL] -pants_version = "2.0.0.dev9" +pants_version = "2.0.0.dev8" pantsd = true # Enable the Pants daemon for better performance. backend_packages.add = [ From 23c1ba8e2482a91b1fdbfcc8e99f04d1ac9f80c0 Mon Sep 17 00:00:00 2001 From: Asher Foa <1268088+asherf@users.noreply.github.com> Date: Wed, 19 Aug 2020 20:16:39 -0700 Subject: [PATCH 19/21] dev9 --- .travis.yml | 2 +- pants.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4826b0d..af23065 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ script: # Smoke test that our release process will work. - ./pants binary helloworld/main.py helloworld/main_py2.py - ./pants run helloworld/main.py - - ./pants run helloworld/main_pb2.py + # - ./pants run helloworld/main_pb2.py - ./pants setup-py --args="bdist_wheel" helloworld/util:dist - ./pants awslambda helloworld:helloworld-awslambda diff --git a/pants.toml b/pants.toml index 4cd4306..db2d8ed 100644 --- a/pants.toml +++ b/pants.toml @@ -2,7 +2,7 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). [GLOBAL] -pants_version = "2.0.0.dev8" +pants_version = "2.0.0.dev9" pantsd = true # Enable the Pants daemon for better performance. backend_packages.add = [ From bdc74fd526fcf9b9e7b6ec57465b289fd9bc7b84 Mon Sep 17 00:00:00 2001 From: Asher Foa Date: Tue, 1 Sep 2020 19:22:10 -0700 Subject: [PATCH 20/21] Merge from upstream 2.0 (#11) * Update `pants` and `pants.toml` for 2.0 release (#26) * Fix the setup-py part of the example. (#30) Required moving the .proto files to be below the exported target (in the filesystem sense). If we support setting a source root on generated protos in the future, we can modify this further to demo that. * Upgrade to 2.0.0.dev7 (#33) Remaining followups: * [ ] Fix `./pants run helloworld/main.py` failing due to import error. Test it in CI. * [ ] Turn on dependency inference. Needs docs update. * [ ] Enable MyPy. * [ ] Possibly change Protobuf structure to show off the new `python_source_root` plugin field. Needs docs update. * [ ] Use `resolve_all_constraints`. Needs docs update. * [ ] Fix all references to readme.io. Fix the `master` branch too. * [ ] Update the `./pants` script. Fix the `master` branch too. * Update doc URLs to pantsbuild.org (#35) * Upgrade to 2.0.0.dev8 (#37) * Disable failing command. Co-authored-by: Daniel J. Beutel Co-authored-by: Benjy Weinberger Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --- .travis.yml | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index af23065..316f9fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,15 @@ os: linux dist: bionic language: python python: 3.7 - +notifications: + webhooks: + urls: + - https://staging.webhooks.toolchain.com/travis/repo/toolchainlabs/example-python/ + on_success: always + on_failure: always + on_start: always + on_cancel: always + on_error: always cache: directories: - $HOME/.cache/pants/setup @@ -25,16 +33,6 @@ script: # Smoke test that our release process will work. - ./pants binary helloworld/main.py helloworld/main_py2.py - ./pants run helloworld/main.py - # - ./pants run helloworld/main_pb2.py + # - ./pants run helloworld/main_py2.py - ./pants setup-py --args="bdist_wheel" helloworld/util:dist - - ./pants awslambda helloworld:helloworld-awslambda - -notifications: - webhooks: - urls: - - https://staging.webhooks.toolchain.com/travis/repo/toolchainlabs/example-python/ - on_success: always - on_failure: always - on_start: always - on_cancel: always - on_error: always \ No newline at end of file + - ./pants awslambda helloworld:helloworld-awslambda \ No newline at end of file From 764e5153ab43be285df323cdba9fd780614e6bf1 Mon Sep 17 00:00:00 2001 From: Asher Foa Date: Thu, 12 Nov 2020 21:17:27 -0800 Subject: [PATCH 21/21] Merge from pants repo upstream. (#17) * Update `pants` and `pants.toml` for 2.0 release (#26) * Fix the setup-py part of the example. (#30) Required moving the .proto files to be below the exported target (in the filesystem sense). If we support setting a source root on generated protos in the future, we can modify this further to demo that. * Upgrade to 2.0.0.dev7 (#33) Remaining followups: * [ ] Fix `./pants run helloworld/main.py` failing due to import error. Test it in CI. * [ ] Turn on dependency inference. Needs docs update. * [ ] Enable MyPy. * [ ] Possibly change Protobuf structure to show off the new `python_source_root` plugin field. Needs docs update. * [ ] Use `resolve_all_constraints`. Needs docs update. * [ ] Fix all references to readme.io. Fix the `master` branch too. * [ ] Update the `./pants` script. Fix the `master` branch too. * Update doc URLs to pantsbuild.org (#35) * Upgrade to 2.0.0.dev8 (#37) * Add py2 __init__.py files. (#39) Otherwise `./pants run helloworld:helloworld_py2` doesn't work. * Upgrade to Pants 2.0.0.dev9 (#38) * Upgrade to 2.0.0b0 (#40) * Turn on dependency inference (#41) * update ./pants wrapper with PANTS_SHA support (#42) Update the ./pants wrapper to add support for specifying a main branch version of Pants via PANTS_SHA. * Upgrade to 2.0.0b1 (#44) * Upgrade to 2.0.0b3 (#45) * Upgrade to 2.0.0rc0 (#46) * Enable MyPy (#47) This shows off the MyPy Protobuf plugin and partitioning based on interpreter constraints. * Upgrade to 2.0.0rc1 (#48) This corresponds to an updated entry for https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path. * Upgrade to 2.0.0rc2 (#49) * Bump to 2.0.0rc3. (#50) * Upgrade to 2.0.0 (#51) * Upgrade to 2.1.0rc0 (#52) * Update to the latest `./pants` script (#53) This prepares us for 2.2 removing support for running with Py36. * add missing BUILD file. Co-authored-by: Daniel J. Beutel Co-authored-by: Benjy Weinberger Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Co-authored-by: Tom Dyas Co-authored-by: Tom Dyas Co-authored-by: Stu Hood --- .gitignore | 5 +- .travis.yml | 9 +- BUILD | 10 +- README.md | 41 ++++-- .flake8 => build-support/.flake8 | 0 build-support/generate_constraints.sh | 25 ++++ build-support/mypy.ini | 34 +++++ constraints.txt | 51 +++---- helloworld/BUILD | 50 +++---- helloworld/__init__.py | 0 helloworld/greet/BUILD | 22 +--- helloworld/greet/__init__.py | 0 helloworld/greet_py2/BUILD | 2 +- helloworld/greet_py2/__init__.py | 0 helloworld/main.py | 4 +- helloworld/util/BUILD | 17 +-- helloworld/util/__init__.py | 0 helloworld/util/lang.py | 4 +- helloworld/util/proto/BUILD | 12 +- helloworld/util/proto/__init__.py | 0 pants | 183 ++++++++++++++++++-------- pants.toml | 43 +++--- 22 files changed, 334 insertions(+), 178 deletions(-) rename .flake8 => build-support/.flake8 (100%) create mode 100755 build-support/generate_constraints.sh create mode 100644 build-support/mypy.ini create mode 100644 helloworld/__init__.py create mode 100644 helloworld/greet/__init__.py create mode 100644 helloworld/greet_py2/__init__.py create mode 100644 helloworld/util/__init__.py create mode 100644 helloworld/util/proto/__init__.py diff --git a/.gitignore b/.gitignore index 97b1dfa..08c35f4 100644 --- a/.gitignore +++ b/.gitignore @@ -10,11 +10,8 @@ # Python files __pycache__/ *.pyc -/.venv/ +.venv/ # Editors .idea/ *.iml - -# Local databases. -*.sqlite3 diff --git a/.travis.yml b/.travis.yml index 316f9fb..9ff5622 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,10 +29,7 @@ install: - ./pants --version # This will bootstrap Pants script: - - ./pants lint test '**' - # Smoke test that our release process will work. - - ./pants binary helloworld/main.py helloworld/main_py2.py + # We also smoke test that our release process will work by running `package`. + - ./pants lint typecheck test package '::' - ./pants run helloworld/main.py - # - ./pants run helloworld/main_py2.py - - ./pants setup-py --args="bdist_wheel" helloworld/util:dist - - ./pants awslambda helloworld:helloworld-awslambda \ No newline at end of file + - ./pants run helloworld/main_py2.py diff --git a/BUILD b/BUILD index 5f81ec9..932d9e4 100644 --- a/BUILD +++ b/BUILD @@ -12,5 +12,13 @@ # python_requirement('translate>=3.2.1') # ] # ) +# +# Refer to https://www.pantsbuild.org/v2.0/docs/python-third-party-dependencies. -python_requirements() +python_requirements( + # `setuptools` exposes a module different than the project name. We teach this to Pants so that + # it can correctly infer dependencies. + module_mapping={ + "setuptools": ["pkg_resources"], + }, +) diff --git a/README.md b/README.md index b8a7923..a65c1a8 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ in `pants.toml`). Pants commands are called _goals_. You can get a list of goals with ``` -./pants goals +./pants help goals ``` # Targets @@ -54,6 +54,18 @@ If you want Pants itself to expand the globs (which is sometimes necessary), you ./pants lint 'helloworld/util/*.py' ``` +You can run on all changed files: + +``` +./pants --changed-since=HEAD lint +``` + +You can run on all changed files, and any of their "dependees": + +``` +./pants --changed-since=HEAD --changed-dependees=transitive test +``` + ## Target specifications Targets are referenced on the command line using their address, of the form `path/to/dir:name`, e.g., @@ -99,17 +111,23 @@ Try these out in this repo! ## List targets ``` -./pants list helloworld:: # All targets. +./pants list :: # All targets. ./pants list 'helloworld/**/*.py' # Just targets containing Python code. ``` ## Run linters and formatters ``` -./pants lint helloworld:: +./pants lint :: ./pants fmt 'helloworld/**/*.py' ``` +## Run MyPy + +``` +./pants typecheck :: +``` + ## Run tests ``` @@ -119,10 +137,10 @@ Try these out in this repo! ./pants test helloworld/util/lang_test.py -- -k test_language_translator # Run just this one test. ``` -## Create a runnable binary +## Create a PEX binary ``` -./pants binary helloworld/main.py helloworld/main_py2.py +./pants package helloworld/main.py helloworld/main_py2.py ``` ## Run a binary @@ -138,22 +156,27 @@ Try these out in this repo! ./pants repl --shell=ipython helloworld/greet ``` -## Run `setup.py` commands +## Build a wheel / generate `setup.py` + +This will build both a `.whl` bdist and a `.tar.gz` sdist. ``` -./pants setup-py --args="bdist_wheel" helloworld/util:dist # Build a wheel. +./pants package helloworld/util:dist ``` +We can also remove the `setup_py_commands` field from `helloworld/util/BUILD` to have Pants instead generate a +`setup.py` file, with all the relevant code in a chroot. + ## Build an AWS Lambda (This example only works on Linux because it has an sdist. See https://www.pantsbuild.org/docs/awslambda-python.) ``` -./pants awslambda helloworld:helloworld-awslambda +./pants package helloworld:helloworld-awslambda ``` ## Count lines of code ``` -./pants cloc '**/*' +./pants count-loc '**/*' ``` diff --git a/.flake8 b/build-support/.flake8 similarity index 100% rename from .flake8 rename to build-support/.flake8 diff --git a/build-support/generate_constraints.sh b/build-support/generate_constraints.sh new file mode 100755 index 0000000..d4d352c --- /dev/null +++ b/build-support/generate_constraints.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +# See https://www.pantsbuild.org/v2.0/docs/python-third-party-dependencies. + +set -euo pipefail + +PYTHON_BIN=python3 +VIRTUALENV=build-support/.venv +PIP="${VIRTUALENV}/bin/pip" +CONSTRAINTS_FILE=constraints.txt + +"${PYTHON_BIN}" -m venv "${VIRTUALENV}" +"${PIP}" install pip --upgrade +"${PIP}" install -r <(./pants dependencies --type=3rdparty ::) +echo "# Generated by build-support/generate_constraints.sh on $(date)" > "${CONSTRAINTS_FILE}" +"${PIP}" freeze --all >> "${CONSTRAINTS_FILE}" + +# This example repo includes some Python 2-only code for demonstration. Because we generated +# the constraints using Python 3.7, we must manually ensure things still work with Python 2.7. So, +# we warn to the user that they should check the diff. You can delete this if you don't use Python 2. +echo "Check the diff for ${CONSTRAINTS_FILE} and restore any entries that were specific to" \ + "Python 2, i.e. entries ending in \`python_version == '2.7'\`. Those are needed for Python 2 to" \ + "work properly, and this script will overwrite it." 1>&2 diff --git a/build-support/mypy.ini b/build-support/mypy.ini new file mode 100644 index 0000000..1ffad0c --- /dev/null +++ b/build-support/mypy.ini @@ -0,0 +1,34 @@ +[mypy] +# Optionals +no_implicit_optional = True + +# Strictness +allow_untyped_globals = False +allow_redefinition = False +implicit_reexport = False +strict_equality = True + +# Warnings +warn_unused_ignores = True +warn_no_return = True +warn_return_any = True +warn_redundant_casts = True +warn_unreachable = True + +# Error output +show_column_numbers = True +show_error_context = True +show_error_codes = True +show_traceback = True +pretty = True +color_output = True +error_summary = True + +[mypy-colors] +ignore_missing_imports = True + +[mypy-translate] +ignore_missing_imports = True + +[mypy-pytest] +ignore_missing_imports = True diff --git a/constraints.txt b/constraints.txt index 4625cb3..2e7e886 100644 --- a/constraints.txt +++ b/constraints.txt @@ -1,35 +1,36 @@ -# Copyright 2020 Pants project contributors. -# Licensed under the Apache License, Version 2.0 (see LICENSE). - -# Generated by first creating a virtual environment with `python3.7 -m venv .venv`, then running -# `.venv/bin/pip install -r requirements.txt` and `.venv/bin/pip freeze`. - +# Generated by build-support/generate_constraints.sh on Wed Sep 16 11:30:54 MST 2020 ansicolors==1.1.8 -appdirs==1.4.3 -certifi==2020.4.5.1 -cfgv==3.1.0 +appdirs==1.4.4 +certifi==2020.6.20 +cfgv==2.0.1 ; python_version == '2.7' +cfgv==3.1.0 ; python_version > '2.7' chardet==3.0.4 click==7.1.2 -distlib==0.3.0 +distlib==0.3.1 filelock==3.0.12 -identify==1.4.15 -idna==2.9 -importlib-metadata==1.6.0 -lxml==4.5.0 -nodeenv==1.3.5 -packaging==20.3 +identify==1.5.2 +idna==2.10 +importlib-metadata==1.7.0 +importlib-resources==3.0.0 +lxml==4.5.2 +nodeenv==1.5.0 +packaging==20.4 +pip==20.2.3 pluggy==0.13.1 -pre-commit==2.3.0 -protobuf==3.11.3 -py==1.8.1 +pre-commit==1.21.0 ; python_version == '2.7' +pre-commit==2.7.1 ; python_version > '2.7' +protobuf==3.13.0 +py==1.9.0 pyparsing==2.4.7 PyYAML==5.3.1 -requests==2.23.0 -six==1.14.0 -toml==0.10.0 -tox==3.15.0 +requests==2.24.0 +setuptools==44.1.1 ; python_version == '2.7' +setuptools==50.3.0 ; python_version > '2.7' +six==1.15.0 +toml==0.10.1 +tox==3.20.0 translate==3.5.0 -urllib3==1.25.9 -virtualenv==20.0.20 +urllib3==1.25.10 +virtualenv==20.0.31 zipp==1.2.0 ; python_version == '2.7' zipp==3.1.0 ; python_version > '2.7' diff --git a/helloworld/BUILD b/helloworld/BUILD index b4b9546..074f433 100644 --- a/helloworld/BUILD +++ b/helloworld/BUILD @@ -1,51 +1,39 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). -python_binary( +pex_binary( # name defaults to the name of this directory, i.e., `helloworld`. - sources = ["main.py"], - entry_point = "helloworld.main", - dependencies = [ - ":config", - "//:ansicolors", - "helloworld/greet", - ], + sources=["main.py"], + entry_point="helloworld.main", ) # Note: Has sdist dependencies, so must be built on Linux. python_awslambda( - name='helloworld-awslambda', - sources = ["awslambda.py"], - dependencies=[ - ":config", - "helloworld/greet", - ], - handler='helloworld.awslambda:handler', - runtime='python3.7', + name="helloworld-awslambda", + sources=["awslambda.py"], + handler="helloworld.awslambda:handler", + runtime="python3.7", ) -python_binary( - name = "helloworld_py2", - sources = ["main_py2.py"], - entry_point = "helloworld.main_py2", - compatibility = ">=2.7,<3", - dependencies = [ - "//:ansicolors", - "helloworld/greet_py2", - ], +pex_binary( + name="helloworld_py2", + sources=["main_py2.py"], + entry_point="helloworld.main_py2", + interpreter_constraints=["==2.7.*"], ) python_library( - name = "config", - sources = ["config.py"], - dependencies = [ + name="config", + sources=["config.py"], + dependencies=[ + # Pants cannot infer dependencies on resources targets and Protobuf, so we explicitly + # add them. ":config_file", - "helloworld/util", "helloworld/util/proto", ], ) resources( - name = "config_file", - sources = ["config.json"], + name="config_file", + sources=["config.json"], ) diff --git a/helloworld/__init__.py b/helloworld/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/helloworld/greet/BUILD b/helloworld/greet/BUILD index 75366c7..899ae49 100644 --- a/helloworld/greet/BUILD +++ b/helloworld/greet/BUILD @@ -1,19 +1,11 @@ # Copyright 2020 Pants project contributors. # Licensed under the Apache License, Version 2.0 (see LICENSE). -python_library( - # `name` defaults to the name of this directory, i.e., `greet`. - # `sources` defaults to ['*.py', '!*_test.py', '!test_*.py', '!conftest.py']. - dependencies = [ - "//:translate", - "helloworld/util", - ], -) +# `name` defaults to the name of this directory, i.e., `greet`. +# `sources` defaults to ['*.py', '!*_test.py', '!test_*.py', '!conftest.py']. +# `dependencies` are inferred. +python_library() -python_tests( - name = 'tests', - # `sources` defaults to ['*_test.py', 'test_*.py', 'conftest.py']. - dependencies = [ - ":greet", - ] -) +# `sources` defaults to ['*_test.py', 'test_*.py', 'conftest.py']. +# `dependencies` are inferred. +python_tests(name = 'tests') diff --git a/helloworld/greet/__init__.py b/helloworld/greet/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/helloworld/greet_py2/BUILD b/helloworld/greet_py2/BUILD index de883c7..46e5d91 100644 --- a/helloworld/greet_py2/BUILD +++ b/helloworld/greet_py2/BUILD @@ -5,5 +5,5 @@ python_library( # name defaults to the name of this directory, i.e., `greet_py2`. # sources defaults to ['*.py', '!*_test.py', '!test_*.py', '!conftest.py']. - compatibility = ">=2.7,<3" + interpreter_constraints=["==2.7.*"], ) diff --git a/helloworld/greet_py2/__init__.py b/helloworld/greet_py2/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/helloworld/main.py b/helloworld/main.py index 42164cc..8353e58 100644 --- a/helloworld/main.py +++ b/helloworld/main.py @@ -9,7 +9,9 @@ def say_hello() -> None: config = load_config() - greeter = Greeter(languages=config.languages, greetings=config.greetings) + greeter = Greeter( + languages=list(config.languages), greetings=list(config.greetings) + ) sentence = greeter.greet("world") print(green(sentence)) diff --git a/helloworld/util/BUILD b/helloworld/util/BUILD index e1c97d7..12d83e0 100644 --- a/helloworld/util/BUILD +++ b/helloworld/util/BUILD @@ -4,7 +4,9 @@ # See https://www.pantsbuild.org/docs/python-setup-py-goal. python_distribution( name="dist", + # Because this has no source code, Pants cannot infer dependencies. dependencies=[":util"], + setup_py_commands=["bdist_wheel", "sdist"], provides=setup_py( name='helloworld.util', version='0.0.1', @@ -12,22 +14,21 @@ python_distribution( ), ) +# `name` defaults to the name of this directory, i.e., `util`. +# `sources` defaults to ['*.py', '!*_test.py', '!test_*.py', '!conftest.py']. python_library( - # `name` defaults to the name of this directory, i.e., `util`. - # `sources` defaults to ['*.py', '!*_test.py', '!test_*.py', '!conftest.py']. - dependencies=[ - "//:setuptools", - "//:translate", - "helloworld/util/proto", - ], + # Pants cannot infer dependencies on Protobuf, so we explicitly add it. + dependencies=[ + "helloworld/util/proto", + ], ) python_tests( name='tests', # `sources` defaults to ['*_test.py', 'test_*.py', 'conftest.py']. + # Pants cannot infer dependencies on `resources` targets, so we explicitly add it. dependencies=[ ":config_loader_test_data", - ":util", ], ) diff --git a/helloworld/util/__init__.py b/helloworld/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/helloworld/util/lang.py b/helloworld/util/lang.py index 3a75cab..9fc49fe 100644 --- a/helloworld/util/lang.py +++ b/helloworld/util/lang.py @@ -2,7 +2,7 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). import random -from typing import List +from typing import List, cast from translate import Translator @@ -18,7 +18,7 @@ def translate(self, lang: str, phrase: str) -> str: if lang not in self._langs: raise self.UnknownLanguage(lang) translator = Translator(lang) - return translator.translate(phrase) + return cast(str, translator.translate(phrase)) def translate_to_random_language(self, phrase: str) -> str: return self.translate(self._pick_random_language(), phrase) diff --git a/helloworld/util/proto/BUILD b/helloworld/util/proto/BUILD index 3ddf1d9..b39f32d 100644 --- a/helloworld/util/proto/BUILD +++ b/helloworld/util/proto/BUILD @@ -3,4 +3,14 @@ # `name` defaults to the name of this directory, i.e., `proto`. # `sources` defaults to ["*.proto"]. -protobuf_library() +protobuf_library(dependencies=[":init"]) + + +# Note that we need an `__init__.py` file for MyPy to recognize our generated code as a valid +# module. We include this in the dependencies for the `protobuf_library` to ensure the +# `__init__.py` is always used. +# +# Often, you won't want to put `__init__.py` files in your Protobuf directories; see +# https://www.pantsbuild.org/docs/protobuf#protobuf-and-source-roots for how to change where the +# protobuf code is generated. +python_library(name="init") \ No newline at end of file diff --git a/helloworld/util/proto/__init__.py b/helloworld/util/proto/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pants b/pants index e2217aa..7f7ad8e 100755 --- a/pants +++ b/pants @@ -12,23 +12,27 @@ set -eou pipefail +# NOTE: To use an unreleased version of Pants from the pantsbuild/pants master branch, +# locate the master branch SHA, set PANTS_SHA= in the environment, and run this script as usual. +# +# E.g., PANTS_SHA=725fdaf504237190f6787dda3d72c39010a4c574 ./pants --version + PYTHON_BIN_NAME="${PYTHON:-unspecified}" -# Set one of these to specify a non-standard location for this script to read the pants version from. +# Set this to specify a non-standard location for this script to read the Pants version from. # NB: This will *not* cause Pants itself to use this location as a config file. # You can use PANTS_CONFIG_FILES or --pants-config-files to do so. -PANTS_INI=${PANTS_INI:-pants.ini} PANTS_TOML=${PANTS_TOML:-pants.toml} PANTS_BIN_NAME="${PANTS_BIN_NAME:-$0}" -PANTS_HOME="${PANTS_HOME:-${XDG_CACHE_HOME:-$HOME/.cache}/pants/setup}" +PANTS_SETUP_CACHE="${PANTS_SETUP_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/pants/setup}" # If given a relative path, we fix it to be absolute. -if [[ "$PANTS_HOME" != /* ]]; then - PANTS_HOME="${PWD}/${PANTS_HOME}" +if [[ "$PANTS_SETUP_CACHE" != /* ]]; then + PANTS_SETUP_CACHE="${PWD}/${PANTS_SETUP_CACHE}" fi -PANTS_BOOTSTRAP="${PANTS_HOME}/bootstrap-$(uname -s)-$(uname -m)" +PANTS_BOOTSTRAP="${PANTS_SETUP_CACHE}/bootstrap-$(uname -s)-$(uname -m)" VENV_VERSION=${VENV_VERSION:-16.4.3} @@ -57,38 +61,24 @@ function tempdir { } function get_exe_path_or_die { - exe="$1" + local exe="$1" if ! command -v "${exe}"; then die "Could not find ${exe}. Please ensure ${exe} is on your PATH." fi } -pants_config_file="" -if [[ -f "${PANTS_INI}" ]]; then - pants_config_file="${PANTS_INI}" -fi -if [[ -f "${PANTS_TOML}" ]]; then - pants_config_file="${PANTS_TOML}" -fi - function get_pants_config_value { - config_key="$1" - optional_space="[[:space:]]*" - if [[ "${pants_config_file}" == *.ini ]]; then - valid_delimiters="[:=]" - prefix="^${config_key}${optional_space}${valid_delimiters}${optional_space}" - sed -ne "/${prefix}/ s#${prefix}##p" "${PANTS_INI}" && return 0 - fi - if [[ "${pants_config_file}" == *.toml ]]; then - prefix="^${config_key}${optional_space}=${optional_space}" - raw_value="$(sed -ne "/${prefix}/ s#${prefix}##p" "${PANTS_TOML}")" - echo "${raw_value}" | tr -d \"\' && return 0 - fi + local config_key="$1" + local optional_space="[[:space:]]*" + local prefix="^${config_key}${optional_space}=${optional_space}" + local raw_value + raw_value="$(sed -ne "/${prefix}/ s#${prefix}##p" "${PANTS_TOML}")" + echo "${raw_value}" | tr -d \"\' && return 0 return 0 } function get_python_major_minor_version { - python_exe="$1" + local python_exe="$1" "$python_exe" <&2 fi @@ -202,10 +262,15 @@ function bootstrap_pants { # Ensure we operate from the context of the ./pants buildroot. cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" -python="$(determine_python_exe)" -pants_version="$(determine_pants_version "${python}")" -pants_dir="$(bootstrap_pants "${pants_version}" "${python}")" - +pants_version="$(determine_pants_version)" +python="$(determine_python_exe "${pants_version}")" +pants_dir="$(bootstrap_pants "${pants_version}" "${python}" "${PANTS_SHA:-}")" +pants_python="${pants_dir}/bin/python" +pants_binary="${pants_dir}/bin/pants" +pants_extra_args="" +if [[ -n "${PANTS_SHA:-}" ]]; then + pants_extra_args="${pants_extra_args} --python-repos-repos=$(find_links_url "$pants_version" "$PANTS_SHA")" +fi # We set the env var no_proxy to '*', to work around an issue with urllib using non # async-signal-safe syscalls after we fork a process that has already spawned threads. @@ -213,4 +278,6 @@ pants_dir="$(bootstrap_pants "${pants_version}" "${python}")" # See https://blog.phusion.nl/2017/10/13/why-ruby-app-servers-break-on-macos-high-sierra-and-what-can-be-done-about-it/ export no_proxy='*' -exec "${pants_dir}/bin/python" "${pants_dir}/bin/pants" --pants-bin-name="${PANTS_BIN_NAME}" "$@" +# shellcheck disable=SC2086 +exec "${pants_python}" "${pants_binary}" ${pants_extra_args} \ + --pants-bin-name="${PANTS_BIN_NAME}" --pants-version=${pants_version} "$@" diff --git a/pants.toml b/pants.toml index db2d8ed..5e164d4 100644 --- a/pants.toml +++ b/pants.toml @@ -2,17 +2,21 @@ # Licensed under the Apache License, Version 2.0 (see LICENSE). [GLOBAL] -pants_version = "2.0.0.dev9" +pants_version = "2.1.0rc0" pantsd = true # Enable the Pants daemon for better performance. backend_packages.add = [ - 'pants.backend.awslambda.python', - 'pants.backend.codegen.protobuf.python', - 'pants.backend.python', - 'pants.backend.python.lint.docformatter', - 'pants.backend.python.lint.black', - 'pants.backend.python.lint.flake8', - 'pants.backend.python.lint.isort', + "pants.backend.awslambda.python", + "pants.backend.codegen.protobuf.python", + "pants.backend.python", + "pants.backend.python.lint.docformatter", + "pants.backend.python.lint.black", + "pants.backend.python.lint.flake8", + "pants.backend.python.lint.isort", + "pants.backend.python.typecheck.mypy", + # Adds the `py-constraints` goal, as this example repository has + # both Python 2 and Python 3 code. + "pants.backend.python.mixed_interpreter_constraints", ] [source] @@ -20,21 +24,28 @@ backend_packages.add = [ root_patterns = ["/"] [python-setup] -# The default interpreter compatibility for code in this repo. Individual targets can ovverride -# this with the `compatibility` field. See +# The default interpreter compatibility for code in this repo. Individual targets can override +# this with the `interpreter_constraints` field. See # https://www.pantsbuild.org/docs/python-interpreter-compatibility. interpreter_constraints = [">=3.7"] -# Use a lockfile. See https://www.pantsbuild.org/docs/python-third-party-dependencies. +# Use a constraints file. See https://www.pantsbuild.org/docs/python-third-party-dependencies. requirement_constraints = "constraints.txt" +# We search for interpreters on both on the $PATH and in the `$(pyenv root)/versions` folder. +# If you're using macOS, you may want to leave off the entry to avoid using the +# problematic system Pythons. See +# https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path. +interpreter_search_paths = ["", ""] -[protoc] -runtime_targets = ["//:protobuf"] +[python-protobuf] +# See https://www.pantsbuild.org/docs/protobuf. +runtime_dependencies = ["//:protobuf"] +mypy_plugin = true [flake8] -config = ".flake8" +config = "build-support/.flake8" [isort] config = [".isort.cfg"] -[python-infer] -imports = false +[mypy] +config = "build-support/mypy.ini"