forked from jadolg/APKStore
/
cherryd.py
160 lines (135 loc) · 5.11 KB
/
cherryd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# -*- coding: UTF-8 -*
# cherryd.py
#
# Copyright 2014 Jorge Alberto Díaz Orozco <jaorozco@estudiantes.uci.cu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import os
import socket
import urllib
import urlparse
import cherrypy
from pydaemonlib.pydaemonlib import Daemon
from sys import argv
from sys import exit as Exit
from pyQR.pyQR import *
# Django settings
#~ sys.path.append(os.path.abspath(os.getcwd())+'/WebBlog')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'APKStore.settings')
# Celery loader
os.environ['CELERY_LOADER'] = 'djcelery.loaders.DjangoLoader'
class DjangoApplication(object):
def __init__(self):
self.servers = []
self.domains = {}
def add_server(self, netloc, path, config):
"""Add a new CherryPy Application for a Virtual Host.
Creates a new CherryPy WSGI Server instance if the host resolves
to a different IP address or port.
"""
from cherrypy._cpwsgi_server import CPWSGIServer
from cherrypy.process.servers import ServerAdapter
host, port = urllib.splitnport(netloc, 80)
host = socket.gethostbyname(host)
bind_addr = (host, port)
if bind_addr not in self.servers:
self.servers.append(bind_addr)
server = CPWSGIServer()
server.bind_addr = bind_addr
adapter = ServerAdapter(cherrypy.engine, server, server.bind_addr)
adapter.subscribe()
self.domains[netloc] = cherrypy.Application(root=None,
config={path.rstrip('/') or '/': config})
def cfg_assets(self, url, root):
"""Configure hosting of static and media asset directories.
Can either mount to a specific path or add a Virtual Host. Sets
Expires header to 1 year.
"""
url_parts = urlparse.urlsplit(url)
path = url_parts.path.rstrip('/')
config = {
'tools.staticdir.on': True,
'tools.staticdir.dir': root,
'tools.expires.on': True,
'tools.expires.secs': 60 * 60 * 24 * 365, # 1 year
'tools.gzip.on': True,
'tools.gzip.mime_types': [
'text/*',
'application/javascript',
'application/x-javascript',
],
}
if url_parts.netloc:
self.add_server(url_parts.netloc, path, config)
elif path:
cherrypy.tree.mount(None, path, {'/': config})
def cfg_favicon(self, root):
"""Configure a default favicon.
Expects it to be in STATIC_ROOT.
"""
favicon = os.path.join(root, 'favicon.png')
config = {
'tools.staticfile.on': True,
'tools.staticfile.filename': favicon,
'tools.expires.on': True,
'tools.expires.secs': 60 * 60 * 24 * 365, # 1 year
}
cherrypy.tree.mount(None, '/favicon.png', {'/': config})
def run(self, host='localhost',port=8000, reload=True, log=True):
"""Run the CherryPy server."""
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
cherrypy.config.update({
'server.socket_host': host,
'server.socket_port': port,
'log.screen': log,
'engine.autoreload_on': reload,
})
self.cfg_assets(settings.MEDIA_URL, settings.MEDIA_ROOT)
self.cfg_assets(settings.STATIC_URL, settings.STATIC_ROOT)
c = 0
for i in settings.APK_ROOT:
self.cfg_assets("/apk"+str(c)+"/",i)
c += 1
self.cfg_assets(settings.ICONS_URL,settings.ICONS_ROOT)
self.cfg_favicon(settings.STATIC_ROOT)
app = WSGIHandler()
if self.domains:
app = cherrypy.wsgi.VirtualHost(app, self.domains)
cherrypy.tree.graft(app)
cherrypy.engine.start()
cherrypy.engine.block()
def main(**kwargs):
app = DjangoApplication()
app.run(**kwargs)
class server(Daemon):
def run(self):
main(host='0.0.0.0',port=8800)
if __name__ == '__main__':
daemon = server('/tmp/apkstore.pid',)
if len(argv) == 2:
if argv[1] == 'start':
daemon.start()
elif argv[1] == 'stop':
daemon.stop()
elif argv[1] == 'restart':
daemon.restart()
else:
Exit('Use: rcserver start|stop|restart')
else:
main(host='0.0.0.0',port=8800)
#Exit('Invalid params number\nUse: rcserver start|stop|restart')