
    f]P                        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	 ddl
Z
ddlmZ ddlmZ ddlmZ  ej"                  d       ej"                  d	      gZd
dgZda G d de      Zed'd       Zd(dZd Zd)defdZd Zd)dZd Zd Zd*dZ d)dZ!d)dZ"d Z#d Z$d Z%d Z&d Z'd  Z(d! Z)d" Z*d# Z+d$ Z,d% Z-e.d&k(  r e-        yy)+a  pipreqs - Generate pip requirements.txt file based on imports

Usage:
    pipreqs [options] [<path>]

Arguments:
    <path>                The path to the directory containing the application
                          files for which a requirements file should be
                          generated (defaults to the current working
                          directory).

Options:
    --use-local           Use ONLY local package info instead of querying PyPI.
    --pypi-server <url>   Use custom PyPi server.
    --proxy <url>         Use Proxy, parameter will be passed to requests
                          library. You can also just set the environments
                          parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore <dirs>...    Ignore extra directories, each separated by a comma
    --no-follow-links     Do not follow symbolic links in the project
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --print               Output the list of requirements in the standard
                          output
    --force               Overwrite existing requirements.txt
    --diff <file>         Compare modules in requirements.txt to project
                          imports
    --clean <file>        Clean up requirements.txt by removing modules
                          that are not imported in project
    --mode <scheme>       Enables dynamic versioning with <compat>,
                          <gt> or <non-pin> schemes.
                          <compat> | e.g. Flask~=1.1.2
                          <gt>     | e.g. Flask>=1.1.2
                          <no-pin> | e.g. Flask
    --scan-notebooks      Look for imports in jupyter notebook files.
    )contextmanagerN)docopt)json2package)	HTTPError)__version__z^import (.+)$z!^from ((?!\.+).*?) import (?:.*)$z.pyz.pywFc                   &     e Zd ZdZef fd	Z xZS )NbconvertNotInstalledzVIn order to scan jupyter notebooks, please install the nbconvert and ipython librariesc                 $    t         |   |       y N)super__init__)selfmessage	__class__s     J/var/www/cvtools/html/venv/lib/python3.12/site-packages/pipreqs/pipreqs.pyr   zNbconvertNotInstalled.__init__A   s    !    )__name__
