vpackager / bot.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#!/usr/bin/env python

#    This file is part of vpackager.
#
#    vpackager is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v2 as published by
#    the Free Software Foundation.
#
#    vpackager 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 vpackager.  If not, see <http://www.gnu.org/licenses/>.

import threading
import time
import gobject
import gtk
import bbot
import dbconn
import dialogs

gtk.gdk.threads_init()
todo = dbconn.refresh_todo()

_busy = False
class Monitor(threading.Thread):
    def __init__(self, label):
        super(Monitor, self).__init__()
        self.label = label
        self.quit = False
        global _busy
        
    def update_label(self, txt):
        self.label.set_text(txt)
        #return False
    
    def run(self):
        counter = 0
        global _busy
        while _busy is True:
           gobject.idle_add(self.update_label,'Working')
         
           time.sleep(1)

        gobject.idle_add(self.update_label,'Idle')
        time.sleep(1)
        

class tbp(threading.Thread):
    def __init__(self, src):
        global _busy
        global w
        super(tbp, self).__init__()
        self.src = src
        _busy  = True

        ### Take a split second to log the activity to the database
        sql = 'INSERT INTO en_history (srcpath, buildstart, result)' + \
        " values ('%s',%s,'Running')"% (src.path, 'CURRENT_TIMESTAMP')
        dbconn.push_data(sql)
        ### Another update is made when the process ends in exit_build
        
    def supdate(self, txt):
        w.info_label1.set_text(txt)
        w.info_label2.set_text(self.src.app)
        #return False
    def run(self):
        global _busy
        
        gobject.idle_add(self.supdate,'Extracting')
        if self.src.extract() > 0:
            _busy = False
        
            gobject.idle_add(self.supdate,'Etraction error')
            return self.exit_build('Fail', 1)
        
        gobject.idle_add(self.supdate, 'Running configure')
        if self.src.configure() > 0:
            _busy = False
        
            gobject.idle_add(self.supdate, 'configure error')
            return self.exit_build('Fail', 1)
        
        gobject.idle_add(self.supdate, 'RUnning make')
        if self.src.compile() > 0:
            _busy = False
        
            gobject.idle_add(self.supdate, 'Compilation error')
            return self.exit_build('Fail', 1)
        
        gobject.idle_add(self.supdate, 'Installing')
        if self.src.mkinstall() > 0:
            _busy = False
        
            gobject.idle_add(self.supdate, 'installation error')
            return self.exit_build('Fail', 1)
        
        gobject.idle_add(self.supdate, 'Packaging')
        if self.src.package() > 0:
            _busy = False
        
            gobject.idle_add(self.supdate, 'Error packaging')
            return self.exit_build('Fail', 1)
        
        gobject.idle_add(self.supdate, 'Packaged')
        
        _busy = False
        return self.exit_build('Success', 0)
        
        
    def exit_build(self, result, retval):
        logfile = self.src.logfile


        sql = 'UPDATE en_history SET stdout="%s", buildend=%s, result="%s" WHERE srcpath="%s" AND result="Running"'%(logfile,
        'CURRENT_TIMESTAMP',result, self.src.path)
        
        sql2 = 'DELETE FROM todo WHERE srcpath="%s"'% self.src.path

        dbconn.push_data(sql)  ## Update status to finished with result
        dbconn.push_data(sql2) ## Remove from todo list
    
        return retval
        

class dispatcher(threading.Thread):
    def __init__(self):
        global todo
        global _busy
        super(dispatcher, self).__init__()

    def run(self):
        global todo
        global _busy
        for task in todo:
            src = bbot.Source(task[1])
            while _busy is True:
                time.sleep(1)
            threaded_build = tbp(src)
            print '[dispatch] %s'% task[1]
            threaded_build.start()
        #print 'Finished building'

    def update_status(self, status):
        w.info_label3.set_text(status)
