Source

drivcenDRIVENvpackager / src / vpackager / vpackager.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
#!/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 v3 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 gtk
import os
import buildutils
import bottools
import user
import message
import time
import threading

__author__ = 'M0E-lnx'
__author_email__ = 'moc.liamg@xnl.E0M'[::-1]
__version__ = '0.1'

""" Pygtk interface for vpackager. Consists of a wizard guiding the user
though the stems necessary to perform a single package build."""

def _(str):
    return str

class MissingSrcLocationDir(Exception):
    pass

class Build(object):
    """ Generic Build project object representing a build session. This object
    does nothing other than store some global variables and attributes for
    the final steps to read"""
    def __init__(self):
        self.source = None
        self.arch = None
        self.release = '1'
        self.tag = None
        self.packager = 'packagebot'
        self.buildoptions = []
        self.cflags = ''
        self.summary = ''
        self.description = ''
        self.url = ''
        self.licence = ''

class Page(gtk.VBox):
    ''' Dedicated class based on gtk.VBox to be used as a wizard page
    ARGUMENTS:
        parent = parent wizard. Available via the ._parent attribute'''
    def __init__(self, parent=None):
        gtk.VBox.__init__(self)
        self._parent = parent
        self.set_border_width(4)
        self.complete = False
        self.type = gtk.ASSISTANT_PAGE_CONTENT
        self.title = ''

class PageVerify(Page):
    """ Verify the build details. (Wizard page)"""
    def __init__(self, parent=None):
        Page.__init__(self, parent)
        self.title = _('Verify Build Details')
        self.type = gtk.ASSISTANT_PAGE_CONTENT
        self.complete = True
        self.status = gtk.Label()
        self.status.set_property('use-markup', True)
        self.status.set_property('xalign', 0.0)

        self.pack_start(self.get_content(), True, True, 8)
        self.pack_start(self.status, False, False, 12)

    def _update_status(self, status):
        return self.status.set_markup(status)

    def get_content(self):
        '''Returns the content of this page'''
        l = gtk.VBox()
        txt = _('Ready to build')
        lb = gtk.Label()
        lb.set_use_markup(True)
        lb.set_markup('<b> %s </b>'%txt)

        txt = [
            _('Please verify that all the information about the package is correct.'),
            _('You may still go back and make any adjustments as necessary.')
        ]
        lbl = gtk.Label()
        lbl.set_property('justify', gtk.JUSTIFY_FILL)
        lbl.set_property('wrap', True)
        lbl.set_text(' '.join(txt).strip())

        l.pack_start(lb, False, False, 4)
        l.pack_start(lbl, False, False, 12)

        lbr = gtk.Label()
        lbr.set_use_markup(True)
        lbr.set_markup(
            '<b>%s</b>'% _('Click forward to continue')  )

        l.pack_start(lbr, True, True, 12)
        return l

    def process_data(self):
        #pass
        return self._do_create_build()

    def _do_create_build(self):
        if self._parent.project.source.makeSlackBuild(
            buildno = self._parent.project.release,
            packager = self._parent.project.packager):
            pass
        else:
            dia = message.Error(text = _('Error creating slackbuild'),
                                         parent = self.parent)
            if dia.run():
                dia.destroy()

class PageDoBuild(Page):
    """ Build process. Wizard page displaying the build output"""
    def __init__(self, parent=None):
        Page.__init__(self, parent)
        self.title = _('Compile Source')
        self.type = gtk.ASSISTANT_PAGE_PROGRESS
        self.pack_start(self.get_content(), True, True, 8)

    def get_content(self):
        ''' Return the content of this page'''
        box = gtk.VBox()

        self.pbar = gtk.ProgressBar()
        box.pack_start(self.pbar, False, True, 8)


        frmout = gtk.Frame(_('Output'))
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        tview = gtk.TextView()
        for edge in (gtk.TEXT_WINDOW_LEFT, gtk.TEXT_WINDOW_RIGHT,
                     gtk.TEXT_WINDOW_TOP, gtk.TEXT_WINDOW_BOTTOM):
            tview.set_border_window_size(edge, 4)

        sw.add(tview)
        frmout.add(sw)
        self.stdoutview = tview
        self.scrollw = sw
        tview.set_property('editable', False)

        box.pack_start(frmout, True, True, 8)
        return box


    def process_data(self):
        #pass
        if os.getuid() > 0:
            dia = message.Error(
                text = _('You must be root to use this application'),
                parent = self._parent)
            if dia.run():
                dia.destroy()
            return
        return self._act()

    def action(self):
        _prevdir = os.getcwd()
        try:
            os.chdir(self._parent.project.source.srclocation)
        except:
            raise MissingSrcLocationDir
        self._parent.task = self._parent.project.source.buildSlackBuild()
        self._parent.task.observer.add_callback(self.update_textview)
        # launch the task
        self._parent.task.run()
        self._parent.set_page_complete(self, True)
        self.pbar.set_fraction(1.0)
        self._parent.set_page_type(self, gtk.ASSISTANT_PAGE_SUMMARY)

        try:
            os.chdir(_prevdir)
        except:
            pass

    def update_textview(self, line):
        gtk.gdk.threads_enter()
        buf = self.stdoutview.get_buffer()
        buf.insert(buf.get_end_iter(), line)
        vad = self.scrollw.get_vadjustment()

        # Scroll the view
        newpos = vad.get_upper() - vad.get_page_size()
        vad.set_value(newpos)
        self.scrollw.set_vadjustment(vad)

        gtk.gdk.threads_leave()

    def _pulse_bar(self):
        self.pbar.set_text(
            _('Compiling') + ' %s-%s'% (self._parent.project.source.app,
                                        self._parent.project.source.ver)
        )
        while self._parent.task._popen_running:
            gtk.gdk.threads_enter()
            self.pbar.pulse()
            gtk.gdk.threads_leave()
            time.sleep(0.1)

    def _act(self, widget=None):
        tpopen = threading.Thread(target = self.action, args=())
        tpulse = threading.Thread(target = self._pulse_bar, args=())

        for t in (tpopen, tpulse):
            t.start()

