aboutsummaryrefslogtreecommitdiff
blob: 85bccf38c537da0fd758a8dcf6da1c48df4b5372 (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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
    g-octave
    ~~~~~~~~
    
    Main script of g-Octave.
    
    :copyright: (c) 2009-2010 by Rafael Goncalves Martins
    :license: GPL-2, see LICENSE for more details.
"""

from __future__ import print_function

has_fetch = True

__issue_tracker = 'Mail to: rafael@rafaelmartins.eng.br'

import sys

# This block ensures that ^C interrupts are handled quietly.
# Code snippet from Portage
try:
    import signal

    def exithandler(signum,frame):
        signal.signal(signal.SIGINT, signal.SIG_IGN)
        signal.signal(signal.SIGTERM, signal.SIG_IGN)
        sys.exit(1)

    signal.signal(signal.SIGINT, exithandler)
    signal.signal(signal.SIGTERM, exithandler)

except KeyboardInterrupt:
    sys.exit(1)


import getpass
import os
import optparse
import portage
import subprocess

out = portage.output.EOutput()

current_dir = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(os.path.join(current_dir, '..', 'g_octave')):
    sys.path.insert(0, os.path.join(current_dir, '..'))

from g_octave.log import Log
log = Log('g-octave')

log.info('Initializing...')
import g_octave

def main():

    parser = optparse.OptionParser(
        usage = '%prog [options] <package_name | package_name-version>',
        version = '%prog ' + g_octave.__version__,
        description = g_octave.__description__
    )
    
    parser.add_option(
        '-l', '--list',
        action = 'store_true',
        dest = 'list',
        default = False,
        help = 'show a list of packages available to install and exit'
    )
    
    parser.add_option(
        '-i', '--info',
        action = 'store_true',
        dest = 'info',
        default = False,
        help = 'show a description of the required package and exit'
    )
    
    parser.add_option(
        '-p', '--pretend',
        action = 'store_true',
        dest = 'pretend',
        default = False,
        help = 'don\'t (un)merge packages, only create ebuilds and solve the dependencies'
    )
    
    parser.add_option(
        '-a', '--ask',
        action = 'store_true',
        dest = 'ask',
        default = False,
        help = 'ask to confirmation before perform (un)merges'
    )
    
    parser.add_option(
        '-v', '--verbose',
        action = 'store_true',
        dest = 'verbose',
        default = False,
        help = 'Portage makes a lot of noise.'
    )
    
    parser.add_option(
        '-u', '--update',
        action = 'store_true',
        dest = 'update',
        default = False,
        help = 'try to update a package or all the installed packages'
    )
    
    parser.add_option(
        '-s', '--search',
        action = 'store_true',
        dest = 'search',
        default = False,
        help = 'search for packages with some term on the name (regular expressions allowed)'
    )
    
    parser.add_option(
        '-C', '--unmerge',
        action = 'store_true',
        dest = 'unmerge',
        default = False,
        help = 'try to unmerge a package instead of merge'
    )
    
    parser.add_option(
        '--scm',
        action = 'store_true',
        dest = 'scm',
        default = False,
        help = 'enable the installation of the current live version of a package, if disabled on the configuration file'
    )
    
    parser.add_option(
        '--no-scm',
        action = 'store_true',
        dest = 'no_scm',
        default = False,
        help = 'disable the installation of the current live version of a package, if enabled on the configuration file'
    )
    
    parser.add_option(
        '-f', '--force',
        action = 'store_true',
        dest = 'force',
        default = False,
        help = 'forces the recreation of the ebuilds'
    )
    
    parser.add_option(
        '--force-all',
        action = 'store_true',
        dest = 'force_all',
        default = False,
        help = 'forces the recreation of the overlay and of the ebuilds'
    )
    
    parser.add_option(
        '--no-colors',
        action = 'store_false',
        dest = 'colors',
        default = True,
        help = 'don\'t use colors on the CLI'
    )
    
    parser.add_option(
        '--sync',
        action = 'store_true',
        dest = 'sync',
        default = False,
        help = 'search for updates of the package database, patches and auxiliary files'
    )
    
    parser.add_option(
        '--config',
        action = 'store_true',
        dest = 'config',
        default = False,
        help = 'return a value from the configuration file (/etc/g-octave.cfg)'
    )
    
    options, args = parser.parse_args()
    
    if not options.colors:
        portage.output.nocolor()
    
    from g_octave.config import Config
    from g_octave.fetch import need_update, check_updates, download_files, check_db_cache
    
    conf_prefetch = Config(True)
    
    if options.config:
        try:
            log.info('Returning configuration data.')
            print(conf_prefetch.__getattr__(args[0]))
        except:
            log.error('Invalid configuration key: %s' % args[0])
            return os.EX_DATAERR
        return os.EX_OK
    
    from g_octave.package_manager import Portage, Pkgcore, Paludis
    
    if conf_prefetch.package_manager == 'portage':
        log.info('Your package manager is: Portage')
        pkg_manager = Portage(options.ask, options.verbose, options.pretend, not options.colors)
    elif conf_prefetch.package_manager == 'pkgcore':
        log.info('Your package manager is: Pkgcore')
        pkg_manager = Pkgcore(options.ask, options.verbose, options.pretend, not options.colors)
    elif conf_prefetch.package_manager == 'paludis':
        log.info('Your package manager is: Paludis')
        pkg_manager = Paludis(options.ask, options.verbose, options.pretend, not options.colors)
    else:
        log.error('Invalid package manager: %s' % conf_prefetch.package_manager)
        out.eerror('Invalid package manager: %s' % conf_prefetch.package_manager)
        return os.EX_CONFIG
    
    # checking if the package manager is installed
    if not pkg_manager.is_installed():
        log.error('Package manager not installed: %s' % conf_prefetch.package_manager)
        out.eerror('Package manager not installed: %s' % conf_prefetch.package_manager)
        return os.EX_CONFIG
    
    # checking if the current user is allowed to run g-octave
    current_user = getpass.getuser()
    if current_user not in pkg_manager.allowed_users():
        log.error(
            'The current user (%s) can\'t run the current selected '
            'package manager (%s)' % (current_user, conf_prefetch.package_manager)
        )
        out.eerror(
            'The current user (%s) can\'t run the current selected '
            'package manager (%s)' % (current_user, conf_prefetch.package_manager)
        )
        return os.EX_NOPERM
    
    # checking if our overlay is correctly added to PORTDIR_OVERLAY
    if not pkg_manager.check_overlay(conf_prefetch.overlay, out):
        log.error('Overlay not properly configured.')
        return os.EX_CONFIG
    
    if has_fetch:
        log.info('You can fetch package databases.')
        
        # checking if we have a package database
        if need_update() and not options.sync:
            log.error('No package database found.')
            out.eerror('Please run "g-octave --sync" to download a package database!')
            return os.EX_USAGE
        
        if options.sync:
            
            log.info('Searching updates ...')
            out.einfo('Searching updates ...')
            
            files = check_updates()
            
            if len(files) > 0:
                
                log.info('Updates are available. Downloading ...')
                out.einfo('Updates are available. Downloading ...')
                print()
                download_files(files)
                print()
                log.info('Package database update done.')
                out.einfo('Done.')
            else:
                log.info('No updates available.')
                out.einfo('No updates available.')
            
            return os.EX_OK
    else:
        log.info('You can\'t fetch package databases.')
        if options.sync:
            log.error('You can\'t fetch package databases.')
            out.eerror('"--sync" not available, please install g-octave-9999 if you want this.')
            return os.EX_USAGE
    
    check_db_cache()
    
    conf = Config()
    
    from g_octave.description import Description
    from g_octave.description_tree import DescriptionTree
    from g_octave.ebuild import Ebuild, EbuildException
    from g_octave.overlay import create_overlay

    if options.list:
        log.info('Listing available packages.')
        tree = DescriptionTree()
        print(portage.output.blue('Available packages:\n'))
        for category in tree.pkg_list:
            print(portage.output.white('%s:') % category)
            for pkg in tree.pkg_list[category]:
                print('\t%s-%s' % (pkg['name'], pkg['version']))
            print()
        return os.EX_OK
    elif options.update:
        pass
    elif len(args) == 0:
        log.error('You need provide an argument.')
        out.eerror('You need provide an argument.')
        return os.EX_USAGE
    elif len(args) > 1:
        log.error('g-octave can install only one package at once.')
        out.eerror('At this moment g-octave can install only one package at once')
        return os.EX_USAGE
    
    # if we're alive yet, we have a package to install! :D
    # or a search to do! :P
    
    create_overlay(options.force_all)
    
    if len(args) > 0:
        
        if options.search:
            log.info('Searching for packages: %s' % args[0])
            tree = DescriptionTree()
            print(
                portage.output.blue('Search results for '),
                portage.output.white(args[0]),
                portage.output.blue(':\n'),
                sep = ''
            )
            packages = tree.search(args[0])
            for pkg in packages:
                print(
                    portage.output.green('Package:'),
                    portage.output.white(pkg)
                )
                print(
                    portage.output.green('Available versions:'),
                    portage.output.red(', '.join(packages[pkg]))
                )
                print()
            return os.EX_OK
        
        log.info('Processing a package: %s' % args[0])
        try:
            ebuild = Ebuild(args[0], options.force or options.force_all, pkg_manager=pkg_manager)
        except EbuildException:
            log.error('Package not found: %s' % args[0])
            out.eerror('Package not found: %s' % args[0])
            return os.EX_DATAERR
        
        if options.info:
            log.info('Returning info about the package.')
            pkg = ebuild.description()
            print(portage.output.blue('Package:'), portage.output.white(str(pkg.name)))
            print(portage.output.blue('Version:'), portage.output.white(str(pkg.version)))
            print(portage.output.blue('Date:'), portage.output.white(str(pkg.date)))
            print(portage.output.blue('Maintainer:'), portage.output.white(str(pkg.maintainer)))
            print(portage.output.blue('Description:'), portage.output.white(str(pkg.description)))
            print(portage.output.blue('Categories:'), portage.output.white(str(pkg.categories)))
            print(portage.output.blue('License:'), portage.output.white(str(pkg.license)))
            print(portage.output.blue('Url:'), portage.output.white(str(pkg.url)))
            return os.EX_OK
        
        atom, catpkg = ebuild.create()
    
    if options.unmerge:
        log.info('Calling the package manager to uninstall the package.')
        ret = pkg_manager.uninstall_package(atom, catpkg)
    elif options.update:
        if len(args) > 0:
            log.info('Calling the package manager to update the package.')
            ret = pkg_manager.update_package(atom, catpkg)
        else:
            log.info('Calling the package manager to update all the installed packages.')
            ret = pkg_manager.update_package()
    else:
        log.info('Calling the package manager to install the package.')
        ret = pkg_manager.install_package(atom, catpkg)
    
    if ret != os.EX_OK:
        log.error('"%s" returned an error.' % conf.package_manager)
        out.eerror('"%s" returned an error.' % conf.package_manager)
        sys.exit(ret)
    
    if options.unmerge and len(pkg_manager.post_uninstall) > 0:
        log.info(' '.join(pkg_manager.post_uninstall))
        print()
        for i in pkg_manager.post_uninstall:
            out.einfo(i)
    
    log.info('Finishing g-octave... all OK!')
    return os.EX_OK


if __name__ == '__main__':
    
    from g_octave.exception import *
    
    return_code = os.EX_OK
    
    try:
        return_code = main()
    except ConfigException as error:
        log.error('Config class error - %s' % error)
        out.eerror('Config class error - %s' % error)
        return_code = os.EX_CONFIG
    except DescriptionException as error:
        log.error('Description class error - %s' % error)
        out.eerror('Description class error - %s' % error)
        return_code = os.EX_SOFTWARE
    except DescriptionTreeException as error:
        log.error('DescriptionTree class error - %s' % error)
        out.eerror('DescriptionTree class error - %s' % error)
        return_code = os.EX_SOFTWARE
    except EbuildException as error:
        log.error('Ebuild class error - %s' % error)
        out.eerror('Ebuild class error - %s' % error)
        return_code = os.EX_SOFTWARE
    except FetchException as error:
        log.error('Fetch module error - %s' % error)
        out.eerror('Fetch module error - %s' % error)
        return_code = os.EX_SOFTWARE
    except OSError as error:
        log.error('Operating System error - %s' % error)
        out.eerror('Operating System error - %s' % error)
        out.eerror('Try run "g-octave" as root.')
        return_code = os.EX_OSERR
    except IOError as error:
        log.error('I/O error - %s' % error)
        out.eerror('I/O error - %s' % error)
        out.eerror('Try run "g-octave" as root.')
        return_code = os.EX_IOERR
    except KeyError as error:
        log.error('Key error - %s' % error)
        out.eerror('Key error - %s' % error)
        out.eerror('Probably you have more than one overlay configured to use with g-octave')
        out.eerror('Try remove the oldest and maintain only the overlay actually in use.')
        return_code = os.EX_SOFTWARE
    except Exception as error:
        log.error('Unknown error - %s' % error)
        out.eerror('Unknown error - %s' % error)
        return_code = os.EX_SOFTWARE
    
    if return_code not in [os.EX_OK, os.EX_CONFIG, os.EX_USAGE, os.EX_DATAERR, os.EX_NOPERM]:
        out.einfo('If you fell that this is a bug, please report to us.')
        out.einfo(__issue_tracker)
    
    sys.exit(return_code)