Skip to content

Instantly share code, notes, and snippets.

@caiush
Created March 30, 2015 14:51
Show Gist options
  • Save caiush/30e5a946d39fac32082b to your computer and use it in GitHub Desktop.
Save caiush/30e5a946d39fac32082b to your computer and use it in GitHub Desktop.
apt-server
import os
import posixpath
import urllib
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
BASE_PATH = "/Users/caius/apt-mirror/mirror"
# modify this to add additional routes
ROUTES = (
# [url_prefix , directory_path]
['/ceph-firefly', os.path.join(BASE_PATH, 'ceph.com/debian-firefly')],
['/ceph-extras', os.path.join(BASE_PATH, 'ceph.com/debian-extras')],
['/ceph-apache', os.path.join(BASE_PATH, 'gitbuilder.ceph.com/apache2-deb-precise-x86_64-basic/ref/master')],
['/ceph-fcgi', os.path.join(BASE_PATH, 'gitbuilder.ceph.com/libapache-mod-fastcgi-deb-precise-x86_64-basic/ref/master')],
['/percona', os.path.join(BASE_PATH, 'repo.percona.com/apt')],
['/ubuntu-cloud', os.path.join(BASE_PATH, 'ubuntu-cloud.archive.canonical.com/ubuntu')],
['/rabbitmq', os.path.join(BASE_PATH, 'www.rabbitmq.com/debian')],
['/ubuntu', os.path.join(BASE_PATH, 'us.archive.ubuntu.com/ubuntu')],
['/haproxy', os.path.join(BASE_PATH, 'ppa.launchpad.net/vbernat/haproxy-1.5/ubuntu')],
)
class RequestHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
"""translate path given routes"""
# set default root to cwd
root = BASE_PATH
# look up routes and set root directory accordingly
for pattern, rootdir in ROUTES:
if path.startswith(pattern):
# found match!
path = path[len(pattern):] # consume path up to pattern len
root = rootdir
break
# normalize path and prepend root directory
path = path.split('?',1)[0]
path = path.split('#',1)[0]
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = root
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
if __name__ == '__main__':
BaseHTTPServer.test(RequestHandler, BaseHTTPServer.HTTPServer)
@caiush
Copy link
Author

caiush commented Mar 30, 2015

sudo apt-server.py 80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment