a
    g	g>                     @  s  d dl m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	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ erBddlmZ ddl m!Z! ddl m"Z" ddl#m$Z$ zd dl%m&Z& W n e'yj   dZ&Y n0 ee(ee( f Z)e*dZ+e*dZ,e*dZ-e*dZ.e*d Z/d!Z0e*d"Z1e*d#Z2G d$d% d%Z3G d&d' d'ej$Z4dS )(    )annotations)contextmanagerN)
ModuleType)Any)cast)Dict)Iterator)List)Optional)Sequence)Set)Tuple)TYPE_CHECKING)Union   revision)write_hooks   )util)	migration)not_none)Config)RevisionStep)	StampStep)Revision)tzz (?!\.\#|__init__)(.*\.py)(c|o)?$z(?!\.\#|__init__)(.*\.py)$z([a-f0-9]+)\.py$z(upgrade|downgrade)_([a-z0-9]+)z\w+z%(rev)s_%(slug)sz
, *|(?: +)z, *|(?: +)|\:c                   @  s  e Zd ZdZedddddddfddddd	dd
dd	dd
ddZeddddZej	dd Z
ddddZedd dddZeddd
d
d
d
d
ddddZdedddd"d#d$Zd%d&d'd(d)Zd*d+d'd,d-Zdd.d'd/d0Zd
d1d'd2d3Zd4d4d5dd6d7d8Zd
dd9d:Zd;dd<d=Zd
dd>d?Zd;dd@dAZdddBdCdDdEZdd
dBdCdFdGZd%d%dHdIdJdKZdddLdMZedNdO Zddd5ddPdQdRZddddSdTdUZ dddVdWdXZ!dYddZd[Z"dfdd
d
d	d\d
d
d]d5d.d^
d_d`Z#ddd
dYddadbdcZ$dS )gScriptDirectoryaH  Provides operations upon an Alembic script directory.

    This object is useful to get information as to current revisions,
    most notably being able to get at the "head" revision, for schemes
    that want to test if the current revision in the database is the most
    recent::

        from alembic.script import ScriptDirectory
        from alembic.config import Config
        config = Config()
        config.set_main_option("script_location", "myapp:migrations")
        script = ScriptDirectory.from_config(config)

        head_revision = script.get_current_head()



    (   NFutf-8strzOptional[int]zOptional[List[str]]boolzOptional[str]zOptional[Dict[str, str]]None)
dirfile_templatetruncate_slug_lengthversion_locations
sourcelessoutput_encodingtimezonehook_configrecursive_version_locationsreturnc
           
      C  sp   || _ || _|| _|pd| _|| _|| _t| j| _	|| _
|| _|	| _t|tjsltdtj| d S )Nr   zVPath doesn't exist: %r.  Please use the 'init' command to create a new scripts folder.)r#   r$   r&   r%   r'   r(   r   ZRevisionMap_load_revisionsrevision_mapr)   r*   r+   osaccessF_OKr   CommandErrorpathabspath)
selfr#   r$   r%   r&   r'   r(   r)   r*   r+    r6   V/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/alembic/script/base.py__init__I   s     

zScriptDirectory.__init__r,   c                 C  s*   | j }t|dkrtdn|d S d S )Nr   z"Multiple version_locations presentr   )_version_locationslenr   r2   )r5   locr6   r6   r7   versionsg   s    zScriptDirectory.versionsc                 C  s4   | j rdd | j D S tjtj| jdfS d S )Nc                 S  s   g | ]}t jt|qS r6   )r/   r3   r4   r   coerce_resource_to_filename).0locationr6   r6   r7   
<listcomp>r   s   z6ScriptDirectory._version_locations.<locals>.<listcomp>r=   )r&   r/   r3   r4   joinr#   r5   r6   r6   r7   r:   o   s
    z"ScriptDirectory._version_locationszIterator[Script]c           	      c  s   | j rdd | jD }n| jg}t }|D ]z}t| |D ]h}tj|}||v rbt	
d|  q:|| tj|}tj|}t| ||}|d u rq:|V  q:q*d S )Nc                 S  s   g | ]}t j|r|qS r6   )r/   r3   exists)r?   versr6   r6   r7   rA   {   s   z3ScriptDirectory._load_revisions.<locals>.<listcomp>zJFile %s loaded twice! ignoring. Please ensure version_locations is unique.)r&   r:   r=   setScript_list_py_dirr/   r3   realpathr   warnaddbasenamedirname_from_filename)	r5   pathsZdupesrE   	file_path	real_pathfilenamedir_namescriptr6   r6   r7   r-   y   s.    
zScriptDirectory._load_revisionsr   )configr,   c                 C  sP  | d}|du rtd| d}|dur8t|}nd}| d}|r| d}ddtjdd	d
}z|| }W n2 ty }	 ztd| |	W Y d}	~	qd}	~	0 0 |du rt	|}
qdd |	|D }
nd}
| d}|rt
t	|tjdd< | ddk}tt|| dt|| ddk| dd|
| d|di |d	S )zProduce a new :class:`.ScriptDirectory` given a :class:`.Config`
        instance.

        The :class:`.Config` need only have the ``script_location`` key
        present.

        script_locationNz0No 'script_location' key found in configuration.r%   r&   version_path_separator :;)Nspacer/   rY   rZ   zV'%s' is not a valid value for version_path_separator; expected 'space', 'os', ':', ';'c                 S  s   g | ]}|r|qS r6   r6   )r?   xr6   r6   r7   rA      s   z/ScriptDirectory.from_config.<locals>.<listcomp>prepend_sys_pathr   r+   truer$   r'   r(   r   r)   post_write_hooks)r$   r%   r'   r(   r&   r)   r*   r+   )Zget_main_optionr   r2   intr/   pathsepKeyError
ValueError_split_on_space_commasplitlist_split_on_space_comma_colonsysr3   r   r>   _default_file_templateZget_section)clsrU   rV   Ztslr%   Zversion_locations_strrW   Zsplit_on_pathZ
split_charZker&   r]   Zrvlr6   r6   r7   from_config   sv    	






zScriptDirectory.from_configzIterator[None])ancestormultiple_headsstartend
resolutionr,   c           
   
   c  sR  zd V  W n@ t jy| } zV|d u r2tt|j}|d u rFtt|j}|sNd}|||d }t||W Y d }~nd }~0  t jy } z<|sd}||p|j	t
|jd }t||W Y d }~nd }~0  t jy } z*|d u rd|j	 }t||W Y d }~nBd }~0  t jyL }	 zt|	jd |	W Y d }	~	n
d }	~	0 0 d S )NzgRequested range %(start)s:%(end)s does not refer to ancestor/descendant revisions along the same branchrn   ro   zMultiple head revisions are present for given argument '%(head_arg)s'; please specify a specific target revision, '<branchname>@%(head_arg)s' to narrow to a specific head, or 'heads' for all heads)Zhead_argheadsz(Can't locate revision identified by '%s'r   )r   ZRangeNotAncestorErrorr   r   lowerupperr   r2   ZMultipleHeadsargumentformat_as_commarr   ResolutionErrorRevisionErrorargs)
r5   rl   rm   rn   ro   rp   ZrnaZmhreerrr6   r6   r7   _catch_revision_errors   s:    	 
  z&ScriptDirectory._catch_revision_errorsbaserr   )r}   headr,   c                 c  sX   | j ||d6 | jj||dddD ]}tt|V  q$W d   n1 sJ0    Y  dS )a-  Iterate through all revisions.

        :param base: the base revision, or "base" to start from the
         empty revision.

        :param head: the head revision; defaults to "heads" to indicate
         all head revisions.  May also be "head" to indicate a single
         head revision.

        rq   TF)Z	inclusiveZassert_relative_lengthN)r|   r.   iterate_revisionsr   rG   )r5   r}   r~   revr6   r6   r7   walk_revisions  s
    
zScriptDirectory.walk_revisions
_RevIdTypezTuple[Optional[Script], ...])id_r,   c                 C  sJ   |   . tttt df | j|W  d   S 1 s<0    Y  dS )zReturn the :class:`.Script` instance with the given rev identifier,
        symbolic name, or sequence of identifiers.

        .N)r|   r   r   r
   rG   r.   get_revisionsr5   r   r6   r6   r7   r   '  s
    

zScriptDirectory.get_revisionszTuple[str, ...]zSet[Optional[Script]]c                 C  sF   |   * tttt  | j|W  d    S 1 s80    Y  d S N)r|   r   r   r
   rG   r.   Z_get_all_currentr   r6   r6   r7   get_all_current2  s    
zScriptDirectory.get_all_currentOptional[Script]c                 C  sB   |   & ttt | j|W  d   S 1 s40    Y  dS )zReturn the :class:`.Script` instance with the given rev id.

        .. seealso::

            :meth:`.ScriptDirectory.get_revisions`

        N)r|   r   r
   rG   r.   get_revisionr   r6   r6   r7   r   8  s    	
zScriptDirectory.get_revisionz%Optional[Union[str, Tuple[str, ...]]]c                 C  sX   |     | j|\}}W d   n1 s.0    Y  |s@dS |dkrL|S |d S dS )z[Convert a symbolic revision, i.e. 'head' or 'base', into
        an actual revision number.Nrr   r   )r|   r.   Z_resolve_revision_number)r5   r   r   branch_namer6   r6   r7   as_revision_numberD  s    
.z"ScriptDirectory.as_revision_numberz!Union[str, Tuple[str, ...], None]r   )rt   rs   kwr,   c                 K  s    t tt | jj||fi |S )a  Iterate through script revisions, starting at the given
        upper revision identifier and ending at the lower.

        The traversal uses strictly the `down_revision`
        marker inside each migration script, so
        it is a requirement that upper >= lower,
        else you'll get nothing back.

        The iterator yields :class:`.Script` objects.

        .. seealso::

            :meth:`.RevisionMap.iterate_revisions`

        )r   r   rG   r.   r   )r5   rt   rs   r   r6   r6   r7   r   U  s    z!ScriptDirectory.iterate_revisionsc                 C  s:   | j dd | j W  d   S 1 s,0    Y  dS )aG  Return the current head revision.

        If the script directory has multiple heads
        due to branching, an error is raised;
        :meth:`.ScriptDirectory.get_heads` should be
        preferred.

        :return: a string revision number.

        .. seealso::

            :meth:`.ScriptDirectory.get_heads`

        z}The script directory has multiple heads (due to branching).Please use get_heads(), or merge the branches using alembic merge.rm   N)r|   r.   get_current_headrC   r6   r6   r7   r   o  s    z ScriptDirectory.get_current_head	List[str]c                 C  s   t | jjS )aU  Return all "versioned head" revisions as strings.

        This is normally a list of length one,
        unless branches are present.  The
        :meth:`.ScriptDirectory.get_current_head()` method
        can be used normally when a script directory
        has only one head.

        :return: a tuple of string revision numbers.
        )rf   r.   rr   rC   r6   r6   r7   	get_heads  s    zScriptDirectory.get_headsc                 C  s4   |   }t|dkr tdn|r,|d S dS dS )a#  Return the "base" revision as a string.

        This is the revision number of the script that
        has a ``down_revision`` of None.

        If the script directory has multiple bases, an error is raised;
        :meth:`.ScriptDirectory.get_bases` should be
        preferred.

        r   z@The script directory has multiple bases. Please use get_bases().r   N)	get_basesr;   r   r2   )r5   basesr6   r6   r7   get_base  s    zScriptDirectory.get_basec                 C  s   t | jjS )zreturn all "base" revisions as strings.

        This is the revision number of all scripts that
        have a ``down_revision`` of None.

        )rf   r.   r   rC   r6   r6   r7   r     s    zScriptDirectory.get_baseszList[RevisionStep])destinationcurrent_revr,   c                   s\    j d|d:  j||dd} fddtt|D W  d    S 1 sN0    Y  d S )NzFDestination %(end)s is not a valid upgrade target from current head(s)rl   ro   T)Zimplicit_basec                   s   g | ]}t j j|qS r6   )r   MigrationStepZupgrade_from_scriptr.   r?   rT   rC   r6   r7   rA     s   z1ScriptDirectory._upgrade_revs.<locals>.<listcomp>)r|   r   reversedrf   r5   r   r   revsr6   rC   r7   _upgrade_revs  s    

zScriptDirectory._upgrade_revsc                   sT    j d|d2  j||dd} fdd|D W  d    S 1 sF0    Y  d S )NzHDestination %(end)s is not a valid downgrade target from current head(s)r   T)Zselect_for_downgradec                   s   g | ]}t j j|qS r6   )r   r   Zdowngrade_from_scriptr.   r   rC   r6   r7   rA     s   z3ScriptDirectory._downgrade_revs.<locals>.<listcomp>)r|   r   r   r6   rC   r7   _downgrade_revs  s    
zScriptDirectory._downgrade_revszList[StampStep])r   rr   r,   c              	     s   j dd  |}g }|s&d}g }t|D ]*}|r4| jjttt	 ||dd q4t
|} |pxd g}|D ]}|d u r| fdd|D  q~n
||v rq~t j|g}	t j|g}
|	|r"|
|rJ dd |D }t||jd	d	 j}|| q~q~|
|r`d
d |D }t||jdd	 j}|| q~q~td|jdd j}|| q~q~|W  d    S 1 s0    Y  d S )NzCMultiple heads are present; please specify a single target revisionr   r}   T)Zinclude_dependenciesc              	     s"   g | ]}t |jd dd jqS )NFT)r   r   r   r.   r?   r~   rC   r6   r7   rA     s   z/ScriptDirectory._stamp_revs.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r6   r   r   r6   r6   r7   rA         Fc                 S  s   g | ]
}|j qS r6   r   r   r6   r6   r7   rA   "  r   r6   )r|   r   r   to_tupleextendr.   Zfilter_for_lineager   r   rG   Zunique_listrF   Z_get_descendant_nodesZ_get_ancestor_nodesintersectionr   r   r   append)r5   r   rr   Z
heads_revsZstepsZfiltered_headsr   ZdestsdestZdescendantsZ	ancestorsZ
todo_headsstepr6   rC   r7   _stamp_revs  s|    







