Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 53 additions & 48 deletions .github/workflows/free-threaded-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ name: Free-threaded Python

# Builds the Device Detection and IP Intelligence on-premise packages on the
# free-threaded builds of Python and runs their tests there, with the free
# Lite data files. The first job downloads those once for each commit of the
# data submodule, which links them, and puts them in the cache. The test jobs
# restore them from there and never download them.
# Lite data files. Those come from the assets common-ci fetches for each run,
# as every other workflow's tests do. The first job finds them in the asset
# cache, or fetches them into it with common-ci's prefetch_assets action when
# it misses, and the test jobs restore them from there. No job downloads
# them from the links in the data submodule.
# ci/free-threaded-python.Tests.ps1 checks this. The native modules do not declare that they run without
# the GIL, so Python turns the GIL back on when it loads them.
# tests/test_free_threaded.py in each package checks that it does, and is
Expand Down Expand Up @@ -37,54 +39,51 @@ permissions:
contents: read

jobs:
free-data-files:
name: Fetch the free Lite data files once
# The job is named Configure, as common-ci's is, because this repository's
# ci/fetch-assets.ps1 fetches the assets of every product only in a job of
# that name. The asset cache it fills is the one the other workflows that
# day restore, so it must never hold only some of them.
Configure:
name: Find or fetch the run's assets
runs-on: ubuntu-latest
outputs:
key: ${{ steps.key.outputs.key }}
key: ${{ steps.prefetch-assets.outputs.cache-key }}
defaults:
run:
shell: pwsh
steps:
# common-ci's action and steps run from its checkout, with this
# repository below it, as in common-ci's own workflows.
- name: Checkout common-ci
uses: actions/checkout@v4
with:
repository: 51Degrees/common-ci
ref: main
persist-credentials: false

# The fetch links the assets into the data submodule, so it is
# checked out too.
- name: Checkout repository
uses: actions/checkout@v4
with:
path: ${{ github.event.repository.name }}
persist-credentials: false
submodules: recursive

- name: Name the cache after the data submodule
id: key
run: |
# The data submodule's .link files say which files are fetched and
# from where, so a new commit of it needs the files again. Change
# v2 when the list of files below changes.
$data = git -C cxx rev-parse HEAD:data
if ($LASTEXITCODE) { exit $LASTEXITCODE }
"key=free-lite-data-v2-$data" >> $env:GITHUB_OUTPUT

- name: Look for the files in the cache
id: cache
uses: actions/cache@v4
# Restores nothing, and fetches the assets into the cache only when it
# has no copy under the day's key.
- name: Find or fetch the assets
id: prefetch-assets
uses: ./.github/actions/prefetch_assets
with:
path: |
cxx/data/**/51Degrees-LiteV4.1.hash
cxx/data/**/20000 Evidence Records.yml
cxx/data/**/51Degrees-LiteV41.ipi
cxx/data/**/51Degrees-IPIV4AsnIpiV41.ipi
key: ${{ steps.key.outputs.key }}
# The Windows job restores what this Linux job saves.
enableCrossOsArchive: true
lookup-only: true

# Runs only when the cache has no copy, and the cache saves the files
# when the job ends.
- name: Fetch the free Lite data files
if: steps.cache.outputs.cache-hit != 'true'
run: ./cxx/data/get-data-files.ps1 51Degrees-LiteV4.1.hash '20000 Evidence Records.yml' 51Degrees-LiteV41.ipi 51Degrees-IPIV4AsnIpiV41.ipi
repo: ${{ github.event.repository.name }}
secrets: ${{ toJSON(secrets) }}
vars: ${{ toJSON(vars) }}
license-key: ${{ secrets.LICENSE_KEY_V4TAC_IPIV4ENTERPRISE }}

free-threaded:
name: Python ${{ matrix.python }} on ${{ matrix.os }}
needs: free-data-files
needs: Configure
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -158,26 +157,32 @@ jobs:
-e device-detection/fiftyone_devicedetection `
-e device-detection/fiftyone_devicedetection_examples

- name: Restore the free Lite data files
uses: actions/cache/restore@v4
# The path and the cross-OS setting are those prefetch_assets saves
# with, so the key matches.
- name: Restore the run's assets
uses: actions/cache/restore@v5
with:
path: |
cxx/data/**/51Degrees-LiteV4.1.hash
cxx/data/**/20000 Evidence Records.yml
cxx/data/**/51Degrees-LiteV41.ipi
cxx/data/**/51Degrees-IPIV4AsnIpiV41.ipi
key: ${{ needs.free-data-files.outputs.key }}
path: assets
key: ${{ needs.Configure.outputs.key }}
enableCrossOsArchive: true
# The first job fetched them, so a miss is a fault, never a reason
# to download them here.
fail-on-cache-miss: true

- name: Put the IP Intelligence file where the tests look for it
# Copies, not links, so the Windows job needs no right to make links.
- name: Put the free data files where the tests look for them
run: |
New-Item -ItemType Directory -Force -Path ip-intelligence/assets | Out-Null
New-Item -ItemType HardLink `
-Path ip-intelligence/assets/51Degrees-LiteIpiV41.ipi `
-Target cxx/data/ip-intelligence/51Degrees-LiteV41.ipi | Out-Null
$dd = 'cxx/data/device-detection'
$ipi = 'cxx/data/ip-intelligence'
New-Item -ItemType Directory -Force -Path $dd, $ipi, ip-intelligence/assets | Out-Null
Copy-Item -LiteralPath 'assets/51Degrees-LiteV4.1.hash' -Destination $dd
Copy-Item -LiteralPath 'assets/20000 Evidence Records.yml' -Destination $dd
Copy-Item -LiteralPath 'assets/51Degrees-IPIV4AsnIpiV41.ipi' -Destination $ipi
# The data repository saves the Lite IP Intelligence file as
# 51Degrees-LiteV41.ipi, and the tests also look for it under the
# name the assets give it.
Copy-Item -LiteralPath 'assets/51Degrees-LiteIpiV41.ipi' -Destination "$ipi/51Degrees-LiteV41.ipi"
Copy-Item -LiteralPath 'assets/51Degrees-LiteIpiV41.ipi' -Destination ip-intelligence/assets

- name: Build the native modules
run: |
Expand Down
117 changes: 55 additions & 62 deletions ci/free-threaded-python.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Tests that .github/workflows/free-threaded-python.yml fetches the free Lite
# data files once, in one job, and that the jobs that test take them from the
# cache that job fills, rather than each downloading them again.
# Tests that .github/workflows/free-threaded-python.yml takes the free Lite
# data files from the assets common-ci fetches for each run, in one job, and
# that the jobs that test restore them from that job's cache, rather than any
# job downloading them from the links in the data submodule.
# The workflow is read as text, as the runners have no YAML module.

BeforeAll {
$script:Workflow = Join-Path $PSScriptRoot '..' '.github' 'workflows' `
'free-threaded-python.yml'
$lines = @(Get-Content -LiteralPath $script:Workflow)
$script:Text = $lines -join "`n"

# Each job's lines, by the job's name, from the two-space keys under jobs.
$script:Jobs = [ordered]@{}
Expand Down Expand Up @@ -41,22 +43,7 @@ BeforeAll {
}
if ($null -ne $current) { $steps += , $current }
return @($steps | Where-Object {
($_ -join "`n") -match "uses:\s*$([regex]::Escape($Uses))@" })
}

# The lines of a "path: |" block in a step, trimmed.
function Get-Paths($Step) {
$paths = @()
$inPaths = $false
foreach ($line in $Step) {
if ($line -match '^\s+path:\s*\|\s*$') { $inPaths = $true; continue }
if ($inPaths -and $line -match '^\s{12,}\S') {
$paths += $line.Trim()
continue
}
$inPaths = $false
}
return $paths
($_ -join "`n") -match "(?m)uses:\s*$([regex]::Escape($Uses))(@|\s*$)" })
}

function Get-Value($Step, [string]$Key) {
Expand All @@ -66,66 +53,83 @@ BeforeAll {
}

$script:Fetching = @($script:Jobs.Keys | Where-Object {
($script:Jobs[$_] -join "`n") -match 'get-data-files\.ps1' })
($script:Jobs[$_] -join "`n") -match 'uses:\s*\S*prefetch_assets' })
$script:Testing = @($script:Jobs.Keys | Where-Object {
($script:Jobs[$_] -join "`n") -match 'pytest' })
}

