Skip to content

Commit

Permalink
Build PEX binary (#16)
Browse files Browse the repository at this point in the history
* Build PEX binary for `//circleci:workflows`.

* Set CPython version onto pex rule.

* Setup BAzel 3.12 in CI

* Try removing pex constraints in CI again.
  • Loading branch information
helly25 authored Sep 11, 2024
1 parent b4c6ee1 commit c0f4787
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: 3.11
python-version: 3.12
- uses: pre-commit/[email protected]
- uses: pre-commit-ci/[email protected]
if: always()
Expand All @@ -17,6 +17,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: 3.12

- name: Setup Bazelisk
uses: bazelbuild/setup-bazelisk@v3
Expand Down
10 changes: 9 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ _PYTHON_VERSION = "3.12"
_PYTHON_VERSION_SNAKE = _PYTHON_VERSION.replace(".", "_")

bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_python", version = "0.34.0")

bazel_dep(name = "rules_python", version = "0.34.0", dev_dependency = True)

bazel_dep(name = "rules_mypy", version = "0.6.0")
bazel_dep(name = "aspect_rules_py", version = "0.8.0")

bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)

Expand All @@ -37,6 +40,7 @@ python = use_extension(
dev_dependency = True,
)
python.toolchain(
configure_coverage_tool = True,
is_default = True,
python_version = _PYTHON_VERSION,
)
Expand Down Expand Up @@ -67,3 +71,7 @@ types.requirements(
requirements_txt = "//:requirements.in",
)
use_repo(types, "pip_types")

tools = use_extension("@aspect_rules_py//py:extensions.bzl", "py_tools")
tools.rules_py_tools()
use_repo(tools, "rules_py_pex_2_3_1")
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ stats from the CircleCI API server and writes them as a CSV file.

## Usage
```
bazel run //circleci:workflows -- <command> [args...]
workflows.pex <command> [args...]
```

### Commands
* combine: Read multiple files generated by `workflow.py fetch` and combine them.
* combine: Read multiple files generated by `workflows.pex fetch` and combine them.
* fetch: Fetch workflow data from the CircleCI API server and writes them as a CSV file.
* fetch_details: Given a workflow CSV file, fetch details for each workflow (slow).
* filter: Read CSV files generated from `workflow.py fetch` and filters them.
* filter: Read CSV files generated from `workflows.pex fetch` and filters them.
* help: Provides help for the program.
* request_branches: Read and display the list of branches for `workflow` from CircleCI API.
* request_workflow: Given a workflow ID return its details.
Expand All @@ -29,12 +29,12 @@ they have a '.gz' or '.bz2' extension respectively.

### For command specific help use
```
bazel run //circleci:workflows -- <command> --help.
workflows.pex <command> --help.
```

### Command combine

Read multiple files generated by `workflow.py fetch` and combine them.
Read multiple files generated by `workflows.pex fetch` and combine them.

```
bazel run //circleci:workflows -- combine --output=/tmp/circleci.csv "${PWD}/data/circleci_workflows*.csv*"
Expand All @@ -44,7 +44,7 @@ bazel run //circleci:workflows -- combine --output=/tmp/circleci.csv "${PWD}/dat

`input`

> List of CSV files generated from `workflow.py fetch`.
> List of CSV files generated from `workflows.pex fetch`.
### options:

Expand Down Expand Up @@ -216,7 +216,7 @@ bazel run //circleci:workflows -- fetch_details --input "${PWD}/data/circleci_wo
`--input INPUT`

> A CSV file generated from `workflow.py fetch`.
> A CSV file generated from `workflows.pex fetch`.
`--output OUTPUT`

Expand All @@ -228,7 +228,7 @@ bazel run //circleci:workflows -- fetch_details --input "${PWD}/data/circleci_wo
### Command filter

Read CSV files generated from `workflow.py fetch` and filters them.
Read CSV files generated from `workflows.pex fetch` and filters them.

```
bazel run //circleci:workflows -- filter --workflow default_workflow,pre_merge --input /tmp/circleci.csv --output "${HOME}/circleci_filtered_workflows.csv"
Expand All @@ -248,7 +248,7 @@ bazel run //circleci:workflows -- filter --workflow default_workflow,pre_merge -
`--input INPUT`

> CSV file generated from `workflow.py fetch`.
> CSV file generated from `workflows.pex fetch`.
`--output OUTPUT`

Expand Down
12 changes: 10 additions & 2 deletions circleci/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

"""Tool to fetch and analyze CircleCI workflows."""

load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_pex_binary")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@my_pip_deps//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:defs.bzl", "py_library", "py_test")

package(default_visibility = ["//visibility:private"])

Expand Down Expand Up @@ -64,16 +65,23 @@ py_test(
)

py_binary(
name = "workflows",
name = "workflows_bin",
testonly = True,
srcs = ["workflows.py"],
tags = ["manual"],
visibility = ["//visibility:private"],
deps = [
":workflows_lib_py",
"//mbo/app:commands_py",
],
)

py_pex_binary(
name = "workflows",
testonly = True,
binary = ":workflows_bin",
)

genrule(
name = "readme_gen",
testonly = True,
Expand Down
10 changes: 5 additions & 5 deletions circleci/workflows_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def __init__(self, parser: argparse.ArgumentParser):
parser.add_argument(
"--input",
type=Path,
help="A CSV file generated from `workflow.py fetch`.",
help="A CSV file generated from `workflows.pex fetch`.",
)
parser.add_argument(
"--output",
Expand Down Expand Up @@ -497,7 +497,7 @@ def Main(self) -> None:


class Combine(CircleCiCommand):
"""Read multiple files generated by `workflow.py fetch` and combine them.
"""Read multiple files generated by `workflows.pex fetch` and combine them.
```
bazel run //circleci:workflows -- combine --output=/tmp/circleci.csv "${PWD}/data/circleci_workflows*.csv*"
Expand All @@ -510,7 +510,7 @@ def __init__(self, parser: argparse.ArgumentParser):
"input",
type=Path,
nargs="+",
help="List of CSV files generated from `workflow.py fetch`.",
help="List of CSV files generated from `workflows.pex fetch`.",
)
parser.add_argument(
"--output",
Expand Down Expand Up @@ -566,7 +566,7 @@ def Main(self) -> None:


class Filter(Command):
"""Read CSV files generated from `workflow.py fetch` and filters them.
"""Read CSV files generated from `workflows.pex fetch` and filters them.
```
bazel run //circleci:workflows -- filter --workflow default_workflow,pre_merge --input /tmp/circleci.csv --output "${HOME}/circleci_filtered_workflows.csv"
Expand All @@ -585,7 +585,7 @@ def __init__(self, parser: argparse.ArgumentParser):
parser.add_argument(
"--input",
type=Path,
help="CSV file generated from `workflow.py fetch`.",
help="CSV file generated from `workflows.pex fetch`.",
)
parser.add_argument(
"--output",
Expand Down
7 changes: 7 additions & 0 deletions mbo/app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ def Run(argv: list[str] = sys.argv):
)
if match:
program = f"bazel run //{match.group(1)}:{match.group(2)} --"
else:
match = re.fullmatch(
".*/unzipped_pexes/[0-9a-zA-Z]*/([^/]+)[.]py",
program,
)
if match:
program = f"{match.group(1)}.pex"

parser = argparse.ArgumentParser(
prog=program,
Expand Down

0 comments on commit c0f4787

Please sign in to comment.