zScriptDirectory._stamp_revsc                 C  s   t | jd dS )zRun the script environment.

        This basically runs the ``env.py`` script present
        in the migration environment.   It is called exclusively
        by the command functions in :mod:`alembic.command`.


        env.pyN)r   load_python_filer#   rC   r6   r6   r7   run_env7  s    	zScriptDirectory.run_envc                 C  s   t jt j| jdS )Nr   )r/   r3   r4   rB   r#   rC   r6   r6   r7   env_py_locationB  s    zScriptDirectory.env_py_location)srcr   r   r,   c                 K  s.   t jdtj| t j||| jfi | d S NzGenerating %s)r   statusr/   r3   r4   Ztemplate_to_filer(   )r5   r   r   r   r6   r6   r7   _generate_templateF  s    z"ScriptDirectory._generate_template)r   r   r,   c                 C  s"   t dtj| tj|| d S r   )r   r   r/   r3   r4   shutilcopy)r5   r   r   r6   r6   r7   
_copy_fileP  s    zScriptDirectory._copy_file)r3   r,   c                 C  s0   t j|}t j|s,td| t j| d S )NzCreating directory %s)r/   r3   r4   rD   r   r   makedirs)r5   r3   r6   r6   r7   _ensure_directoryU  s    z!ScriptDirectory._ensure_directoryzdatetime.datetimec                 C  s   | j d urvtd u rtdt| j }|d u r@t| j  }|d u rXtd| j  tj jt	 d
|}n
tj }|S )Nz>The library 'python-dateutil' is required for timezone supportzCan't locate timezone: %s)tzinfo)r)   r   r   r2   Zgettzrt   datetimeutcnowreplaceZtzutc
astimezonenow)r5   r   create_dater6   r6   r7   _generate_create_dateZ  s(    


z%ScriptDirectory._generate_create_dateOptional[bool]zOptional[_RevIdType])
revidmessager~   refreshsplicebranch_labelsversion_path
depends_onr   r,   c	              
     s>  |du rd}zt | W n8 tjyR }
 zt|
