U
    LaJ,                     @   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	Z
edZejddejdfddZdd Zdd	d
Zedkre  dS )z
.. NOTE:: To the developer:
    This module is moved to the root of the standalone zip-archive,
    to be used as its entry-point. Therefore DO NOT import relative.
    )print_functionNzpypiserver.mainc                 C   s   |p
t  }||  t |}t|jdkrZ|d k	rZt |}|| |t | |r|t 	|}|| || d S )Nr   )
logging	getLoggersetLevel	FormatterlenhandlersStreamHandlersetFormatter
addHandlerFileHandler)levelfrmtfilenamestreamlogger	formatterhandler r   7/tmp/pip-unpacked-wheel-8ds7z0ev/pypiserver/__main__.pyinit_logging   s    





r   c                   C   s
   t dS )Na    pypi-server [OPTIONS] [PACKAGES_DIRECTORY...]
    start PyPI compatible package server serving packages from
    PACKAGES_DIRECTORY. If PACKAGES_DIRECTORY is not given on the
    command line, it uses the default ~/packages.  pypiserver scans this
    directory recursively for packages. It skips packages and
    directories starting with a dot. Multiple package directories can be
    specified.

  pypi-server understands the following options:

    -p, --port PORT
      Listen on port PORT (default: 8080).

    -i, --interface INTERFACE
      Listen on interface INTERFACE (default: 0.0.0.0, any interface).

    -a, --authenticate (update|download|list), ...
      Comma-separated list of (case-insensitive) actions to authenticate.
      Requires to have set the password (-P option).
      To password-protect package downloads (in addition to uploads) while
      leaving listings public, use:
        -P foo/htpasswd.txt -a update,download
      To allow unauthorized access, use:
        -P . -a .
      Note that when uploads are not protected, the `register` command
      is not necessary, but `~/.pypirc` still need username and password fields,
      even if bogus.
      By default, only 'update' is password-protected.

    -P, --passwords PASSWORD_FILE
      Use apache htpasswd file PASSWORD_FILE to set usernames & passwords when
      authenticating certain actions (see -a option).
      To allow unauthorized access, use:
        -P . -a .

    --disable-fallback
      Disable redirect to real PyPI index for packages not found in the
      local index.

    --fallback-url FALLBACK_URL
      For packages not found in the local index, this URL will be used to
      redirect to (default: https://pypi.org/simple/).

    --server METHOD
      Use METHOD to run the server. Valid values include paste,
      cherrypy, twisted, gunicorn, gevent, wsgiref, auto. The
      default is to use "auto" which chooses one of paste, cherrypy,
      twisted or wsgiref.

    -r, --root PACKAGES_DIRECTORY
      [deprecated] Serve packages from PACKAGES_DIRECTORY.

    -o, --overwrite
      Allow overwriting existing package files.

    --hash-algo ALGO
      Any `hashlib` available algo used as fragments on package links.
      Set one of (0, no, off, false) to disabled it (default: md5).

    --welcome HTML_FILE
      Uses the ASCII contents of HTML_FILE as welcome message response.

    -v
      Enable verbose logging; repeat for more verbosity.

    --log-file FILE
      Write logging info into this FILE, as well as to stdout or stderr, if configured.

    --log-stream STREAM
      Log messages to the specified STREAM. Valid values are "stdout", "stderr", or "none"

    --log-frmt FORMAT
      The logging format-string.  (see `logging.LogRecord` class from standard python library)
      [Default: %(asctime)s|%(name)s|%(levelname)s|%(thread)d|%(message)s]

    --log-req-frmt FORMAT
      A format-string selecting Http-Request properties to log; set to  '%s' to see them all.
      [Default: %(bottle.request)s]

    --log-res-frmt FORMAT
      A format-string selecting Http-Response properties to log; set to  '%s' to see them all.
      [Default: %(status)s]

    --log-err-frmt FORMAT
      A format-string selecting Http-Error properties to log; set to  '%s' to see them all.
      [Default: %(body)s: %(exception)s 
%(traceback)s]

    --cache-control AGE
      Add "Cache-Control: max-age=AGE, public" header to package downloads.
      Pip 6+ needs this for caching.


  pypi-server -h, --help
    Show this help message.

  pypi-server --version
    Show pypi-server's version.

  pypi-server -U [OPTIONS] [PACKAGES_DIRECTORY...]
    Update packages in PACKAGES_DIRECTORY. This command searches
    pypi.org for updates and shows a pip command line which
    updates the package.

  The following additional options can be specified with -U:

    -x
      Execute the pip commands instead of only showing them.

    -d DOWNLOAD_DIRECTORY
      Download package updates to this directory. The default is to use
      the directory which contains the latest version of the package to
      be updated.

    -u
      Allow updating to unstable version (alpha, beta, rc, dev versions).
      
    --blacklist-file BLACKLIST_FILE
      Don't update packages listed in this file (one package name per line,
      without versions, '#' comments honored). This can be useful if you upload
      private packages into pypiserver, but also keep a mirror of public
      packages that you regularly update. Attempting to pull an update of
      a private package from `pypi.org` might pose a security risk - e.g. a
      malicious user might publish a higher version of the private package,
      containing arbitrary code.

  Visit https://pypi.org/project/pypiserver/ for more information.
  )textwrapdedentr   r   r   r   usage%   s    r   c                 C   s@  dd l }| d krtj} d}|jf | }d}d }d}d }zHt| dd  ddddd	d