class PageIntro(Page):
    ''' Introduction page. Collect information about the build process.'''
    def __init__(self, parent=None):
        Page.__init__(self, parent)
        self.title = _('Load source package')
        self.type = gtk.ASSISTANT_PAGE_INTRO

        self.pack_start(self.get_content(), False, True, 8)
        self.pack_start(self.__pkgline1(), False, True, 4)
        self.pack_start(self.__pkgline2(), False, True, 4)

        self.pack_start(self.__pkgline4(), True, True, 4)
        self.vboxDesc.set_property('sensitive', False)

    def get_content(self):
        ''' Return a container filled with stuff '''
        frm = gtk.Frame(_('Source Archive'))
        box = gtk.HBox()
        self.srcentry = gtk.Entry()
        box.pack_start(self.srcentry, True, True, 2)
        btbrowse = gtk.Button(stock=gtk.STOCK_OPEN)
        btbrowse.connect('clicked', self._browse)
        box.pack_start(btbrowse, False, False, 2)
        frm.add(box)
        return frm

    def _browse(self, widget=None):

        dia = gtk.FileChooserDialog(
            parent = self._parent,
            buttons = (gtk.STOCK_CANCEL,
                       gtk.RESPONSE_CANCEL,
                       gtk.STOCK_OK,
                       gtk.RESPONSE_OK),
            title = _('Select source to build'))
        filter = gtk.FileFilter()
        filter.add_pattern('*.[bg]z*')
        filter.set_name(_('Compressed Files'))
        try:
            dia.set_current_folder(os.path.join(user.home, 'Downloads'))
        except:
            pass
        dia.add_filter(filter)
        res = dia.run()
        dia.hide()
        if res == gtk.RESPONSE_OK:
            self.srcentry.set_text(dia.get_filename())
            self._src_change(dia.get_filename())
        else:
            self.srcentry.set_text('')

        self._parent.set_page_complete(self, res == gtk.RESPONSE_OK)

    def _src_change(self, newpath):
        srcob = buildutils.Source(newpath)
        if srcob.builder is not None:
            self._parent.project.source = srcob
            self.eName.set_text(srcob.app)
            self.eVer.set_text(srcob.ver)

        # update the text on the next page
        self._parent.pages[1]._update_status(
                _('Readyto build <b>%s</b> version <b>%s</b>')%
                    (srcob.app, srcob.ver))
        return

    def _license_change(self, widget):

        newl = widget.get_model()[widget.get_active()][0]
        self._parent.project.license = newl

    def _tag_change(self, widget):
        newtag = widget.get_model()[widget.get_active()][0]
        if newtag is 'None':
            self._parent.project.tag = ''
        else:
            self._parent.project.tag = newtag

    def _packager_change(self, widget):
        self._parent.project.packager = widget.get_text()

    def _release_change(self, widget):
        self._parent.project.release = widget.get_text()

    def __pkgline1(self):
        l = gtk.HBox()
        lName = gtk.Label(_("Application"))
        self.eName = gtk.Entry()
        l.pack_start(lName, False, False, 2)
        l.pack_start(self.eName, True ,True, 2)

        lVer = gtk.Label(_('Version'))
        self.eVer = gtk.Entry()
        l.pack_start(lVer, False, False, 2)
        l.pack_start(self.eVer, True, True, 2)

        return l

    def __pkgline2(self):
        l = gtk.HBox()

        lRel = gtk.Label(_('Release'))
        self.eRel = gtk.Entry(1)
        self.eRel.connect('changed', self._release_change)
        self.eRel.set_text('1')
        self.eRel.set_property('width-chars', 4)

        lPackager = gtk.Label(_('Packager'))
        self.ePackager = gtk.Entry()
        self.ePackager.connect('changed', self._packager_change)

        l.pack_start(lRel, False, False, 2)
        l.pack_start(self.eRel, True, True, 2)
        l.pack_start(lPackager, False, False, 2)
        l.pack_start(self.ePackager, True, True, 2)

        return l

    def __pkgline3(self):
        l = gtk.HBox()

        lSummary = gtk.Label(_('Summary'))
        self.eSummary = gtk.Entry(70)

        l.pack_start(lSummary, False, False, 2)
        l.pack_start(self.eSummary, True, True, 2)

        return l

    def __pkgline4(self):
        l = gtk.Frame(_('Package Description'))
        h = gtk.HBox()
        v = gtk.VBox()
        l.add(v)
        desc_choice = []
        self.radLoadDesc = gtk.RadioButton(group=None,
                                           label=_('Existing slack-desc'))
        self.eDescPath = gtk.Entry()
        self.btDescBrowse = gtk.Button(stock=gtk.STOCK_OPEN)
        h.pack_start(self.radLoadDesc, False, False, 2)
        h.pack_start(self.eDescPath, True, True, 2)
        h.pack_start(self.btDescBrowse, False, False, 2)
        v.pack_start(h, False, True, 4)

        self.radTypeInDesc = gtk.RadioButton(group=self.radLoadDesc,
                                             label=_('Write New'))
        v.pack_start(self.radTypeInDesc, False, True, 2)
        self.radLoadDesc.connect('toggled', self._toggle_desc_entry_mode)
        self.vboxDesc = gtk.VBox()

        self.vboxDesc.pack_start(self.__pkgline3(), False, True, 2)

        self.descriptionArea = self.__pkgline5()
        self.vboxDesc.pack_start(self.descriptionArea, True, True, 2)

        v.pack_start(self.vboxDesc, True, True, 2)

        return l

    def _toggle_desc_entry_mode(self, widget):

        for wid in (self.eDescPath,
                    self.btDescBrowse):
            wid.set_property('sensitive', widget.get_active())

        self.vboxDesc.set_property('sensitive', not widget.get_active())

    def __pkgline5(self):
        f = gtk.Frame(_('Description (7 lines max.)'))
        sw = gtk.ScrolledWindow()
        f.add(sw)
        self.descTextView = gtk.TextView()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.add(self.descTextView)
        self.descTextView.get_buffer().connect('changed', self._desc_changed)
        for edge in (gtk.TEXT_WINDOW_LEFT, gtk.TEXT_WINDOW_RIGHT,
                     gtk.TEXT_WINDOW_TOP, gtk.TEXT_WINDOW_BOTTOM):
            self.descTextView.set_border_window_size(edge, 4)

        return f

    def _desc_changed(self, widget=None):
        #print widget.get_line_count()
        if widget.get_line_count() > 7:
            dia = message.Error(
                text='7 Lines max please',
                parent = self._parent)
            if dia.run():
                dia.destroy()
                return
        #print widget.get_insert().get_name()
        iter = widget.get_end_iter()
        if iter.get_chars_in_line() > 70:
            print "WARNING: Line is too long"
            if iter.backward_word_start():
                widget.insert_at_cursor('\n')

    def process_data(self):
        pass
        #print 'Processing data for intro page'


