Created
February 17, 2020 06:29
-
-
Save pmrowla/6ed79cc0c4d20e26ab8e86d25570c983 to your computer and use it in GitHub Desktop.
Simple flask app for testing dvc http remote push/pull
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Requires: | |
# pip install flask flask-httpauth | |
from pathlib import Path | |
from flask import abort, Flask, request, send_file | |
from flask_httpauth import HTTPBasicAuth, HTTPDigestAuth | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'foo' | |
basic_auth = HTTPBasicAuth() | |
digest_auth = HTTPDigestAuth() | |
users = {"dvc": "password1"} | |
def _get_password(username): | |
if username in users: | |
return users.get(username) | |
return False | |
@basic_auth.get_password | |
def get_basic_password(username): | |
return _get_password(username) | |
@digest_auth.get_password | |
def get_digest_password(username): | |
return _get_password(username) | |
def _test_dvc_http(path): | |
path = Path(path) | |
if request.method in ["HEAD", "GET"]: | |
if path.exists(): | |
return send_file(path) | |
else: | |
abort(404) | |
elif request.method == "POST": | |
try: | |
path.parent.mkdir(parents=True, exist_ok=True) | |
with open(path, "wb") as fd: | |
fd.write(request.data) | |
return str(path) | |
except Exception: | |
abort(403) | |
abort(405) | |
@app.route("/public/<path:path>", methods=["GET", "POST"]) | |
def test_public(path): | |
return _test_dvc_http(path) | |
@app.route("/basic/<path:path>", methods=["GET", "POST"]) | |
@basic_auth.login_required | |
def test_basic(path): | |
return _test_dvc_http(path) | |
@app.route("/digest/<path:path>", methods=["GET", "POST"]) | |
@digest_auth.login_required | |
def test_digest(path): | |
return _test_dvc_http(path) | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment