3 گa(Y@sbUdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddl m Z ddl mZddl mZddl mZddl mZdd l mZdd l mZdd l mZdd l mZddlZddlZdd lmZddlmZddlmZddlmZddlmZe jj dZ!e!r4ddl"Z"erBddl#Z$ej%e&Z'ej(ddZ)ej(ddZ*dZ+dZ,dZ-ej.j/d{Z0ej1Z2iZ3ee4ej5f3ej6dej7Z8ee4e4fddd Z9e'j:fee4e e4gdfee4e4fd!d"d#Z;e4eddd*d+Z?e4e@e\sz1env_no_snap_for_external_calls..)rr)renvironcopyjoinsplit)Z path_namer)rr env_no_snap_for_external_callsGs  *r&)paramslogrcCsy tj|dtjtjdtd}Wn8ttfk rXddj|}||tj|YnX|j dkrddj||j |j f}||tj||j |j fS)zRun the script with the given params. :param list params: List of parameters to pass to subprocess.run :param callable log: Logger method to use for errors FT)checkstdoutstderruniversal_newlinesrzUnable to run the command: %s rzError while running %s. %s %s) subprocessrunPIPEr&OSError ValueErrorr$r ZSubprocessError returncoder*r+)r'r(procmsgrrr run_script`s"  r6)exercCsVtjj|\}}|rtj|Sx2tjdjtjD]}tjtjj||r2dSq2WdS)zDetermine whether path/name refers to an executable. :param str exe: Executable path or name :returns: If exe is a valid executable :rtype: bool rTF)rpathr%rZ is_executabler"pathsepr$)r7r8_rrr exe_exists~s  r;)dir_pathrcCs&ts tt|tkr"tj|t|<dS)zLock the directory at dir_path until program exit. :param str dir_path: path to directory :raises errors.LockError: if the lock is held by another process N)_LOCKSatexit_register_release_locksrZlock_dir)r<rrr lock_dir_until_exitsr@c CsPxBtjD]6}y |jWq dj|}tj|ddYq Xq WtjdS)Nz(Exception occurred releasing lock: {0!r}T)exc_info)r=valuesreleaseformatloggerdebugclear)Zdir_lockr5rrr r?s  r?) directorymodestrictrcCs^yt|||t|Wn@tk rX}z$tjdddtjtj|WYdd}~XnXdS)ahEnsure directory exists with proper permissions and is locked. :param str directory: Path to a directory. :param int mode: Directory mode. :param bool strict: require directory to be owned by current user :raises .errors.LockError: if the directory cannot be locked :raises .errors.Error: if the directory cannot be made or verified zException was:T)rAN) make_or_verify_dirr@r1rErFr Error PERM_ERR_FMTrD)rHrIrJerrorrrr set_up_core_dirs   rOFcCspytj||WnZtk rj}z>|jtjkrX|rZtj|| rZtjd|t|fnWYdd}~XnXdS)aMake sure directory exists with proper permissions. :param str directory: Path to a directory. :param int mode: Directory mode. :param bool strict: require directory to be owned by current user :raises .errors.Error: if a directory already exists, but has wrong permissions or owner :raises OSError: if invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. zE%s exists, but it should be owned by current user with permissions %sN) rmakedirsr1errnoEEXISTZcheck_permissionsr rLoct)rHrIrJZ exceptionrrr rKs rKw)r8rIchmodrcCsHf}|dk r|f}f}tj|tjtjBtjBf|}tj||f|S)zSafely open a file. :param str path: Path to a file. :param str mode: Same os `mode` for `open`. :param int chmod: Same as `mode` for `filesystem.open`, uses Python defaults if ``None``. N)ropenrO_CREATO_EXCLO_RDWRfdopen)r8rIrVZ open_argsZ fdopen_argsfdrrr safe_opens  r])r8 filename_patcountrVrIrcCspxjtjj|||}yt|||dtjj|fStk r^}z|jtjkrNWYdd}~XnX|d7}qWdS)N)rVrI)rr8r$r]abspathr1rRrS)r8r^r_rVrIZ current_patherrrrr _unique_files rc)r8rVrIrcs*tjj|\}t|fddd||dS)zSafely finds a unique file. :param str path: path/filename.ext :param int chmod: File mode :param str mode: Open mode :returns: tuple of file object and file name cs d|fS)Nz%04d_%sr)r_)tailrr szunique_file..r)r^r_rVrI)rr8r%rc)r8rVrIr)rer unique_files  rg)r8filenamerVrIrcsntjj|d}yt||d|fStk rR}z|jtjkrBWYdd}~XnXt|fddd||dS)aSafely finds a unique file using lineage convention. :param str path: directory path :param str filename: proposed filename :param int chmod: file mode :param str mode: open mode :returns: tuple of file object and file name (which may be modified from the requested one by appending digits to ensure uniqueness) :raises OSError: if writing files fails for an unanticipated reason, such as a full disk or a lack of permission to write to specified location. z%s.conf)rVNcs d|fS)Nz %s-%04d.confr)r_)rirr rfsz%unique_lineage_name..r`)r^r_rVrI)rr8r$r]r1rRrSrc)r8rirVrIZpreferred_pathrbr)rir unique_lineage_names  rj)r8rcCsDytj|Wn0tk r>}z|jtjkr.WYdd}~XnXdS)z!Remove a file that may not exist.N)rremover1rRENOENT)r8rbrrr safely_remove"s  rm) all_namesrc CsRt}xF|D]>}y|jt|Wq tjk rHtjd|ddYq Xq W|S)zRemoves names that aren't considered valid by Let's Encrypt. :param set all_names: all names found in the configuration :returns: all found names that are considered valid by LE :rtype: set zNot suggesting name "%s"T)rA)setaddenforce_le_validityr ConfigurationErrorrErF)rnZfiltered_namesnamerrr get_filtered_names+s  rtcCs tddS)zc Get OS name and version :returns: (os_name, os_version) :rtype: `tuple` of `str` F)pretty)get_python_os_inforrrr get_os_info<srwcCs0trtjdd}t s| r,djtddS|S)z^ Get OS name and version string for User Agent :returns: os_ua :rtype: `str` T)rur-) _USE_DISTROdistrorsr$rv)Zos_inforrr get_os_info_uaFs   rzcCstrtjjdSgS)z Get a list of strings that indicate the distribution likeness to other distributions. :returns: List of distribution acronyms :rtype: `list` of `str` r-)rxryZliker%rrrr get_systemd_os_likeTs r{/etc/os-release)varnamefilepathrc Csn|d}tjj|sdSt|d}|j}WdQRXx2|D]*}|jj|rs r>)version_stringrc CsTddtj|D}x:t|D].\}}yt|||<Wqtk rJYqXqW|S)aParses a version string into its components. This code and the returned tuple is based on the now deprecated distutils.version.LooseVersion class from the Python standard library. Two LooseVersion classes and two lists as returned by this function should compare in the same way. See https://github.com/python/cpython/blob/v3.10.0/Lib/distutils/version.py#L205-L347. :param str version_string: version string :returns: list of parsed version string components :rtype: list cSsg|]}|r|dkr|qS)rr)rrrrr sz'parse_loose_version..)_VERSION_COMPONENT_REr% enumerateintr2)rZ componentsiobjrrr parse_loose_versions rcOsttjkr|||dS)N) _INITIAL_PIDrgetpid)rrrrrr rs r)rrr)rPF)rUN)rdrU)rhrU)r|)F).N)^rrr collectionsrRZloggingrrrr.sysZtypingrrrrrrrr r r rrZcertbotr Zcertbot._internalr rZcertbot.compatrrrrxryrrZ getLoggerrrE namedtuplerrZ ANSI_SGR_BOLDZ ANSI_SGR_REDZANSI_SGR_RESETlinesepr$rMrrr=rZLockFilecompileVERBOSErr&rNr6boolr;r@r?rrOrKr]rcrgrjrmrtrwrzr{rrrvrrZActionrrrqrrrrrrr>rrrrrr s                   .      8 ";