class Wizard(gtk.Assistant):
    """ Main application class"""
    def __init__(self):
        gtk.Assistant.__init__(self)
        self.connect('destroy', self._exit)
        self.connect('delete-event', self._exit)
        self.connect('cancel', self._exit)
        self.connect('close', self._exit)
        self.connect('prepare', self._prepare)
        self.set_property('title', 'vpackager')
        self.set_size_request(600, 550)
        self.set_position(gtk.WIN_POS_CENTER)
        self.project = Build()

        self.task = None
        self.pages = [
                PageIntro(self),
                PageVerify(self),
                PageDoBuild(self)]

        for page in self.pages:
            self.append_page(page)
            self.set_page_title(page, page.title)
            self.set_page_complete(page, page.complete)
            self.set_page_type(page, page.type)

        self.show_all()

    def _force_stop(self):
        dia = message.Question(
                parent = self,
                text = _('The build process is currently running.') \
                        + " " + _('Are you sure you want to force it to stop?') \
                        +'\n <b>' + _('WARNING:') + ' </b>'\
                        +_('Quitting now will stop ALL build processes now.'))
        res = dia.run()
        dia.destroy()
        return res ==  gtk.RESPONSE_YES

    def _terminate(self):
        if self.task:
            if self.task._popen_running:
                if not self._force_stop():
                    return True
        try:
            self.task.kill_popen()
        except:
            pass
        return False

    def _exit(self, Widget = None, ud=None):
        if self._terminate() is True:
            return
        self.set_page_complete(self.get_nth_page(
            self.get_current_page()), True)
        return gtk.main_quit()


    def _prepare(self, assistant=None, page=None):
        return page.process_data()



if __name__ == '__main__':
    w = Wizard()
    gtk.gdk.threads_init()
    gtk.main()
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.