├── .gitignore ├── MANIFEST.in ├── README.rst ├── docs ├── Makefile ├── make.bat └── source │ ├── conf.py │ ├── index.rst │ ├── installing.rst │ ├── intro.rst │ └── using.rst ├── flask_pypi_proxy ├── __init__.py ├── app.py ├── templates │ ├── simple.html │ └── simple_package.html ├── utils.py └── views │ ├── __init__.py │ ├── package.py │ ├── pypi.py │ └── simple.py ├── requirements.txt └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[co] 2 | 3 | # Packages 4 | *.egg 5 | *.egg-info 6 | dist 7 | build 8 | eggs 9 | parts 10 | bin 11 | var 12 | sdist 13 | develop-eggs 14 | .installed.cfg 15 | 16 | # Installer logs 17 | pip-log.txt 18 | 19 | # Unit test / coverage reports 20 | .coverage 21 | .tox 22 | 23 | #Translations 24 | *.mo 25 | 26 | #Mr Developer 27 | .mr.developer.cfg 28 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.rst 2 | recursive-include flask_pypi_proxy/templates * 3 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ================ 2 | Flask-Pypi-Proxy 3 | ================ 4 | 5 | A PyPI proxy done using Flask. It will use PyPI, and then keep the downloaded 6 | egg for future reference. After the package is downloaded from PyPI, it 7 | won't download it from PyPI again and instead use the local copy of the file. 8 | 9 | How to use it 10 | ============= 11 | 12 | To use it after the right configuration, run: 13 | 14 | .. code-block:: bash 15 | 16 | pip install -i http://mypypiserver.com/simple/ 17 | 18 | 19 | Documentation can be found here: 20 | `https://flask-pypi-proxy.readthedocs.org/en/latest/index.html 21 | `_. 22 | 23 | 24 | Advantages 25 | ========== 26 | 27 | * A local PyPI mirror to download the eggs faster and doesn't depends on 28 | PyPI or any other service. 29 | 30 | * Uploading your private python packages. This is useful if you work for a 31 | company that have eggs, but doesn't open source them :( 32 | 33 | * Uploading the compiled packages. There are some packages (lxml, Pillow) that 34 | are compiled each time that are installed. You can upload the compiled 35 | package to save some time. 36 | 37 | * It does get newer versions. Supose that you installed Flask (0.8.0), and 38 | a new release is required. This is version 0.9. Then the new package will 39 | be downloaded from PyPI, and the proxy will be updated. 40 | 41 | 42 | Special Thanks 43 | ============== 44 | 45 | Special thanks to `robin-jarry `_, 46 | `jokull `_, 47 | `michaelmior `_ and to 48 | `Tenzer `_. 49 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source 16 | 17 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 18 | 19 | help: 20 | @echo "Please use \`make ' where is one of" 21 | @echo " html to make standalone HTML files" 22 | @echo " dirhtml to make HTML files named index.html in directories" 23 | @echo " singlehtml to make a single large HTML file" 24 | @echo " pickle to make pickle files" 25 | @echo " json to make JSON files" 26 | @echo " htmlhelp to make HTML files and a HTML help project" 27 | @echo " qthelp to make HTML files and a qthelp project" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 31 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 32 | @echo " text to make text files" 33 | @echo " man to make manual pages" 34 | @echo " texinfo to make Texinfo files" 35 | @echo " info to make Texinfo files and run them through makeinfo" 36 | @echo " gettext to make PO message catalogs" 37 | @echo " changes to make an overview of all changed/added/deprecated items" 38 | @echo " linkcheck to check all external links for integrity" 39 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 40 | 41 | clean: 42 | -rm -rf $(BUILDDIR)/* 43 | 44 | html: 45 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 48 | 49 | dirhtml: 50 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 51 | @echo 52 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 53 | 54 | singlehtml: 55 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 56 | @echo 57 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 58 | 59 | pickle: 60 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 61 | @echo 62 | @echo "Build finished; now you can process the pickle files." 63 | 64 | json: 65 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 66 | @echo 67 | @echo "Build finished; now you can process the JSON files." 68 | 69 | htmlhelp: 70 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 71 | @echo 72 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 73 | ".hhp project file in $(BUILDDIR)/htmlhelp." 74 | 75 | qthelp: 76 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 77 | @echo 78 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 79 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 80 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/Flask-Pypi-Proxy.qhcp" 81 | @echo "To view the help file:" 82 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/Flask-Pypi-Proxy.qhc" 83 | 84 | devhelp: 85 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 86 | @echo 87 | @echo "Build finished." 88 | @echo "To view the help file:" 89 | @echo "# mkdir -p $$HOME/.local/share/devhelp/Flask-Pypi-Proxy" 90 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/Flask-Pypi-Proxy" 91 | @echo "# devhelp" 92 | 93 | epub: 94 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 95 | @echo 96 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 97 | 98 | latex: 99 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 100 | @echo 101 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 102 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 103 | "(use \`make latexpdf' here to do that automatically)." 104 | 105 | latexpdf: 106 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 107 | @echo "Running LaTeX files through pdflatex..." 108 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 109 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 110 | 111 | text: 112 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 113 | @echo 114 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 115 | 116 | man: 117 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 118 | @echo 119 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 120 | 121 | texinfo: 122 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 123 | @echo 124 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 125 | @echo "Run \`make' in that directory to run these through makeinfo" \ 126 | "(use \`make info' here to do that automatically)." 127 | 128 | info: 129 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 130 | @echo "Running Texinfo files through makeinfo..." 131 | make -C $(BUILDDIR)/texinfo info 132 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 133 | 134 | gettext: 135 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 136 | @echo 137 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 138 | 139 | changes: 140 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 141 | @echo 142 | @echo "The overview file is in $(BUILDDIR)/changes." 143 | 144 | linkcheck: 145 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 146 | @echo 147 | @echo "Link check complete; look for any errors in the above output " \ 148 | "or in $(BUILDDIR)/linkcheck/output.txt." 149 | 150 | doctest: 151 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 152 | @echo "Testing of doctests in the sources finished, look at the " \ 153 | "results in $(BUILDDIR)/doctest/output.txt." 154 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source 10 | set I18NSPHINXOPTS=%SPHINXOPTS% source 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. linkcheck to check all external links for integrity 37 | echo. doctest to run all doctests embedded in the documentation if enabled 38 | goto end 39 | ) 40 | 41 | if "%1" == "clean" ( 42 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 43 | del /q /s %BUILDDIR%\* 44 | goto end 45 | ) 46 | 47 | if "%1" == "html" ( 48 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 49 | if errorlevel 1 exit /b 1 50 | echo. 51 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 52 | goto end 53 | ) 54 | 55 | if "%1" == "dirhtml" ( 56 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 57 | if errorlevel 1 exit /b 1 58 | echo. 59 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 60 | goto end 61 | ) 62 | 63 | if "%1" == "singlehtml" ( 64 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 68 | goto end 69 | ) 70 | 71 | if "%1" == "pickle" ( 72 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished; now you can process the pickle files. 76 | goto end 77 | ) 78 | 79 | if "%1" == "json" ( 80 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished; now you can process the JSON files. 84 | goto end 85 | ) 86 | 87 | if "%1" == "htmlhelp" ( 88 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can run HTML Help Workshop with the ^ 92 | .hhp project file in %BUILDDIR%/htmlhelp. 93 | goto end 94 | ) 95 | 96 | if "%1" == "qthelp" ( 97 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 98 | if errorlevel 1 exit /b 1 99 | echo. 100 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 101 | .qhcp project file in %BUILDDIR%/qthelp, like this: 102 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Flask-Pypi-Proxy.qhcp 103 | echo.To view the help file: 104 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Flask-Pypi-Proxy.ghc 105 | goto end 106 | ) 107 | 108 | if "%1" == "devhelp" ( 109 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 110 | if errorlevel 1 exit /b 1 111 | echo. 112 | echo.Build finished. 113 | goto end 114 | ) 115 | 116 | if "%1" == "epub" ( 117 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 118 | if errorlevel 1 exit /b 1 119 | echo. 120 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 121 | goto end 122 | ) 123 | 124 | if "%1" == "latex" ( 125 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 129 | goto end 130 | ) 131 | 132 | if "%1" == "text" ( 133 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The text files are in %BUILDDIR%/text. 137 | goto end 138 | ) 139 | 140 | if "%1" == "man" ( 141 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 145 | goto end 146 | ) 147 | 148 | if "%1" == "texinfo" ( 149 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 150 | if errorlevel 1 exit /b 1 151 | echo. 152 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 153 | goto end 154 | ) 155 | 156 | if "%1" == "gettext" ( 157 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 158 | if errorlevel 1 exit /b 1 159 | echo. 160 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 161 | goto end 162 | ) 163 | 164 | if "%1" == "changes" ( 165 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 166 | if errorlevel 1 exit /b 1 167 | echo. 168 | echo.The overview file is in %BUILDDIR%/changes. 169 | goto end 170 | ) 171 | 172 | if "%1" == "linkcheck" ( 173 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 174 | if errorlevel 1 exit /b 1 175 | echo. 176 | echo.Link check complete; look for any errors in the above output ^ 177 | or in %BUILDDIR%/linkcheck/output.txt. 178 | goto end 179 | ) 180 | 181 | if "%1" == "doctest" ( 182 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 183 | if errorlevel 1 exit /b 1 184 | echo. 185 | echo.Testing of doctests in the sources finished, look at the ^ 186 | results in %BUILDDIR%/doctest/output.txt. 187 | goto end 188 | ) 189 | 190 | :end 191 | -------------------------------------------------------------------------------- /docs/source/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # Flask-Pypi-Proxy documentation build configuration file, created by 4 | # sphinx-quickstart on Thu Jan 17 12:02:15 2013. 5 | # 6 | # This file is execfile()d with the current directory set to its containing dir. 7 | # 8 | # Note that not all possible configuration values are present in this 9 | # autogenerated file. 10 | # 11 | # All configuration values have a default; values that are commented out 12 | # serve to show the default. 13 | 14 | import sys, os 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | #sys.path.insert(0, os.path.abspath('.')) 20 | 21 | # -- General configuration ----------------------------------------------------- 22 | 23 | # If your documentation needs a minimal Sphinx version, state it here. 24 | #needs_sphinx = '1.0' 25 | 26 | # Add any Sphinx extension module names here, as strings. They can be extensions 27 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 28 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.ifconfig', 'sphinx.ext.viewcode'] 29 | 30 | # Add any paths that contain templates here, relative to this directory. 31 | templates_path = ['_templates'] 32 | 33 | # The suffix of source filenames. 34 | source_suffix = '.rst' 35 | 36 | # The encoding of source files. 37 | #source_encoding = 'utf-8-sig' 38 | 39 | # The master toctree document. 40 | master_doc = 'index' 41 | 42 | # General information about the project. 43 | project = u'Flask-Pypi-Proxy' 44 | copyright = u'2013, Tomas Zulberti' 45 | 46 | # The version info for the project you're documenting, acts as replacement for 47 | # |version| and |release|, also used in various other places throughout the 48 | # built documents. 49 | # 50 | # The short X.Y version. 51 | version = '0.1.0' 52 | # The full version, including alpha/beta/rc tags. 53 | release = '0.1.0' 54 | 55 | # The language for content autogenerated by Sphinx. Refer to documentation 56 | # for a list of supported languages. 57 | #language = None 58 | 59 | # There are two options for replacing |today|: either, you set today to some 60 | # non-false value, then it is used: 61 | #today = '' 62 | # Else, today_fmt is used as the format for a strftime call. 63 | #today_fmt = '%B %d, %Y' 64 | 65 | # List of patterns, relative to source directory, that match files and 66 | # directories to ignore when looking for source files. 67 | exclude_patterns = [] 68 | 69 | # The reST default role (used for this markup: `text`) to use for all documents. 70 | #default_role = None 71 | 72 | # If true, '()' will be appended to :func: etc. cross-reference text. 73 | #add_function_parentheses = True 74 | 75 | # If true, the current module name will be prepended to all description 76 | # unit titles (such as .. function::). 77 | #add_module_names = True 78 | 79 | # If true, sectionauthor and moduleauthor directives will be shown in the 80 | # output. They are ignored by default. 81 | #show_authors = False 82 | 83 | # The name of the Pygments (syntax highlighting) style to use. 84 | pygments_style = 'sphinx' 85 | 86 | # A list of ignored prefixes for module index sorting. 87 | #modindex_common_prefix = [] 88 | 89 | 90 | # -- Options for HTML output --------------------------------------------------- 91 | 92 | # The theme to use for HTML and HTML Help pages. See the documentation for 93 | # a list of builtin themes. 94 | html_theme = 'default' 95 | 96 | # Theme options are theme-specific and customize the look and feel of a theme 97 | # further. For a list of options available for each theme, see the 98 | # documentation. 99 | #html_theme_options = {} 100 | 101 | # Add any paths that contain custom themes here, relative to this directory. 102 | #html_theme_path = [] 103 | 104 | # The name for this set of Sphinx documents. If None, it defaults to 105 | # " v documentation". 106 | #html_title = None 107 | 108 | # A shorter title for the navigation bar. Default is the same as html_title. 109 | #html_short_title = None 110 | 111 | # The name of an image file (relative to this directory) to place at the top 112 | # of the sidebar. 113 | #html_logo = None 114 | 115 | # The name of an image file (within the static path) to use as favicon of the 116 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 117 | # pixels large. 118 | #html_favicon = None 119 | 120 | # Add any paths that contain custom static files (such as style sheets) here, 121 | # relative to this directory. They are copied after the builtin static files, 122 | # so a file named "default.css" will overwrite the builtin "default.css". 123 | html_static_path = ['_static'] 124 | 125 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 126 | # using the given strftime format. 127 | #html_last_updated_fmt = '%b %d, %Y' 128 | 129 | # If true, SmartyPants will be used to convert quotes and dashes to 130 | # typographically correct entities. 131 | #html_use_smartypants = True 132 | 133 | # Custom sidebar templates, maps document names to template names. 134 | #html_sidebars = {} 135 | 136 | # Additional templates that should be rendered to pages, maps page names to 137 | # template names. 138 | #html_additional_pages = {} 139 | 140 | # If false, no module index is generated. 141 | #html_domain_indices = True 142 | 143 | # If false, no index is generated. 144 | #html_use_index = True 145 | 146 | # If true, the index is split into individual pages for each letter. 147 | #html_split_index = False 148 | 149 | # If true, links to the reST sources are added to the pages. 150 | #html_show_sourcelink = True 151 | 152 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 153 | #html_show_sphinx = True 154 | 155 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 156 | #html_show_copyright = True 157 | 158 | # If true, an OpenSearch description file will be output, and all pages will 159 | # contain a tag referring to it. The value of this option must be the 160 | # base URL from which the finished HTML is served. 161 | #html_use_opensearch = '' 162 | 163 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 164 | #html_file_suffix = None 165 | 166 | # Output file base name for HTML help builder. 167 | htmlhelp_basename = 'Flask-Pypi-Proxydoc' 168 | 169 | 170 | # -- Options for LaTeX output -------------------------------------------------- 171 | 172 | latex_elements = { 173 | # The paper size ('letterpaper' or 'a4paper'). 174 | #'papersize': 'letterpaper', 175 | 176 | # The font size ('10pt', '11pt' or '12pt'). 177 | #'pointsize': '10pt', 178 | 179 | # Additional stuff for the LaTeX preamble. 180 | #'preamble': '', 181 | } 182 | 183 | # Grouping the document tree into LaTeX files. List of tuples 184 | # (source start file, target name, title, author, documentclass [howto/manual]). 185 | latex_documents = [ 186 | ('index', 'Flask-Pypi-Proxy.tex', u'Flask-Pypi-Proxy Documentation', 187 | u'Tomas Zulberti', 'manual'), 188 | ] 189 | 190 | # The name of an image file (relative to this directory) to place at the top of 191 | # the title page. 192 | #latex_logo = None 193 | 194 | # For "manual" documents, if this is true, then toplevel headings are parts, 195 | # not chapters. 196 | #latex_use_parts = False 197 | 198 | # If true, show page references after internal links. 199 | #latex_show_pagerefs = False 200 | 201 | # If true, show URL addresses after external links. 202 | #latex_show_urls = False 203 | 204 | # Documents to append as an appendix to all manuals. 205 | #latex_appendices = [] 206 | 207 | # If false, no module index is generated. 208 | #latex_domain_indices = True 209 | 210 | 211 | # -- Options for manual page output -------------------------------------------- 212 | 213 | # One entry per manual page. List of tuples 214 | # (source start file, name, description, authors, manual section). 215 | man_pages = [ 216 | ('index', 'flask-pypi-proxy', u'Flask-Pypi-Proxy Documentation', 217 | [u'Tomas Zulberti'], 1) 218 | ] 219 | 220 | # If true, show URL addresses after external links. 221 | #man_show_urls = False 222 | 223 | 224 | # -- Options for Texinfo output ------------------------------------------------ 225 | 226 | # Grouping the document tree into Texinfo files. List of tuples 227 | # (source start file, target name, title, author, 228 | # dir menu entry, description, category) 229 | texinfo_documents = [ 230 | ('index', 'Flask-Pypi-Proxy', u'Flask-Pypi-Proxy Documentation', 231 | u'Tomas Zulberti', 'Flask-Pypi-Proxy', 'One line description of project.', 232 | 'Miscellaneous'), 233 | ] 234 | 235 | # Documents to append as an appendix to all manuals. 236 | #texinfo_appendices = [] 237 | 238 | # If false, no module index is generated. 239 | #texinfo_domain_indices = True 240 | 241 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 242 | #texinfo_show_urls = 'footnote' 243 | 244 | 245 | # Example configuration for intersphinx: refer to the Python standard library. 246 | intersphinx_mapping = {'http://docs.python.org/': None} 247 | -------------------------------------------------------------------------------- /docs/source/index.rst: -------------------------------------------------------------------------------- 1 | .. Flask-Pypi-Proxy documentation master file, created by 2 | sphinx-quickstart on Thu Jan 17 12:02:15 2013. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to Flask-Pypi-Proxy's documentation! 7 | ============================================ 8 | 9 | Contents: 10 | 11 | .. toctree:: 12 | :maxdepth: 2 13 | 14 | intro 15 | installing 16 | using 17 | 18 | 19 | Indices and tables 20 | ================== 21 | 22 | * :ref:`genindex` 23 | * :ref:`modindex` 24 | * :ref:`search` 25 | 26 | -------------------------------------------------------------------------------- /docs/source/installing.rst: -------------------------------------------------------------------------------- 1 | ========= 2 | Deploying 3 | ========= 4 | 5 | Flask-Pypi-Proxy is a normal Python application, so it can be deployed 6 | as any other Flask application. For more information, you can check here: 7 | `http://flask.pocoo.org/docs/deploying/ `_ 8 | 9 | 10 | Configuration 11 | ============= 12 | 13 | The project uses the environment key **FLASK_PYPI_PROXY_CONFIG** that references 14 | the path where the configuration file is. This file is in JSON format with the 15 | following keys: 16 | 17 | BASE_FOLDER_PATH 18 | the base path where all the eggs will be stored. For example: 19 | /home/pypi/eggs. For each egg name, a subfolder will be created 20 | inside this folder. This value is required. 21 | 22 | PRIVATE_EGGS 23 | a list with all the private eggs. For these eggs, PyPI won't be used 24 | and it will only answer with local data. By default, there 25 | are no private eggs, so everytime it will hit PyPI. This is useful 26 | for private projects, or for eggs that were uploaded after compilation 27 | (lxml, PIL, etc...) 28 | 29 | PYPI_URL 30 | the URL where from where the eggs should be downloaded. By default it 31 | uses: http://pypi.python.org but it can be changed to any other. This might 32 | be useful, for example if you have a development/local proxy, that 33 | uses the production proxy. 34 | 35 | LOGGING_PATH 36 | the complete filepath of the logging for the application. This file must 37 | include the path and the filename. This value is required. 38 | 39 | LOGGING_LEVEL 40 | the string that represents the logging level that the application 41 | should use. In this case, it should be a string: DEBUG, INFO, WARNING, 42 | ERROR. By default, it uses DEBUG. 43 | 44 | SHOULD_USE_EXISTING 45 | if True, then when getting the index for a package, ie the different 46 | versions, it will use the ones that exists on the local repo instead 47 | of using the information found on PYPI_URL. If False, and the package 48 | isn't private, then it will use the PYPI_URL to get the file package. 49 | Setting this value to True might do the things a little faster, but 50 | on the other hand, it might get you into trouble if trying to download 51 | a version of a package that doesn't exist, but other versions do exist. 52 | For example, you download version 1.4.2 of Django. If this value is 53 | set to True, then if you try to download version 1.5, it will return 54 | that it doesn't exist. 55 | 56 | If you don't want to use a configuration file, then environment variables 57 | could be used. The variable names are the same as the keys in the 58 | configuration file, but they take the prefix: **PYPI_PROXY**. So the 59 | variable names used are: 60 | 61 | * PYPI_PROXY_BASE_FOLDER_PATH 62 | * PYPI_PROXY_LOGGING_PATH 63 | * PYPI_PROXY_LOGGING_LEVEL 64 | * PYPI_PROXY_PRIVATE_EGGS 65 | * PYPI_PROXY_PYPI_URL 66 | * PYPI_PROXY_SHOULD_USE_EXISTING 67 | 68 | If the configuration file exists, then the values that are in the file 69 | will be used and not the values of the system environment. 70 | 71 | An example of the file could be: 72 | 73 | .. code-block:: json 74 | 75 | { 76 | BASE_FOLDER_PATH: '/mnt/eggs/', 77 | LOGGING_PATH: '/mnt/eggs/debug.log', 78 | PRIVATE_EGGS: [ 79 | 'miproject1', 80 | 'miproject2', 81 | 'miproject3' 82 | ], 83 | PYPI_URL: 'https://pypy.miserver.com' 84 | } 85 | 86 | But a more common configuration file, will be: 87 | 88 | .. code-block:: json 89 | 90 | { 91 | BASE_FOLDER_PATH: '/mnt/eggs/', 92 | LOGGING_PATH: '/mnt/eggs/debug.log', 93 | } 94 | 95 | 96 | Debian/Ubuntu example installation 97 | ================================== 98 | 99 | This is a **VERY** simple/basic configuration. It doesn't provide any 100 | authentication, so this shouldn't be used on production. 101 | 102 | .. code-block:: bash 103 | 104 | >>> sudo apt-get install apache2 libapache2-mod-wsgi 105 | >>> sudo apt-get install python-setuptools python-dev libxml2-dev libxslt-dev 106 | >>> sudo easy_install Flask-Pypi-Proxy 107 | 108 | >>> mkdir -p /mnt/eggs/ 109 | >>> sudo chown www-data:www-data -R /mnt/eggs/ 110 | 111 | Now, lets create the WSGI configuration file (in this example, I will 112 | create it on /mnt/eggs/flask_pypi_proxy.wsgi). The content of that file 113 | will be something like: 114 | 115 | .. code-block:: python 116 | 117 | import os 118 | 119 | os.environ['PYPI_PROXY_BASE_FOLDER_PATH'] = '/mnt/eggs/' 120 | os.environ['PYPI_PROXY_LOGGING_PATH'] = '/mnt/eggs/proxy.logs' 121 | 122 | # if installed inside a virtualenv, then do this: 123 | # activate_this = 'VIRTUALENENV_PATH/bin/activate_this.py' 124 | # execfile(activate_this, dict(__file__=activate_this)) 125 | 126 | from flask_pypi_proxy.views import app as application 127 | 128 | Finally, the Apache configuration. Create a file at 129 | /etc/apache2/sites-enabled/flask_pypi_proxy with the following content: 130 | 131 | :: 132 | 133 | 134 | WSGIDaemonProcess pypi_proxy threads=5 135 | WSGIScriptAlias / /mnt/eggs/flask_pypi_proxy.wsgi 136 | 137 | 138 | Restart Apache 139 | 140 | .. code-block:: bash 141 | 142 | >>> sudo service apache2 restart 143 | 144 | 145 | More advanced configuration 146 | =========================== 147 | 148 | The following steps will show you how to install this service inside a 149 | virtualenv, also using HTTP basic auth to create some security for the eggs. 150 | 151 | .. code-block:: bash 152 | 153 | >>> sudo apt-get install apache2 libapache2-mod-wsgi 154 | >>> sudo apt-get install python-setuptools python-dev libxml2-dev libxslt-dev 155 | 156 | Now, create the user where the virtualenv will be installed: 157 | 158 | .. code-block:: bash 159 | 160 | >>> sudo adduser pypi-proxy 161 | Adding user `pypi-proxy' ... 162 | Adding new group `pypi-proxy' (1001) ... 163 | Adding new user `pypi-proxy' (1001) with group `pypi-proxy' ... 164 | Creating home directory `/home/pypi-proxy' ... 165 | Copying files from `/etc/skel' ... 166 | Enter new UNIX password: 167 | Retype new UNIX password: 168 | >>> sudo easy_install virtualenv 169 | >>> sudo su - pypi-proxy 170 | 171 | The following steps will be executed as **pypi-proxy**: 172 | 173 | .. code-block:: bash 174 | 175 | mkdir ~/envs 176 | virtualenv ~/envs/proxy 177 | source ~/envs/proxy/bin/activate 178 | pip instal Flask-Pypi-Proxy 179 | mkdir /home/pypi-proxy/eggs/ # where the eggs will be 180 | chgrp www-data /home/pypi-proxy/eggs/ 181 | chmod 775 /home/pypi-proxy/eggs/ 182 | mkdir /home/pypi-proxy/logs/ # the same but for the logs files 183 | chgrp www-data /home/pypi-proxy/logs/ 184 | chmod 775 /home/pypi-proxy/logs/ 185 | 186 | htpasswd -c /home/pypi-proxy/htpasswd.file MY_USERNAME # creates the password file 187 | sudo chown www-data:www-data /home/pypi-proxy/htpasswd.file 188 | sudo chmod 620 /home/pypi-proxy/htpasswd.file 189 | 190 | Under the same user, lets create the WSGI file (for this example, I will 191 | put it on /home/pypi-proxy/pypi-proxy.wsgi). The content of this file is 192 | as follows: 193 | 194 | .. code-block:: python 195 | 196 | import os 197 | 198 | os.environ['PYPI_PROXY_BASE_FOLDER_PATH'] = '/home/pypi-proxy/eggs/' 199 | os.environ['PYPI_PROXY_LOGGING_PATH'] = '/home/pypi-proxy/logs/proxy.log' 200 | 201 | # if installed inside a virtualenv, then do this: 202 | activate_this = '/home/pypi-proxy/envs/proxy/bin/activate_this.py' 203 | execfile(activate_this, dict(__file__=activate_this)) 204 | 205 | from flask_pypi_proxy.views import app as application 206 | 207 | 208 | Now return to the normal user, and create the following Apache configuration 209 | (/etc/apache2/sites-enabled/flask_pypi_proxy): 210 | 211 | :: 212 | 213 | 214 | 215 | AuthType Basic 216 | AuthUserFile /home/pypi-proxy/htpasswd.file 217 | AuthName "Private files" 218 | Require valid-user 219 | Order deny,allow 220 | Allow from all 221 | 222 | 223 | WSGIDaemonProcess pypi_proxy threads=5 224 | WSGIScriptAlias / /home/pypi-proxy/proxy.wsgi 225 | 226 | 227 | Restart Apache 228 | 229 | .. code-block:: bash 230 | 231 | sudo service apache2 restart 232 | -------------------------------------------------------------------------------- /docs/source/intro.rst: -------------------------------------------------------------------------------- 1 | ============ 2 | Introduction 3 | ============ 4 | 5 | Flask-Pypi-Proxy works is a proxy for PyPI that also enables you to upload 6 | your custom packages. 7 | 8 | Advantages 9 | ========== 10 | 11 | * Once a package is downloaded from PyPI, then it won't be downloaded 12 | again. Bacause of this, the package installation is quicker. 13 | Let's assume that you have some servers where your application run. 14 | You configure one of these servers with Flask-Pypi-Proxy, and because of that 15 | all the servers download the required Python packages from an internal server. 16 | 17 | * You can upload your custom eggs. For example, you have your project which is 18 | close sourced and because of that it can't be uploaded to PyPI. This 19 | will solve the problem because you will have an internal package system. 20 | 21 | * Upload compiled packages. Some packages (lxml, Pillow) compile using the 22 | system libraries. If all the servers are using the same versions of the 23 | libraries, then you can upload the compiled package and save the compilation 24 | time for each install. 25 | 26 | Disadvantages 27 | ============= 28 | 29 | * When downloading the package for the first time, it will take some extra time. 30 | This happens because the package will have to be downloaded from PyPI first, 31 | and then from the Flask-Pypi-Proxy. 32 | -------------------------------------------------------------------------------- /docs/source/using.rst: -------------------------------------------------------------------------------- 1 | =============== 2 | Using the proxy 3 | =============== 4 | 5 | 6 | Downloading packages 7 | ==================== 8 | 9 | To download a package from the proxy, there are two choices: 10 | 11 | * Specify the server where the server is installed when runing **pip** or 12 | **easy_install**. 13 | 14 | .. code-block:: bash 15 | 16 | pip install -i http://mypypiproxy/simple/ Flask 17 | easy_install -i http://mypypiproxy/simple/ Flask 18 | 19 | * Use the index url in a configuration file. For easy_install, it 20 | should be on **~/.pydistutils.cfg** (on Linux), and the file should have 21 | the following format:: 22 | 23 | [easy_install] 24 | index_url = http://mypypiproxy/simple/ 25 | 26 | For **pip**, the configuration file is **~/.pip/pip.conf**, and the file 27 | should have the following format:: 28 | 29 | [global] 30 | index-url = http://mypypiproxy/simple/ 31 | 32 | Also, you should increment the timeout option for **pip** or **easy_install**. 33 | For pip, the **~/.pip/pip.conf** configuration file should be something like: 34 | 35 | [global] 36 | index-url = http://mypypiproxy/simple/ 37 | timeout = 60 38 | 39 | 40 | Uploading packages 41 | ================== 42 | 43 | To upload a package, the **~/.pypirc** should be updated to something 44 | like: 45 | 46 | .. code-block:: ini 47 | 48 | [distutils] 49 | index-servers = 50 | miserver 51 | 52 | [myserver] 53 | username:foo 54 | password:bar 55 | repository:http://mypypiproxy/pypi/ 56 | 57 | I you are using the configuration with basic auth, then the configuration 58 | file should look something like this: 59 | 60 | .. code-block:: 61 | 62 | [distutils] 63 | index-servers = 64 | miserver 65 | 66 | [myserver] 67 | username:basicauth_username 68 | password:basciauth_password 69 | repository:http://mypypiproxy/pypi/ 70 | 71 | Basically, you should put in the username and password used for the basic auth. 72 | 73 | The username and password values aren't required by Flask-Pypi-Proxy. 74 | They are used by distutils when uploading the package. If you don't have 75 | any authentication after this, then you can put any values. After that, 76 | go to the **setup.py** of your project and run: 77 | 78 | .. code-block:: bash 79 | 80 | python setup.py sdist upload -r myserver 81 | 82 | **IMPORTANT:** The command *register*, won't work if you are using basic auth. 83 | For example, if you run 84 | 85 | .. code-block:: bash 86 | 87 | python setup.py register 88 | 89 | and if your server is configured using basic auth, then register will return 90 | a 401 error. Simply upload the package without running register. 91 | -------------------------------------------------------------------------------- /flask_pypi_proxy/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | VERSION = '0.5.1' 4 | -------------------------------------------------------------------------------- /flask_pypi_proxy/app.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' The main Flask application which take care of reading the configuration. 4 | ''' 5 | 6 | import os 7 | import json 8 | import logging 9 | from flask import Flask 10 | 11 | def read_configuration(app, pypi_url='http://pypi.python.org', 12 | private_eggs=[]): 13 | ''' Reads the configuration by using the system file or the configuration 14 | file. 15 | 16 | :param :class:`Flask` app: the app to where set the configuration values 17 | 18 | :param str pypi_url: the base Pypi url from where get the packages. 19 | 20 | :param str private_eggs: the list of the name of the projects for which 21 | the **pypi_url** won't be used. 22 | ''' 23 | filepath = os.environ.get('FLASK_PYPI_PROXY_CONFIG') 24 | if filepath: 25 | # then the configuration file is used 26 | if not os.path.exists(filepath): 27 | raise Exception('There should be a configuration file but ' 28 | 'the path is invalid') 29 | 30 | with open(filepath) as config_file: 31 | configuration = json.load(config_file) 32 | if not 'BASE_FOLDER_PATH' in configuration: 33 | raise Exception('The configuration should have the value ' 34 | 'for BASE_FOLDER_PATH') 35 | 36 | if not 'LOGGING_PATH' in configuration: 37 | raise Exception('The configuration must have the value for ' 38 | 'LOGGING_PATH') 39 | 40 | app.config['PRIVATE_EGGS'] = configuration.get('PRIVATE_EGGS', 41 | private_eggs) 42 | app.config['BASE_FOLDER_PATH'] = configuration['BASE_FOLDER_PATH'] 43 | app.config['SHOULD_USE_EXISTING'] = configuration.get( 44 | 'SHOULD_USE_EXISTING', 45 | False) 46 | app.config['PYPI_URL'] = configuration.get('PYPI_URL', pypi_url) 47 | app.config['LOGGING_PATH'] = configuration.get('LOGGING_PATH') 48 | app.config['LOGGING_LEVEL'] = configuration.get('LOGGING_LEVEL', 49 | 'DEBUG') 50 | 51 | else: 52 | base_path = os.environ.get('PYPI_PROXY_BASE_FOLDER_PATH') 53 | logging_path = os.environ.get('PYPI_PROXY_LOGGING_PATH') 54 | if not base_path: 55 | raise Exception('The PYPI_PROXY_BASE_FOLDER_PATH or ' 56 | 'FLASK_PYPI_PROXY_CONFIG environment keys ' 57 | 'are missing') 58 | 59 | if not logging_path: 60 | raise Exception('The PYPI_PROXY_LOGGING_PATH or ' 61 | 'FLASK_PYPI_PROXY_CONFIG environment keys ' 62 | 'are missing') 63 | 64 | app.config['BASE_FOLDER_PATH'] = base_path 65 | app.config['LOGGING_PATH'] = logging_path 66 | app.config['LOGGING_LEVEL'] = os.environ.get('PYPI_PROXY_LOGGING_LEVEL', 67 | 'DEBUG') 68 | env_value = os.environ.get('PYPI_PROXY_PRIVATE_EGGS', None) 69 | if env_value: 70 | app.config['PRIVATE_EGGS'] = env_value.split(',') 71 | else: 72 | app.config['PRIVATE_EGGS'] = private_eggs 73 | 74 | app.config['PYPI_URL'] = os.environ.get('PYPI_PROXY_PYPI_URL', 75 | pypi_url) 76 | app.config['SHOULD_USE_EXISTING'] = os.environ.get( 77 | 'PYPI_PROXY_SHOULD_USE_EXISTING', 78 | False) 79 | 80 | if not app.config['PYPI_URL'].endswith('/'): 81 | app.config['PYPI_URL'] = app.config['PYPI_URL'] + '/' 82 | 83 | 84 | def configure_logging(app): 85 | ''' Setups the logging that will be used by the views to log the 86 | different problems that it might have. 87 | 88 | :param app: the application that will be used to register the URLs. 89 | ''' 90 | if not app.debug: 91 | logging.basicConfig(filename=app.config['LOGGING_PATH'], 92 | level=getattr(logging, app.config['LOGGING_LEVEL']), 93 | format='%(asctime)s [%(levelname)s] %(message)s') 94 | 95 | app = Flask(__name__) 96 | read_configuration(app) 97 | configure_logging(app) 98 | -------------------------------------------------------------------------------- /flask_pypi_proxy/templates/simple.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Simple index 4 | 5 | 6 | {% for package_name in packages %} 7 | {{ package_name }} 8 |
9 | {% endfor %} 10 | 11 | 12 | -------------------------------------------------------------------------------- /flask_pypi_proxy/templates/simple_package.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Links for {{ package_name }} 4 | 5 | 6 |

Links for {{ package_name }}

7 | {% for package_version in versions %} 8 | {% if not package_version.external_link %} 9 | {{ package_version.name }}
10 | {% else %} 11 | {{ package_version.name }}
12 | {% endif %} 13 | {% endfor %} 14 | 15 | 16 | -------------------------------------------------------------------------------- /flask_pypi_proxy/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from flask_pypi_proxy.app import app 4 | from os.path import join 5 | from hashlib import md5 6 | 7 | 8 | def is_private(egg_name): 9 | ''' Checks if the egg_name is private or if belongs to one of 10 | the eggs that are uploaded to the normal pypi. 11 | 12 | :param str egg_name: the name of the egg without the version information. 13 | 14 | :return: true if the egg is private. 15 | ''' 16 | return egg_name in app.config['PRIVATE_EGGS'] 17 | 18 | 19 | def get_base_path(): 20 | ''' Gets the base path where all the eggs are on this servers 21 | ''' 22 | return app.config['BASE_FOLDER_PATH'] 23 | 24 | 25 | def get_package_path(egg_name): 26 | ''' Given the name of a package, it gets the local path for it. 27 | 28 | :param egg_name: the name (it might also include the version) of 29 | a python package. 30 | 31 | :return: the local path where the file can be found on the local 32 | system. 33 | ''' 34 | return join(get_base_path(), egg_name) 35 | 36 | 37 | def get_md5_for_content(package_content): 38 | ''' Given the content of a package it returns the md5 of the file. 39 | ''' 40 | res = md5(package_content) 41 | return res.hexdigest() 42 | 43 | 44 | def url_is_egg_file(url): 45 | return url is not None and ( url.lower().endswith('.zip') 46 | or url.lower().endswith('.tar.gz') 47 | or url.lower().endswith('.egg') 48 | or url.lower().endswith('.exe') 49 | or url.lower().endswith('.msi') 50 | or url.lower().endswith('.whl')) 51 | -------------------------------------------------------------------------------- /flask_pypi_proxy/views/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from flask_pypi_proxy.app import app 4 | import flask_pypi_proxy.views.package 5 | import flask_pypi_proxy.views.pypi 6 | import flask_pypi_proxy.views.simple 7 | 8 | 9 | if __name__ == '__main__': 10 | app.run(debug=True, host='0.0.0.0') 11 | -------------------------------------------------------------------------------- /flask_pypi_proxy/views/package.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' Handles downloading a package file. 4 | 5 | This is called by easy_install or pip after calling the simple, and getting 6 | the version of the package to download. 7 | 8 | ''' 9 | 10 | import magic 11 | from flask import make_response, request, abort 12 | from flask_pypi_proxy.app import app 13 | from flask_pypi_proxy.utils import (get_base_path, get_package_path, 14 | get_md5_for_content) 15 | from os import makedirs 16 | from os.path import join, exists 17 | from requests import get, head 18 | 19 | 20 | @app.route('/packages////', 21 | methods=['GET', 'HEAD']) 22 | def package(package_type, letter, package_name, package_file): 23 | ''' Downloads the egg 24 | 25 | :param str package_type: the nature of the package. For example: 26 | 'source' or '2.7' 27 | :param str letter: the first char of the package name. For example: 28 | D 29 | :param str package_name: the name of the package. For example: Django 30 | :param str package_file: the name of the package and it's version. For 31 | example: Django-1.5.0.tar.gz 32 | ''' 33 | egg_filename = join(get_base_path(), package_name, package_file) 34 | url = request.args.get('remote') 35 | 36 | if request.method == 'HEAD': 37 | # in this case the content type of the file is what is 38 | # required 39 | if not exists(egg_filename): 40 | pypi_response = head(url) 41 | return _respond(pypi_response.content, pypi_response.headers['content-type']) 42 | 43 | else: 44 | mimetype = magic.from_file(egg_filename, mime=True) 45 | return _respond('', mimetype) 46 | 47 | app.logger.debug('Downloading: %s', package_file) 48 | if exists(egg_filename): 49 | app.logger.debug('Found local file in repository for: %s', package_file) 50 | # if the file exists, then use the local file. 51 | path = get_package_path(package_name) 52 | path = join(path, package_file) 53 | with open(path, 'rb') as egg: 54 | content = egg.read(-1) 55 | mimetype = magic.from_file(egg_filename, mime=True) 56 | return _respond(content, mimetype) 57 | 58 | else: 59 | # Downloads the egg from pypi and saves it locally, then 60 | # it will return it. 61 | package_path = get_package_path(package_name) 62 | app.logger.debug('Starting to download: %s using the url: %s', 63 | package_file, url) 64 | pypi_response = get(url) 65 | app.logger.debug('Finished downloading package: %s', package_file) 66 | 67 | if pypi_response.status_code != 200: 68 | app.logger.warning('Error respose while downloading for proxy: %s' 69 | 'Response details: %s', package_file, 70 | pypi_response.text) 71 | abort(pypi_response.status_code) 72 | 73 | if not exists(package_path): 74 | makedirs(package_path) 75 | 76 | with open(egg_filename, 'w') as egg_file: 77 | egg_file.write(pypi_response.content) 78 | 79 | with open(egg_filename) as egg_file: 80 | filecontent = egg_file.read(-1) 81 | mimetype = magic.from_file(egg_filename, mime=True) 82 | 83 | with open(egg_filename + '.md5', 'w') as md5_output: 84 | md5 = get_md5_for_content(filecontent) 85 | md5_output.write(md5) 86 | 87 | return _respond(filecontent, mimetype) 88 | 89 | 90 | def _respond(filecontent, mimetype): 91 | return make_response(filecontent, 200, { 92 | 'Content-Type': mimetype 93 | } 94 | ) 95 | -------------------------------------------------------------------------------- /flask_pypi_proxy/views/pypi.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' Handles the submition of the different packages. 4 | 5 | This is used on **register** o **upload**. 6 | 7 | For example: 8 | 9 | .. code-block:: bash 10 | 11 | python setup.py register 12 | python setup.py sdist upload 13 | 14 | 15 | ''' 16 | 17 | from os import makedirs 18 | from os.path import exists, join 19 | 20 | from flask import request 21 | from werkzeug import secure_filename 22 | 23 | from flask_pypi_proxy.utils import get_package_path 24 | from flask_pypi_proxy.app import app 25 | 26 | 27 | @app.route('/pypi/', methods=['POST']) 28 | def index(): 29 | path = get_package_path(request.form['name']) 30 | if not exists(path): 31 | makedirs(path) 32 | 33 | if request.files: 34 | file = request.files['content'] 35 | filename = secure_filename(file.filename) 36 | file.save(join(path, filename)) 37 | with open(join(path, filename + '.md5'), 'w') as md5_file: 38 | md5_file.write(request.form['md5_digest']) 39 | return 'Registered' 40 | -------------------------------------------------------------------------------- /flask_pypi_proxy/views/simple.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | ''' Gets the list of the packages that can be downloaded. 5 | ''' 6 | 7 | import urllib 8 | import urlparse 9 | from collections import namedtuple 10 | from os import listdir 11 | from os.path import join, exists, basename 12 | 13 | from bs4 import BeautifulSoup 14 | from flask import abort, render_template 15 | from requests import get 16 | 17 | from flask_pypi_proxy.app import app 18 | from flask_pypi_proxy.utils import (get_package_path, get_base_path, 19 | is_private, url_is_egg_file) 20 | 21 | 22 | VersionData = namedtuple('VersionData', ['name', 'md5', 'external_link']) 23 | 24 | 25 | @app.route('/simple/') 26 | def simple(): 27 | ''' Return the template which list all the packages that are installed 28 | ''' 29 | packages = [] 30 | for filename in listdir(get_base_path()): 31 | packages.append(filename) 32 | 33 | packages.sort() 34 | return render_template('simple.html', packages=packages) 35 | 36 | 37 | @app.route('/simple//') 38 | def simple_package(package_name): 39 | ''' Given a package name, returns all the versions for downloading 40 | that package. 41 | 42 | If the package doesn't exists, then it will call PyPI (CheeseShop). 43 | But if the package exists in the local path, then it will get all 44 | the versions for the local package. 45 | 46 | This will take into account if the egg is private or if it is a normal 47 | egg that was uploaded to PyPI. This is important to take into account 48 | the version of the eggs. For example, a project requires requests==1.0.4 49 | and another package uses requests==1.0.3. Then the instalation of the 50 | second package will fail because it wasn't downloaded and the **requests** 51 | folder only has the 1.0.4 version. 52 | 53 | To solve this problem, the system uses 2 different kinds of eggs: 54 | 55 | * private eggs: are the eggs that you uploaded to the private repo. 56 | * normal eggs: are the eggs that are downloaded from PyPI. 57 | 58 | So the normal eggs will always get the simple page from the PyPI repo, 59 | will the private eggs will always be read from the filesystem. 60 | 61 | 62 | :param package_name: the name of the egg package. This is only the 63 | name of the package with the version or anything 64 | else. 65 | 66 | :return: a template with all the links to download the packages. 67 | ''' 68 | app.logger.debug('Requesting index for: %s', package_name) 69 | package_folder = get_package_path(package_name) 70 | if (is_private(package_name) or ( 71 | exists(package_name) and app.config['SHOULD_USE_EXISTING'])): 72 | 73 | app.logger.debug('Found information of package: %s in local repository', 74 | package_name) 75 | package_versions = [] 76 | template_data = dict( 77 | source_letter=package_name[0], 78 | package_name=package_name, 79 | versions=package_versions 80 | ) 81 | 82 | for filename in listdir(package_folder): 83 | if not filename.endswith('.md5'): 84 | # I only read .md5 files so I skip this egg (or tar, 85 | # or zip) file 86 | continue 87 | 88 | with open(join(package_folder, filename)) as md5_file: 89 | md5 = md5_file.read(-1) 90 | 91 | # remove .md5 extension 92 | name = filename[:-4] 93 | data = VersionData(name, md5, None) 94 | package_versions.append(data) 95 | 96 | return render_template('simple_package.html', **template_data) 97 | else: 98 | app.logger.debug('Didnt found package: %s in local repository. ' 99 | 'Using proxy.', package_name) 100 | url = app.config['PYPI_URL'] + 'simple/%s/' % package_name 101 | response = get(url) 102 | 103 | if response.status_code != 200: 104 | app.logger.warning('Error while getting proxy info for: %s' 105 | 'Errors details: %s', package_name, 106 | response.text) 107 | abort(response.status_code) 108 | 109 | if response.history: 110 | app.logger.debug('The url was redirected') 111 | # in this case, the request was redirect, so I should also 112 | # take into account this change. For example, this happens 113 | # when requesting flask-bcrypt and on Pypi the request is 114 | # redirected to Flask-Bcrypt 115 | package_name = urlparse.urlparse(response.url).path 116 | package_name = package_name.replace('/simple/', '') 117 | package_name = package_name.replace('/', '') 118 | 119 | content = response.content 120 | external_links = set() 121 | 122 | # contains the list of pacges whih where checked because 123 | # on the link they had the information of 124 | visited_download_pages = set() 125 | soup = BeautifulSoup(content) 126 | package_versions = [] 127 | 128 | for panchor in soup.find_all('a'): 129 | if panchor.get('rel') and panchor.get('rel')[0] == 'homepage': 130 | # skip getting information on the project homepage 131 | continue 132 | 133 | href = panchor.get('href') 134 | app.logger.debug('Found the link: %s', href) 135 | if href.startswith('../../packages/'): 136 | # then the package is hosted on PyPI. 137 | pk_name = basename(href) 138 | pk_name, md5_data = pk_name.split('#md5=') 139 | pk_name = pk_name.replace('#md5=', '') 140 | 141 | # remove md5 part to make the url shorter. 142 | split_data = urlparse.urlsplit(href) 143 | absolute_url = urlparse.urljoin(url, split_data.path) 144 | 145 | external_link= urllib.urlencode({'remote': absolute_url}) 146 | data = VersionData(pk_name, md5_data, external_link) 147 | package_versions.append(data) 148 | continue 149 | 150 | parsed = urlparse.urlparse(href) 151 | if parsed.hostname: 152 | # then the package had a full path to the file 153 | if parsed.hostname == 'pypi.python.org': 154 | # then it is hosted on the PyPI server, so I change 155 | # it to make it a relative url 156 | pk_name = basename(parsed.path) 157 | if '#md5=' in parsed.path: 158 | pk_name, md5_data = pk_name.split('#md5=') 159 | pk_name = pk_name.replace('#md5=', '') 160 | else: 161 | md5_data = '' 162 | 163 | absolute_url = urlparse.urljoin(url, parsed.path) 164 | external_link= urllib.urlencode({'remote': absolute_url}) 165 | data = VersionData(pk_name, md5_data, external_link) 166 | package_versions.append(data) 167 | 168 | else: 169 | # the python package is hosted on another server 170 | # that isn't PyPI. The packages that don't have 171 | # rel=download are links to some pages 172 | if panchor.get('rel') and panchor.get('rel')[0] == 'download': 173 | if url_is_egg_file(parsed.path): 174 | external_links.add(href) 175 | else: 176 | # href points to an external page where the links 177 | # to download the package will be found 178 | if href not in visited_download_pages: 179 | visited_download_pages.add(href) 180 | external_links.update(find_external_links(href)) 181 | 182 | # after collecting all external links, we insert them in the html page 183 | for external_url in external_links: 184 | package_version = basename(external_url) 185 | existing_value = filter(lambda pv: pv.name == package_version, 186 | package_versions) 187 | if existing_value: 188 | # if the package already exists on PyPI, then 189 | # use its version instead of using the one that is 190 | # hosted on a remote server 191 | continue 192 | 193 | external_link = urllib.urlencode({'remote': external_url}) 194 | data = VersionData(package_version, '', external_link) 195 | package_versions.append(data) 196 | 197 | package_versions.sort(key=lambda v: v.name) 198 | 199 | template_data = dict( 200 | source_letter=package_name[0], 201 | package_name=package_name, 202 | versions=package_versions 203 | ) 204 | return render_template('simple_package.html', **template_data) 205 | 206 | 207 | def find_external_links(url): 208 | '''Look for links to files in a web page and returns a set. 209 | ''' 210 | links = set() 211 | try: 212 | response = get(url) 213 | if response.status_code != 200: 214 | app.logger.warning('Error while getting proxy info for: %s' 215 | 'Errors details: %s', url, 216 | response.text) 217 | else: 218 | content_type = response.headers.get('content-type', '') 219 | if content_type in ('application/x-gzip'): 220 | # in this case the URL was a redirection to download 221 | # a package. For example, sourceforge. 222 | links.add(response.url) 223 | return links 224 | if response.content: 225 | soup = BeautifulSoup(response.content) 226 | for anchor in soup.find_all('a'): 227 | href = anchor.get("href") 228 | if url_is_egg_file(href): 229 | # href points to a filename 230 | if not url.endswith('/'): 231 | url += '/' 232 | href = get_absolute_url(href, url) 233 | links.add(href) 234 | except: 235 | # something happened when looking for external links: 236 | # timeout, HTML parser error, etc. 237 | # we must not fail and only log the error 238 | app.logger.exception('') 239 | return links 240 | 241 | 242 | def get_absolute_url(url, root_url): 243 | '''Make relative URLs absolute 244 | 245 | >>> get_absolute_url('/src/blah.zip', 'https://awesome.org/') 246 | 'https://awesome.org/src/blah.zip' 247 | >>> get_absolute_url('http://foo.bar.org/blah.zip', 'https://awesome.org/') 248 | 'http://foo.bar.org/blah.zip' 249 | ''' 250 | parsed = urlparse.urlparse(url) 251 | if url.startswith('//'): 252 | # this are the URLS parsed from code.google.com 253 | return 'http:' + url 254 | elif parsed.scheme: 255 | return url 256 | else: 257 | return urlparse.urljoin(root_url, parsed.path) 258 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.9 2 | Jinja2==2.7 3 | MarkupSafe==0.17 4 | Werkzeug==0.8.3 5 | beautifulsoup4==4.2.0 6 | python-magic==0.4.3 7 | requests==1.2.1 8 | wsgiref==0.1.2 9 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/bin/env python 2 | 3 | from setuptools import setup, find_packages 4 | from flask_pypi_proxy import VERSION 5 | 6 | setup( 7 | name='Flask-Pypi-Proxy', 8 | version=VERSION, 9 | description='A PyPI proxy', 10 | long_description=open('README.rst').read(-1), 11 | author='Tomas Zulberti', 12 | author_email='tzulberti@gmail.com', 13 | license='BSD', 14 | url='https://github.com/tzulberti/Flask-PyPi-Proxy', 15 | install_requires=[ 16 | "Flask", 17 | "requests", 18 | "python-magic", 19 | "beautifulsoup4" 20 | ], 21 | packages=find_packages(), 22 | include_package_data=True, 23 | zip_safe=False, 24 | keywords='pypi flask proxy', 25 | classifiers=[ 26 | 'Development Status :: 3 - Alpha', 27 | 'Environment :: Web Environment', 28 | 'Intended Audience :: Developers', 29 | 'Intended Audience :: System Administrators', 30 | 'License :: OSI Approved :: BSD License', 31 | 'Operating System :: OS Independent', 32 | 'Programming Language :: Python :: 2.6', 33 | 'Programming Language :: Python :: 2.7', 34 | 'Topic :: Software Development :: Libraries :: Python Modules', 35 | ] 36 | ) 37 | --------------------------------------------------------------------------------