a
    »±üjü-  ã                   @  sø  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
m	Z	 ddlmZ ddlmZ ddlmZ edƒZed	ƒZed
ƒZdZdZe e e¡› de e¡› �ej¡Ze d¡Zddœdd„Zdddœdd„Zddddœdd„Zdddœdd„Zdddœdd „Zddd!œd"d#„Z ddd$œd%d&„Z!ddddddd'œd(d)„Z"ddd*œd+d,„Z#ddd*œd-d.„Z$ddd/d0œd1d2„Z%d3dœd4d5„Z&e'd6k�rôze(e&ƒ ƒ‚W nP e)e*e+ej,f�yò Z- z,e.d7e-› �e/d8ƒj0d9� e(d:ƒ‚W Y dZ-[-n
dZ-[-0 0 dS );au  Update the generated Version & contact section in about.html.

Inputs:
  data/version.json
  release_notes.md

The script replaces only the content between:
  <!-- VERSION-INFO:START -->
  <!-- VERSION-INFO:END -->

It also updates the version number in the existing footer. Everything else in
about.html, including the runtime update_status.json JavaScript, is preserved.
é    )ÚannotationsN)ÚdateÚdatetime)ÚPath)ÚAny)Ú
navigationzsite/about.htmlzdata/version.jsonzsite/release_notes.mdz<!-- VERSION-INFO:START -->z<!-- VERSION-INFO:END -->z.*?u�   (<div class="footer">Automotive Cybersecurity Dashboard Â· )(?:Version [^Â·<]+ Â· )?(Evidence-based threat analysis research prototype</div>)zargparse.Namespace©Úreturnc                  C  sT   t jdd�} | jdttd� | jdttd� | jdttd� | jddd	d
� |  ¡ S )NzDUpdate version, release notes and contact information in about.html.)Zdescriptionz--about)ÚtypeÚdefaultz	--versionz--release-notesz--checkÚ
store_truezFValidate inputs and show what would change without writing about.html.)ÚactionÚhelp)ÚargparseÚArgumentParserÚadd_argumentr   ÚDEFAULT_ABOUTÚDEFAULT_VERSIONÚDEFAULT_RELEASE_NOTESÚ
parse_args)Úparser© r   úbuild_about_page.pyr   /   s    ÿýr   r   zdict[str, Any])Úpathr	   c                 C  sj   |   ¡ std| › �ƒ‚| jddd��}t |¡}W d   ƒ n1 sD0    Y  t|tƒsftd| › �ƒ‚|S )NzVersion metadata not found: Úrúutf-8©ÚencodingzExpected a JSON object in )Úis_fileÚFileNotFoundErrorÚopenÚjsonÚloadÚ
isinstanceÚdictÚ
ValueError)r   ÚhandleÚdatar   r   r   Ú	load_json>   s    (
r(   Ústr)r'   Úkeyr	   c                 C  s,   t |  |d¡ƒ ¡ }|s(td|›d�ƒ‚|S )NÚ zMissing required field z in version metadata)r)   ÚgetÚstripr%   )r'   r*   Úvaluer   r   r   Úrequired_textH   s    r/   )r.   r	   c                 C  s&   |   ¡ } t d| ¡s"td| ›�ƒ‚| S )Nz[^\s@]+@[^\s@]+\.[^\s@]+zInvalid contact_email: )r-   ÚreÚ	fullmatchr%   )r.   r   r   r   Úvalidate_emailO   s    r2   c                 C  sD   zt  | ¡}W n ty$   |  Y S 0 |j› d| d¡› d|j› �S )z8Convert ISO YYYY-MM-DD to a stable English display date.ú z%B)r   Zfromisoformatr%   ZdayÚstrftimeZyear)r.   Zparsedr   r   r   Údisplay_dateV   s
    