__module____qualname__default_messager   __classcell__)r   s   @r   r	   r	   <   s    `   / " "r   r	   c              #     K   | r| dk(  rF|rd|v rt         j                  }n;d|v rt         j                  }n&t        dj	                  |            t        | |      }	 | |t         j                  t         j                  fvr|j                          yy# |t         j                  t         j                  fvr|j                          w w xY ww)a  Open a file or ``sys.stdout`` depending on the provided filename.

    Args:
        filename (str): The path to the file that should be opened. If
            ``None`` or ``'-'``, ``sys.stdout`` or ``sys.stdin`` is
            returned depending on the desired mode. Defaults to ``None``.
        mode (str): The mode that should be used to open the file.

    Yields:
        A file handle.

    -rwzInvalid mode for file: {}N)sysstdinstdout
ValueErrorformatopenclose)filenamemodefiles      r   _openr&   E   s      x3sd{99DD[::D8??EFFHd#
		3::..JJL /4		3::..JJL /s   ACB !4C5C

Cc           	      2   t               }t               }g }d}g d}|reg }	|D ]M  }
|	j                  t        j                  j	                  t        j                  j                  |
                   O |j                  |	       t               }t        j                  | |      }|D ]  \  }}}|D cg c]	  }||vs| c}|d d  |j                  t        j                  j	                  |             |D cg c]  }t        |t              s| }}|j                  |D cg c]$  }t        j                  j                  |      d   & c}       |D cg c]  }t        ||      s| }}|D ]  }t        j                  j                  ||      }t        ||      }	 t        j                  |      }t        j                  |      D ]  }t!        |t        j"                        r-|j$                  D ]  }|j'                  |j(                          Jt!        |t        j*                        se|j'                  |j,                            |D cg c]  }|s|	 c}D ](  }|j;                  d      \  }}}|j'                  |       * |t        |      |z  z
  }t5        j<                  dj?                  |             tA        t        d      d	      5 } | D !ch c]  }!|!jC                          }"}!d d d        tE        |"z
        S c c}w c c}w c c}w c c}w # t.        $ rT}|r3t1        j2                  |       t5        j6                  d|z         Y d }~t5        j8                  d|z         |d }~ww xY wc c}w c c}!w # 1 sw Y   xY w)
NF)z.hgz.svnz.gitz.tox__pycache__envvenvz.ipynb_checkpoints)followlinksr   zFailed on file: %s.zFound packages: {0}stdlibr   )#setappendospathbasenamerealpathextendget_file_extensionswalkfile_ext_is_allowedDEFAULT_EXTENSIONSsplitextjoinread_file_contentastparse
isinstanceImportnamesaddname
ImportFrommodule	Exception	traceback	print_excloggingwarnerror	partitiondebugr    r!   striplist)#r1   encodingextra_ignore_dirsfollow_linksimportsraw_imports
candidatesignore_errorsignore_dirsignore_dirs_parsede
extensionsr6   rootdirsfilesdr%   py_filesr#   fn	file_namecontentstreenodesubnodeexcnrB   cleaned_name_packagesfxdatas#                                      r   get_all_importsrm   d   s   eG%KJM	K " 	MA%%bgg&6&6rww7G7G7J&KL	M-.$&J774\2D! dE";a{&:1;Q"''**401%*\T.A$HZ.[D\\RX277++H5a8RS#K':2z'JKK 	IT95I(H=Hyy*HHTN 5D!$

3'+zz :G'OOGLL9:#D#..9#45	> (-q1- " "^^C0aL!" #j/G34HMM'..x89	d8nc	" &a#$%a	%%& 4  [ < ]RK   '',LL!5	!ABMM"6"BCI . && &sy   3	L=L9LL$)L
L*L"BL#1L#NNN N7N#	N ,/M;!M;;N NNc                  .    t         r
t        dgz   S t        S )N.ipynb)scan_noteboooksr8    r   r   r5   r5      s    .=
*UCUUr   r`   c                     t        | t              r)t        | d|      5 }|j                         }d d d        |S t        | dg      rt        rt        | |      }S # 1 sw Y   S xY w)Nr   rO   ro   )r7   r8   r!   readrp   
ipynb_2_py)r`   rO   rj   ra   s       r   r;   r;      se    9&89)S84 	 vvxH	  O 
Y
	3i(;O		  Os   AA&c                 J    t         j                  j                  |       d   |v S )N   )r0   r1   r9   )r`   
acceptables     r   r7   r7      s!    77I&q)Z77r   c                 `    t               }|j                  |       \  }}|j                  |      S )z

    Args:
        file_name (str): notebook file path to parse as python script
        encoding  (str): encoding of file

    Returns:
        str: parsed string

    )PythonExporterfrom_filenameencode)r`   rO   exporterbodyrh   s        r   ru   ru      s/     H&&y1IT1;;x  r   c                 Z   t        | d      5 }t        j                  dj                  t	        |      | dj                  |D cg c]  }|d   	 c}                   d|z   dz   |j                  dj                  fd	|D              dz          d d d        y c c}w # 1 sw Y   y xY w)
Nr   z/Writing {num} requirements: {imports} to {file}, rB   )numr%   rR   {name}z	{version}
c              3   p   K   | ]-  }|d    r j                   di |n dj                   di | / yw)versionr   Nrq   )r    ).0itemfmts     r   	<genexpr>z-generate_requirements_file.<locals>.<genexpr>   s@       '+9o


