3 �Xj�@s4ddlZddlmZmZdd�Zdd�Zdd�ZdS) �N)�PIPE�PopencCsftjjd�std��d}t|dttd�}dd�|j�D�\}}|rPtd|����t|d d �jd ��S) z� Get user count using the common CloudLinux library. This number is more accurate for systems with a control panel installed. z/opt/cloudlinux/venv/binz(CloudLinux virtual environment not foundzZ/opt/cloudlinux/venv/bin/python3 -c "from clcommon.cpapi import cpusers; print(cpusers())"T)�shell�stdout�stderrcSsg|]}|j�j��qS�)�decode�strip)�.0�resultrr� /usr/lib/python3.6/statistics.py� sz.get_users_count_from_cllib..z-Failed to get users from CloudLinux library: �z, �����) �os�path�exists� ValueErrorrrZ communicate�len�split)�cmdZprocess�output�errorsrrr �get_users_count_from_cllibs rcCsddlm}|�}t|j��S)zY Fallback method to get the user count by looking into the /etc/passwd file. r)�ClPwd)Zup2date_client.clpwdrrZ get_uid_dict)r�pwdrrr �get_users_count_generics rc Cs*y t�}Wntk r$t�}YnX|S)z� Get the total count of users on the server. Tries the CloudLinux library first, falls back to generic method if it fails. )r� Exceptionr)Z users_countrrr �count_server_users"s   r)r� subprocessrrrrrrrrr �s