|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# install_certifi.py |
| 4 | +# |
| 5 | +# sample script to install or update a set of default Root Certificates |
| 6 | +# for the ssl module. Uses the certificates provided by the certifi package: |
| 7 | +# https://pypi.python.org/pypi/certifi |
| 8 | +# |
| 9 | +# References: |
| 10 | +# |
| 11 | +# - https://stackoverflow.com/a/44649450 |
| 12 | +# - https://github.com/Unbabel/COMET/issues/29#issuecomment-945601519 |
| 13 | +# - https://github.com/python/cpython/blob/main/Mac/BuildScript/resources/install_certificates.command |
| 14 | + |
| 15 | +import os |
| 16 | +import os.path |
| 17 | +import ssl |
| 18 | +import stat |
| 19 | +import subprocess |
| 20 | +import sys |
| 21 | + |
| 22 | +STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
| 23 | + | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP |
| 24 | + | stat.S_IROTH | stat.S_IXOTH ) |
| 25 | + |
| 26 | + |
| 27 | +def main(): |
| 28 | + openssl_dir, openssl_cafile = os.path.split( |
| 29 | + ssl.get_default_verify_paths().openssl_cafile) |
| 30 | + |
| 31 | + print(" -- pip install --upgrade certifi") |
| 32 | + subprocess.check_call([sys.executable, |
| 33 | + "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"]) |
| 34 | + |
| 35 | + import certifi |
| 36 | + |
| 37 | + # change working directory to the default SSL directory |
| 38 | + os.chdir(openssl_dir) |
| 39 | + relpath_to_certifi_cafile = os.path.relpath(certifi.where()) |
| 40 | + print(" -- removing any existing file or link") |
| 41 | + try: |
| 42 | + os.remove(openssl_cafile) |
| 43 | + except FileNotFoundError: |
| 44 | + pass |
| 45 | + print(" -- creating symlink to certifi certificate bundle") |
| 46 | + os.symlink(relpath_to_certifi_cafile, openssl_cafile) |
| 47 | + print(" -- setting permissions") |
| 48 | + os.chmod(openssl_cafile, STAT_0o775) |
| 49 | + print(" -- update complete") |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + main() |
0 commit comments