"T"?8??;RT;RRs   36)r&   rH   rL   r    lenr:   write)r1   rR   symbolout_filerk   r   s        @r   generate_requirements_filer      s    	tS	 
X=DDLtTYYSZ?[a&	?[5\ E 	

 +-II #  		

 
 @\
 
s   5B!BAB!B!!B*c                     t        d| |       y )Nr   )r   )rR   r   s     r   output_requirementsr      s    sGV4r   c                    g }| D ]%  }	 t        j                  d|       t        j                  dj	                  ||      |      }|j
                  dk(  rPt        |j                  d      r$t        |j                  j                               }nFt        |j                        }n0|j
                  dk\  r!t        |j
                  |j                        t        j                  d	|j                  |j                  |j                         |j                  ||j                  d
       ( |S # t        $ r t        j                  d|       Y Mw xY w)NzMImport named "%s" not found locally. Trying to resolve it at the PyPI server.z{0}{1}/json)proxies   decodei,  )status_codereasonz/Package "%s" does not exist or network problemszImport named "%s" was resolved to "%s:%s" package (%s).
Please, verify manually the final list of requirements.txt to avoid possible dependency confusions.rB   r   )rH   warningrequestsgetr    r   hasattrcontentr   r   r   r   rB   latest_release_idpypi_urlr/   )rR   pypi_serverproxyresultr   responserl   s          r   get_imports_infor      s-   F I	OOb  ||M$8$8d$KUZ[H##s*8++X6'(8(8(?(?(ABD'(8(89D%%,H,@,@YY 	7 II""MM	
 	t0F0FGH5I6 M  	OOMtT	s   CD00EEc           
         g }g d}t         j                  D ]C  }t        j                  |      D ]'  \  }}}|D ]  }d|v s	t        j                  j	                  ||      }t        |d|       5 }|j                  t        j                        d   j                  d      }		 |j                         j                         j                  d      }
t               }|
D ]   }||vs|	d   |vs|j                  |       " d }t        |	      d	kD  r%|	d	   j                  d
d      j                  dd      }|j                  |	d   ||d       d d d         * F |S #  Y d d d        3xY w# 1 sw Y   @xY w)N)tests_testseggEGGinfo	top_levelr   rs   r   r   r   rw   z.dist z.egg)rB   r   exports)r   r1   r0   r6   r:   r!   splitseprt   rM   rN   r/   r   replace)rO   ri   ignorer1   rZ   r[   r\   r   rj   packagetop_level_modulesfiltered_top_level_modulesrD   r   s                 r   get_locally_installed_packagesr     s   H6F "!# !	D$  $&77<<d3DdC(; q"&**RVV"4R"8">">s"C%010@0F0Ft0L- 6:V2&7 JF &f 471:V;S : A A& IJ
 #'w<!+&-aj&8&8"&E&M&MfVX&YG !(/
+2+E/  !	"F O5%$   s7   12E0	$-E E0	%E0	-A E0	 E-"E0	-E0	0E:c                     t               }g }| D ]*  }|D ]#  }||d   v s	||d   k(  s|j                  |       % , t        |      D cg c]  \  }}|||dz   d  vs| }}}|S c c}}w )Nr   rB   rw   )r   r/   	enumerate)	rR   rO   localr   r   r   rf   iresult_uniques	            r   get_import_localr   /  s    *,EF ' 	'G wy))TWV_-Dg&	'' $-V#4P41aA8OQPMP Qs   
A&A&c                     t               }t        t        d      d      5 }t        d |D              }ddd       | D ]#  }|j	                  j                  ||             % t        |d       S # 1 sw Y   ?xY w)zGet PyPI package names from a list of imports.

    Args:
        pkgs (List[str]): List of import names.

    Returns:
        List[str]: The corresponding PyPI package names.

    mappingr   c              3   Z   K   | ]#  }|j                         j                  d        % yw):N)rM   r   r   rk   s     r   r   z get_pkg_names.<locals>.<genexpr>P  s     4QAGGIOOC(4s   )+Nc                 "    | j                         S r   lower)ss    r   <lambda>zget_pkg_names.<locals>.<lambda>V  s    	 r   key)r.   r!   r:   dictrA   r   sorted)pkgsr   rj   rl   pkgs        r   get_pkg_namesr   D  sx     UF	d9os	# 5q4!445 ' 	

488C%&'
 &1225 5s   A11A:c                     d| v r<t         d   j                  | j                               }|r|j                  d      d   } | j	                  d      d   j	                  d      d   j                         S )Nzimport r   z as r,   )REGEXPmatchrM   groupsrK   )rB   r   s     r   get_name_without_aliasr   Y  sd    Dq	

