U kf@sdZddlmZddlmZddlmZddlZddlZddlZddl m Z ddl Z ddl Z ddl Z ddlZddlmZddlmZdd lmZdd lmZdd lmZd d lmZGdddZdS)zsupport routines for the helpers in tools/. These aren't imported by the enclosing util package as the are not needed for normal library use. ) annotations)ArgumentParser) NamespaceN)Path)Any)Dict)Iterator)Optional)Union)compatc@seZdZUded<ded<ded<ded<ded <ded <d d d dZd ddddZd ddddZd dddddZd ddddZd dddddZ d d dd d!d"Z d#d#d$dd%d%dd&d'd(Z e j d)d*d+d,Ze j d-d*d.d/Zd#S)0code_writer_cmdrparserrargsboolsuppress_outputdiffs_detectedr source_rootpyproject_toml_pathstr) tool_scriptcCs^t|jj|_|jtd|_|js,tt|_|jjdddd|jjddddd dS) Nzpyproject.tomlz--stdout store_truez)Write to stdout instead of saving to file)actionhelp-cz--checkzDon't write the files back, just return the status. Return code 0 means nothing would change. Return code 1 means some files would be reformatted)rr) rparentrrexistsAssertionErrorrr add_argument)selfrr L/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/util/tool_support.py__init__+szcode_writer_cmd.__init__None)tempfilereturncCs"|t|dd|jddS)NZzimportsz--toml-config  entrypointoptions_run_console_scriptrrrr$r r r! run_zimports?s  zcode_writer_cmd.run_zimportscCs"|t|dd|jddS)Nblackz --config r&r)r+r r r! run_blackHs  zcode_writer_cmd.run_blackzDict[str, Any])pathr(r%c Cstjdk}|d}tdD]}|j|kr|}qBqtd||dd}tj||d|g}i} |jr~t j | d<| d <t j t j d d |j|j|jfg|fd t|ji| d S)zeRun a Python console application from within the process. Used for black, zimports posixr'console_scriptsz*Could not find entrypoint console_scripts.r()r0stdoutstderrrzimport %s; %s.%s()cwdN)osnamer Zimportlib_metadata_get Exceptiongetshlexsplitr subprocessDEVNULLrunsys executablemoduleattrrr) rr/r(Zis_posixZentrypoint_nameentryimplZcmdline_options_strZcmdline_options_listkwr r r!r*Qs>   z#code_writer_cmd._run_console_script)textr%cGs|jstjd|dS)N )rr?r4writejoin)rrFr r r! write_statusxszcode_writer_cmd.write_statuszUnion[str, Path])rFdestination_pathr%cCs^|jjr|j||dnB|jjr*t|n0|d|dt|j|ddd|ddS)N)sourceWriting ...utf-8 )encodingnewlinedone )rcheck _run_diffr3printrJr write_text)rrFrKr r r!write_output_file_from_text|s z+code_writer_cmd.write_output_file_from_text)r$rKr%c Cs|jjr"|j||dt|n\|jjrVt|}t|W5QRXt|n(| d|dt ||| ddS)N) source_filerMrNrS) rrTrUr6unlinkr3openrVreadrJshutilmove)rr$rKtfr r r!write_output_file_from_tempfiles    z/code_writer_cmd.write_output_file_from_tempfileN)rLrYz Optional[str])rKrLrYr%c Cs|r&t|dd}t|}W5QRXn"|dk r<|jdd}n dsHtdt|ddH}tjt||t|ddd d }t|}|rd|_t d |W5QRXdS) NrO)rQT)keependsFz!source or source_file is requiredzrP)fromfiletofilenZlinetermr2) r[list splitlinesrdifflibZ unified_diffras_posixrrVrI) rrKrLrYr_Z source_linesZdpdZ d_as_listr r r!rUs&  zcode_writer_cmd._run_diffzIterator[ArgumentParser])r%ccs |jVdS)N)rrr r r! add_argumentsszcode_writer_cmd.add_argumentszIterator[None]ccsf|j|_|jjr"d|_d|_n|jjr2d|_nd|_dV|jjrX|jrXtdn tddS)NFTr r) r parse_argsrrTrrr3r?exitrkr r r! run_programs  zcode_writer_cmd.run_program)__name__ __module__ __qualname____annotations__r"r,r.r*rJrXr`rU contextlibcontextmanagerrlror r r r!r #s(   'r )__doc__ __future__rargparserrrtrhr6pathlibrr:r]r<r?typingrrrr r r2r r r r r r!s$