Usage of subprocess with cx_freeze

67 Views Asked by At

I wrote this litte script to extract public keys from .p12 files

import os
import pandas as pd
import subprocess

def extract_public_key_from_p12_and_match_password(excel_file_path, p12_folder, output_folder):
       
    excel_data = pd.read_excel(excel_file_path)

    p12_files = [file for file in os.listdir(p12_folder) if file.endswith('.p12')]

    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    for p12_file in p12_files:
        email = p12_file.split('_')[1].replace('-', '.')

        password = excel_data.loc[excel_data['Email'] == email, 'Password'].values
        if len(password) == 0:
            print(f"Password not found {email}")
            continue

        password = password[0]

        input_path = os.path.join(p12_folder, p12_file)
        output_path = os.path.join(output_folder, f'{p12_file.replace(".p12", "_pubkey.pem")}')
        command = ['openssl', 'pkcs12', '-in', input_path, '-clcerts', '-nokeys', '-out', output_path, '-passin', f'pass:{password}']

        subprocess.Popen(command)
        print(f"Public key saved {email}.")

excel_file_path = r'.\myfile.xlsx'
p12_folder = r'.\p12-folder'
output_folder = r'.\pubcert-folder'

extract_public_key_from_p12_and_match_password(excel_file_path, p12_folder, output_folder)

If I use this script in Spyder everything runs well, but I'd like to freeze it with cx_freeze. I used this setup.py

import sys
import os.path
from cx_Freeze import setup, Executable

main_script = 'extract_pubkey.py'

executables = [Executable(main_script, base = None)]

build_exe_options = {
    "packages": ['subprocess', 'os', 'pandas', 'pip', 'cryptography'],
}

setup(
    name = 'name',
    version = '0.1',
    options={"build_exe": build_exe_options},
    executables = executables

When I run the .exe-file I got this Error:

Traceback (most recent call last):
  File "C:\Users\MyUser\Desktop\...\venv_pubcert\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 124, in run
    module_init.run(name + "__main__")
  File "C:\Users\MyUser\Desktop\...\venv_pubcert\Lib\site-packages\cx_Freeze\initscripts\console.py", line 16, in run
    exec(code, module_main.__dict__)
  File "extract_pubkey.py", line 53, in <module>
  File "extract_pubkey.py", line 44, in extract_public_key_from_p12_and_match_password
  File "C:\Users\MyUser\anaconda3\lib\subprocess.py", line 858, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\MyUser\anaconda3\lib\subprocess.py", line 1311, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden

I'm very stuck, can please anybody help?? Suggested solutions didn't help so far.

0

There are 0 best solutions below