
    ~j#                        d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	  ej
        e          Z G d de          ZddZdS )u  Brief HTML deployment to the VPS webroot.

Writes `<slug>.html` to the bind-mounted `BRIEF_OUTPUT_DIR` so it is served by
the VPS nginx under `https://just2done.com/brief/`. No `scp` / `rsync` over the
wire: in production this volume is `--volume /var/www/just2done.com/brief:/brief-output`
and we simply drop a UTF-8 file.

Idempotent — re-running with the same slug atomically replaces the file.
    )annotationsN)Path)settingsc                      e Zd ZdS )DeploymentErrorN)__name__
__module____qualname__     6/workspace/show-city-briefing-agent/app/html/deploy.pyr   r      s        Dr   r   slugstrhtmlreturnc                  K   t          t          j                                                  }|                    dd           ||  dz  }|r|                                st          d          	 t          j        |j	         ddt          |                    \  }}	 t          j        |dd	
          5 }|                    |           ddd           n# 1 swxY w Y   t          j        |d           t          j        ||           t          j                            |          rt          j        |           n9# t          j                            |          rt          j        |           w w xY wn(# t&          $ r}t          d| d|           |d}~ww xY wt(                              d|           t          |          S )zWrite `html` to `<BRIEF_OUTPUT_DIR>/<slug>.html` (atomic write).

    Returns the *absolute path of the file on disk* (inside the container).
    Public URL is built by the orchestrator from `PUBLIC_BASE_URL + '/' + slug`.
    T)parentsexist_okz.htmlzrefusing to deploy empty HTML.z.tmp)prefixsuffixdirwzutf-8)encodingNi  zfailed to write z: zdeployed brief html -> %s)r   r   BRIEF_OUTPUT_DIRresolvemkdirstripr   tempfilemkstempnamer   osfdopenwritechmodreplacepathexistsremoveOSErrorloggerinfo)r   r   	directorytargetfdtmp_pathfhexcs           r   deploy_brief_htmlr3      s
      X.//7799IOOD4O000D'F ?tzz|| ?=>>>K'&+/@/@/@UXYbUcUcdddH	$2sW555               HXu%%%Jx(((w~~h'' $	(### w~~h'' $	(####$ K K K@@@3@@AAsJK KK+V444v;;sT   3/E? #E :CE C  E #C $-E 4E? 6E;;E? ?
F$	FF$)r   r   r   r   r   r   )__doc__
__future__r   loggingr"   r   pathlibr   
app.configr   	getLoggerr   r+   RuntimeErrorr   r3   r   r   r   <module>r;      s     # " " " " "  				             		8	$	$	 	 	 	 	l 	 	 	     r   