ci.yml 16 KB

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