-<<?1%D>>&!!$..s3A6<<>>r   c                     t         j                  j                  t         j                  j                  t              |       S r   )r0   r1   r:   dirname__file__)rj   s    r   r:   r:   a  s$    77<<1155r   c           	         g }g d}	 t        | d      }	 |j                         D cg c]  }|dk7  s	|j                          }}|j                          D cg c]  }|d
   j                         s| }}|D ]  }t        |D cg c]  }||v  c}      s|j                  |d	d       |D ]M  }||v s|j                  |      }|d
   }	|d   j!                  dd      }
|	|
d}||vr|j                  |          |S c c}w # |j                          w xY w# t        $ r' t        d|  d       t        j                  d       Y t        $ r+}t        j                  d|  dt        |              |d	}~ww xY wc c}w c c}w )u9  Parse a requirements formatted file.

    Traverse a string until a delimiter is detected, then split at said
    delimiter, get module name by element index, create a dict consisting of
    module:version, and add dict to list of parsed modules.

    If file ´file_´ is not found in the system, the program will print a
    helpful message and end its execution immediately.

    Args:
        file_: File to parse.

    Raises:
        OSerror: If there's any issues accessing the file.

    Returns:
        list: The contents of the file, excluding comments.
    )<>=!~r   r   zFile z- was not found. Please, fix it and run again.rw   z$There was an error opening the file z: Nr   r   r   r   r   )r!   	readlinesrM   r"   FileNotFoundErrorprintr   exitOSErrorrH   rJ   strisalphaanyr/   r   r   )file_modulesdelimrj   rk   rl   rJ   yrD   module_namemodule_versions              r   parse_requirementsr   e  s   & G &E	'({{}B!T	AGGIBDBGGI.!qt||~A.D. E*qAF*+NNA$78 
	AEz$Qi!'!3!3C!<"-.I(NN6*
		  N- CGGI  eWIJK <UG2c%j\RS / +sP   D D 
DDD F3FF
D D-E?E?&E::E?c                     t        |       }t        t        |            D cg c]
  }||   d    }}t        t        |            D cg c]
  }||   d    }}t        |      t        |      z
  }|S c c}w c c}w )a>  Compare modules in a file to imported modules in a project.

    Args:
        file_ (str): File to parse for modules to be compared.
        imports (tuple): Modules being imported in the project.

    Returns:
        set: The modules not imported in the project, but do exist in the
            specified file.
    rB   )r   ranger   r.   )r   rR   r   r   modules_not_importeds        r   compare_modulesr     sz     !'G+0W+>?awqz&!?G?+0W+>?awqz&!?G?w<#g,6	 @?s   A3	A8c           	          t        | |      }t        j                  dj                  | dj	                  d |D                           y)zFDisplay the difference between modules in a file and imported modules.zBThe following modules are in {} but do not seem to be imported: {}r   c              3       K   | ]  }|  y wr   rq   r   s     r   r   zdiff.<locals>.<genexpr>  s     $E1Q$Es   N)r   rH   r   r    r:   )r   rR   r   s      r   diffr     s<    *5':LL	VE499$E0D$EEFr   c                    t        | |      }t        |      dk(  rt        j                  d| z          yt	        j
                  dj                  |            }g }	 t        | d      }	 |j                         D ]%  }|j                  |      |j                  |       ' |j                  d       |j                          |D ]  }|j                  |        	 |j                          t        j                  d| z          y# |j                          w xY w# t        $ r& t        j                   dj#                  |               w xY w)z9Remove modules that aren't imported in project from file.r   zNothing to clean in N|zr+zFailed on file: {}z(Successfully cleaned up requirements in )r   r   rH   r   recompiler:   r!   r   r   r/   seektruncater   r"   r   rJ   r    )r   rR   r   	re_removeto_writerj   r   s          r   cleanr     s   *5':
 A%+e34

388$89:IH

	[[] '??1%-OOA&' FF1IJJL 
 GGILL;eCD GGI  *11%89s   D ($D AD D/Ec                 x    | dk(  r|D cg c]
  }|d   dd }}d}||fS | dk(  rd}||fS | dk(  rd}|fS c c}w )	zDEnables dynamic versioning with <compat>, <gt> or <non-pin> schemes.no-pinrB   r   r   gtz>=compatz~=rq   )schemerR   r   r   s       r   dynamic_versioningr     sp    ELMTDLR8MM
 F?	 
