o
    Uh                     @   s   d Z ddlZddlZddlZddlZddlmZ dZdZefddZ	eedfd	d
Z
edkrFeejdkr;ejd ZneZee
ed dS dS )a  Determine last version string from tags.

Alternate to `Versioneer <https://pypi.python.org/pypi/versioneer/>`_ using
`Dulwich <https://pypi.python.org/pypi/dulwich>`_ to sort tags by time from
newest to oldest.

Copy the following into the package ``__init__.py`` module::

    from dulwich.contrib.release_robot import get_current_version
    __version__ = get_current_version()

This example assumes the tags have a leading "v" like "v0.3", and that the
``.git`` folder is in a project folder that contains the package folder.

EG::

    * project
    |
    * .git
    |
    +-* package
      |
      * __init__.py  <-- put __version__ here


    N   )Repo.z[ a-zA-Z_\-]*([\d\.]+[\-\w\.]*)c              
   C   s$  t | {}| }i }| D ]h\}}|d}||}d|vr"q|dd\}}z|j}	W n ty<   |}	d}
Y n w tjt	
|jdd  |jd|jdf}
||	d }	tjt	
|	jdd  |	jd|	jd|
g||< qW d   n1 sw   Y  t| dd d	d
S )a  Get list of tags in order from newest to oldest and their datetimes.

    Args:
      projdir: path to ``.git``
    Returns:
      list of tags sorted by commit time from newest to oldest

    Each tag in the list contains the tag name, commit time, commit id, author
    and any tag meta. If a tag isn't annotated, then its tag meta is ``None``.
    Otherwise the tag meta is a tuple containing the tag time, tag id and tag
    name. Time is in UTC.
    zutf-8tags/   N   c                 S   s   | d d S )Nr   r    )tagr	   r	   q/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/dulwich/contrib/release_robot.py<lambda>o   s    z!get_recent_tags.<locals>.<lambda>T)keyreverse)r   get_refsitemsdecode
get_objectrsplitobjectAttributeErrordatetimetimegmtimetag_timeidnamecommit_timeauthorsorted)projdirprojectrefsr   r   valueobj_r
   committag_metar	   r	   r   get_recent_tags<   s8   








&r'   c              
   C   s   t | }z|d d }W n
 ty   Y dS w t||}z|d}W |S  ttfyB } z|r6|| |W  Y d}~S d}~ww )a/  Return the most recent tag, using an options regular expression pattern.

    The default pattern will strip any characters preceding the first semantic
    version. *EG*: "Release-0.2.1-rc.1" will be come "0.2.1-rc.1". If no match
    is found, then the most recent tag is return without modification.

    Args:
      projdir: path to ``.git``
      pattern: regular expression pattern with group that matches version
      logger: a Python logging instance to capture exception
    Returns:
      tag matching first group in regular expression pattern
    r   Nr   )r'   
IndexErrorrematchgroupr   	exception)r   patternloggerr   r
   matchescurrent_versionerrr	   r	   r   get_current_versionr   s    
r2   __main__r   )r   )__doc__r   r)   sysr   repor   PROJDIRPATTERNr'   r2   __name__lenargv_PROJDIRprintr	   r	   r	   r   <module>   s    6