File indexing completed on 2024-05-12 04:22:31

0001 # SPDX-FileCopyrightText: 2021 Python Software Foundation
0002 # SPDX-License-Ref: PSF-2.0 OR 0BSD
0003 
0004 # install_certifi.py
0005 #
0006 # sample script to install or update a set of default Root Certificates
0007 # for the ssl module.  Uses the certificates provided by the certifi package:
0008 #       https://pypi.org/project/certifi/
0009 
0010 import os
0011 import os.path
0012 import ssl
0013 import stat
0014 import subprocess
0015 import sys
0016 
0017 STAT_0o775 = (stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
0018               | stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
0019               | stat.S_IROTH | stat.S_IXOTH)
0020 
0021 
0022 def main():
0023     openssl_dir, openssl_cafile = os.path.split(
0024         ssl.get_default_verify_paths().openssl_cafile)
0025 
0026     print(" -- pip install --upgrade certifi")
0027     subprocess.check_call([sys.executable,
0028                            "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
0029 
0030     import certifi
0031 
0032     # change working directory to the default SSL directory
0033     os.chdir(openssl_dir)
0034     relpath_to_certifi_cafile = os.path.relpath(certifi.where())
0035     print(" -- removing any existing file or link")
0036     try:
0037         os.remove(openssl_cafile)
0038     except FileNotFoundError:
0039         pass
0040     print(" -- creating symlink to certifi certificate bundle")
0041     os.symlink(relpath_to_certifi_cafile, openssl_cafile)
0042     print(" -- setting permissions")
0043     os.chmod(openssl_cafile, STAT_0o775)
0044     print(" -- update complete")
0045 
0046 
0047 if __name__ == '__main__':
0048     main()