This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
568 lines (488 loc) · 20.9 KB
/
ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
---
name: CI
on:
pull_request:
branches: [master]
types: [opened, synchronize, reopened]
push:
branches: [master]
workflow_dispatch:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
backport:
name: Backport
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: Git patches
run: |
echo "::group::apply git patches"
pushd python-plexapi
git apply -v --ignore-whitespace ../patches/*.patch
popd
echo "::endgroup::"
- name: Set up Python
uses: LizardByte/[email protected]
with:
python-version: '2.7'
- name: Set up Python 3
id: python3
uses: actions/setup-python@v5
with:
python-version: '3.11'
update-environment: false
- name: Set up Python Dependencies
run: |
echo "Installing Requirements"
python --version
python -m pip --no-python-version-warning --disable-pip-version-check install --upgrade pip setuptools wheel
# install python requirements
python -m pip install --upgrade -r requirements_ci.txt
python -m pip install --upgrade -r requirements_dev.txt
- name: Set up Python 3 Dependencies
run: |
echo "Installing Requirements"
${{ steps.python3.outputs.python-path }} --version
${{ steps.python3.outputs.python-path }} -m pip install --upgrade pip setuptools wheel
# install python requirements
${{ steps.python3.outputs.python-path }} -m pip install --upgrade -r requirements_ci.txt
- name: Find python files
id: find_files
working-directory: python-plexapi
run: |
found_files=$(find . -type f -iname "*.py")
echo "found files: $found_files"
echo found_files=${found_files} >> $GITHUB_OUTPUT
- name: Patch plexapi python
env:
found_files: ${{ steps.find_files.outputs.found_files }}
working-directory: python-plexapi
run: |
# array of files to skip
skip_files=(./setup.py)
for file in ${found_files}; do
# skip files in skip_files array
if [[ " ${skip_files[@]} " =~ " ${file} " ]]; then
echo "Skipping ${file}"
continue
fi
echo "::group::patch ${file}"
echo "Replacing f-strings in ${file}"
# set a backup file path
temp_file="${file}temp.py"
# writing to the original file does not work, data will be missing
future-fstrings-show ${file} > ${temp_file}
# run future-fstrings for a second pass, to handle nested f-strings
# this also replaces the original file!
future-fstrings-show ${temp_file} > ${file}
# remove the temp file
rm -f ${temp_file}
# strip type hints
echo "Stripping type hints from ${file}"
# this must run under Python 3 due to https://github.com/abarker/strip-hints/issues/5
# update path env with directory name of where python 3 installs
TEMP_PATH=$(${{ steps.python3.outputs.python-path }} -c "import sys, os; \
print(os.path.dirname(sys.executable))")
PATH="$TEMP_PATH:$PATH" strip-hints --inplace ${file}
# apply custom LizardByte patches
echo "Applying custom patches to ${file}"
python ../custom_patches.py ${file}
echo "Applying autopep8 to ${file}"
python -m autopep8 --recursive --in-place --jobs $(nproc) --max-line-length 125 ${file}
echo "Applying pasteurize to ${file}"
# -x arg is not working, need to specify each fix to include
pasteurize -j $(nproc) --verbose --write --nobackups \
-f libpasteurize.fixes.fix_add_all__future__imports \
-f libpasteurize.fixes.fix_annotations \
-f libpasteurize.fixes.fix_division \
-f libpasteurize.fixes.fix_fullargspec \
-f libpasteurize.fixes.fix_future_builtins \
-f libpasteurize.fixes.fix_kwargs \
-f libpasteurize.fixes.fix_newstyle \
-f libpasteurize.fixes.fix_throw \
-f libpasteurize.fixes.fix_unpacking \
${file}
# -f libfuturize.fixes.fix_future_standard_library \ # does some install_aliases()
# adds absolute import, division, print_function, unicode_literals
# -f libpasteurize.fixes.fix_add_all__future__imports \
# from future import standard_library... install_aliases()
# -f libpasteurize.fixes.fix_add_future_standard_library_import \
# -f libpasteurize.fixes.fix_annotations \ # tbd
# -f libpasteurize.fixes.fix_division \ # division is different in Python 3
# -f libpasteurize.fixes.fix_fullargspec \ # tbd
# -f libpasteurize.fixes.fix_future_builtins \ # from builtins import next, round, int, str, super, etc.
# -f libpasteurize.fixes.fix_getcwd \ # replaces os.getcwd with os.getcwdu (unicode version)
# -f libpasteurize.fixes.fix_imports \ # does some install_aliases()
# -f libpasteurize.fixes.fix_imports2 \ # from future import standard_library... install_aliases()
# -f libpasteurize.fixes.fix_kwargs \ # tbd
# -f libpasteurize.fixes.fix_newstyle \ # tbd
# -f libpasteurize.fixes.fix_printfunction \ # tbd
# -f libpasteurize.fixes.fix_throw \ # tbd
# -f libpasteurize.fixes.fix_unpacking \ # tbd
# excluding these fixes
# libfuturize.fixes.fix_future_standard_library # do not want to install_aliases()
# libpasteurize.fixes.fix_add_future_standard_library_import # do not want to install_aliases()
# libpasteurize.fixes.fix_getcwd # cwd probably doesn't need to be unicode
# libpasteurize.fixes.fix_imports # do not want to install_aliases()
# libpasteurize.fixes.fix_imports2 # do not want to install_aliases()
# libpasteurize.fixes.fix_printfunction # print in Python 3 is okay in Python 2.7.
# remove `from __future__ import unicode_literals` from each file
echo "Removing unwanted __future__ imports from ${file}"
sed -i '/from __future__ import print_function/d' ${file}
sed -i '/from __future__ import unicode_literals/d' ${file}
echo "Removing unwanted from builtin imports from ${file}"
sed -i '/from builtins import int/d' ${file}
sed -i '/from builtins import open/d' ${file}
sed -i '/from builtins import str/d' ${file}
sed -i '/from builtins import super/d' ${file}
sed -i '/from builtins import \*/d' ${file}
# git diff to github job summary
echo "### ${file}" >> $GITHUB_STEP_SUMMARY
echo '```diff' >> $GITHUB_STEP_SUMMARY
echo "$(git diff ${file})" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "::endgroup::"
done
# patch the README examples
echo "::group::patch README examples"
echo "Replacing f-strings in README examples"
# set a backup file path
file="./README.rst"
temp_file="./README.rsttemp"
future-fstrings-show ${file} > ${temp_file}
future-fstrings-show ${temp_file} > ${file}
rm -f ${temp_file}
echo "::endgroup::"
- name: Merge backport specific files
run: |
# copy custom backports
cp -f -r ./plexapi_backports.py ./python-plexapi/plexapi/backports.py
# copy test configs
cp -f ./.flake8 ./python-plexapi/.flake8-backport
# remove line that starts with `requests==` from requirements_dev.txt
# (it will interfere with the main requirements.txt)
sed -i '/^requests==/d' ./python-plexapi/requirements_dev.txt
# apply `;python_version>="3"` to every requirement in requirements.txt if line does not start with `#`
# upstream is using python 3.8... so let them manage 3.8 or greater deps
sed -i '/^[^#]/ s/$/;python_version>="3.8"/' ./python-plexapi/requirements.txt
sed -i '/^[^#]/ s/$/;python_version>="3.8"/' ./python-plexapi/requirements_dev.txt
# merge requirements
cat ./requirements.txt >> ./python-plexapi/requirements.txt
cat ./requirements_dev.txt >> ./python-plexapi/requirements_dev.txt
# amend our readme with plexapi readme
og_readme=$(cat ./python-plexapi/README.rst)
cp -f ./README.rst ./python-plexapi/README.rst
echo "${og_readme}" >> ./python-plexapi/README.rst
# copy readthedocs config
cp -f ./.readthedocs.yaml ./python-plexapi/.readthedocs.yml
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: python-plexapi-backport
if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
path: |
${{ github.workspace }}/python-plexapi
!**/*.git
build:
name: Build
needs:
- backport
runs-on: ubuntu-latest
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: python-plexapi-backport
- name: Set up Python
uses: LizardByte/[email protected]
with:
python-version: '2.7'
- name: Set up Python Dependencies
run: |
echo "Installing Requirements"
python --version
python -m pip --no-python-version-warning --disable-pip-version-check install --upgrade pip setuptools wheel
# install python requirements
python -m pip install --upgrade -r requirements_dev.txt
- name: build
env:
BUILD_VERSION: ${{ github.run_number }}
run: |
python setup.py sdist bdist_wheel
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: dist
if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
path: dist
flake8:
name: flake8
needs:
- backport
runs-on: ubuntu-latest
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: python-plexapi-backport
- name: Set up Python
uses: LizardByte/[email protected]
with:
python-version: '2.7'
- name: Set up Python Dependencies
run: |
echo "Installing Requirements"
python --version
python -m pip --no-python-version-warning --disable-pip-version-check install --upgrade pip setuptools wheel
# install python requirements
python -m pip install --upgrade -r requirements_dev.txt
- name: flake8
run: |
# since we are automating this, we need to ignore more rules than we normally would
# also don't run against tests, as it seems that upstream is not doing this
python -m flake8 -v --append-config ./.flake8-backport ./plexapi
pytest:
name: pytest (${{ matrix.plex }}, Python ${{ matrix.python }})
needs:
- backport
runs-on: ubuntu-latest
env:
PLEXAPI_AUTH_SERVER_BASEURL: http://127.0.0.1:32400
PLEX_CONTAINER: plexinc/pms-docker
PLEX_CONTAINER_TAG: latest
strategy:
fail-fast: false
matrix:
plex: ['unclaimed', 'claimed']
python: ['2.7', '3.7', '3.8', '3.9', '3.10', '3.11']
trusted:
- if: >-
(github.event_name == 'pull_request' && github.event.pull_request.head.owner.login == 'LizardByte') ||
(github.event_name == 'push' && github.ref == 'refs/heads/master')
exclude:
- trusted: false
plex: claimed
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: python-plexapi-backport
- name: Set up Python
uses: LizardByte/[email protected]
with:
python-version: ${{ matrix.python }}
- name: Set up Python Dependencies
run: |
echo "Installing Requirements"
python --version
python -m pip --no-python-version-warning --disable-pip-version-check install --upgrade pip setuptools wheel
# install python requirements
python -m pip install --upgrade -r requirements_dev.txt
# install python-plexapi-backport
python -m pip install -e .
- name: Get PMS Docker image digest
id: docker-digest
run: |
mkdir -p ~/.cache/docker/${{ env.PLEX_CONTAINER }}
echo "Image: ${{ env.PLEX_CONTAINER }}"
echo "Tag: ${{ env.PLEX_CONTAINER_TAG }}"
token=$(curl \
--silent \
"https://auth.docker.io/token?scope=repository:${{ env.PLEX_CONTAINER }}:pull&service=registry.docker.io" \
| jq -r '.token')
digest=$(curl \
--silent \
--header "Accept: application/vnd.docker.distribution.manifest.v2+json" \
--header "Authorization: Bearer $token" \
"https://registry-1.docker.io/v2/${{ env.PLEX_CONTAINER }}/manifests/${{ env.PLEX_CONTAINER_TAG }}" \
| jq -r '.config.digest')
echo "Digest: $digest"
echo "digest=$digest" >> $GITHUB_OUTPUT
- name: Cache PMS Docker image
id: docker-cache
uses: actions/cache@v4
with:
path: ~/.cache/docker/plexinc
key: ${{ runner.os }}-docker-pms-${{ steps.docker-digest.outputs.digest }}
- name: Pull PMS Docker image
if: steps.docker-cache.outputs.cache-hit != 'true'
run: |
docker pull ${{ env.PLEX_CONTAINER }}:${{ env.PLEX_CONTAINER_TAG }}
docker save -o ~/.cache/docker/${{ env.PLEX_CONTAINER }}-${{ env.PLEX_CONTAINER_TAG }}.tar \
${{ env.PLEX_CONTAINER }}:${{ env.PLEX_CONTAINER_TAG }}
echo "Saved image: ${{ env.PLEX_CONTAINER }}:${{ env.PLEX_CONTAINER_TAG }}"
- name: Load PMS Docker image
if: steps.docker-cache.outputs.cache-hit == 'true'
run: |
docker load -i ~/.cache/docker/${{ env.PLEX_CONTAINER }}-${{ env.PLEX_CONTAINER_TAG }}.tar
- name: Set Plex credentials
if: matrix.plex == 'claimed'
run: |
if [ "${{ matrix.python }}" == "3.7" ]; then
echo "PLEXAPI_AUTH_SERVER_TOKEN=${{ secrets.PLEXAPI_AUTH_SERVER_TOKEN_1 }}" >> $GITHUB_ENV
elif [ "${{ matrix.python }}" == "3.8" ]; then
echo "PLEXAPI_AUTH_SERVER_TOKEN=${{ secrets.PLEXAPI_AUTH_SERVER_TOKEN_2 }}" >> $GITHUB_ENV
elif [ "${{ matrix.python }}" == "3.9" ]; then
echo "PLEXAPI_AUTH_SERVER_TOKEN=${{ secrets.PLEXAPI_AUTH_SERVER_TOKEN_3 }}" >> $GITHUB_ENV
elif [ "${{ matrix.python }}" == "3.10" ]; then
echo "PLEXAPI_AUTH_SERVER_TOKEN=${{ secrets.PLEXAPI_AUTH_SERVER_TOKEN_4 }}" >> $GITHUB_ENV
elif [ "${{ matrix.python }}" == "3.11" ]; then
echo "PLEXAPI_AUTH_SERVER_TOKEN=${{ secrets.PLEXAPI_AUTH_SERVER_TOKEN_5 }}" >> $GITHUB_ENV
else
# python 2.7 and fallback
echo "PLEXAPI_AUTH_SERVER_TOKEN=${{ secrets.PLEXAPI_AUTH_SERVER_TOKEN }}" >> $GITHUB_ENV
fi
- name: Bootstrap ${{ matrix.plex }} Plex server
id: boostrap
uses: nick-fields/[email protected]
with:
max_attempts: 3
timeout_minutes: 2
command: |
python \
-u tools/plex-bootstraptest.py \
--destination plex \
--advertise-ip 127.0.0.1 \
--bootstrap-timeout 540 \
--docker-tag ${{ env.PLEX_CONTAINER_TAG }} \
--${{ matrix.plex }}
on_retry_command: |
if ["${{ matrix.plex }}" == "claimed"]; then
python -u tools/plex-teardowntest.py
fi
# remove docker container
docker rm -f $(docker ps --latest)
- name: Main tests with ${{ matrix.plex }} server
env:
TEST_ACCOUNT_ONCE: ${{ matrix.plex == 'unclaimed' }}
SHARED_USERNAME: ${{ secrets.PLEX_SHARED_USERNAME }}
run: |
python -m pytest \
-rxXs \
--cache-clear \
--ignore=tests/test_sync.py \
--tb=native \
--verbose \
--cov=plexapi \
tests
- name: Unlink PMS from MyPlex account
if: matrix.plex == 'claimed' && steps.bootstrap.outcome != 'skipped'
run: |
python -u tools/plex-teardowntest.py
- name: Upload coverage artifact
uses: actions/upload-artifact@v3
with:
name: coverage-${{ matrix.plex }}-${{ matrix.python }}
path: .coverage
coverage:
name: Process test coverage (${{ matrix.plex }})
runs-on: ubuntu-latest
needs: pytest
if: always() # code coverage tracks coverage on all branches
strategy:
fail-fast: false
matrix:
plex: ['unclaimed', 'claimed']
python: ['2.7', '3.7', '3.8', '3.9', '3.10', '3.11']
trusted:
- if: >-
(github.event_name == 'pull_request' && github.event.pull_request.head.owner.login == 'LizardByte') ||
(github.event_name == 'push' && github.ref == 'refs/heads/master')
exclude:
- trusted: false
plex: claimed
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
with:
name: python-plexapi-backport
- name: Set up Python
id: python
uses: actions/setup-python@v5
with:
python-version: 3.11
- name: Set up Python Dependencies
run: |
echo "Installing Requirements"
python --version
python -m pip --no-python-version-warning --disable-pip-version-check install --upgrade pip setuptools wheel
# install python requirements
python -m pip install --upgrade -r requirements_dev.txt
- name: Download all coverage artifacts
uses: actions/download-artifact@v3
- name: Combine ${{ matrix.plex }} coverage results
run: |
coverage combine coverage-${{ matrix.plex }}-${{ matrix.python }}/.coverage*
coverage report --fail-under=50
coverage xml
- name: Upload ${{ matrix.plex }} coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: Plex-${{ matrix.plex }},Python-${{ matrix.python }}
# due to running pytest on a patched version, the code lines will not match without overriding the branch
- name: Upload ${{ matrix.plex }} coverage to Codecov (dist)
if: (github.event_name == 'push' && github.ref == 'refs/heads/master')
uses: codecov/codecov-action@v3
with:
flags: Plex-${{ matrix.plex }},Python-${{ matrix.python }}
override_branch: dist
publish:
name: Publish
if: >-
(github.event_name == 'push' && github.ref == 'refs/heads/master') ||
(github.event_name == 'workflow_dispatch')
needs:
- backport
- build
- flake8
- pytest
runs-on: ubuntu-latest
environment:
name: publish
url: https://pypi.org/p/PlexAPI-backport
permissions:
id-token: write
steps:
- name: Checkout dist
uses: actions/checkout@v4
with:
ref: dist
path: dist_branch
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of the personal token
fetch-depth: 0 # otherwise, will fail to push refs to dest repo
- name: Download backport artifacts
# this will be published to dist branch
uses: actions/download-artifact@v3
with:
name: python-plexapi-backport
path: python-plexapi-backport
- name: Download dist artifacts
# this will be published to PyPI
uses: actions/download-artifact@v3
with:
name: dist
path: dist
- name: Prepare dist branch
run: |
# empty contents
rm -f -r ./dist_branch/*
# copy build files to dist_branch
cp -f -r ./python-plexapi-backport/. ./dist_branch/
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
skip-existing: true # skip existing due to how we are triggering the workflow
- name: Deploy to dist branch
uses: actions-js/[email protected]
with:
github_token: ${{ secrets.GH_BOT_TOKEN }}
author_email: ${{ secrets.GH_BOT_EMAIL }}
author_name: ${{ secrets.GH_BOT_NAME }}
directory: dist_branch
branch: dist
force: false