ddddddddddddddddg\}}	W n2 tjk
r   t d }
td|
f  Y nX |D ]\}}|dkrzt	||_
W n4 tk
r   t d }
td||
f  Y nX q|dkrdd  td!|d"D |_|jd#gkrRg |_n4d$}|jD ](}||kr\d%}t||||f  q\q|d&kr||_q|d'kr|	| q|d(krd)|_q|d*kr||_q|d+kr||_q|d,kr||_q|d-krtd.|j   d S |d/kr&d0}q|d1kr6d)}q|d2krFd)}q|d3krV|}q|d4krf|}q|d5krx||_q|d6krd|_q|d7kr|||jsd n||_q|d8kr||_q|d9kr||_q|d:kr||_q|d;kr||_q|d<kr
||_ q|d=kr||_!q|d>kr.||_"q|d?krH| j#d7  _#q|d@krtt$  td q|jsz|jd#ks|jr|jd#krdA}t||j  t%|	dkr|	t&j'(dB dCd  |	D }	|	|_)t*j+t*j,t*j-t*j.g}t/t0|t1|j#dD d }d tj2tj3dE}|j|kr@tdF|jdG4|5 f  t6||j|j||j dH |d0krddIl7m8} ||	||||dJ d S |jr|j9dKrdd l:}|j;<  ddLl m=} |j|j>krtdM|jdG4|j>5 f  |?|j#dk t@A|jBt*C|jDt*j,|_E|jFf tG|}|jH||j|j
|jdN d S )ONr   ZserveT   zi:p:a:r:d:P:Uuvxohz
interface=z
passwords=zauthenticate=zport=zroot=zserver=zfallback-url=zdisable-fallback	overwritez
hash-algo=zblacklist-file=z	log-file=zlog-stream=z	log-frmt=zlog-req-frmt=zlog-res-frmt=zlog-err-frmt=zwelcome=zcache-control=versionhelpzusage error: %s)z-pz--portzInvalid port(%r) due to: %s)z-az--authenticatec                 S   s   g | ]}|r|  qS r   )lower).0ar   r   r   
<listcomp>   s   zmain.<locals>.<listcomp>z[, ]+z ,.)listdownloadupdatez*Action '%s' for option `%s` not one of %s!)z-iz--interface)z-rz--rootz--disable-fallbackFz--fallback-urlz--serverz	--welcomez	--versionzpypiserver %s
z-Ur%   z-xz-uz-dz--blacklist-file)z-Pz--passwords)z-oz--overwritez--hash-algoz
--log-filez--log-streamz
--log-frmtz--log-req-frmtz--log-res-frmtz--log-err-frmtz--cache-controlz-v)z-hz--helpzSWhen auth-ops-list is empty (-a=.), password-file (-P=%r) must also be empty ('.')!z
~/packagesc                 S   s   g | ]}t j|qS r   )ospathabspath)r   xr   r   r   r!   !  s     )nonestderrstdoutz'invalid log stream %s. choose one of %sz, )r   r   r   r   )update_all_packages)dry_runZstable_onlyZblacklist_filegevent)bottlez#unknown server %r. choose one of %s)apphostportserver)I
pypiserversysargvConfigurationZdefault_configgetoptGetoptErrorexc_infoexitintr4   	Exceptionresplitstripauthenticatedr3   appendZredirect_to_fallbackZfallback_urlr5   Zwelcome_fileprint__version__Zpassword_filer   Zstr2boolZ	hash_algoZlog_fileZ
log_streamZlog_frmtZlog_req_frmtZlog_res_frmtZlog_err_frmtZcache_control	verbosityr   r   r&   r'   
expanduserrootr   WARNINGINFODEBUGNOTSETr#   zipranger,   r-   joinkeysr   Zpypiserver.manager.   
startswithZgevent.monkeyZmonkeyZ	patch_allr1   Zserver_namesdebugftpartialZ	_logwriter   __name___stderrr2   varsrun)r8   r6   commandcZupdate_dry_runZupdate_directoryZupdate_stable_onlyZupdate_blacklist_fileoptsrootserrkvactionsr    errmsgZauth_errZverbose_levelsZ	log_levelZvalid_streamsr.   r0   r1   r2   r   r   r   main   s>   



























    
  
 
 rc   __main__)N)__doc__
__future__r   r:   r   r&   r@   r7   r   	functoolsrT   r   logrM   r,   r   r   rc   rV   r   r   r   r   <module>   s    
 
 )