r5   )Útextr	   c                 C  s.   t j| dd�}t dd|¡}t dd|¡}|S )zCRender a deliberately small, safe subset of Markdown inline syntax.T©Zquotez	`([^`]+)`z<code>\1</code>z\*\*([^*]+)\*\*z<strong>\1</strong>)ÚhtmlÚescaper0   Úsub)r6   Zrenderedr   r   r   Úinline_markdown_   s    r;   )Úmarkdownr	   c                   s.  g ‰d‰ ddœ‡ ‡fdd„}|   ¡ D ]è}| ¡ }|s<|ƒ  q$| d¡rHq$| d¡r||ƒ  ˆ dt|d	d
…  ¡ ƒ› d�¡ q$| d¡r°|ƒ  ˆ dt|dd
…  ¡ ƒ› d�¡ q$| d¡rðˆ sÌˆ d¡ d‰ ˆ dt|dd
…  ¡ ƒ› d�¡ q$|ƒ  ˆ dt|ƒ› d�¡ q$|ƒ  ˆ�s$ˆ d¡ d ˆ¡S )zARender headings, bullets and paragraphs used by release_notes.md.FÚNoner   c                     s   ˆ rˆ  d¡ d‰ d S )Nz        </ul>F)Úappendr   ©Zin_listÚoutputr   r   Ú
close_listl   s    
z)release_notes_to_html.<locals>.close_listz# z## z        <h3>é   Nz</h3>z### z        <h4>é   z</h4>)z- z* z        <ul>Tz          <li>é   z</li>z        <p>z</p>z>        <p class="small muted">No release notes available.</p>Ú
)Ú
splitlinesr-   Ú
startswithr>   r;   Újoin)r<   rA   Zraw_lineÚliner   r?   r   Úrelease_notes_to_htmlg   s<    

"
"

"
rJ   )ÚversionÚrelease_dateÚcontact_emailÚcontact_labelÚrelease_notes_htmlr	   c           	      C  sh   t j| dd�}t jt|ƒdd�}t j|dd�}t j|dd�}t› d|› d|› d|› d|› d|› dt› �S )	NTr7   aY  
  <section class="card" id="version-info">
    <h2>Version &amp; contact</h2>
    <p>Release information and contact details for the Automotive Cybersecurity Dashboard research prototype.</p>

    <div class="version-grid">
      <div class="version-item">
        <span class="version-label">Version</span>
        <span class="version-value">z�</span>
      </div>

      <div class="version-item">
        <span class="version-label">Released</span>
        <span class="version-value">zž</span>
      </div>

      <div class="version-item">
        <span class="version-label">Contact</span>
        <span class="version-value"><a href="mailto:z">zî</a></span>
      </div>
    </div>
    <p class="small muted">Any questions or suggestions? Feel free to reach out.</p>
    <details class="release-notes">
      <summary>Release notes</summary>
      <div class="release-notes-content">
z,
      </div>
    </details>
  </section>
  )r8   r9   r5   ÚSTART_MARKERÚ
END_MARKER)	rK   rL   rM   rN   rO   Z	version_eZ
released_eZemail_eZcontact_label_er   r   r   Úbuild_version_block™   s"    øóîîçãrR   )Úaboutr	   c                 C  sZ   d}d}|   |¡}|   ||d ¡}|dk s2|dk r:tdƒ‚d}| d |… | | |d …  S )NzG  <section class="card">
    <h2>The Threat Register at the centre</h2>z6  <section class="card">
    <h2>Evidence sources</h2>é   r   z:Could not locate The Threat Register at the centre sectiona¶    <section class="card">
    <h2>The Threat Register at the centre</h2>
    <p>Different evidence sources describe different parts of the cybersecurity landscape. The Threat Register brings those sources together around the risk question. In addition to observable external evidence, threats can now be related to applicable regulatory requirements and analytical regulatory mappings. This keeps evidence, risk analysis and regulatory traceability connected without treating correlation or regulatory relevance as proof of compliance.</p>
    <div class="model" aria-label="Conceptual threat, evidence and regulatory model">
      <div class="model-register">Threat Register</div>
      <div class="model-line"></div>
      <div class="model-sources">
        <span class="model-source">CVEs</span>
        <span class="model-source">Papers</span>
        <span class="model-source">Repositories</span>
        <span class="model-source">Signals</span>
        <span class="model-source">Incidents</span>
        <span class="model-source">Assessments</span>
        <span class="model-source">Narratives</span>
      </div>
      <div class="model-line"></div>
      <div class="model-sources">
        <span class="model-source">UN R155 requirements</span>
        <span class="model-source">Annex 5 analytical candidates</span>
        <span class="model-source">Regulatory Hub</span>
      </div>
      <div class="model-line"></div>
      <div class="model-sources">
        <span class="model-source">Accepted evidence</span>
        <span class="model-source">Review candidates</span>
        <span class="model-source">Evidence gaps</span>
        <span class="model-source">Expert review</span>
      </div>
    </div>
    <p class="small muted" style="margin-top:14px;">The Regulatory Hub complements this threat-level traceability by explaining the wider regulatory frameworks, official sources, expert views and unresolved regulatory questions.</p>
  </section>
)Úfindr%   )rS   ÚstartÚendZ	start_posZend_posZsectionr   r   r   Úupdate_threat_register_sectionÆ   s    
rX   c                 C  s@   t  dt j¡}|jdd„ | dd�\}}|dkr<td|› �ƒ‚|S )Nz(<header class="site-header">.*?</header>c                 S  s   t dƒS )NZAbout)Úsite_navigation©Ú_r   r   r   Ú<lambda>Ò   ó    z#update_navigation.<locals>.<lambda>rT   ©Úcountz(Expected exactly one site header; found )r0   ÚcompileÚDOTALLÚsubnr%   )rS   ÚpatternÚupdatedr_   r   r   r   Úupdate_navigationÐ   s
    re   r=   )r   r6   r	   c                 C  s¾   | j jddd� tj| jd d| j d�\}}t|ƒ}zrtj|dddd	��0}| |¡ | 	¡  t 
| ¡ ¡ W d   ƒ n1 s~0    Y  t || ¡ W | ¡ rº| ¡  n| ¡ r¸| ¡  0 d S )
NT)ÚparentsÚexist_okÚ.z.tmp)ÚprefixÚsuffixÚdirÚwr   r+   )r   Únewline)ÚparentÚmkdirÚtempfileZmkstempÚnamer   ÚosÚfdopenÚwriteÚflushÚfsyncÚfilenoÚreplaceÚexistsÚunlink)r   r6   ÚfdZ	temp_nameZ	temp_pathr&   r   r   r   Úatomic_write×   s    
,
ÿr|   Úintc                    sö  t ƒ } | j ¡ s td| j› �ƒ‚| j ¡ s:td| j› �ƒ‚t| jƒ}t|dƒ}t|dƒ}tt|dƒƒ}t	| 
d|¡ƒ ¡ p||}| jjdd�}| jjdd�}t |¡}t|ƒd	krÊtd
| j› dt|ƒ› �ƒ‚t||||t|ƒd�‰ tj‡ fdd„|d	d�}	t|	ƒ}	t|	ƒ}	dt |¡› d�}
tj|
|	d	d�\}	}|d	k�r@tdƒ‚|	|k}| j�r–td|› �ƒ td|› �ƒ td|› �ƒ td|�rˆdnd› �ƒ dS |�s¸td| j› d|› d�ƒ dS t| j|	ƒ td| j› �ƒ td|› dt|ƒ› d|› �ƒ dS ) NzAbout page not found: zRelease notes not found: rK   rL   rM   rN   r   r   rT   z+Expected exactly one VERSION-INFO block in z; found )rK   rL   rM   rN   rO   c                   s   ˆ S )Nr   rZ   ©Úblockr   r   r\     r]   zmain.<locals>.<lambda>r^   z\g<1>Version u	    Â· \g<2>z.Could not locate the expected dashboard footerz[ABOUT] Version: z[ABOUT] Release date: z[ABOUT] Contact: z[ABOUT] Would change: ZyesZnor   z[ABOUT] z already up to date (version ú)z[ABOUT] Updated z[ABOUT] Version u    Â· )r   rS   r   r   Úrelease_notesr(   rK   r/   r2   r)   r,   r-   Ú	read_textÚVERSION_BLOCK_RXÚfindallÚlenr%   rR   rJ   r:   rX   re   r8   r9   Ú	FOOTER_RXrb   ZcheckÚprintr|   r5   )ÚargsÚmetadatarK   rL   rM   rN   r�   rS   Zmatchesrd   Zfooter_replacementZfooter_countZchangedr   r~   r   Úmainæ   sZ    





ÿû
rŠ   Ú__main__zERROR: Úsys)ÚfilerD   )1Ú__doc__Z
__future__r   r   r8   r!   rr   r0   rp   r   r   Úpathlibr   Útypingr   rY   r   r   r   r   rP   rQ   r`   r9   ra   rƒ   r†   r   r(   r/   r2   r5   r;   rJ   rR   rX   re   r|   rŠ   Ú__name__Ú
SystemExitr   r%   ÚOSErrorZJSONDecodeErrorÚexcr‡   Ú
__import__Ústderrr   r   r   r   Ú<module>   sR   þÿ
	2-
:
