|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +from __future__ import unicode_literals, absolute_import, print_function |
| 5 | + |
| 6 | +""" |
| 7 | + Main script including runserver and delete-paste. |
| 8 | +""" |
| 9 | + |
| 10 | +import sys |
| 11 | + |
| 12 | +from sigtools.modifiers import annotate, autokwoargs |
| 13 | +import re |
| 14 | + |
| 15 | +try: |
| 16 | + import thread |
| 17 | +except ImportError: |
| 18 | + import _thread as thread |
| 19 | + |
| 20 | +from zerobin.utils import (settings, SettingsValidationError, |
| 21 | + drop_privileges) |
| 22 | +from zerobin.routes import get_app |
| 23 | +from zerobin.paste import Paste |
| 24 | + |
| 25 | +from bottle import run |
| 26 | + |
| 27 | +import clize |
| 28 | + |
| 29 | +@annotate(debug=bool, compressed_static=bool) |
| 30 | +def runserver(host='', port='', debug=None, user='', group='', |
| 31 | + settings_file='', compressed_static=None, |
| 32 | + version=False, paste_id_length=None, server="cherrypy"): |
| 33 | + |
| 34 | + if version: |
| 35 | + print('0bin V%s' % settings.VERSION) |
| 36 | + sys.exit(0) |
| 37 | + |
| 38 | + settings.HOST = host or settings.HOST |
| 39 | + settings.PORT = port or settings.PORT |
| 40 | + settings.USER = user or settings.USER |
| 41 | + settings.GROUP = group or settings.GROUP |
| 42 | + settings.PASTE_ID_LENGTH = paste_id_length or settings.PASTE_ID_LENGTH |
| 43 | + |
| 44 | + try: |
| 45 | + _, app = get_app(debug, settings_file, compressed_static, settings=settings) |
| 46 | + except SettingsValidationError as err: |
| 47 | + print('Configuration error: %s' % err.message, file=sys.stderr) |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + thread.start_new_thread(drop_privileges, (settings.USER, settings.GROUP)) |
| 51 | + |
| 52 | + if settings.DEBUG: |
| 53 | + run(app, host=settings.HOST, port=settings.PORT, reloader=True, |
| 54 | + server="cherrypy") |
| 55 | + else: |
| 56 | + run(app, host=settings.HOST, port=settings.PORT, server="cherrypy") |
| 57 | + |
| 58 | + |
| 59 | +# The regex parse the url and separate the paste's id from the decription key |
| 60 | +# After the '/paste/' part, there is several caracters, identified as |
| 61 | +# the uuid of the paste. Followed by a '#', the decryption key of the paste. |
| 62 | +paste_url = re.compile('/paste/(?P<paste_id>.*)#(?P<key>.*)') |
| 63 | + |
| 64 | +def unpack_paste(paste): |
| 65 | + """Take either the ID or the URL of a paste, and return its ID""" |
| 66 | + |
| 67 | + try_url = paste_url.search(paste) |
| 68 | + |
| 69 | + if try_url: |
| 70 | + return try_url.group('paste_id') |
| 71 | + return paste |
| 72 | + |
| 73 | +@annotate(quiet='q') |
| 74 | +@autokwoargs |
| 75 | +def delete_paste(quiet=False, *pastes): |
| 76 | + """ |
| 77 | + Remove pastes, given its ID or its URL |
| 78 | +
|
| 79 | + quiet: Don't print anything |
| 80 | +
|
| 81 | + pastes: List of pastes, given by ID or URL |
| 82 | + """ |
| 83 | + |
| 84 | + for paste_uuid in map(unpack_paste, pastes): |
| 85 | + try: |
| 86 | + Paste.load(paste_uuid).delete() |
| 87 | + |
| 88 | + if not quiet: |
| 89 | + print('Paste {} is removed'.format(paste_uuid)) |
| 90 | + |
| 91 | + except ValueError: |
| 92 | + if not quiet: |
| 93 | + print('Paste {} doesn\'t exist'.format(paste_uuid)) |
| 94 | + |
| 95 | +def main(): |
| 96 | + subcommands = [runserver, delete_paste] |
| 97 | + subcommand_names = [clize.util.name_py2cli(name) |
| 98 | + for name in clize.util.dict_from_names(subcommands).keys()] |
| 99 | + if len(sys.argv) < 2 or sys.argv[1] not in subcommand_names: |
| 100 | + sys.argv.insert(1, subcommand_names[0]) |
| 101 | + clize.run(runserver, delete_paste) |
| 102 | + |
0 commit comments