jd |
W Y d}
~
n
d}
~
0 0  jddD tt	t
d df  j|}|D ]}|dksJ qW d   n1 s0    Y  tt|t|krtd	  }|du r<t jd
kr6|D ]0}|durt|t sJ tj|j} q<qtdn j}tjtj|} jD ]}tj||krV qqVtd|  jr |  ||||}|s|D ](}|dur|jstd|j q|r.  2 dd  fddt|D D }W d   n1 s"0    Y  nd} jtj jd|ft |t!t"dd |D t#|t!||tj$|dur|ndd|	  j%}|rt&'|| zt ( |}W n: tjy }
 zt|
jd |
W Y d}
~
n
d}
~
0 0 |du rdS |r.|j)s.td|j||jf  j*| |S )a  Generate a new revision file.

        This runs the ``script.py.mako`` template, given
        template arguments, and creates a new file.

        :param revid: String revision id.  Typically this
         comes from ``alembic.util.rev_id()``.
        :param message: the revision message, the one passed
         by the -m argument to the ``revision`` command.
        :param head: the head revision to generate against.  Defaults
         to the current "head" if no branches are present, else raises
         an exception.
        :param splice: if True, allow the "head" version to not be an
         actual head; otherwise, the selected head must be a head
         (e.g. endpoint) revision.
        :param refresh: deprecated.

        Nr~   r   z{Multiple heads are present; please specify the head revision on which the new revision should be based, or perform a merge.r   r   .r}   z"Duplicate head revisions specifiedr   zAMultiple version locations present, please specify --version-pathz7Path %s is not represented in current version locationszeRevision %s is not a head revision; please specify --splice to create a new branch from this revisionc                 S  s$   g | ]\}}||j v r|n|jqS r6   )r   r   )r?   r   depr6   r6   r7   rA     s   z5ScriptDirectory.generate_revision.<locals>.<listcomp>c                   s    g | ]}t  j||fqS r6   )r   r.   r   )r?   r   rC   r6   r7   rA     s   zscript.py.makoc                 s  s    | ]}|d ur|j nd V  qd S r   r   )r?   hr6   r6   r7   	<genexpr>  r   z4ScriptDirectory.generate_revision.<locals>.<genexpr>zempty message)Zup_revisiondown_revisionr   r   r   commar   zVersion %s specified branch_labels %s, however the migration file %s does not have them; have you upgraded your script.py.mako to include the 'branch_labels' section?)+rG   Zverify_rev_idr   rx   r   r2   ry   r|   r   r   r
   r.   r   r;   rF   r   r:   
