diff --git a/.github/actions/build-binaries/action.yaml b/.github/actions/build-binaries/action.yaml index 2d371183..04d34235 100644 --- a/.github/actions/build-binaries/action.yaml +++ b/.github/actions/build-binaries/action.yaml @@ -1,10 +1,10 @@ -name: "Setup, Build, and Test" -description: "Set up Python with Poetry, build and test binaries" +name: "Setup, Build, and Test Pyinstaller Binaries" +description: "Build, test and upload binaries to GitHub releases and/or artifacts (ensure poetry dependencies are installed before running this action)" inputs: package_name: description: "The name of the package to build and test" required: true - upload_binaries: + production_release: description: "Flag to determine if this is a production release" required: true operating_system: @@ -24,41 +24,60 @@ runs: run: ${{ inputs.build_command }} shell: bash - - name: Test Executable + - name: Set TEST_CLI_PATH run: | - ls -l dist - ./dist/${{ inputs.package_name }}/${{ inputs.package_name }}${{ inputs.operating_system == 'windows-latest' && '.exe' || '' }} --help + TEST_CLI_PATH="${{ github.workspace }}/dist/${{ inputs.package_name }}/${{ inputs.package_name }}" + if [ "${{ inputs.operating_system }}" == "Windows" ]; then + TEST_CLI_PATH="${{ github.workspace }}\dist\${{ inputs.package_name }}\${{ inputs.package_name }}.exe" + fi + echo "TEST_CLI_PATH=$TEST_CLI_PATH" >> $GITHUB_ENV shell: bash + - name: Run portability tests + run: | + echo "Executing: ${{ env.TEST_CLI_PATH }}" + git config --global user.email "actions@github.com" && git config --global user.name "github-actions" + poetry run pytest tests/ -m pyinstaller_binary_tests --cli_path "${{ env.TEST_CLI_PATH }}" --log-cli-level=INFO + shell: ${{ inputs.operating_system == 'Windows' && 'cmd' || 'bash' }} + - name: Set release version shell: bash continue-on-error: true - if: ${{ inputs.upload_binaries == 'true' }} + if: ${{ inputs.production_release == 'true' }} run: | echo "RELEASE_VERSION=$(git describe --tags $(git rev-list --tags --max-count=1))" >> $GITHUB_ENV git describe --tags $(git rev-list --tags --max-count=1) - name: Zip binaries shell: bash - continue-on-error: true - if: ${{ inputs.upload_binaries == 'true' }} run: | ls -l dist cd dist/algokit/ - tar -zcvf ../../algokit-${{ env.RELEASE_VERSION }}-${{ inputs.operating_system }}-py${{ inputs.python_version }}.tar.gz * + if [ "${{ inputs.production_release }}" == "true" ]; then + tar -zcvf ../../algokit-${{ env.RELEASE_VERSION }}-${{ inputs.operating_system }}-py${{ inputs.python_version }}.tar.gz * + else + tar -zcvf ../../algokit-${{ inputs.operating_system }}-py${{ inputs.python_version }}.tar.gz * + fi cd ../.. ls -l - - name: Upload binary as artifact - if: ${{ inputs.upload_binaries == 'true' }} + - name: Upload binary as artifact (release) + if: ${{ inputs.production_release == 'true' }} uses: actions/upload-artifact@v4 with: - name: algokit-cli-${{ inputs.operating_system }}-py${{ inputs.python_version }} + name: algokit-${{ inputs.operating_system }}-py${{ inputs.python_version }} path: algokit-${{ env.RELEASE_VERSION }}-${{ inputs.operating_system }}-py${{ inputs.python_version }}.tar.gz + - name: Upload binary as artifact (dev) + if: ${{ inputs.production_release != 'true' }} + uses: actions/upload-artifact@v4 + with: + name: algokit-${{ inputs.operating_system }}-py${{ inputs.python_version }} + path: algokit-${{ inputs.operating_system }}-py${{ inputs.python_version }}.tar.gz + - name: Append binary to release continue-on-error: true - if: ${{ inputs.upload_binaries == 'true' }} + if: ${{ inputs.production_release == 'true' }} uses: softprops/action-gh-release@v1 with: files: | diff --git a/.github/workflows/build-binaries.yaml b/.github/workflows/build-binaries.yaml index e3b2e471..7901e1e7 100644 --- a/.github/workflows/build-binaries.yaml +++ b/.github/workflows/build-binaries.yaml @@ -3,7 +3,7 @@ name: Build, Test and Publish Pyinstaller Binaries on: workflow_call: inputs: - upload_binaries: + production_release: required: true type: string python_version: @@ -11,113 +11,34 @@ on: type: string jobs: - build-binaries-ubuntu: - runs-on: ubuntu-latest + build-binaries: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + include: + - os: ubuntu-latest + build_command: "poetry run poe package_unix" + - os: windows-latest + build_command: "poetry run poe package_windows" + - os: macos-latest + build_command: "poetry run poe package_unix" steps: - - name: Checkout source code (for release) + - name: Checkout source code uses: actions/checkout@v4 - if: ${{ inputs.upload_binaries == 'true' }} with: - fetch-depth: 0 - - - name: Checkout source code (for build) - uses: actions/checkout@v4 - if: ${{ inputs.upload_binaries != 'true' }} - with: - fetch-depth: 1 - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: ${{ inputs.python_version }} - - - name: Set up Poetry - uses: ./.github/actions/setup-poetry - - - uses: actions/cache@v4 - name: Setup poetry cache - with: - path: ./.venv - key: venv-${{ hashFiles('poetry.lock') }}-${{ runner.os }}-${{ inputs.python_version }} - - - name: Install dependencies - run: poetry install --no-interaction - - - name: Build linux binary - uses: ./.github/actions/build-binaries - with: - python_version: ${{ inputs.python_version }} - package_name: "algokit" - upload_binaries: ${{ inputs.upload_binaries }} - operating_system: ${{ runner.os }} - build_command: "poetry run poe package_unix" - - build-binaries-windows: - runs-on: windows-latest - steps: - - name: Checkout source code (for release) - uses: actions/checkout@v4 - if: ${{ inputs.upload_binaries == 'true' }} - with: - fetch-depth: 0 - - - name: Checkout source code (for build) - uses: actions/checkout@v4 - if: ${{ inputs.upload_binaries != 'true' }} - with: - fetch-depth: 1 - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: ${{ inputs.python_version }} - - - name: Set up Poetry - uses: ./.github/actions/setup-poetry - - - uses: actions/cache@v4 - name: Setup poetry cache - with: - path: ./.venv - key: venv-${{ hashFiles('poetry.lock') }}-${{ runner.os }}-${{ inputs.python_version }} - - - name: Install dependencies - run: poetry install --no-interaction - - - name: Build windows binary - uses: ./.github/actions/build-binaries - with: - python_version: ${{ inputs.python_version }} - package_name: "algokit" - upload_binaries: ${{ inputs.upload_binaries }} - operating_system: ${{ runner.os }} - build_command: "poetry run poe package_windows" - - build-binaries-macos: - runs-on: macos-latest - steps: - - name: Checkout source code (for release) - uses: actions/checkout@v4 - if: ${{ inputs.upload_binaries == 'true' }} - with: - fetch-depth: 0 - - - name: Checkout source code (for build) - uses: actions/checkout@v4 - if: ${{ inputs.upload_binaries != 'true' }} - with: - fetch-depth: 1 + fetch-depth: ${{ inputs.production_release == 'true' && '0' || '1' }} - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ inputs.python_version }} - name: Set up Poetry uses: ./.github/actions/setup-poetry - - uses: actions/cache@v4 - name: Setup poetry cache + - name: Setup poetry cache + uses: actions/cache@v4 with: path: ./.venv key: venv-${{ hashFiles('poetry.lock') }}-${{ runner.os }}-${{ inputs.python_version }} @@ -125,11 +46,11 @@ jobs: - name: Install dependencies run: poetry install --no-interaction - - name: Build macos binary + - name: Build & test binary uses: ./.github/actions/build-binaries with: python_version: ${{ inputs.python_version }} package_name: "algokit" - upload_binaries: ${{ inputs.upload_binaries }} + production_release: ${{ inputs.production_release }} operating_system: ${{ runner.os }} - build_command: "poetry run poe package_unix" + build_command: ${{ matrix.build_command }} diff --git a/.github/workflows/build-python.yaml b/.github/workflows/build-python.yaml index 6f96ab3d..dbd4802f 100644 --- a/.github/workflows/build-python.yaml +++ b/.github/workflows/build-python.yaml @@ -15,7 +15,7 @@ jobs: uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} @@ -35,8 +35,10 @@ jobs: - name: pytest + coverage shell: bash + # git config is needed due to several tests relying on e2e copier invocation and copier relies on git during `copy` command run: | set -o pipefail + git config --global user.email "actions@github.com" && git config --global user.name "github-actions" poetry run pytest -n auto --junitxml=pytest-junit.xml --cov-report=term-missing:skip-covered --cov=src | tee pytest-coverage.txt id: pytest diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index 20d2ecef..0fcfaedd 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -39,7 +39,7 @@ jobs: fetch-depth: 0 - name: Set up Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: "3.10" @@ -123,7 +123,7 @@ jobs: uses: ./.github/workflows/build-binaries.yaml needs: release with: - upload_binaries: "true" + production_release: "true" python_version: "3.12" cd-publish-release-packages: diff --git a/.github/workflows/check-python.yaml b/.github/workflows/check-python.yaml index 7fb5c839..98300996 100644 --- a/.github/workflows/check-python.yaml +++ b/.github/workflows/check-python.yaml @@ -11,7 +11,7 @@ jobs: uses: actions/checkout@v4 - name: Set up Python 3.12 - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: "3.12" diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index 02e732bf..7c002623 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -12,14 +12,14 @@ jobs: uses: ./.github/workflows/check-python.yaml pr-build: - name: Build and Test Python + name: Build & Test Python needs: pr-check uses: ./.github/workflows/build-python.yaml pr-binaries-build: - name: Build Binaries + name: Build & Test Binaries needs: pr-check uses: ./.github/workflows/build-binaries.yaml with: - upload_binaries: "false" + production_release: "false" python_version: "3.12" diff --git a/docs/tutorials/algokit-template.md b/docs/tutorials/algokit-template.md index d0f12023..178f15fd 100644 --- a/docs/tutorials/algokit-template.md +++ b/docs/tutorials/algokit-template.md @@ -143,6 +143,35 @@ When creating an AlgoKit template, there are a few default behaviors that you ca By combining predefined Copier answers with these default behaviors, you can create a smooth, efficient, and intuitive initialization experience for the users of your template. +### Executing Python Tasks in Templates + +If you need to use Python scripts as tasks within your Copier templates, ensure that you have Python installed on the host machine. +By convention, AlgoKit automatically detects the Python installation on your machine and fills in the `python_path` variable accordingly. +This process ensures that any Python scripts included as tasks within your Copier templates will execute using the system's Python interpreter. +It's important to note that the use of `_copier_python` is not recommended. Here's an example of specifying a Python script execution in your `copier.yaml` without needing to explicitly use `_copier_python`: + +```yaml +- "{{ python_path }} your_python_script.py" +``` + +If you'd like your template to be backwards compatible with versions of `algokit-cli` older than `v1.11.3` when executing custom python scripts via `copier` tasks, you can use a conditional statement to determine the Python path: + +```yaml +- "{{ python_path if python_path else _copier_python }} your_python_script.py" +# _copier_python above is used for backwards compatibility with versions < v1.11.3 of the algokit cli +``` + +And to define `python_path` in your Copier questions: + +```yaml +# Auto determined by algokit-cli from v1.11.3 to allow execution of python script +# in binary mode. +python_path: + type: str + help: Path to the sys.executable. + when: false +``` + ### Working with Generators After mastering the use of `copier` and building your templates based on the official AlgoKit template repositories, you can enhance your proficiency by learning to define `custom generators`. Essentially, generators are smaller-scope `copier` templates designed to provide additional functionality after a project has been initialized from the template. diff --git a/poetry.lock b/poetry.lock index bfaa8a31..eb39ce67 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1341,16 +1341,6 @@ files = [ {file = "MarkupSafe-2.1.3-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:5bbe06f8eeafd38e5d0a4894ffec89378b6c6a625ff57e3028921f8ff59318ac"}, {file = "MarkupSafe-2.1.3-cp311-cp311-win32.whl", hash = "sha256:dd15ff04ffd7e05ffcb7fe79f1b98041b8ea30ae9234aed2a9168b5797c3effb"}, {file = "MarkupSafe-2.1.3-cp311-cp311-win_amd64.whl", hash = "sha256:134da1eca9ec0ae528110ccc9e48041e0828d79f24121a1a146161103c76e686"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-macosx_10_9_universal2.whl", hash = "sha256:f698de3fd0c4e6972b92290a45bd9b1536bffe8c6759c62471efaa8acb4c37bc"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:aa57bd9cf8ae831a362185ee444e15a93ecb2e344c8e52e4d721ea3ab6ef1823"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ffcc3f7c66b5f5b7931a5aa68fc9cecc51e685ef90282f4a82f0f5e9b704ad11"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:47d4f1c5f80fc62fdd7777d0d40a2e9dda0a05883ab11374334f6c4de38adffd"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:1f67c7038d560d92149c060157d623c542173016c4babc0c1913cca0564b9939"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:9aad3c1755095ce347e26488214ef77e0485a3c34a50c5a5e2471dff60b9dd9c"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_i686.whl", hash = "sha256:14ff806850827afd6b07a5f32bd917fb7f45b046ba40c57abdb636674a8b559c"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8f9293864fe09b8149f0cc42ce56e3f0e54de883a9de90cd427f191c346eb2e1"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-win32.whl", hash = "sha256:715d3562f79d540f251b99ebd6d8baa547118974341db04f5ad06d5ea3eb8007"}, - {file = "MarkupSafe-2.1.3-cp312-cp312-win_amd64.whl", hash = "sha256:1b8dd8c3fd14349433c79fa8abeb573a55fc0fdd769133baac1f5e07abf54aeb"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-macosx_10_9_x86_64.whl", hash = "sha256:8e254ae696c88d98da6555f5ace2279cf7cd5b3f52be2b5cf97feafe883b58d2"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:cb0932dc158471523c9637e807d9bfb93e06a95cbf010f1a38b98623b929ef2b"}, {file = "MarkupSafe-2.1.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9402b03f1a1b4dc4c19845e5c749e3ab82d5078d16a2a4c2cd2df62d57bb0707"}, @@ -2548,7 +2538,6 @@ files = [ {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:69b023b2b4daa7548bcfbd4aa3da05b3a74b772db9e23b982788168117739938"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:81e0b275a9ecc9c0c0c07b4b90ba548307583c125f54d5b6946cfee6360c733d"}, {file = "PyYAML-6.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ba336e390cd8e4d1739f42dfe9bb83a3cc2e80f567d8805e11b46f4a943f5515"}, - {file = "PyYAML-6.0.1-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:326c013efe8048858a6d312ddd31d56e468118ad4cdeda36c719bf5bb6192290"}, {file = "PyYAML-6.0.1-cp310-cp310-win32.whl", hash = "sha256:bd4af7373a854424dabd882decdc5579653d7868b8fb26dc7d0e99f823aa5924"}, {file = "PyYAML-6.0.1-cp310-cp310-win_amd64.whl", hash = "sha256:fd1592b3fdf65fff2ad0004b5e363300ef59ced41c2e6b3a99d4089fa8c5435d"}, {file = "PyYAML-6.0.1-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:6965a7bc3cf88e5a1c3bd2e0b5c22f8d677dc88a455344035f03399034eb3007"}, @@ -2556,15 +2545,8 @@ files = [ {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:42f8152b8dbc4fe7d96729ec2b99c7097d656dc1213a3229ca5383f973a5ed6d"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:062582fca9fabdd2c8b54a3ef1c978d786e0f6b3a1510e0ac93ef59e0ddae2bc"}, {file = "PyYAML-6.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d2b04aac4d386b172d5b9692e2d2da8de7bfb6c387fa4f801fbf6fb2e6ba4673"}, - {file = "PyYAML-6.0.1-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:e7d73685e87afe9f3b36c799222440d6cf362062f78be1013661b00c5c6f678b"}, {file = "PyYAML-6.0.1-cp311-cp311-win32.whl", hash = "sha256:1635fd110e8d85d55237ab316b5b011de701ea0f29d07611174a1b42f1444741"}, {file = "PyYAML-6.0.1-cp311-cp311-win_amd64.whl", hash = "sha256:bf07ee2fef7014951eeb99f56f39c9bb4af143d8aa3c21b1677805985307da34"}, - {file = "PyYAML-6.0.1-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:855fb52b0dc35af121542a76b9a84f8d1cd886ea97c84703eaa6d88e37a2ad28"}, - {file = "PyYAML-6.0.1-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:40df9b996c2b73138957fe23a16a4f0ba614f4c0efce1e9406a184b6d07fa3a9"}, - {file = "PyYAML-6.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c22bec3fbe2524cde73d7ada88f6566758a8f7227bfbf93a408a9d86bcc12a0"}, - {file = "PyYAML-6.0.1-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:8d4e9c88387b0f5c7d5f281e55304de64cf7f9c0021a3525bd3b1c542da3b0e4"}, - {file = "PyYAML-6.0.1-cp312-cp312-win32.whl", hash = "sha256:d483d2cdf104e7c9fa60c544d92981f12ad66a457afae824d146093b8c294c54"}, - {file = "PyYAML-6.0.1-cp312-cp312-win_amd64.whl", hash = "sha256:0d3304d8c0adc42be59c5f8a4d9e3d7379e6955ad754aa9d6ab7a398b59dd1df"}, {file = "PyYAML-6.0.1-cp36-cp36m-macosx_10_9_x86_64.whl", hash = "sha256:50550eb667afee136e9a77d6dc71ae76a44df8b3e51e41b77f6de2932bfe0f47"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:1fe35611261b29bd1de0070f0b2f47cb6ff71fa6595c077e42bd0c419fa27b98"}, {file = "PyYAML-6.0.1-cp36-cp36m-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:704219a11b772aea0d8ecd7058d0082713c3562b4e271b849ad7dc4a5c90c13c"}, @@ -2581,7 +2563,6 @@ files = [ {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:a0cd17c15d3bb3fa06978b4e8958dcdc6e0174ccea823003a106c7d4d7899ac5"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:28c119d996beec18c05208a8bd78cbe4007878c6dd15091efb73a30e90539696"}, {file = "PyYAML-6.0.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7e07cbde391ba96ab58e532ff4803f79c4129397514e1413a7dc761ccd755735"}, - {file = "PyYAML-6.0.1-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:49a183be227561de579b4a36efbb21b3eab9651dd81b1858589f796549873dd6"}, {file = "PyYAML-6.0.1-cp38-cp38-win32.whl", hash = "sha256:184c5108a2aca3c5b3d3bf9395d50893a7ab82a38004c8f61c258d4428e80206"}, {file = "PyYAML-6.0.1-cp38-cp38-win_amd64.whl", hash = "sha256:1e2722cc9fbb45d9b87631ac70924c11d3a401b2d7f410cc0e3bbf249f2dca62"}, {file = "PyYAML-6.0.1-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:9eb6caa9a297fc2c2fb8862bc5370d0303ddba53ba97e71f08023b6cd73d16a8"}, @@ -2589,7 +2570,6 @@ files = [ {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5773183b6446b2c99bb77e77595dd486303b4faab2b086e7b17bc6bef28865f6"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b786eecbdf8499b9ca1d697215862083bd6d2a99965554781d0d8d1ad31e13a0"}, {file = "PyYAML-6.0.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:bc1bf2925a1ecd43da378f4db9e4f799775d6367bdb94671027b73b393a7c42c"}, - {file = "PyYAML-6.0.1-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:04ac92ad1925b2cff1db0cfebffb6ffc43457495c9b3c39d3fcae417d7125dc5"}, {file = "PyYAML-6.0.1-cp39-cp39-win32.whl", hash = "sha256:faca3bdcf85b2fc05d06ff3fbc1f83e1391b3e724afa3feba7d13eeab355484c"}, {file = "PyYAML-6.0.1-cp39-cp39-win_amd64.whl", hash = "sha256:510c9deebc5c0225e8c96813043e62b680ba2f9c50a08d3724c7f28a747d1486"}, {file = "PyYAML-6.0.1.tar.gz", hash = "sha256:bfdf460b1736c775f2ba9f6a92bca30bc2095067b8a9d77876d1fad6cc3b4a43"}, diff --git a/pyproject.toml b/pyproject.toml index d753497e..dc7f6a19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,8 +58,8 @@ docs_generate = "sphinx-build -b markdown -E docs/sphinx docs/cli" docs_toc = "gfm-toc docs/cli/index.md -e 3" docs_title = {shell = "(echo \"# AlgoKit CLI Reference Documentation\\n\\n\"; cat docs/cli/index.md) > docs/cli/temp.md && mv docs/cli/temp.md docs/cli/index.md"} docs = ["docs_generate", "docs_toc", "docs_title"] -package_unix = "pyinstaller --onedir --hidden-import jinja2_ansible_filters --hidden-import multiformats_config --copy-metadata algokit --name algokit --noconfirm src/algokit/__main__.py --add-data './misc/multiformats_config/multibase-table.json:multiformats_config/' --add-data './misc/multiformats_config/multicodec-table.json:multiformats_config/'" -package_windows = "pyinstaller --onedir --hidden-import jinja2_ansible_filters --hidden-import multiformats_config --copy-metadata algokit --name algokit --noconfirm src/algokit/__main__.py --add-data ./misc/multiformats_config/multibase-table.json;multiformats_config/ --add-data ./misc/multiformats_config/multicodec-table.json;multiformats_config/" +package_unix = "pyinstaller --clean --onedir --hidden-import jinja2_ansible_filters --hidden-import multiformats_config --copy-metadata algokit --name algokit --noconfirm src/algokit/__main__.py --add-data './misc/multiformats_config/multibase-table.json:multiformats_config/' --add-data './misc/multiformats_config/multicodec-table.json:multiformats_config/'" +package_windows = "pyinstaller --clean --onedir --hidden-import jinja2_ansible_filters --hidden-import multiformats_config --copy-metadata algokit --name algokit --noconfirm src/algokit/__main__.py --add-data ./misc/multiformats_config/multibase-table.json;multiformats_config/ --add-data ./misc/multiformats_config/multicodec-table.json;multiformats_config/" [tool.ruff] @@ -161,7 +161,9 @@ suppress-none-returning = true pythonpath = ["src", "tests"] markers = [ "mock_platform_system", + "pyinstaller_binary_tests: mark as a test that require algokit binary to be passed via cli arg" ] +addopts = "-m 'not pyinstaller_binary_tests'" # by default, exclude pyinstaller_binary_tests [tool.mypy] files = ["src", "tests"] exclude = ["dist"] diff --git a/src/algokit/__main__.py b/src/algokit/__main__.py index 68e5bacc..91df6dec 100644 --- a/src/algokit/__main__.py +++ b/src/algokit/__main__.py @@ -1,3 +1,7 @@ +from multiprocessing import freeze_support + from algokit.cli import algokit +# Required to support full feature parity when running in binary execution mode +freeze_support() algokit() diff --git a/src/algokit/cli/init.py b/src/algokit/cli/init.py index 06460477..6457ce28 100644 --- a/src/algokit/cli/init.py +++ b/src/algokit/cli/init.py @@ -13,6 +13,7 @@ from algokit.core.bootstrap import bootstrap_any_including_subdirs, project_minimum_algokit_version_check from algokit.core.log_handlers import EXTRA_EXCLUDE_FROM_CONSOLE from algokit.core.sandbox import DEFAULT_ALGOD_PORT, DEFAULT_ALGOD_SERVER, DEFAULT_ALGOD_TOKEN, DEFAULT_INDEXER_PORT +from algokit.core.utils import get_python_paths logger = logging.getLogger(__name__) @@ -216,6 +217,12 @@ def init_command( # noqa: PLR0913 # provide the directory name as an answer to the template, if not explicitly overridden by user answers_dict.setdefault("project_name", directory_name) + system_python_path = next(get_python_paths(), None) + if system_python_path is not None: + answers_dict.setdefault("python_path", system_python_path) + else: + answers_dict.setdefault("python_path", "no_system_python_available") + logger.info("Starting template copy and render...") # copier is lazy imported for two reasons # 1. it is slow to import on first execution after installing diff --git a/src/algokit/core/generate.py b/src/algokit/core/generate.py index 254f78be..a1838991 100644 --- a/src/algokit/core/generate.py +++ b/src/algokit/core/generate.py @@ -5,6 +5,7 @@ import click from algokit.core.conf import ALGOKIT_CONFIG, get_algokit_config +from algokit.core.utils import get_python_paths logger = logging.getLogger(__name__) @@ -33,6 +34,15 @@ def run_generator(answers: dict, path: Path) -> None: :param path: Path to the generator. """ + # Below ensures that if the generator copier.yaml relies on python_path answer + # it will be set to the system python path if available by algokit cli + answers_dict = answers.copy() + system_python_path = next(get_python_paths(), None) + if system_python_path is not None: + answers_dict.setdefault("python_path", system_python_path) + else: + answers_dict.setdefault("python_path", "no_system_python_available") + # copier is lazy imported for two reasons # 1. it is slow to import on first execution after installing # 2. the import fails if git is not installed (which we check above) @@ -42,13 +52,15 @@ def run_generator(answers: dict, path: Path) -> None: with Worker( src_path=str(path), dst_path=cwd, - data=answers, + data=answers_dict, quiet=True, unsafe=True, ) as copier_worker: logger.debug(f"Running generator in {copier_worker.src_path}") copier_worker.run_copy() + logger.info(f"Generator {path} executed successfully") + def load_generators(project_dir: Path) -> list[Generator]: """ diff --git a/src/algokit/core/utils.py b/src/algokit/core/utils.py index 9ebb08f5..349e2e10 100644 --- a/src/algokit/core/utils.py +++ b/src/algokit/core/utils.py @@ -123,7 +123,7 @@ def get_python_paths() -> Iterator[str]: def get_base_python_path() -> str | None: this_python: str | None = sys.executable - if not this_python: + if not this_python or this_python.endswith("algokit"): # Not: can be empty or None... yikes! unlikely though # https://docs.python.org/3.10/library/sys.html#sys.executable return None diff --git a/tests/conftest.py b/tests/conftest.py index 43fa6510..29984307 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,7 @@ import json import logging import os +import subprocess import typing from collections.abc import Callable, Sequence # noqa: RUF100, TCH003 from pathlib import Path @@ -190,3 +191,41 @@ def _delete_password(service_name: str, username: str) -> None: # noqa: ARG001 # Teardown step: reset the credentials for key in credentials: credentials[key] = None + + +def pytest_addoption(parser: pytest.Parser) -> None: + parser.addoption("--cli_path", action="store") + + +@pytest.fixture(scope="session") +def cli_path(request: pytest.FixtureRequest) -> str | None: + cli_path_value = str(request.config.option.cli_path) + if cli_path_value is None: + pytest.skip() + return cli_path_value + + +@pytest.fixture() +def dummy_algokit_template_with_python_task(tmp_path_factory: pytest.TempPathFactory) -> dict[str, Path]: + """ + Used in init approval tests and binary portability tests + """ + + cwd = tmp_path_factory.mktemp("cwd") + dummy_template_path = cwd / "dummy_template" + dummy_template_path.mkdir() + (dummy_template_path / "copier.yaml").write_text( + """ + _tasks: + - "echo '==== 1/1 - Emulate fullstack template python task ===='" + - '{{ python_path }} -c ''print("hello world")''' + + python_path: + type: str + help: Path to the sys.executable. + """ + ) + subprocess.run(["git", "init"], cwd=dummy_template_path, check=False) + subprocess.run(["git", "add", "."], cwd=dummy_template_path, check=False) + subprocess.run(["git", "commit", "-m", "chore: setup dummy test template"], cwd=dummy_template_path, check=False) + return {"template_path": dummy_template_path, "cwd": cwd} diff --git a/tests/generate/test_generate_custom_generate_commands.py b/tests/generate/test_generate_custom_generate_commands.py index a6cfb5a8..572f8da7 100644 --- a/tests/generate/test_generate_custom_generate_commands.py +++ b/tests/generate/test_generate_custom_generate_commands.py @@ -155,3 +155,23 @@ def test_generate_custom_generate_commands_valid_generator_invalid_path( assert result.exit_code == 0 verify(result.output) + + +def test_generate_custom_generate_commands_valid_generator_run_with_python_path( + dummy_algokit_template_with_python_task: dict[str, Path], +) -> None: + cwd = dummy_algokit_template_with_python_task["cwd"] + template_path = str(dummy_algokit_template_with_python_task["template_path"]).replace("\\", r"\\") + (cwd / ALGOKIT_CONFIG).write_text( + f""" +[generate.smart_contract] +description = "Generates a new smart contract" +path = "{template_path}" + """.strip(), + encoding="utf-8", + ) + + result = invoke("generate smart-contract", cwd=cwd, input="y\n") + + assert result.exit_code == 0 + verify(result.output) diff --git a/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run.approved.txt b/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run.approved.txt index 4b7baaf8..7251215c 100644 --- a/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run.approved.txt +++ b/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run.approved.txt @@ -1,3 +1,4 @@ DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml You are about to run a generator. Please make sure it's from a trusted source (for example, official AlgoKit Templates). Do you want to proceed? [y/N]: y DEBUG: Running generator in {current_working_directory}/smart_contract +Generator {current_working_directory}/smart_contract executed successfully diff --git a/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run_with_python_path.approved.txt b/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run_with_python_path.approved.txt new file mode 100644 index 00000000..3d266ed0 --- /dev/null +++ b/tests/generate/test_generate_custom_generate_commands.test_generate_custom_generate_commands_valid_generator_run_with_python_path.approved.txt @@ -0,0 +1,5 @@ +DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml +You are about to run a generator. Please make sure it's from a trusted source (for example, official AlgoKit Templates). Do you want to proceed? [y/N]: y +DEBUG: Running generator in {current_working_directory}/dummy_template +No git tags found in template; using HEAD as ref +Generator {current_working_directory}/dummy_template executed successfully diff --git a/tests/init/test_init.py b/tests/init/test_init.py index be6172cc..e71c3f93 100644 --- a/tests/init/test_init.py +++ b/tests/init/test_init.py @@ -685,6 +685,56 @@ def test_init_with_custom_env(tmp_path_factory: TempPathFactory) -> None: ) +def test_init_template_with_python_task_fails_on_missing_python( + mocker: MockerFixture, dummy_algokit_template_with_python_task: dict[str, Path] +) -> None: + which_mock = WhichMock() + mocker.patch("algokit.core.utils.which").side_effect = which_mock.which + mocker.patch("algokit.core.utils.get_base_python_path", return_value=None) + which_mock.remove("python") + which_mock.remove("python3") + + ref = "HEAD" + result = invoke( + [ + "init", + "--name", + "myapp", + "--no-git", + "--defaults", + f"--template-url={dummy_algokit_template_with_python_task['template_path']}", + f"--template-url-ref={ref}", + "--UNSAFE-SECURITY-accept-template-url", + ], + cwd=dummy_algokit_template_with_python_task["cwd"], + input="y\n", + ) + + assert result.exit_code == 1 + verify(result.output, scrubber=make_output_scrubber()) + + +def test_init_template_with_python_task_works(dummy_algokit_template_with_python_task: dict[str, Path]) -> None: + ref = "HEAD" + result = invoke( + [ + "init", + "--name", + "myapp", + "--no-git", + "--defaults", + f"--template-url={dummy_algokit_template_with_python_task['template_path']}", + f"--template-url-ref={ref}", + "--UNSAFE-SECURITY-accept-template-url", + ], + cwd=dummy_algokit_template_with_python_task["cwd"], + input="y\n", + ) + + assert result.exit_code == 0 + verify(result.output, scrubber=make_output_scrubber()) + + def _remove_git_hints(output: str) -> str: git_init_hint_prefix = "DEBUG: git: hint:" lines = [line for line in output.splitlines() if not line.startswith(git_init_hint_prefix)] diff --git a/tests/init/test_init.test_init_template_with_python_task_fails_on_missing_python.approved.txt b/tests/init/test_init.test_init_template_with_python_task_fails_on_missing_python.approved.txt new file mode 100644 index 00000000..4e790ca9 --- /dev/null +++ b/tests/init/test_init.test_init_template_with_python_task_fails_on_missing_python.approved.txt @@ -0,0 +1,6 @@ +WARNING: Community templates have not been reviewed, and can execute arbitrary code. +Please inspect the template repository, and pay particular attention to the values of _tasks, _migrations and _jinja_extensions in copier.yml +DEBUG: template source = {current_working_directory}/dummy_template@HEAD +DEBUG: project path = {current_working_directory}/myapp +Starting template copy and render... +DEBUG: final clone URL = {current_working_directory}/dummy_template diff --git a/tests/init/test_init.test_init_template_with_python_task_works.approved.txt b/tests/init/test_init.test_init_template_with_python_task_works.approved.txt new file mode 100644 index 00000000..ec197f48 --- /dev/null +++ b/tests/init/test_init.test_init_template_with_python_task_works.approved.txt @@ -0,0 +1,11 @@ +WARNING: Community templates have not been reviewed, and can execute arbitrary code. +Please inspect the template repository, and pay particular attention to the values of _tasks, _migrations and _jinja_extensions in copier.yml +DEBUG: template source = {current_working_directory}/dummy_template@HEAD +DEBUG: project path = {current_working_directory}/myapp +Starting template copy and render... +DEBUG: final clone URL = {current_working_directory}/dummy_template +Template render complete! +DEBUG: Attempting to load project config from {current_working_directory}/myapp/.algokit.toml +DEBUG: No .algokit.toml file found in the project directory. +Executed `algokit bootstrap all` in {current_working_directory}/myapp +🙌 Project initialized at `myapp`! For template specific next steps, consult the documentation of your selected template 🧐 diff --git a/tests/portability/test_pyinstaller_binary.py b/tests/portability/test_pyinstaller_binary.py new file mode 100644 index 00000000..47537885 --- /dev/null +++ b/tests/portability/test_pyinstaller_binary.py @@ -0,0 +1,77 @@ +import logging +import subprocess +import sys +import time +from os import environ +from pathlib import Path + +import pytest + +logger = logging.getLogger(__name__) + +pytestmark = pytest.mark.pyinstaller_binary_tests + + +def command_str_to_list(command: str) -> list[str]: + return command.split(" ") + + +@pytest.mark.parametrize( + ("command", "exit_codes"), + [ + (command_str_to_list("--help"), [0]), + (command_str_to_list("doctor"), [0]), + (command_str_to_list("task vanity-address PY"), [0]), + ], +) +def test_non_interactive_algokit_commands( + command: list[str], exit_codes: list[int], cli_path: str, tmp_path_factory: pytest.TempPathFactory +) -> None: + cwd = tmp_path_factory.mktemp("cwd") + execution_result = subprocess.run([cli_path, *command], capture_output=True, text=True, check=False, cwd=cwd) + logger.info(f"Command {command} returned {execution_result.stdout}") + + # Parts of doctor will fail in CI on macOS and windows on github actions since docker isn't available by default + if "doctor" in command and sys.platform in ["darwin", "windows"] and environ.get("CI"): + exit_codes.append(1) + + assert execution_result.returncode in exit_codes, f"Command {command} failed with {execution_result.stderr}" + + +def test_algokit_init( + cli_path: str, + dummy_algokit_template_with_python_task: dict[str, Path], +) -> None: + command = command_str_to_list( + "init --name testproject " + "--UNSAFE-SECURITY-accept-template-url " + f"--template-url {dummy_algokit_template_with_python_task['template_path']} " + "--template-url-ref=HEAD --no-git --no-ide --defaults" + ) + + process = subprocess.Popen( + [cli_path, *command], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + cwd=dummy_algokit_template_with_python_task["cwd"], + ) + + full_output = "" + logger.info(f'Running command: {" ".join([cli_path, *command])}') + while process.poll() is None and process.stdout and process.stdin: + output = process.stdout.readline() + full_output += output # Accumulate the output + logger.debug(output.strip()) # Log each line of stdout in real-time + + if "y/n" in output.lower(): # adjust this as needed based on the exact prompt text + answer = "y\n" + process.stdin.write(answer) + process.stdin.flush() + + time.sleep(0.1) + + # After the process ends, log the full stdout + logger.info(f"Command init returned:\n{full_output}") + assert process.returncode == 0, f"Command init failed with {process.stderr}"