ci.yml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. name: CI
  2. env:
  3. all-cpython-versions: 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12
  4. main-cpython-versions: 2.7, 3.2, 3.5, 3.9, 3.11
  5. pypy-versions: pypy-2.7, pypy-3.6, pypy-3.7
  6. cpython-versions: main
  7. test-set: core
  8. on:
  9. push:
  10. inputs:
  11. cpython-versions:
  12. type: string
  13. default: all
  14. test-set:
  15. type: string
  16. default: core
  17. pull_request:
  18. inputs:
  19. cpython-versions:
  20. type: string
  21. default: main
  22. test-set:
  23. type: string
  24. default: both
  25. workflow_dispatch:
  26. inputs:
  27. cpython-versions:
  28. type: choice
  29. description: CPython versions (main = 2.7, 3.2, 3.5, 3.9, 3.11)
  30. options:
  31. - all
  32. - main
  33. required: true
  34. default: main
  35. test-set:
  36. type: choice
  37. description: core, download
  38. options:
  39. - both
  40. - core
  41. - download
  42. required: true
  43. default: both
  44. permissions:
  45. contents: read
  46. jobs:
  47. select:
  48. name: Select tests from inputs
  49. runs-on: ubuntu-latest
  50. outputs:
  51. cpython-versions: ${{ steps.run.outputs.cpython-versions }}
  52. test-set: ${{ steps.run.outputs.test-set }}
  53. own-pip-versions: ${{ steps.run.outputs.own-pip-versions }}
  54. steps:
  55. - name: Make version array
  56. id: run
  57. run: |
  58. # Make a JSON Array from comma/space-separated string (no extra escaping)
  59. json_list() { \
  60. ret=""; IFS="${IFS},"; set -- $*; \
  61. for a in "$@"; do \
  62. ret=$(printf '%s"%s"' "${ret}${ret:+, }" "$a"); \
  63. done; \
  64. printf '[%s]' "$ret"; }
  65. tests="${{ inputs.test-set || env.test-set }}"
  66. [ $tests = both ] && tests="core download"
  67. printf 'test-set=%s\n' "$(json_list $tests)" >> "$GITHUB_OUTPUT"
  68. versions="${{ inputs.cpython-versions || env.cpython-versions }}"
  69. if [ "$versions" = all ]; then \
  70. versions="${{ env.all-cpython-versions }}"; else \
  71. versions="${{ env.main-cpython-versions }}"; \
  72. fi
  73. printf 'cpython-versions=%s\n' \
  74. "$(json_list ${versions}${versions:+, }${{ env.pypy-versions }})" >> "$GITHUB_OUTPUT"
  75. # versions with a special get-pip.py in a per-version subdirectory
  76. printf 'own-pip-versions=%s\n' \
  77. "$(json_list 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6)" >> "$GITHUB_OUTPUT"
  78. tests:
  79. name: Run tests
  80. needs: select
  81. permissions:
  82. contents: read
  83. packages: write
  84. runs-on: ${{ matrix.os }}
  85. env:
  86. PIP: python -m pip
  87. PIP_DISABLE_PIP_VERSION_CHECK: true
  88. PIP_NO_PYTHON_VERSION_WARNING: true
  89. strategy:
  90. fail-fast: true
  91. matrix:
  92. os: [ubuntu-20.04]
  93. python-version: ${{ fromJSON(needs.select.outputs.cpython-versions) }}
  94. python-impl: [cpython]
  95. ytdl-test-set: ${{ fromJSON(needs.select.outputs.test-set) }}
  96. run-tests-ext: [sh]
  97. include:
  98. - os: windows-2019
  99. python-version: 3.4
  100. python-impl: cpython
  101. ytdl-test-set: ${{ contains(needs.select.outputs.test-set, 'core') && 'core' || 'nocore' }}
  102. run-tests-ext: bat
  103. - os: windows-2019
  104. python-version: 3.4
  105. python-impl: cpython
  106. ytdl-test-set: ${{ contains(needs.select.outputs.test-set, 'download') && 'download' || 'nodownload' }}
  107. run-tests-ext: bat
  108. # jython
  109. - os: ubuntu-20.04
  110. python-version: 2.7
  111. python-impl: jython
  112. ytdl-test-set: ${{ contains(needs.select.outputs.test-set, 'core') && 'core' || 'nocore' }}
  113. run-tests-ext: sh
  114. - os: ubuntu-20.04
  115. python-version: 2.7
  116. python-impl: jython
  117. ytdl-test-set: ${{ contains(needs.select.outputs.test-set, 'download') && 'download' || 'nodownload' }}
  118. run-tests-ext: sh
  119. steps:
  120. - name: Checkout
  121. uses: actions/checkout@v3
  122. #-------- Python 3 -----
  123. - name: Set up supported Python ${{ matrix.python-version }}
  124. id: setup-python
  125. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version != '2.6' && matrix.python-version != '2.7' && matrix.python-version != '3.12'}}
  126. # wrap broken actions/setup-python@v4
  127. uses: ytdl-org/setup-python@v1
  128. with:
  129. python-version: ${{ matrix.python-version }}
  130. cache-build: true
  131. allow-build: info
  132. - name: Locate supported Python ${{ matrix.python-version }}
  133. if: ${{ env.pythonLocation }}
  134. shell: bash
  135. run: |
  136. echo "PYTHONHOME=${pythonLocation}" >> "$GITHUB_ENV"
  137. export expected="${{ steps.setup-python.outputs.python-path }}"
  138. dirname() { printf '%s\n' \
  139. 'import os, sys' \
  140. 'print(os.path.dirname(sys.argv[1]))' \
  141. | ${expected} - "$1"; }
  142. expd="$(dirname "$expected")"
  143. export python="$(command -v python)"
  144. [ "$expd" = "$(dirname "$python")" ] || echo "PATH=$expd:${PATH}" >> "$GITHUB_ENV"
  145. [ -x "$python" ] || printf '%s\n' \
  146. 'import os' \
  147. 'exp = os.environ["expected"]' \
  148. 'python = os.environ["python"]' \
  149. 'exps = os.path.split(exp)' \
  150. 'if python and (os.path.dirname(python) == exp[0]):' \
  151. ' exit(0)' \
  152. 'exps[1] = "python" + os.path.splitext(exps[1])[1]' \
  153. 'python = os.path.join(*exps)' \
  154. 'try:' \
  155. ' os.symlink(exp, python)' \
  156. 'except AttributeError:' \
  157. ' os.rename(exp, python)' \
  158. | ${expected} -
  159. printf '%s\n' \
  160. 'import sys' \
  161. 'print(sys.path)' \
  162. | ${expected} -
  163. #-------- Python 3.12 -
  164. - name: Set up CPython 3.12 environment
  165. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '3.12' }}
  166. shell: bash
  167. run: |
  168. PYENV_ROOT=$HOME/.local/share/pyenv
  169. echo "PYENV_ROOT=${PYENV_ROOT}" >> "$GITHUB_ENV"
  170. - name: Cache Python 3.12
  171. id: cache312
  172. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '3.12' }}
  173. uses: actions/cache@v3
  174. with:
  175. key: python-3.12
  176. path: |
  177. ${{ env.PYENV_ROOT }}
  178. - name: Build and set up Python 3.12
  179. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '3.12' && ! steps.cache312.outputs.cache-hit }}
  180. # dl and build locally
  181. shell: bash
  182. run: |
  183. # Install build environment
  184. sudo apt-get install -y build-essential llvm libssl-dev tk-dev \
  185. libncursesw5-dev libreadline-dev libsqlite3-dev \
  186. libffi-dev xz-utils zlib1g-dev libbz2-dev liblzma-dev
  187. # Download PyEnv from its GitHub repository.
  188. export PYENV_ROOT=${{ env.PYENV_ROOT }}
  189. export PATH=$PYENV_ROOT/bin:$PATH
  190. git clone "https://github.com/pyenv/pyenv.git" "$PYENV_ROOT"
  191. pyenv install 3.12.0b4
  192. - name: Locate Python 3.12
  193. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '3.12' }}
  194. shell: bash
  195. run: |
  196. PYTHONHOME="${{ env.PYENV_ROOT }}/versions/3.12.0b4"
  197. echo "PYTHONHOME=$PYTHONHOME" >> "$GITHUB_ENV"
  198. echo "PATH=${PYTHONHOME}/bin:$PATH" >> "$GITHUB_ENV"
  199. #-------- Python 2.7 --
  200. - name: Set up Python 2.7
  201. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '2.7' }}
  202. # install 2.7
  203. shell: bash
  204. run: |
  205. sudo apt-get install -y python2 python-is-python2
  206. echo "PYTHONHOME=/usr" >> "$GITHUB_ENV"
  207. #-------- Python 2.6 --
  208. - name: Set up Python 2.6 environment
  209. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '2.6' }}
  210. shell: bash
  211. run: |
  212. openssl_name=openssl-1.0.2u
  213. echo "openssl_name=${openssl_name}" >> "$GITHUB_ENV"
  214. openssl_dir=$HOME/.local/opt/$openssl_name
  215. echo "openssl_dir=${openssl_dir}" >> "$GITHUB_ENV"
  216. PYENV_ROOT=$HOME/.local/share/pyenv
  217. echo "PYENV_ROOT=${PYENV_ROOT}" >> "$GITHUB_ENV"
  218. sudo apt-get install -y openssl ca-certificates
  219. - name: Cache Python 2.6
  220. id: cache26
  221. if: ${{ matrix.python-version == '2.6' }}
  222. uses: actions/cache@v3
  223. with:
  224. key: python-2.6.9
  225. path: |
  226. ${{ env.openssl_dir }}
  227. ${{ env.PYENV_ROOT }}
  228. - name: Build and set up Python 2.6
  229. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '2.6' && ! steps.cache26.outputs.cache-hit }}
  230. # dl and build locally
  231. shell: bash
  232. run: |
  233. # Install build environment
  234. sudo apt-get install -y build-essential llvm libssl-dev tk-dev \
  235. libncursesw5-dev libreadline-dev libsqlite3-dev \
  236. libffi-dev xz-utils zlib1g-dev libbz2-dev liblzma-dev
  237. # Download and install OpenSSL 1.0.2, back in time
  238. openssl_name=${{ env.openssl_name }}
  239. openssl_targz=${openssl_name}.tar.gz
  240. openssl_dir=${{ env.openssl_dir }}
  241. openssl_inc=$openssl_dir/include
  242. openssl_lib=$openssl_dir/lib
  243. openssl_ssl=$openssl_dir/ssl
  244. curl -L "https://www.openssl.org/source/$openssl_targz" -o $openssl_targz
  245. tar -xf $openssl_targz
  246. ( cd $openssl_name; \
  247. ./config --prefix=$openssl_dir --openssldir=${openssl_dir}/ssl \
  248. --libdir=lib -Wl,-rpath=${openssl_dir}/lib shared zlib-dynamic && \
  249. make && \
  250. make install )
  251. rm -rf $openssl_name
  252. rmdir $openssl_ssl/certs && ln -s /etc/ssl/certs $openssl_ssl/certs
  253. # Download PyEnv from its GitHub repository.
  254. export PYENV_ROOT=${{ env.PYENV_ROOT }}
  255. export PATH=$PYENV_ROOT/bin:$PATH
  256. git clone "https://github.com/pyenv/pyenv.git" "$PYENV_ROOT"
  257. # Prevent pyenv build trying (and failing) to update pip
  258. export GET_PIP=get-pip-2.6.py
  259. echo 'import sys; sys.exit(0)' > ${GET_PIP}
  260. GET_PIP=$(realpath $GET_PIP)
  261. # Build and install Python
  262. export CFLAGS="-I$openssl_inc"
  263. export LDFLAGS="-L$openssl_lib"
  264. export LD_LIBRARY_PATH="$openssl_lib"
  265. pyenv install 2.6.9
  266. - name: Locate Python 2.6
  267. if: ${{ matrix.python-impl == 'cpython' && matrix.python-version == '2.6' }}
  268. shell: bash
  269. run: |
  270. PYTHONHOME="${{ env.PYENV_ROOT }}/versions/2.6.9"
  271. echo "PYTHONHOME=$PYTHONHOME" >> "$GITHUB_ENV"
  272. echo "PATH=${PYTHONHOME}/bin:$PATH" >> "$GITHUB_ENV"
  273. echo "LD_LIBRARY_PATH=${{ env.openssl_dir }}/lib${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}" >> "$GITHUB_ENV"
  274. #-------- Jython ------
  275. - name: Set up Java 8
  276. if: ${{ matrix.python-impl == 'jython' }}
  277. uses: actions/setup-java@v2
  278. with:
  279. java-version: 8
  280. distribution: 'zulu'
  281. - name: Setup Jython environment
  282. if: ${{ matrix.python-impl == 'jython' }}
  283. shell: bash
  284. run: |
  285. echo "JYTHON_ROOT=${HOME}/jython" >> "$GITHUB_ENV"
  286. echo "PIP=pip" >> "$GITHUB_ENV"
  287. - name: Cache Jython
  288. id: cachejy
  289. if: ${{ matrix.python-impl == 'jython' && matrix.python-version == '2.7' }}
  290. uses: actions/cache@v3
  291. with:
  292. # 2.7.3 now available, may solve SNI issue
  293. key: jython-2.7.1
  294. path: |
  295. ${{ env.JYTHON_ROOT }}
  296. - name: Install Jython
  297. if: ${{ matrix.python-impl == 'jython' && matrix.python-version == '2.7' && ! steps.cachejy.outputs.cache-hit }}
  298. shell: bash
  299. run: |
  300. JYTHON_ROOT="${{ env.JYTHON_ROOT }}"
  301. curl -L "https://repo1.maven.org/maven2/org/python/jython-installer/2.7.1/jython-installer-2.7.1.jar" -o jython-installer.jar
  302. java -jar jython-installer.jar -s -d "${JYTHON_ROOT}"
  303. echo "${JYTHON_ROOT}/bin" >> "$GITHUB_PATH"
  304. - name: Set up cached Jython
  305. if: ${{ steps.cachejy.outputs.cache-hit }}
  306. shell: bash
  307. run: |
  308. JYTHON_ROOT="${{ env.JYTHON_ROOT }}"
  309. echo "${JYTHON_ROOT}/bin" >> $GITHUB_PATH
  310. - name: Install supporting Python 2.7 if possible
  311. if: ${{ steps.cachejy.outputs.cache-hit }}
  312. shell: bash
  313. run: |
  314. sudo apt-get install -y python2.7 || true
  315. #-------- pip ---------
  316. - name: Set up supported Python ${{ matrix.python-version }} pip
  317. if: ${{ (matrix.python-version != '3.2' && steps.setup-python.outputs.python-path) || matrix.python-version == '2.7' }}
  318. # This step may run in either Linux or Windows
  319. shell: bash
  320. run: |
  321. echo "$PATH"
  322. echo "$PYTHONHOME"
  323. # curl is available on both Windows and Linux, -L follows redirects, -O gets name
  324. python -m ensurepip || python -m pip --version || { \
  325. get_pip="${{ contains(needs.select.outputs.own-pip-versions, matrix.python-version) && format('{0}/', matrix.python-version) || '' }}"; \
  326. curl -L -O "https://bootstrap.pypa.io/pip/${get_pip}get-pip.py"; \
  327. python get-pip.py; }
  328. - name: Set up Python 2.6 pip
  329. if: ${{ matrix.python-version == '2.6' }}
  330. shell: bash
  331. run: |
  332. python -m pip --version || { \
  333. curl -L -O "https://bootstrap.pypa.io/pip/2.6/get-pip.py"; \
  334. curl -L -O "https://files.pythonhosted.org/packages/ac/95/a05b56bb975efa78d3557efa36acaf9cf5d2fd0ee0062060493687432e03/pip-9.0.3-py2.py3-none-any.whl"; \
  335. python get-pip.py --no-setuptools --no-wheel pip-9.0.3-py2.py3-none-any.whl; }
  336. # work-around to invoke pip module on 2.6: https://bugs.python.org/issue2751
  337. echo "PIP=python -m pip.__main__" >> "$GITHUB_ENV"
  338. - name: Set up other Python ${{ matrix.python-version }} pip
  339. if: ${{ matrix.python-version == '3.2' && steps.setup-python.outputs.python-path }}
  340. shell: bash
  341. run: |
  342. python -m pip --version || { \
  343. curl -L -O "https://bootstrap.pypa.io/pip/3.2/get-pip.py"; \
  344. curl -L -O "https://files.pythonhosted.org/packages/b2/d0/cd115fe345dd6f07ec1c780020a7dfe74966fceeb171e0f20d1d4905b0b7/pip-7.1.2-py2.py3-none-any.whl"; \
  345. python get-pip.py --no-setuptools --no-wheel pip-7.1.2-py2.py3-none-any.whl; }
  346. #-------- unittest ----
  347. - name: Upgrade Unittest for Python 2.6
  348. if: ${{ matrix.python-version == '2.6' }}
  349. shell: bash
  350. run: |
  351. # Work around deprecation of support for non-SNI clients at PyPI CDN (see https://status.python.org/incidents/hzmjhqsdjqgb)
  352. $PIP -qq show unittest2 || { \
  353. for u in "65/26/32b8464df2a97e6dd1b656ed26b2c194606c16fe163c695a992b36c11cdf/six-1.13.0-py2.py3-none-any.whl" \
  354. "f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl" \
  355. "c7/a3/c5da2a44c85bfbb6eebcfc1dde24933f8704441b98fdde6528f4831757a6/linecache2-1.0.0-py2.py3-none-any.whl" \
  356. "17/0a/6ac05a3723017a967193456a2efa0aa9ac4b51456891af1e2353bb9de21e/traceback2-1.4.0-py2.py3-none-any.whl" \
  357. "72/20/7f0f433060a962200b7272b8c12ba90ef5b903e218174301d0abfd523813/unittest2-1.1.0-py2.py3-none-any.whl"; do \
  358. curl -L -O "https://files.pythonhosted.org/packages/${u}"; \
  359. $PIP install ${u##*/}; \
  360. done; }
  361. # make tests use unittest2
  362. for test in ./test/test_*.py ./test/helper.py; do
  363. sed -r -i -e '/^import unittest$/s/test/test2 as unittest/' "$test"
  364. done
  365. #-------- nose --------
  366. - name: Install nose for Python ${{ matrix.python-version }}
  367. if: ${{ (matrix.python-version != '3.2' && steps.setup-python.outputs.python-path) || matrix.python-version == '2.7' || matrix.python-version == '3.12' }}
  368. shell: bash
  369. run: |
  370. echo "$PATH"
  371. echo "$PYTHONHOME"
  372. # Use PyNose for recent Pythons instead of Nose
  373. py3ver="${{ matrix.python-version }}"
  374. py3ver=${py3ver#3.}
  375. [ "$py3ver" != "${{ matrix.python-version }}" ] && py3ver=${py3ver%.*} || py3ver=0
  376. [ "$py3ver" -ge 9 ] && nose=pynose || nose=nose
  377. $PIP -qq show $nose || $PIP install $nose
  378. - name: Install nose for other Python 2
  379. if: ${{ matrix.python-impl == 'jython' || matrix.python-version == '2.6' }}
  380. shell: bash
  381. run: |
  382. # Work around deprecation of support for non-SNI clients at PyPI CDN (see https://status.python.org/incidents/hzmjhqsdjqgb)
  383. $PIP -qq show nose || { \
  384. curl -L -O "https://files.pythonhosted.org/packages/99/4f/13fb671119e65c4dce97c60e67d3fd9e6f7f809f2b307e2611f4701205cb/nose-1.3.7-py2-none-any.whl"; \
  385. $PIP install nose-1.3.7-py2-none-any.whl; }
  386. - name: Install nose for other Python 3
  387. if: ${{ matrix.python-version == '3.2' && steps.setup-python.outputs.python-path }}
  388. shell: bash
  389. run: |
  390. $PIP -qq show nose || { \
  391. curl -L -O "https://files.pythonhosted.org/packages/15/d8/dd071918c040f50fa1cf80da16423af51ff8ce4a0f2399b7bf8de45ac3d9/nose-1.3.7-py3-none-any.whl"; \
  392. $PIP install nose-1.3.7-py3-none-any.whl; }
  393. - name: Set up nosetest test
  394. if: ${{ contains(needs.select.outputs.test-set, matrix.ytdl-test-set ) }}
  395. shell: bash
  396. run: |
  397. # set PYTHON_VER
  398. PYTHON_VER=${{ matrix.python-version }}
  399. [ "${PYTHON_VER#*-}" != "$PYTHON_VER" ] || PYTHON_VER="${{ matrix.python-impl }}-${PYTHON_VER}"
  400. echo "PYTHON_VER=$PYTHON_VER" >> "$GITHUB_ENV"
  401. echo "PYTHON_IMPL=${{ matrix.python-impl }}" >> "$GITHUB_ENV"
  402. # define a test to validate the Python version used by nosetests
  403. printf '%s\n' \
  404. 'from __future__ import unicode_literals' \
  405. 'import sys, os, platform' \
  406. 'try:' \
  407. ' import unittest2 as unittest' \
  408. 'except ImportError:' \
  409. ' import unittest' \
  410. 'class TestPython(unittest.TestCase):' \
  411. ' def setUp(self):' \
  412. ' self.ver = os.environ["PYTHON_VER"].split("-")' \
  413. ' def test_python_ver(self):' \
  414. ' self.assertEqual(["%d" % v for v in sys.version_info[:2]], self.ver[-1].split(".")[:2])' \
  415. ' self.assertTrue(sys.version.startswith(self.ver[-1]))' \
  416. ' self.assertIn(self.ver[0], ",".join((sys.version, platform.python_implementation())).lower())' \
  417. ' def test_python_impl(self):' \
  418. ' self.assertIn(platform.python_implementation().lower(), (os.environ["PYTHON_IMPL"], self.ver[0]))' \
  419. > test/test_python.py
  420. #-------- TESTS -------
  421. - name: Run tests
  422. if: ${{ contains(needs.select.outputs.test-set, matrix.ytdl-test-set ) }}
  423. continue-on-error: ${{ matrix.ytdl-test-set == 'download' || matrix.python-impl == 'jython' }}
  424. env:
  425. YTDL_TEST_SET: ${{ matrix.ytdl-test-set }}
  426. run: |
  427. ./devscripts/run_tests.${{ matrix.run-tests-ext }}
  428. flake8:
  429. name: Linter
  430. runs-on: ubuntu-latest
  431. steps:
  432. - uses: actions/checkout@v3
  433. - name: Set up Python
  434. uses: actions/setup-python@v4
  435. with:
  436. python-version: 3.9
  437. - name: Install flake8
  438. run: pip install flake8
  439. - name: Run flake8
  440. run: flake8 .