isinstancer/   r3   rM   r=   normpathr4   r&   r   	_rev_pathis_headZto_listr   rB   r#   r    Ztuple_rev_as_scalartupler   rv   r*   r   Z
_run_hooks
_from_pathr   Zadd_revision)r5   r   r   r~   r   r   r   r   r   r   r{   rr   r   r   Zhead_Z	norm_pathZ	vers_pathr3   Zresolved_depends_onr_   rT   r6   rC   r7   generate_revisions  s    (
,






(
(
z!ScriptDirectory.generate_revision)r3   rev_idr   r   r,   c                 C  s   t | }dt|pd }t|| jkrP|d | j ddd d }d| j	||||j
|j|j|j|j|jd	  }tj||S )N_ r   r   z%s.py)	r   slugepochyearmonthdayhourminutesecond)r`   	timestamprB   _slug_refindallrs   r;   r%   rsplitr$   r   r   r   r   r   r   r/   r3   )r5   r3   r   r   r   r   r   rR   r6   r6   r7   r     s&    zScriptDirectory._rev_path)NNNNN)r}   rr   )NFFNNN)%__name__
__module____qualname____doc__ri   r8   propertyr=   r   Zmemoized_propertyr:   r-   classmethodrk   r   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r   4   sl   "
	O     . 	`

      " r   c                      s  e Zd ZU dZdddd fddZded< ded< d	Zd
ed< eddddZeddddZ	eddddZ
dd Zd+dddddddddZd,dddddddddZddddZed dd!d"d#d$Zed dd%d"d&d'Zed ddd!d(d)d*Z  ZS )-rG   zRepresent a single revision file in a ``versions/`` directory.

    The :class:`.Script` instance is returned by methods
    such as :meth:`.ScriptDirectory.iterate_revisions`.

    r   r    )moduler   r3   c              	     sJ   || _ || _t j||jtjt|dd ddtjt|dd ddd d S )Nr   r6   )defaultr   )r   dependencies)r   r3   superr8   r   r   r   getattr)r5   r   r   r3   	__class__r6   r7   r8   +  s    zScript.__init__r   r3   Nr   _db_current_indicatorr9   c                 C  s   t d| jd S ))Return the docstring given in the script.z

r   )rz   re   longdocrC   r6   r6   r7   docC  s    z
Script.docc                 C  s6   | j j}|r.t| j dr&|| j j}| S dS dS )r   _alembic_source_encodingr   N)r   r   hasattrdecoder   strip)r5   r   r6   r6   r7   r   I  s    zScript.longdocc                 C  s   d| j | jrdnd| jrdnd| jr(dnd| jr4dndf }| jrV|d|  f 7 }n|d|  f 7 }| jr|d	t| j 7 }| jr|d
t| j	 7 }| j
r|dt| j
f 7 }|d| jf 7 }|dddd | j D  7 }|S )NzRev: %s%s%s%s%s
 (head)r    (branchpoint) (mergepoint)
 (current)zMerges: %s
