aboutsummaryrefslogtreecommitdiff
blob: cb93f8836623260608a13daf83be2e80a5058d11 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python

import operator, os, sys, time

import cherrypy

from web.model import latest_per_day, build_centerpkg_list, last_modified
from web.lib import ajax, template, filters
from pkgcore.ebuild.cpv import CPV

LEFTDAYCOUNT=2

class Root(object):

    # This should match /usr/portage/profiles/arch.list
    arches = ['alpha', 'amd64', 'arm', 'hppa', 'ia64', 'm68k', 'mips', 'ppc', 'ppc64', 's390', 'sh', 'sparc', 'sparc-fbsd', 'x86', 'x86-fbsd']
    arches.sort()

    @cherrypy.expose
    @template.expire_on_30_min()
    @template.output('index.html', method='xhtml')
    def index(self,  **kwds):
        db = cherrypy.thread_data.db
        entry_filter = filters.EntryFilters(db)

        latest_entries = entry_filter.unfiltered()
        left_daycount = filters.limit_leftcount(kwds)

        center_pkgs = build_centerpkg_list(latest_entries,
            db.get_package_details_cpv, filters.limit_centercount(kwds),
            use_fullver=True)

        day_list = latest_per_day(latest_entries, left_daycount)

        updated = last_modified(entry_filter.latest_entry())
        return template.render(arches = self.arches,
            daylist = day_list, center_pkgs = center_pkgs,
            lastupdate = updated)

    @cherrypy.expose
    @template.expire_on_30_min()
    @template.output('index.html', method='xhtml')
    def arch(self, arch = "", **kwds):
        if arch == "":
            raise cherrypy.HTTPRedirect("/")

        db = cherrypy.thread_data.db
        entry_filter = filters.EntryFilters(db)

        # Default first
        limit = filters.limit_centercount(kwds)

        if 'stable' in kwds:
            filtered_latest_entries = entry_filter.stable_filter(arch, limit)
        elif 'testing' in kwds:
            filtered_latest_entries = entry_filter.testing_filter(arch, limit)
        else:
            filtered_latest_entries = entry_filter.arch_filter(arch, limit)
        center_pkgs = build_centerpkg_list(filtered_latest_entries,
            db.get_package_details_cpv, limit,
            use_fullver=True)
        if not center_pkgs:
            raise cherrypy.HTTPRedirect("/")

        if 'local_latest' in kwds:
            left_entrylist = filtered_latest_entries
            left_daycount = filters.limit_leftcount(kwds)
        else:
            left_entrylist = entry_filter.unfiltered()
            left_daycount = filters.limit_leftcount(kwds)
                    
        day_list = latest_per_day(left_entrylist, left_daycount)
        
        updated = last_modified(entry_filter.latest_entry())
        return template.render(arches = self.arches,
            daylist = day_list, center_pkgs = center_pkgs,
            lastupdate = updated)

    @cherrypy.expose
    @template.expire_on_30_min()
    @template.output('index.html', method='xhtml')
    def category(self, *args, **kwds):
        category = ''
        if len(args) == 1:
           category = args[0]

        if category == '':
            raise cherrypy.HTTPRedirect("/")

        db = cherrypy.thread_data.db
        entry_filter = filters.EntryFilters(db)
        
        filtered_latest_entries = entry_filter.category_filter(category)

        if 'full_cat' in kwds:
            center_pkgs = build_centerpkg_list(entry_filter.category_filter(category, False),
                db.get_package_details_cpv, None)
        else:
            center_pkgs = build_centerpkg_list(filtered_latest_entries,
                db.get_package_details_cpv, filters.limit_centercount(kwds))

        if not center_pkgs:
            raise cherrypy.HTTPRedirect("/")

        if 'local_latest' in kwds:
            left_entrylist = filtered_latest_entries
            left_daycount = filters.limit_leftcount(kwds)
        else:
            left_entrylist = entry_filter.unfiltered()
            left_daycount = filters.limit_leftcount(kwds)

        day_list = latest_per_day(left_entrylist, left_daycount)

        updated = last_modified(entry_filter.latest_entry())
        return template.render(arches = self.arches,
            daylist = day_list, center_pkgs = center_pkgs,
            lastupdate = updated)

    @cherrypy.expose
    @template.expire_on_30_min()
    @template.output('index.html', method='xhtml')
    def package(self, *args, **kwds):
        #print "len(args)=%d args=%s" % (len(args),repr(args))
        package = ''
        category = ''
        if len(args) == 1:
            package = args[0] 
            cpvtmp = CPV(str('%s/%s' % ('tmp',package)))
            package = cpvtmp.package
        elif len(args) == 2:
            category = args[0]
            package = args[1]
            cpvtmp = CPV(str('%s/%s' % ('tmp',package)))
            package = cpvtmp.package
        
        if package == '':
            raise cherrypy.HTTPRedirect("/")

        db = cherrypy.thread_data.db
        entry_filter = filters.EntryFilters(db)
        if category == '':
            filtered_latest_entries = entry_filter.package_filter(package)
        else:
            filtered_latest_entries = entry_filter.category_package_filter(category, package)

        center_pkgs = build_centerpkg_list(filtered_latest_entries,
            db.get_package_details_cpv, None)
        if not center_pkgs:
            raise cherrypy.HTTPRedirect("/")

        left_entrylist = entry_filter.unfiltered()
        left_daycount = filters.limit_leftcount(kwds)

        day_list = latest_per_day(left_entrylist, left_daycount)

        updated = last_modified(entry_filter.latest_entry())
        return template.render(arches = self.arches,
            daylist = day_list, center_pkgs = center_pkgs,
            lastupdate = updated)

