Esempio n. 1
0
def list_packages():
    fp = request.fullpath
    if not fp.endswith("/"):
        fp += "/"

    files = [x.relfn for x in sorted(find_packages(packages()), key=lambda x: (os.path.dirname(x.relfn), x.pkgname, x.parsed_version))]
    links = [(f.replace("\\", "/"), urljoin(fp, f)) for f in files]
    return template("packages", links=links)
Esempio n. 2
0
def update():
    try:
        action = request.forms[':action']
    except KeyError:
        raise HTTPError(400, output=":action field not found")

    if action in ("verify", "submit"):
        return ""

    if action == "doc_upload":
        try:
            content = request.files['content']
        except KeyError:
            raise HTTPError(400, output="content file field not found")
        zip_data = content.file.read()
        try:
            zf = zipfile.ZipFile(BytesIO(zip_data))
            zf.getinfo('index.html')
        except Exception:
            raise HTTPError(400, output="not a zip file")
        return ""

    if action == "remove_pkg":
        name = request.forms.get("name")
        version = request.forms.get("version")
        if not name or not version:
            raise HTTPError(400, "Name or version not specified")
        found = None
        for pkg in find_packages(packages()):
            if pkg.pkgname == name and pkg.version == version:
                found = pkg
                break
        if found is None:
            raise HTTPError(404, "%s (%s) not found" % (name, version))
        os.unlink(found.fn)
        return ""

    if action != "file_upload":
        raise HTTPError(400, output="action not supported: %s" % action)

    try:
        content = request.files['content']
    except KeyError:
        raise HTTPError(400, output="content file field not found")

    if "/" in content.filename:
        raise HTTPError(400, output="bad filename")

    if not config.overwrite and exists(packages.root, content.filename):
        log.warn("Cannot upload package(%s) since it already exists! \n" +
                 "  You may use `--overwrite` option when starting server to disable this check. ",
                 content.filename)
        raise HTTPError(409, output="file already exists")

    store(packages.root, content.filename, content.save)
    return ""
Esempio n. 3
0
def server_static(filename):
    entries = find_packages(packages())
    for x in entries:
        f = x.relfn.replace("\\", "/")
        if f == filename:
            response = static_file(
                filename, root=x.root, mimetype=mimetypes.guess_type(filename)[0])
            if config.cache_control is not None:
                response.set_header("Cache-Control", "public, max-age={cache_control}".format(cache_control=config.cache_control))
            return response

    return HTTPError(404)
Esempio n. 4
0
def simple(prefix=""):
    global packages

    fp = request.fullpath
    if not fp.endswith("/"):
        fp += "/"

    items = find_packages(packages.items(), prefix=prefix)
    files = [x.relfn for x in sorted(items, key=lambda x: (x.parsed_version, x.relfn))]

    if not files:
        if config.redirect_to_fallback:
            return redirect("%s/%s/" % (config.fallback_url.rstrip("/"), prefix))
        return HTTPError(404)

    links = [(os.path.basename(f), urljoin(fp, "../../packages/%s" % f.replace("\\", "/"))) for f in files]
    return template("simple_prefix", prefix=prefix, links=links)