ci.yml 18 KB

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