Describe 'free-threaded-python.yml' {
It 'downloads the free data files in one job only' {
It 'never downloads from the links in the data submodule' {
$script:Text | Should -Not -Match 'get-data-files'
}

It 'finds or fetches the assets in one job only' {
$script:Fetching.Count | Should -Be 1
}

It 'downloads them in a job that is not a matrix' {
foreach ($job in $script:Fetching) {
($script:Jobs[$job] -join "`n") | Should -Not -Match 'matrix:'
}
It 'names that job Configure, so every product''s assets are fetched' {
# ci/fetch-assets.ps1 fetches every product's assets only in a job of
# that name, and the cache it fills is shared with the other
# workflows.
$script:Fetching[0] | Should -BeExactly 'Configure'
Get-Content -Raw (Join-Path $PSScriptRoot 'fetch-assets.ps1') |
Should -Match "GITHUB_JOB\s+-eq\s+'Configure'"
}

It 'fetches with common-ci''s action, run from common-ci''s checkout' {
$job = $script:Fetching[0]
$prefetch = @(Get-Step $job './.github/actions/prefetch_assets')
$prefetch.Count | Should -Be 1
Get-Value $prefetch[0] 'id' | Should -Not -BeNullOrEmpty
Get-Value $prefetch[0] 'license-key' | Should -Not -BeNullOrEmpty
$checkouts = @(Get-Step $job 'actions/checkout')
@($checkouts | Where-Object {
(Get-Value $_ 'repository') -eq '51Degrees/common-ci' -and
-not (Get-Value $_ 'path') }).Count | Should -Be 1
$own = @($checkouts | Where-Object { -not (Get-Value $_ 'repository') })
$own.Count | Should -Be 1
Get-Value $own[0] 'path' | Should -Be (Get-Value $prefetch[0] 'repo')
Get-Value $own[0] 'submodules' | Should -Be 'recursive'
}

It 'gives the cache key to the jobs that test' {
$job = $script:Fetching[0]
$id = Get-Value (@(Get-Step $job './.github/actions/prefetch_assets')[0]) 'id'
($script:Jobs[$job] -join "`n") | Should -Match (
'\n outputs:\s*\n\s+key:\s*' +
[regex]::Escape('${{ steps.' + $id + '.outputs.cache-key }}'))
}

It 'never downloads them in a job that runs the tests' {
It 'never fetches in a job that runs the tests' {
$script:Testing.Count | Should -BeGreaterThan 0
foreach ($job in $script:Testing) {
$script:Fetching | Should -Not -Contain $job
}
}

It 'saves them in the cache, and downloads them only when it misses' {
It 'restores the assets in each job that runs the tests, as prefetch_assets saves them' {
$job = $script:Fetching[0]
$cache = @(Get-Step $job 'actions/cache')
$cache.Count | Should -Be 1
Get-Value $cache[0] 'id' | Should -Not -BeNullOrEmpty
$id = Get-Value $cache[0] 'id'
$fetch = ($script:Jobs[$job] -join "`n")
$fetch | Should -Match ("if:\s*steps\.$id\.outputs\.cache-hit\s*!=\s*" +
"'true'\s*\n\s+run:\s*[|]?\s*\n?\s*\./cxx/data/get-data-files\.ps1")
}

It 'restores them in each job that runs the tests, from the same key and paths' {
$job = $script:Fetching[0]
$save = @(Get-Step $job 'actions/cache')[0]
$keyStep = Get-Value $save 'key'
$keyStep | Should -Match '^\$\{\{\s*steps\.([\w-]+)\.outputs\.key\s*\}\}$'
($script:Jobs[$job] -join "`n") | Should -Match (
'\n outputs:\s*\n\s+key:\s*' + [regex]::Escape($keyStep))
foreach ($testing in $script:Testing) {
$text = $script:Jobs[$testing] -join "`n"
$text | Should -Match "\n needs:\s*\[?\s*$([regex]::Escape($job))\b"
$restore = @(Get-Step $testing 'actions/cache/restore')
$restore.Count | Should -Be 1
Get-Value $restore[0] 'path' | Should -Be 'assets'
Get-Value $restore[0] 'key' |
Should -Be ('${{ needs.' + $job + '.outputs.key }}')
Get-Value $restore[0] 'fail-on-cache-miss' | Should -Be 'true'
Get-Paths $restore[0] | Should -Be (Get-Paths $save)
Get-Value $restore[0] 'enableCrossOsArchive' | Should -Be 'true'
}
}

It 'saves and restores across operating systems, for the Windows jobs' {
$job = $script:Fetching[0]
$steps = @(Get-Step $job 'actions/cache') + @($script:Testing |
ForEach-Object { Get-Step $_ 'actions/cache/restore' })
$steps.Count | Should -BeGreaterThan 1
foreach ($step in $steps) {
Get-Value $step 'enableCrossOsArchive' | Should -Be 'true'
It 'puts each free data file the tests use in place from the assets' {
foreach ($testing in $script:Testing) {
$text = $script:Jobs[$testing] -join "`n"
foreach ($file in '51Degrees-LiteV4.1.hash', '20000 Evidence Records.yml',
'51Degrees-IPIV4AsnIpiV41.ipi', '51Degrees-LiteIpiV41.ipi') {
$text | Should -Match ([regex]::Escape("assets/$file"))
}
$text | Should -Match ([regex]::Escape('51Degrees-LiteV41.ipi'))
}
}

Expand Down Expand Up @@ -162,15 +166,4 @@ Describe 'free-threaded-python.yml' {
'\s*\n(\s+\S.*\n)*?\s+\$PSNativeCommandUseErrorActionPreference = \$true')
}
}

It 'caches each of the free data files the tests use' {
$paths = Get-Paths (@(Get-Step $script:Fetching[0] 'actions/cache')[0])
foreach ($file in '51Degrees-LiteV4.1.hash', '51Degrees-LiteV41.ipi',
'51Degrees-IPIV4AsnIpiV41.ipi', '20000 Evidence Records.yml') {
$paths | Should -Contain "cxx/data/**/$file"
}
# The evidence is the YAML file, and no other evidence file is cached.
@($paths | Where-Object { $_ -notmatch '\.(hash|ipi|yml)$' }) |
Should -BeNullOrEmpty
}
}
Loading
Loading