class gui(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        body = gtk.VBox(False, 4)
        self.add(body)
        
        banner = gtk.Label('This is a build bot')
        body.pack_start(banner, 0, 0, False)
        
        srcline = gtk.HBox(False, 4)
        src_lbl = gtk.Label('Pick Source Archive')
        srcline.pack_start(src_lbl, False)
        src_input = gtk.Entry()
        self.pathbox = src_input
        srcline.pack_start(src_input,True)
        bt_load_src = gtk.Button(stock=gtk.STOCK_OPEN)
        bt_load_src.connect('clicked', self.bt_load_src_click)
        srcline.pack_start(bt_load_src, False)
        
        body.pack_start(srcline, 0, 0, True)

        info_area = gtk.Frame('About Selection')
        blank1=gtk.Label()
        blank2 = gtk.Label()
        self.info_label1 = gtk.Label('No source has been selected')
        self.info_label2 = gtk.Label()
        self.info_label3 = gtk.Label()
        area = gtk.VBox(False, 4)
        area.pack_start(blank1)
        area.pack_start(self.info_label1)
        area.pack_start(self.info_label2)
        area.pack_start(self.info_label3)
        area.pack_start(blank2)
        
        
        info_area.add(area)
        
        btgo = gtk.Button('GO')
        btgo.connect('clicked', self.start_build)
        body.pack_start(btgo, 0, 0, False)
        body.pack_start(info_area, 0, 0, True)
        self.connect('destroy', gtk.main_quit)
    def start_build(self, widget):
        dispatch = dispatcher()
        dispatch.start()

    def bt_load_src_click(self, widget):
        dia = gtk.FileChooserDialog(title='Select source file',
            parent = self, buttons=(gtk.STOCK_OPEN,
            gtk.RESPONSE_OK,
            gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        dia.set_default_response(gtk.RESPONSE_OK)
        
        flt = gtk.FileFilter()
        flt.set_name('Compressed Archives')
        flt.add_pattern('*.tar.*')
        
        dia.add_filter(flt)
        res = dia.run()
        if res == gtk.RESPONSE_OK:
            self.pathbox.set_text(dia.get_filename())
            self.info_label1.set_property('xalign', 0.5)
            self.info_label2.set_property('label', '')
            self.info_label3.set_property('label', '')
            self.info_label1.set_property('label', 'Loading information...')
            gobject.idle_add(self.update_src_about, dia.get_filename())
            
        else:
            self.pathbox.set_text('')
        
        dia.destroy()
    
    def update_src_about(self, path):
        self.src = bbot.Source(path)
        self.info_label1.set_property('use-markup', True)
        self.info_label2.set_property('use-markup', True)
        self.info_label3.set_property('use-markup', True)
        self.info_label1.set_property('xalign', 0.0)
        self.info_label2.set_property('xalign', 0.0)
        self.info_label3.set_property('xalign', 0.0)
        self.info_label1.set_property('label', 
            '<b>Application: </b>%s'% self.src.app)
        self.info_label2.set_property('label', 
            '<b>Version: </b>%s'% self.src.version)
        self.info_label3.set_property('label',
            '<b>Source Type: </b>%s'% self.src.type)

class mWindow(gtk.Window):
    def treeview_click(self, treeview):
        #print 'tick'
        sel,itr = treeview.get_selection().get_selected()
        id = self._todo_list.get_value(itr, 0)
        src_ = self._todo_list.get_value(itr, 1)
        dsc_ = self._todo_list.get_value(itr, 2)
        
        
        self.selected_job = id
        self.selected_job_src = src_
        self.selected_job_dsc = dsc_
        #sql = "DELETE FROM todo WHERE id='%s'"% id
        #print sql
        #print self._todo_list.get_value(itr, 0)
        
    def delete_job(self, widget, job_id):
        '''Delete a job'''
        if not self.selected_job: 
            dia = dialogs.info(message='Please select a job first', parent=self)
            if dia.run(): dia.destroy()
            return
        ### Present some kind of dialog to verify before you do this
        conf = dialogs.question(parent=self,
        question = 'Are you sure you want to delete job %s?' %self.selected_job )
        
        res = conf.run()
        conf.destroy()
        if res == gtk.RESPONSE_CANCEL:
            return
        
        ### Only continue if the action has been verified by the dialog.
        sql = "DELETE FROM todo WHERE id='%s'"% self.selected_job
        dbconn.push_data(sql)
        self._todo_list.clear()
        self.refresh_task_list()
    def __init__(self):
        self.selected_job = None
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.set_default_size(600,400)
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_property('title', 'buildbot')
        body = gtk.VBox(False, 4)
        banner = gtk.Label()
        banner.set_property('use-markup', True)
        banner.set_property('label', 'Welcome to the buildbot')
        banner.set_property('xalign', 0.0)
        
        body.pack_start(banner, False, False, 4)
        blank = gtk.Label()
        body.pack_start(blank, False, False, 4)
        frm_todo = gtk.Frame('Scheduled Builds')
        ### ------------------ Scheduled builds list -------------------##
        frm_todo_int = gtk.VBox(False, 4)
        
        sw = gtk.ScrolledWindow()
        
        treestore = gtk.TreeStore(str, str, str)
        self._todo_list = treestore
        #treestore.append(None, ['0','Task 1', 'Description Path'])
        #treestore.append(None,['1','Task 2', 'Description Path'])
        
        ## Get the todo list from the database:
        _lst = dbconn.refresh_todo()
        for row in _lst:
            treestore.append(None, [row[0], row[1], row[2]])

        tv = gtk.TreeView(treestore)
        tv.connect('cursor-changed', self.treeview_click)
        col = gtk.TreeViewColumn('Task ID')
        col1 = gtk.TreeViewColumn('Source URI')
        col2 = gtk.TreeViewColumn('Description URI')
        tv.append_column(col)
        tv.append_column(col1)
        tv.append_column(col2)
        
        cell = gtk.CellRendererText()
        cell1 = gtk.CellRendererText()
        cell2 = gtk.CellRendererText()
        col.pack_start(cell, True)
        col1.pack_start(cell1, True)
        col2.pack_start(cell2, True)
        col.add_attribute(cell, 'text', 0)
        col1.add_attribute(cell1, 'text', 1)
        col2.add_attribute(cell2, 'text', 2)
        tv.set_search_column(0)
        col.set_sort_column_id(0)
        tv.set_reorderable(True)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.add(tv)
        ### End of tree, Lets add some action buttons in the same frame
        
        toolbar_frame = gtk.Frame()
        task_toolbar = gtk.HBox(False, 4)
        bt_addtask = gtk.Button(label='New Job')
        #bt_addtask.connect('clicked', self.sample_add)
        bt_addtask.connect('clicked', self._show_add_form)
        img = gtk.Image()
        img.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
        bt_addtask.set_image(img)
        
        img1 = gtk.Image()
        bt_remove = gtk.Button('Delete Job')
        img1.set_from_stock(gtk.STOCK_REMOVE, gtk.ICON_SIZE_BUTTON)
        bt_remove.set_image(img1)
        bt_remove.connect('clicked', self.delete_job, self.selected_job)
        
        bt_edit = gtk.Button('Edit Job')
        img2 = gtk.Image()
        img2.set_from_stock(gtk.STOCK_EDIT, gtk.ICON_SIZE_BUTTON)
        bt_edit.set_image(img2)
        
        lhblank = gtk.Label()
        rhblank = gtk.Label()
        task_toolbar.pack_start(lhblank, True, True, 0)
        task_toolbar.pack_start(bt_addtask, False, False, 2)
        task_toolbar.pack_start(bt_remove, False, False, 2)
        task_toolbar.pack_start(bt_edit, False, False, 2)
        task_toolbar.pack_start(rhblank, True, True, 0)
        toolbar_frame.add(task_toolbar)
        frm_todo_int.pack_start(sw, True, True, 4)
        frm_todo_int.pack_start(toolbar_frame, False, True, 4)
        
        #frm_todo_int.pack_start(int_lbl, True, True, 4)
        frm_todo.add(frm_todo_int)
        
        body.pack_start(frm_todo, True, True, 4)
        ### ------------------- End of scheduled buidls list -----------##
        frm_history = gtk.Frame('Current bot status')
        frm_history_int = gtk.VBox(False, 4)
        frm_history.add(frm_history_int)
        hist_lbl = gtk.Label('current status information here.')
        blnk = gtk.Label()
        frm_history_int.pack_start(hist_lbl, True, True, 4)
        frm_history_int.pack_start(blnk, True, True, 4)
        
        body.pack_start(frm_history, False, False, 4)
        
        self.statusbar = gtk.Statusbar()
        body.pack_start(self.statusbar, False, True, 4)


        
        self.info_label1 = gtk.Label()
        self.info_label2 = gtk.Label()
        self.info_label3 = gtk.Label()


        self.add(body)
        self.connect('destroy', gtk.main_quit)
    def _show_add_form(self, widget):
        ###aw = JobWindow()
        ###aw.show_all()
        ##print aw.tarball
        aj = _AddJobWindow(parent=self)
        res = aj.run()
        if res == gtk.RESPONSE_ACCEPT:
            self.add_job(aj.srcpath, aj.descpath)
        
        aj.destroy()
    
    def add_job(self, srcpath, descpath):
        sql = 'INSERT INTO todo (srcpath, descpath) VALUES ("%s","%s")'% \
        (srcpath, descpath)
        ## Push the sql data into the database
        dbconn.push_data(sql)
        ## Refresh the GUI
        self.refresh_task_list()
        
        
    
    def refresh_task_list(self):
        _lst = dbconn.refresh_todo()
        self._todo_list.clear()
        for row in _lst:
            self._todo_list.append(None, [row[0], row[1], row[2]])

class _AddJobWindow(gtk.Dialog):
    def __init__(self, parent):
        self.srcpath = ''
        self.descpath = ''
        gtk.Dialog.__init__(self, parent=parent, buttons=(gtk.STOCK_OK, 
            gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, 
            gtk.RESPONSE_CANCEL), title='Add new Job')
        self.set_default_size(600,200)
        self.resize(600,200)
        self.set_position(gtk.WIN_POS_CENTER)
        dbody = self.vbox
        
        hdr = gtk.Label()
        hdr.set_property('use-markup', True)
        hdr.set_property('label', 'Add new job')
        hdr.set_property('xalign', 0.0)
        dbody.pack_start(hdr, True, True, 2)
        
        blnk = gtk.Label()
        dbody.pack_start(blnk)
        srcline = gtk.HBox(False, 4)
        dbody.pack_start(srcline, False, False, 2)
        src_line_lbl = gtk.Label()
        src_line_lbl.set_property('xalign', 0.0)
        src_line_lbl.set_property('label', 'URI to Source')
        srcline.pack_start(src_line_lbl, False, False, 0)
        src_line_txt = gtk.Entry()
        srcline.pack_start(src_line_txt, True, True, 2)
        src_line_btn = gtk.Button(stock=gtk.STOCK_OPEN)
        src_line_btn.connect('clicked', self.load_src)
        srcline.pack_start(src_line_btn, False, False, 2)
        self._src_entry = src_line_txt
        
        descline = gtk.HBox(False, 4)
        dsc_line_lbl = gtk.Label()
        dsc_line_lbl.set_property('xalign', 0.0)
        dsc_line_lbl.set_property('label', 'URI to Description')
        descline.pack_start(dsc_line_lbl, False, False, 0)
        dsc_line_txt = gtk.Entry()
        descline.pack_start(dsc_line_txt, True, True, 2)
        dsc_line_btn = gtk.Button(stock=gtk.STOCK_OPEN)
        dsc_line_btn.connect('clicked', self.load_desc)
        descline.pack_start(dsc_line_btn, False, False, 2)
        self._desc_entry = dsc_line_txt
        
        dbody.pack_start(descline, False, False, 2)
        

        self.show_all()
    
    def load_src(self, widget):
        dia = gtk.FileChooserDialog(buttons = (gtk.STOCK_OPEN,
        gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        dia.set_title('Select source file')
        filt = gtk.FileFilter()
        filt.set_name('Compressed Archives')
        filt.add_pattern('*.tar.*')
        dia.add_filter(filt)
        
        ans = dia.run()
        if ans == gtk.RESPONSE_ACCEPT:
            self.srcpath = dia.get_filename()
        else:
            self.srcpath= ''
        
        self._src_entry.set_text(self.srcpath)
        dia.destroy()
    
    def load_desc(self, widget):
        dia = gtk.FileChooserDialog(buttons = (gtk.STOCK_OPEN,
        gtk.RESPONSE_ACCEPT, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        dia.set_title('Select Description File')
        
        ans = dia.run()
        if ans == gtk.RESPONSE_ACCEPT:
            self.descpath = dia.get_filename()
        else:
            self.descpath = ''
        
        self._desc_entry.set_text(self.descpath)
        dia.destroy()
        

        
        
        
#w = gui()
w = mWindow()
w.statusbar.push(0, 'Idle')
w.show_all()
tmon = Monitor(w.info_label1)
tmon.start()
gtk.gdk.threads_enter()
gtk.main()
gtk.gdk.threads_leave()

_busy = False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.