4 F? 
8	F? Ns   7c                  x    t         st        j                  d       y 	 ddlma y # t
        $ r t               w xY w)Nz#Not scanning for jupyter notebooks.r   )rz   )rp   rH   r   	nbconvertrz   ImportErrorr	   rq   r   r   handle_scan_noteboooksr    s5    :;&, &#%%&s   % 9c                    | j                  d      }| j                  d      }| j                  d       }| j                  dd      at                | d   }|d}|-t        j                  j                  t        j                        }|r|j                  d      }| d	   r| d	   nt        j                  j                  |d
      }| d   s?| d	   s:| d   s5t        j                  j                  |      rt        j                  d       y t        ||||      }t        |      }t        j                  ddj                  |      z          d}d }| d   r| d   }| d   r| d   | d   d}| d   r#t        j                  d       t        ||      }	nt        j                  d       t        ||      }
|D cg c]S  }|j!                         |
D cg c]  }|d   D ]  }|  c}}vr%j!                         |
D cg c]  }|d   	 c}vrU }}}|
t#        |||      z   }	t%        |	d       }	| d   rt'        | d   |	       y | d   rt)        | d   |	       y | d    r0| j                  d       }|d!v rt+        ||	      \  }	}nt-        d"      d#}| d   r"t/        |	|       t        j0                  d$       y t3        ||	|       t        j0                  d%|z          y c c}}w c c}w c c}}w )&Nz
--encodingz--ignorez--no-follow-linksz--scan-notebooksFz<path>utf-8,z
--savepathzrequirements.txtz--printz--forcez<requirements.txt already exists, use --force to overwrite it)rO   rP   rQ   zFound imports: r   https://pypi.python.org/pypi/z--pypi-serverz--proxy)httphttpsz--use-localz9Getting package information ONLY from local installation.rs   z,Getting packages information from Local/PyPIr   rB   )r   r   c                 (    | d   j                         S )NrB   r   )rk   s    r   r   zinit.<locals>.<lambda>>  s    AfIOO,= r   r   z--diffz--cleanz--mode)r   r   r   zFInvalid argument for mode flag, use 'compat', 'gt' or 'no-pin' insteadz==z Successfully output requirementsz(Successfully saved requirements file in )r   rp   r  r0   r1   abspathcurdirr   r:   existsrH   r   rm   r   rL   r   r   r   r   r   r   r   r   r   r   r   )argsrO   rP   rQ   
input_pathr1   rT   r   r   rR   r   rk   r   
differencer   r   s                   r   initr    s   xx%H,xx 344Lhh159OhJWW__RYY/
-33C8 #<0\bggll:Oa6b 	 O\"YGGNN4 YZ +!	J z*JMM#dii
&;;<1KEO?+Ii4	?CMQR":ADE h?  


 GGIEA)E1aEaEEGGI7Aai77 


 

 *:UP[\\W"=>GH~T(^W%Id9ow'H~(#//0AOGV[  IGV,78"4&9?$FGC F7

s$   8K+K $K+<K&K+ K+c                     t        t        t              } | d   rt        j                  nt        j
                  }t        j                  |d       	 t        |        y # t        $ r t        j                  d       Y y w xY w)N)r   z--debugz%(levelname)s: %(message)s)levelr    r   )r   __doc__r   rH   DEBUGINFObasicConfigr  KeyboardInterruptr   r   )r  	log_levels     r   mainr  [  sZ    ';/D!%igllIi0LMT
 s   A B ?B __main__)Nr   )r  NT)r  )r  N)/r  
contextlibr   r0   r   r   rH   r<   rF   r   r   yargr   yarg.exceptionsr   pipreqsr   r   r   r8   rp   r  r	   r&   rm   r5   r   r;   r7   ru   r   r   r   r   r   r   r   r:   r   r   r   r   r   r  r  r  r   rq   r   r   <module>r      s  %L & 	 
 	  
     % 
"**%
&


3W(X	YV_ "K "  <G!TV 8!"
"5B&R*3*?68v (E@		&bHJ zF r   