zParent: %s
zAlso depends on: %s
zBranches into: %s
zBranch names: %s
z	Path: %s
z
%s

c                 s  s   | ]}d | V  qdS )z    %sNr6   )r?   parar6   r6   r7   r   w  r   z#Script.log_entry.<locals>.<genexpr>)r   r   is_branch_pointis_merge_pointr   _format_down_revisionr   r   rv   Znextrevr   r3   rB   r   
splitlines)r5   entryr6   r6   r7   	log_entryW  s6    


zScript.log_entryc                 C  s:   d|   | j| jrdnd| jr"dnd| jr.dnd| jf S )Nz%s -> %s%s%s%s, %sr   r   r   r   )r   r   r   r   r   r   rC   r6   r6   r7   __str__{  s    zScript.__str__FTr!   )include_branchesinclude_docinclude_parentstree_indicatorshead_indicatorsr,   c                 C  s   | j }|r<| jr,d|  t| j|f }nd|  |f }|d usHJ |rf| jrf|dt| j 7 }|sn|r|d| jr|dnd| jr| jsdnd| jrdndf 7 }|r|d	| j	rd
nd| j
rdndf 7 }|r|d| j 7 }|S )Nz%s (%s) -> %sz%s -> %sz (%s)z%s%s%sr   r   z (effective head)r   z%s%sr   r   z, %s)r   r   r   r   rv   r   Z_is_real_headr   r   r   r   r   )r5   r  r  r  r  r  textr6   r6   r7   
_head_only  s<    

zScript._head_only)verboser  r  r  r  r,   c                 C  s   |r
| j S | ||||S d S r   )r   r  )r5   r  r  r  r  r  r6   r6   r7   
cmd_format  s
    zScript.cmd_formatc                 C  s   | j s
dS t| jS d S )Nz<base>)r   r   rv   Z_versioned_down_revisionsrC   r6   r6   r7   r     s    zScript._format_down_revisionr   r   )	scriptdirr3   r,   c                 C  s   t j|\}}| |||S r   )r/   r3   re   rN   )rj   r
  r3   dir_rR   r6   r6   r7   r     s    zScript._from_pathr   c                   s   g }t j|ddD ]\}}}|dr(qt|D ]}|t j|| q0|jrt j|dt jrdd |D  |	 fddt 
D  |js q|  q|S )NT)topdown__pycache__c                 S  s   h | ]}| d d qS ).r   )re   )r?   rR   r6   r6   r7   	<setcomp>  r   z&Script._list_py_dir.<locals>.<setcomp>c                 3  s.   | ]&}| d d  vrtj|V  qdS )r  r   N)re   r/   r3   rB   )r?   ZpycnamesZpy_cache_pathr6   r7   r     s   z&Script._list_py_dir.<locals>.<genexpr>)r/   walkendswithsortedr   r3   rB   r'   rD   r   listdirr+   sort)rj   r
  r3   rO   rootdirsfilesrR   r6   r  r7   rH     s"    

zScript._list_py_dir)r
  r  rR   r,   c                 C  s   |j rt|}n
t|}|s$d S |d}|j rR|ddk}|ddk}nd }}|sb|rtjtj||}tjtj||d }	|s|r|	rd S t	
||}
t|
dst|}|st	d| q|d}n|
j}t|
|tj||S )Nr   r   coFr   zCould not determine revision id from filename %s. Be sure the 'revision' variable is declared inside the script (please see 'Upgrading from Alembic 0.1 to 0.2' in the documentation).)r'   _sourceless_rev_filematch_only_source_rev_filegroupr/   r3   rD   rB   r   r   r   _legacy_revr2   r   rG   )rj   r
  r  rR   Zpy_matchZpy_filenameZis_cZis_oZ	py_existsZ
pyc_existsr   mr   r6   r6   r7   rN     s6    



zScript._from_filename)FFFTT)FFFT)r   r   r   r   r8   __annotations__r   r   r   r   r   r   r  r	  r   r   r   rH   rN   __classcell__r6   r6   r   r7   rG   "  s<   
#     )    $rG   )5
__future__r   
contextlibr   r   r/   rz   r   rh   typesr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   Zruntimer   r   rU   r   Zruntime.migrationr   r   Zscript.revisionr   Zdateutilr   ImportErrorr    r   compiler  r  r   Z_mod_def_rer   ri   rd   rg   r   rG   r6   r6   r6   r7   <module>   s`   







     s