# This is how it works in geddit, should be able to do something equal
#   @cherrypy.expose
#   @template.output('index.xml', method='xml')
#   def feed(self, id=None):
#       if id:
#           link = self.data.get(id)
#           if not link:
#               raise cherrypy.NotFound()
#           return template.render('info.xml', link=link)
#       else:
#           links = sorted(self.data.values(), key=operator.attrgetter('time'))
#           return template.render(links=links)

def database_connect(thread_index):
    """Create a DB connection and store it in the current thread"""
    from web.model import SQLitePackageDB
    cherrypy.thread_data.db = SQLitePackageDB("pgo.db")

    #from web.model import MySQLPackageDB
    #cherrypy.thread_data.db = MySQLPackageDB("localhost", "user", "pass", "db")
    #cherrypy.thread.data.db.ping(True)
    
def main():
    """Use this when we run standalone"""

    # site-wide config
    cherrypy.config.update({
        #'tools.caching.on': True,
        #'tools.caching.cache_class': cherrypy.lib.caching.MemoryCache,
        'tools.decode.on': True,
        'tools.encode.on': True,
        'tools.encode.encoding': 'utf-8',
        #'tools.expires.secs': 1800,
        'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
        'tools.trailing_slash.on': True,
        #'server.thread_pool': 10
    })
    
    cherrypy.engine.on_start_thread_list.append(database_connect)

    cherrypy.quickstart(Root(), '/', {
        '/media': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': 'static'
        }
    })


def setup_server():
    """within mod_python use me"""

    # Set up site-wide config. Do this first so that,
    # if something goes wrong, we get a log.
    cherrypy.config.update({'environment': 'production',
        'log.screen': False,
        'log.error_file': '/tmp/cherrypy_packages2.gentoo.log',
        'show_tracebacks': True,
        #'tools.caching.on': True,
        #'tools.caching.cache_class': cherrypy.lib.caching.MemoryCache,
        'tools.decode.on': True,
        'tools.encode.on': True,
        'tools.encode.encoding': 'utf-8',
        #'tools.expires.secs': 1800,
        'tools.staticdir.root': os.path.abspath(os.path.dirname(__file__)),
        'tools.trailing_slash.on': True,
        #'server.thread_pool': 64
        })
    cherrypy.engine.on_start_thread_list.append(database_connect)

    # Static content is served externally
    cherrypy.tree.mount(Root())

    # Per http://cherrypy.org/wiki/ModPython
    cherrypy.engine.SIGHUP = None
    cherrypy.engine.SIGTERM = None

    # You must start the engine in a non-blocking fashion
    # so that mod_python can proceed
    cherrypy.engine.start(blocking=False)


if __name__ == '__main__':
    main()