~whosdaz/wajig/docfix

« back to all changes in this revision

Viewing changes to src/wajig.py

  • Committer: Bazaar Package Importer
  • Author(s): Graham Williams, Tshepang Lekhonkhobe
  • Date: 2010-09-15 17:22:16 UTC
  • Revision ID: james.westby@ubuntu.com-20100915172216-6kv2lnbfnnw11lxo
Tags: 2.0.49
[ Tshepang Lekhonkhobe ]

* UPGRADE: Don't automatically do an "apt-get update" with the backup
  option; the behaviour is not standard, and is not so-damn-fast.

* UPGRADE: Add command line argument for --backup to specify target
  directory.  * Used to be "wajig --backup upgrade BKDIR".  * Now it's
  "wajig --backup=BKDIR upgrade".

* UPGRADE: Proceed to upgrade after using -b|--backup option.

* UPGRADE: Remove the option to install listed packages. This helps
  reduce code complexity.

* CHANGELOG: Now only displays uninstalled entries by default. This
  makes the hacky NEWS command redundant, which is therefore
  removed. (Closes: #424668).

* CHANGELOG: Add -v|--verbose=1 option to display the whole thing. This
  was the default option, but suggestion offered in #424668 is more
  sane.

* CHANGELOG: Add -x|--pager option to allow scrolling. This also implies
  -v|--verbose=1 (see above).

* DIST-UPGRADE: Add -b|--backup=BKDIR option to allow backing up of
  packages before dist-upgrade.

* BUILD: Now uses sudo instead of fakeroot to avoid permission-related.
  build failures for some packages (EG, fakeroot & cdbs). (Closes:
  #464003).

* Fix a parsing bug where "wajig changelog libgtk2.0-0" didn't work.

* Switch to dpkg-source 3.0 (native) format

* RBUILDDEPS: New option allowing one to determine which packages
  build-depends on a particular package. The long form command is
  "reverse-build-depends", else just use "wajig rbuilddeps pkgname".
  (Closes: #335240).

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
import commands
37
37
import changes
38
38
import perform
 
39
import util
39
40
 
40
41
########################################################################
41
42
# Global Variables
44
45
interactive = False  # Set to true for interactive command line
45
46
match_commands = []  # For interactive command line completion
46
47
backup = False
47
 
 
48
 
#------------------------------------------------------------------------
49
 
#
50
 
# SUPPORT METHODS
51
 
#
52
 
#------------------------------------------------------------------------
53
 
def requires_no_args(command, args, test=False):
54
 
    if len(args) > 1:
55
 
        if not test:
56
 
            message = "no further arguments"
57
 
            print "WaJIG Error: " + command.upper() + " requires " + message
58
 
            finishup(1)
59
 
        return False
60
 
    return True
61
 
 
62
 
 
63
 
def requires_one_arg(command, args, message=False):
64
 
    if len(args) != 2:
65
 
        if message:  # checks if this is a unit test
66
 
            print "WaJIG Error: " + command.upper() + " requires " + message
67
 
            finishup(1)
68
 
        return False
69
 
    return True
70
 
 
71
 
 
72
 
def requires_two_args(command, args, message=False):
73
 
    if len(args) != 3:
74
 
        if message:  # checks if this is a unit test
75
 
            print "WaJIG Error: " + command.upper() + " requires " + message
76
 
            finishup(1)
77
 
        return False
78
 
    return True
79
 
 
80
 
 
81
 
def requires_opt_arg(command, args, message=False):
82
 
    if len(args) > 2:
83
 
        if message:  # checks if this is a unit test
84
 
            print "WaJIG Error: " + command.upper() +\
85
 
                  " has one optional arg: " + message
86
 
            finishup(1)
87
 
        return False
88
 
    return True
89
 
 
90
 
 
91
 
def requires_args(command, args, required=False):
92
 
    if len(args) == 1:
93
 
        if required:  # checks if this is a unit test
94
 
            print "WaJIG Error: {0} requires {1}".\
95
 
                   format(command.upper(), required)
96
 
            finishup(1)
97
 
        return False
98
 
    return True
99
 
 
100
 
 
101
 
def requires_package(package, path, test=False):
102
 
    if not os.path.exists(path):
103
 
        if not test:
104
 
            print 'The "' + package + '" package does not appear to be installed.'
105
 
            print 'Consider installing it with "wajig install ' + package + '".'
106
 
            finishup(1)
107
 
        return False
108
 
    return True
109
 
 
110
 
 
111
 
def finishup(code=0):
112
 
    global pause
113
 
    if pause:
114
 
        print "Press Enter to continue...",
115
 
        sys.stdin.readline()
116
 
    if not interactive:
117
 
        sys.exit(code)
 
48
pager = False  # Use a pager?
118
49
 
119
50
 
120
51
def print_help(command, args, verbose=False, exit=False):
121
52
    if   command == "doc" or command == "docs" or command == "documentation":
122
 
        requires_no_args(command, args)
 
53
        util.requires_no_args(command, args)
123
54
        verbose = 2
124
55
        documentation.help(verbose)
125
56
        if exit:
126
 
            finishup(0)
 
57
            util.finishup(0)
127
58
    elif command == "help":
128
 
        requires_no_args(command, args)
 
59
        util.requires_no_args(command, args)
129
60
        documentation.help(verbose)
130
61
        if exit:
131
 
            finishup(0)
 
62
            util.finishup(0)
132
63
 
133
64
 
134
65
#------------------------------------------------------------------------
137
68
#
138
69
#-----------------------------------------------------------------------
139
70
def list_commands():
140
 
    f = os.popen('wajig commands', 'r')
 
71
    f = os.popen('wajig -v commands', 'r')
141
72
    lines = f.readlines()
142
73
    command_patt = r'^ ([a-z][a-z-]*) '
143
74
    command_patt_r = re.compile(command_patt)
167
98
    #
168
99
    global match_commands  # List of cached matching commands
169
100
    n = len(text)
170
 
    word = None
171
101
    if state == 0:
172
102
        match_commands = []
173
103
        for w in all_commands:
182
112
    global all_commands
183
113
    global interactive
184
114
    interactive = True
 
115
    util.interactive = True
185
116
    try:
186
117
        import readline
187
118
        readline.parse_and_bind("tab: complete")
210
141
        elif command in ("doc", "docs", "documentation", "help"):
211
142
            print_help(command, cmd)
212
143
        elif cmd:
213
 
            select_command(command, cmd, False, False)
 
144
            select_command(command, cmd, False)
214
145
 
215
146
#------------------------------------------------------------------------
216
147
#
223
154
    global yes
224
155
    global noauth
225
156
    global backup
 
157
    global pager
226
158
    #
227
159
    # Remove commas and insert the arguments appropriately.
228
160
    #
232
164
        sys.argv += oldargv[i].split(",")
233
165
 
234
166
    try:
235
 
        sopts = "bdhnpqstvy"
236
 
        lopts = ["backup", "debug", "help", "pause", "quiet", "simulate",
237
 
                 "teaching", "verbose=", "version", "yes", "noauth"]
 
167
        sopts = "bdhnpqstvxy"
 
168
        lopts = ["backup=", "debug", "help", "pause", "quiet", "simulate",
 
169
                 "teaching", "verbose=", "version", "yes", "noauth", "pager"]
238
170
        opts, args = getopt.getopt(sys.argv[1:], sopts, lopts)
239
171
    except getopt.error, e:
240
172
        print e
241
173
        documentation.usage()
242
 
        finishup(2)
 
174
        util.finishup(2)
243
175
 
244
176
    simulate = False
245
177
    teaching = False
253
185
    for o, a in opts:
254
186
        if o in ["-h", "--help"]:
255
187
            documentation.usage()
256
 
            finishup()
257
 
        elif o in ["-b", "--backup"]:
 
188
            util.finishup()
 
189
        elif o == "-b":
258
190
            backup = True
 
191
        elif o == "--backup":
 
192
            if a in ("upgrade", "distupgrade") and len(sys.argv) < 4:
 
193
                print 'Should be of the form "wajig --backup=BKDIR upgrade"'
 
194
                util.finishup(1)
 
195
            backup = a
259
196
        elif o in ["-d", "--debug"]:
260
197
            debug = True
261
198
        elif o in ["-p", "--pause"]:
262
199
            pause = True
263
 
            perform.pause = True
 
200
            util.pause = True
 
201
        elif o in ["-x", "--pager"]:
 
202
            pager = True
 
203
            commands.set_verbosity_level(1)
264
204
        elif o in ["-q", "--quiet"]:
265
205
            perform.set_quiet()
266
206
        elif o in ["-s", "--simulate"]:
281
221
        elif o in ["-n", "--noauth"]:
282
222
            noauth = " --allow-unauthenticated "
283
223
        elif o == "-v":
284
 
            verbose = verbose + 1
 
224
            verbose += 1
285
225
            commands.set_verbosity_level(verbose)
286
226
        elif o == "--verbose":
287
 
            verbose = int(a)
 
227
            try:
 
228
                verbose = int(a)
 
229
            except ValueError:
 
230
                print 'Should be of the form "wajig --verbose=1 CMD"'
 
231
                util.finishup(1)
288
232
            commands.set_verbosity_level(verbose)
289
233
        elif o == "--version":
290
234
            documentation.version()
291
 
            finishup()
 
235
            util.finishup()
292
236
 
293
237
    #
294
238
    # NO ARGS => INTERACTIVE COMMAND LINE
330
274
    # Check for sys.exit (SystemExit exceptions) and return code.
331
275
    #
332
276
    if debug:
333
 
        select_command(command, args, verbose, teaching)
 
277
        select_command(command, args, verbose)
334
278
    else:
335
279
        try:
336
 
            select_command(command, args, verbose, teaching)
 
280
            select_command(command, args, verbose)
337
281
        except SystemExit, e:
338
282
            sys.exit(e)
339
283
        except:
340
 
            # print "Exiting...."
341
 
            None
342
 
    finishup(0)
343
 
 
344
 
 
345
 
def select_command(command, args, verbose, teaching):
 
284
            pass
 
285
    util.finishup(0)
 
286
 
 
287
 
 
288
def select_command(command, args, verbose):
346
289
    """Select the appropriate command and execute it.
347
290
 
348
291
    This function was separated out of the main so that I could wrap
351
294
    result = 0
352
295
    changes.start_log()
353
296
    if command in ["addcdrom", "cdromadd"]:
354
 
        if requires_no_args(command, args):
 
297
        if util.requires_no_args(command, args):
355
298
            perform.execute("apt-cdrom add", root=True)
356
299
 
357
300
    elif command == "addrepo":
358
 
        if requires_one_arg(command, args,
 
301
        if util.requires_one_arg(command, args,
359
302
                "a PPA (Personal Package Archive) repository to add"):
360
 
            if requires_package("add-apt-repository",
 
303
            if util.requires_package("add-apt-repository",
361
304
                                "/usr/bin/add-apt-repository"):
362
305
                perform.execute("add-apt-repository " + args[1], root=True)
363
306
 
364
307
    elif command in ["autoalts", "autoalternatives"]:
365
 
        if requires_one_arg(command, args, "name alternative to set as auto"):
 
308
        if util.requires_one_arg(command, args, "name alternative to set as auto"):
366
309
            perform.execute("update-alternatives --auto " + args[1], root=True)
367
310
 
368
311
    elif command == "autodownload":
369
 
        if requires_no_args(command, args):
 
312
        if util.requires_no_args(command, args):
370
313
            if verbose > 0:
371
314
                commands.do_update()
372
315
                filter_str = ""
380
323
            commands.do_newupgrades()
381
324
 
382
325
    elif command == "autoclean":
383
 
        if requires_no_args(command, args):
 
326
        if util.requires_no_args(command, args):
384
327
            perform.execute("apt-get autoclean", root=True)
385
328
 
386
329
    elif command == "autoinstall":
387
 
        if requires_args(command, args, "a list of package names"):
 
330
        if util.requires_args(command, args, "a list of package names"):
388
331
            command = "apt-get install --assume-yes " + noauth + " " +\
389
332
                      perform.concat(args[1:])
390
333
            perform.execute(command, root=True)
391
334
 
392
335
    elif command == "autoremove":
393
 
        if requires_no_args(command, args):
 
336
        if util.requires_no_args(command, args):
394
337
            perform.execute("apt-get autoremove", root=True)
395
338
 
396
339
    elif command in ["available", "avail"]:
397
 
        if requires_args(command, args, "a list of packages"):
 
340
        if util.requires_args(command, args, "a list of packages"):
398
341
            perform.execute("apt-cache policy " + perform.concat(args[1:]))
399
342
 
400
343
    elif command in ["bug", "bugs", "reportbug"]:
401
 
        if requires_one_arg(command, args, "a single named package"):
402
 
            if requires_package("reportbug", "/usr/bin/reportbug"):
 
344
        if util.requires_one_arg(command, args, "a single named package"):
 
345
            if util.requires_package("reportbug", "/usr/bin/reportbug"):
403
346
                # 090430 Specify bts=debian since ubuntu not working at present
404
347
                perform.execute("reportbug --bts=debian " + args[1])
405
348
 
406
349
    elif command == "build":
407
 
        if requires_args(command, args, "a list of package names"):
408
 
            if requires_package("fakeroot", "/usr/bin/fakeroot"):
 
350
        if util.requires_args(command, args, "a list of package names"):
 
351
            if util.requires_package("sudo", "/usr/bin/sudo"):
409
352
                # First make sure dependencies are met
410
353
                result = perform.execute("apt-get build-dep " +
411
 
                                         perform.concat(args[1:]), root=True)
 
354
                                          perform.concat(args[1:]), root=True)
412
355
                if not result:
413
 
                    perform.execute("fakeroot " + "apt-get source -b " +
414
 
                                    perform.concat(args[1:]))
 
356
                    perform.execute("apt-get source -b " +
 
357
                                     perform.concat(args[1:]), root=True)
415
358
 
416
 
    elif command == "builddepend":
417
 
        if requires_args(command, args, "a list of package names"):
 
359
    elif command in ("builddepend", "builddep"):
 
360
        if util.requires_args(command, args, "a list of package names"):
418
361
            perform.execute("apt-get build-dep " + perform.concat(args[1:]),
419
 
                            root=True)
 
362
                             root=True)
 
363
 
 
364
    elif command in ("reverse-build-depends", "rbuilddeps"):
 
365
        if util.requires_one_arg(command, args, "one package name") \
 
366
        and util.requires_package("grep-dctrl", "/usr/bin/grep-dctrl"):
 
367
            commands.rbuilddep(args[1])
420
368
 
421
369
    elif command == "changelog":
422
 
        if requires_args(command, args, "a list of packages"):
423
 
            commands.do_changelog(args[1:])
 
370
        if util.requires_one_arg(command, args, "one package name") \
 
371
        and util.package_exists(args[1]):
 
372
            commands.do_changelog(args[1], pager)
424
373
 
425
374
    elif command == "clean":
426
 
        if requires_no_args(command, args):
 
375
        if util.requires_no_args(command, args):
427
376
            perform.execute("apt-get clean", root=True)
428
377
 
429
378
    elif command == "contents":
430
 
        if requires_one_arg(command, args, "a filename"):
 
379
        if util.requires_one_arg(command, args, "a filename"):
431
380
            perform.execute("dpkg --contents " + args[1])
432
381
 
433
382
    elif command == "dailyupgrade":
434
 
        if requires_no_args(command, args):
 
383
        if util.requires_no_args(command, args):
435
384
            commands.do_update()
436
385
            perform.execute("apt-get --show-upgraded dist-upgrade", root=True)
437
386
 
438
387
    elif command == "dependents":
439
 
        if requires_one_arg(command, args, "package name"):
 
388
        if util.requires_one_arg(command, args, "one package name"):
440
389
            commands.do_dependents(args[1])
441
390
 
442
391
    elif command == "describe":
443
 
        if requires_args(command, args, "a list of packages"):
 
392
        if util.requires_args(command, args, "a list of packages"):
444
393
            commands.do_describe(args[1:])
445
394
 
446
395
    elif command in ["describenew", "newdescribe"]:
447
 
        if requires_no_args(command, args):
 
396
        if util.requires_no_args(command, args):
448
397
            commands.do_describe_new()
449
398
 
450
399
    elif command in ["detail", "details", "show"]:
451
 
        if requires_args(command, args, "a list of packages or package file"):
 
400
        if util.requires_args(command, args, "a list of packages or package file"):
452
401
            verbose = 2
453
402
            commands.set_verbosity_level(verbose)
454
403
            commands.do_describe(args[1:])
455
404
 
456
405
    elif command in ["detailnew", "newdetail"]:
457
 
        if requires_no_args(command, args):
 
406
        if util.requires_no_args(command, args):
458
407
            verbose = 2
459
408
            commands.set_verbosity_level(verbose)
460
409
            commands.do_describe_new()
461
410
 
462
411
    elif command == "distupgrade":
463
 
        if requires_opt_arg(command, args,
 
412
        upgradable = str()
 
413
        if util.requires_opt_arg(command, args,
464
414
                            "the distribution to upgrade to"):
465
 
            cmd = "apt-get -u %s %s " % (yes, noauth)
466
 
            if len(args) == 2:
467
 
                cmd += "-t " + args[1] + " "
468
 
            cmd += "dist-upgrade"
469
 
            perform.execute(cmd, root=True)
 
415
            if backup \
 
416
            and util.requires_package("dpkg-repack", "/usr/bin/dpkg-repack") \
 
417
            and util.requires_package("fakeroot", "/usr/bin/fakeroot"):
 
418
                upgradable = \
 
419
                changes.backup_before_upgrade(backup, distupgrade=True)
 
420
            if upgradable != "quit":
 
421
                cmd = "apt-get -u %s %s " % (yes, noauth)
 
422
                if len(args) == 2:
 
423
                    cmd += "-t " + args[1] + " "
 
424
                cmd += "dist-upgrade"
 
425
                perform.execute(cmd, root=True)
470
426
 
471
427
    elif command == "download":
472
 
        if requires_args(command, args, "a list of packages"):
 
428
        if util.requires_args(command, args, "a list of packages"):
473
429
            pkgs = args[1:]
474
430
            if len(pkgs) == 1 and pkgs[0] == "-":
475
431
                stripped = [x.strip() for x in sys.stdin.readlines()]
493
449
                            root=True)
494
450
 
495
451
    elif command in ["editsources", "setup"]:
496
 
        if requires_no_args(command, args):
497
 
            # if requires_package("base-config", "/usr/sbin/apt-setup"):
 
452
        if util.requires_no_args(command, args):
 
453
            # if util.requires_package("base-config", "/usr/sbin/apt-setup"):
498
454
            #    perform.execute("apt-setup", root=True)
499
455
            perform.execute("editor /etc/apt/sources.list", root=True)
500
456
 
501
457
    elif command == "extract":
502
 
        if requires_two_args(command, args,
 
458
        if util.requires_two_args(command, args,
503
459
                             "a filename and directory to extract into"):
504
460
            perform.execute("dpkg --extract " + args[1] + " " + args[2])
505
461
 
506
462
    elif command in ["filedownload", "downloadfile"]:
507
 
        if requires_one_arg(command, args,
 
463
        if util.requires_one_arg(command, args,
508
464
        "a file name containing list of packages"):
509
465
            stripped = [x.strip() for x in open(args[1]).readlines()]
510
466
            pkgs = str.join(stripped)
511
467
            perform.execute("apt-get --download-only install " + pkgs, root=True)
512
468
 
513
469
    elif command in ["fileinstall", "installfile"]:
514
 
        if requires_one_arg(command, args,
515
 
        "a file name containing list of packages"):
 
470
        if util.requires_one_arg(command, args,
 
471
        "a file name containing a list of packages"):
516
472
            stripped = [x.strip() for x in open(args[1]).readlines()]
517
473
            pkgs = str.join(stripped)
518
474
            perform.execute("apt-get install " + pkgs, root=True)
519
475
 
520
476
    elif command in ["fileremove", "removefile"]:
521
 
        if requires_one_arg(command, args,
522
 
        "a file name containing list of packages"):
 
477
        if util.requires_one_arg(command, args,
 
478
        "a file name containing a list of packages"):
523
479
            stripped = [x.strip() for x in open(args[1]).readlines()]
524
480
            pkgs = str.join(stripped)
525
481
            perform.execute("apt-get remove " + pkgs, root=True)
526
482
 
527
483
    elif command in ["findfile", "locate"]:
528
 
        if requires_one_arg(command, args, "a file name"):
 
484
        if util.requires_one_arg(command, args, "a file name"):
529
485
            perform.execute("dpkg --search " + args[1])
530
486
 
531
487
    elif command in ["findpkg", "unofficial"]:
532
 
        if requires_one_arg(command, args, "a package name"):
 
488
        if util.requires_one_arg(command, args, "one package name") \
 
489
        and util.requires_package("wget", "/usr/bin/wget"):
533
490
            commands.do_findpkg(args[1])
534
491
 
535
492
    elif command == "fixconfigure":
536
 
        if requires_no_args(command, args):
 
493
        if util.requires_no_args(command, args):
537
494
            perform.execute("dpkg --configure -a", root=True)
538
495
 
539
496
    elif command == "fixinstall":
540
 
        if requires_no_args(command, args):
 
497
        if util.requires_no_args(command, args):
541
498
            perform.execute("apt-get --fix-broken install", root=True)
542
499
 
543
500
    elif command == "fixmissing":
544
 
        if requires_no_args(command, args):
 
501
        if util.requires_no_args(command, args):
545
502
            perform.execute("apt-get --fix-missing upgrade", root=True)
546
503
 
547
504
    elif command == "force":
548
 
        if requires_args(command, args, "a package name"):
 
505
        if util.requires_args(command, args, "a package name"):
549
506
            commands.do_force(args[1:])
550
507
 
551
508
    elif command == "geturl":
552
 
        if requires_one_arg(command, args, "a package name"):
 
509
        if util.requires_one_arg(command, args, "one package name"):
553
510
            # Not yet quite working
554
511
            perform.execute("/usr/lib/apt-move/fetch -t " + args[1], root=True)
555
512
 
556
513
    elif command == "hold":
557
 
        if requires_args(command, args, "a list of packages to place on hold"):
 
514
        if util.requires_args(command, args, "a list of packages to place on hold"):
558
515
            commands.do_hold(args[1:])
559
516
            # TODO Perhaps I can use map to "execute" over each package
560
517
 
561
518
    elif command == "info":
562
 
        if requires_one_arg(command, args, "a filename"):
 
519
        if util.requires_one_arg(command, args, "one filename"):
563
520
            perform.execute("dpkg --info " + args[1])
564
521
 
565
522
    elif command == "init":
566
 
        if requires_no_args(command, args):
 
523
        if util.requires_no_args(command, args):
567
524
            changes.reset_files()
568
525
 
569
526
    elif command in ["install", "isntall"]:
570
527
        #
571
528
        # Okay, so I'm sometimes dyslexic :-)
572
529
        #
573
 
        if requires_args(command, args,
 
530
        if util.requires_args(command, args,
574
531
                         "a list of packages, .deb files, or url"):
575
532
            commands.do_install(args[1:], noauth)
576
533
 
577
534
    elif command in ["installr", "recommended"]:
578
 
        if requires_args(command, args, "a list of packages"):
 
535
        if util.requires_args(command, args, "a list of packages"):
579
536
            commands.do_install_suggest(args[1:], "Recommends")
580
537
 
581
538
    elif command == "installrs":
582
 
        if requires_args(command, args, "a list of packages"):
 
539
        if util.requires_args(command, args, "a list of packages"):
583
540
            commands.do_install_suggest(args[1:], "Both")
584
541
 
585
542
    elif command in ["installs", "suggested"]:
586
 
        if requires_args(command, args, "a list of packages"):
 
543
        if util.requires_args(command, args, "a list of packages"):
587
544
            commands.do_install_suggest(args[1:], "Suggests")
588
545
 
589
546
    elif re.compile(r'install.*').match(command):
590
547
        # For example: install/unsable
591
 
        if requires_args(command, args,
 
548
        if util.requires_args(command, args,
592
549
                         "a list of packages, .deb files, or url"):
593
550
            command = "apt-get --target-release %s install %s" % \
594
551
                  (re.compile(r'install').sub("", command),
596
553
            perform.execute(command, root=True)
597
554
 
598
555
    elif command == "integrity":
599
 
        if requires_no_args(command, args):
 
556
        if util.requires_no_args(command, args):
600
557
            perform.execute("debsums -s -a")
601
558
 
602
559
    elif command == "large":
603
560
        commands.do_size(args[1:], 10000)
604
561
 
605
562
    elif command == "lastupdate":
606
 
        if requires_no_args(command, args):
 
563
        if util.requires_no_args(command, args):
607
564
            perform.execute("/bin/ls -l --full-time " +
608
565
                            changes.available_file +
609
566
                            " 2>/dev/null |awk '{printf \"Last update was " +
611
568
                            ", $6, $7, $8}' | sed 's|\.000000000||'")
612
569
 
613
570
    elif command in ["list", "listwide"]:
614
 
        if requires_opt_arg(command, args, "string to filter on"):
 
571
        if util.requires_opt_arg(command, args, "string to filter on"):
615
572
            cmd = ""
616
573
            if command == "listwide":
617
574
                cmd += "COLUMNS=200 "
623
580
            perform.execute(cmd)
624
581
 
625
582
    elif command == "listall":
626
 
        if requires_opt_arg(command, args, "string to filter on"):
 
583
        if util.requires_opt_arg(command, args, "string to filter on"):
627
584
            cmd = "apt-cache dumpavail |" +\
628
585
                            "egrep \"^(Package|Description): \" |" +\
629
586
                            "awk '/^Package: /{pkg=$2} /^Description: /" +\
635
592
            perform.execute(cmd)
636
593
 
637
594
    elif command in ["listalts", "listalternatives"]:
638
 
        if requires_no_args(command, args):
 
595
        if util.requires_no_args(command, args):
639
596
            perform.execute("ls /etc/alternatives/ | " +\
640
597
                            "egrep -v '(\.1|\.1\.gz|\.8|\.8\.gz|README)$'")
641
598
 
642
599
    elif command == "listcache":
643
 
        if requires_opt_arg(command, args, "string to filter on"):
 
600
        if util.requires_opt_arg(command, args, "string to filter on"):
644
601
            cmd = "printf 'Found %d files %s in the cache.\n\n'\
645
602
            $(ls /var/cache/apt/archives/ | wc -l) \
646
603
            $(ls -sh /var/cache/apt/archives/ | head -1 | awk '{print $2}')"
652
609
            perform.execute(cmd)
653
610
 
654
611
    elif command in ["listcommands", "commands"]:
655
 
        if requires_no_args(command, args):
 
612
        if util.requires_no_args(command, args):
656
613
            documentation.help(verbose)
657
614
 
658
615
    elif command == "listdaemons":
659
 
        if requires_no_args(command, args):
 
616
        if util.requires_no_args(command, args):
660
617
            perform.execute("printf 'Found %d daemons in /etc/init.d.\n\n'\
661
618
            $(ls /etc/init.d/ | \
662
619
            egrep -v '(~$|README|-(old|dist)|\.[0-9]*$)' | wc -l)")
665
622
            pr --columns=3 --omit-header")
666
623
 
667
624
    elif command == "listfiles":
668
 
        if requires_one_arg(command, args,
 
625
        if util.requires_one_arg(command, args,
669
626
                            "the name of a single Debian package or deb file"):
670
627
            if re.match(".*\.deb$", args[1]):
671
628
                perform.execute("dpkg --contents " + args[1])
673
630
                perform.execute("dpkg --listfiles " + args[1])
674
631
 
675
632
    elif command == "listsection":
676
 
        if requires_one_arg(command, args, "the name of a Debian Section." +
 
633
        if util.requires_one_arg(command, args, "the name of a Debian Section." +
677
634
                            "\nUse the LIST-SECTIONS command for a list " +
678
635
                            "of Debian Sections."):
679
636
            commands.do_listsection(args[1])
680
637
 
681
638
    elif command == "listsections":
682
 
        if requires_no_args(command, args):
 
639
        if util.requires_no_args(command, args):
683
640
            commands.do_listsections()
684
641
 
685
642
    elif command == "listhold":
686
 
        if requires_no_args(command, args):
 
643
        if util.requires_no_args(command, args):
687
644
            perform.execute("dpkg --get-selections | egrep 'hold$' | cut -f1")
688
645
 
689
646
    elif command == "listinstalled":
690
 
        if requires_opt_arg(command, args, "string to filter on"):
 
647
        if util.requires_opt_arg(command, args, "string to filter on"):
691
648
            commands.do_listinstalled(args[1:])
692
649
 
693
650
    elif command == "listlog":
694
 
        if requires_opt_arg(command, args, "string to filter on"):
 
651
        if util.requires_opt_arg(command, args, "string to filter on"):
695
652
            cmd = "cat " + changes.log_file + "| sed 's|T| |'"
696
653
            if len(args) == 2:
697
654
                cmd = cmd + " | grep '" + args[1] + "'"
699
656
 
700
657
    elif command == "listnames":
701
658
        # pdb.set_trace()
702
 
        if requires_opt_arg(command, args, "at most one argument"):
 
659
        if util.requires_opt_arg(command, args, "at most one argument"):
703
660
            result = commands.do_listnames(args[1:])
704
661
 
705
662
    elif command == "listscripts":
706
 
        if requires_one_arg(command, args, "a package name or deb file"):
 
663
        if util.requires_one_arg(command, args, "a package name or deb file"):
707
664
            result = commands.do_listscripts(args[1])
708
665
 
709
666
    elif command == "liststatus":
710
 
        if requires_opt_arg(command, args, "package name"):
 
667
        if util.requires_opt_arg(command, args, "package name"):
711
668
            cmd = "COLUMNS=400 "
712
669
            cmd += "dpkg --list '*' | grep -v 'no description avail'"
713
670
            cmd += " | awk '{print $1,$2}'"
716
673
            perform.execute(cmd)
717
674
 
718
675
    elif command == "localdistupgrade":
719
 
        if requires_no_args(command, args):
 
676
        if util.requires_no_args(command, args):
720
677
            perform.execute("apt-get --no-download --ignore-missing " +
721
678
                            "--show-upgraded dist-upgrade", root=True)
722
679
 
723
680
    elif command == "localupgrade":
724
 
        if requires_no_args(command, args):
 
681
        if util.requires_no_args(command, args):
725
682
            perform.execute("apt-get --no-download --ignore-missing " \
726
683
                            + "--show-upgraded upgrade", root=True)
727
684
 
732
689
        perform.execute("apt-cache madison " + perform.concat(args[1:]))
733
690
 
734
691
    elif command == "move":
735
 
        if requires_no_args(command, args):
 
692
        if util.requires_no_args(command, args):
736
693
            perform.execute("apt-move update", root=True)
737
694
            # Then clean out the cached archive.
738
695
            perform.execute("apt-get clean", root=True)
739
696
 
740
697
    elif command == "new":
741
 
        if requires_opt_arg(command, args, "whether to INSTALL the new pkgs"):
 
698
        if util.requires_opt_arg(command, args, "whether to INSTALL the new pkgs"):
742
699
            if len(args) == 1:
743
700
                commands.do_describe_new()
744
701
            elif args[1].lower() == "install":
746
703
            else:
747
704
                print "WaJIG Error: NEW only accepts optional " +\
748
705
                      "argument INSTALL"
749
 
                finishup(1)
 
706
                util.finishup(1)
750
707
                return False
751
708
 
752
 
    elif command == "news":
753
 
        if requires_args(command, args, "a list of packages"):
754
 
            if requires_package("lynx", "/usr/bin/lynx"):
755
 
                commands.do_news(args[1:])
756
 
 
757
709
    elif command in ["newupgrades", "newupgrade"]:
758
 
        if requires_opt_arg(command, args, "whether to INSTALL upgraded pkgs"):
 
710
        if util.requires_opt_arg(command, args, "whether to INSTALL upgraded pkgs"):
759
711
            if len(args) == 1:
760
712
                commands.do_newupgrades()
761
713
            elif args[1].lower() == "install":
763
715
            else:
764
716
                print "WaJIG Error: NEWUPGRADES only accepts " +\
765
717
                      "optional argument INSTALL"
766
 
                finishup(1)
 
718
                util.finishup(1)
767
719
                return False
768
720
 
769
721
    elif command == "nonfree":
770
 
        if requires_no_args(command, args):
771
 
            if requires_package("vrms", "/usr/bin/vrms"):
 
722
        if util.requires_no_args(command, args):
 
723
            if util.requires_package("vrms", "/usr/bin/vrms"):
772
724
                perform.execute("vrms")
773
725
 
774
726
    elif command in ["orphans", "listorphans"]:
775
 
        if requires_no_args(command, args):
776
 
            if requires_package("deborphan", "/usr/bin/deborphan"):
 
727
        if util.requires_no_args(command, args):
 
728
            if util.requires_package("deborphan", "/usr/bin/deborphan"):
777
729
                perform.execute("deborphan")
778
730
 
779
731
    elif command == "policy":
780
732
        perform.execute("apt-cache policy " + perform.concat(args[1:]))
781
733
 
782
734
    elif command == "purge":
783
 
        if requires_args(command, args, "a list of packages"):
 
735
        if util.requires_args(command, args, "a list of packages"):
784
736
            perform.execute("dpkg --purge " + perform.concat(args[1:]), root=True)
785
737
 
786
738
    elif command == "purgedepend":
787
 
        if requires_one_arg(command, args, "a single package"):
788
 
            # Bug#579419 - this is more efficient than do_removedepend 
 
739
        if util.requires_one_arg(command, args, "a single package"):
789
740
            perform.execute("apt-get remove --purge --auto-remove " + args[1], root=True)
790
 
            # commands.do_removedepend(args[1], purge=True)
791
741
 
792
742
    elif command == "purgeorphans":
793
 
        #
794
743
        # Deborphans does not require root, but dpkg does.
795
744
        # So build up the orphans list first, then apss that to dpkg.
796
 
        #
797
 
        if requires_no_args(command, args):
798
 
            if requires_package("deborphan", "/usr/bin/deborphan"):
 
745
        if util.requires_no_args(command, args):
 
746
            if util.requires_package("deborphan", "/usr/bin/deborphan"):
799
747
                pkgs = ""
800
748
                for p in perform.execute("deborphan", pipe=True):
801
749
                    pkgs += " " + p.strip()
803
751
                    perform.execute("apt-get remove --purge" + pkgs, root=True)
804
752
 
805
753
    elif command == "purgeremoved":
806
 
        if requires_no_args(command, args):
 
754
        if util.requires_no_args(command, args):
807
755
            pkgs = ""
808
756
            cmd = "dpkg-query --show --showformat='${Package}\t${Status}\n' |"\
809
757
            + " grep \"deinstall ok config-files\" | cut -f 1 "
816
764
                perform.execute("dpkg --purge" + pkgs, root=True)
817
765
 
818
766
    elif command == "readme":
819
 
        if requires_one_arg(command, args, "a single package"):
 
767
        if util.requires_one_arg(command, args, "a single package"):
820
768
            docpath = "/usr/share/doc/" + args[1] + "/"
821
769
            if not os.path.exists(docpath):
822
770
                print "No docs found for '%s'. Is it installed?" % args[1]
831
779
                    cat = "zcat"
832
780
                if os.path.exists(readme):
833
781
                    found = True
834
 
                    print "="*30 + " " + r + " " + "="*30
 
782
                    print "{0:=^72}".format(" {0} ".format(r))
835
783
                    sys.stdout.flush()
836
784
                    perform.execute(cat + " " + readme)
837
785
            if not found:
838
786
                print "No README found for '%s'" % args[1]
839
787
 
840
788
    elif command in ["recursive", "recdownload"]:
841
 
        if requires_args(command, args, "a list of packages"):
 
789
        if util.requires_args(command, args, "a list of packages"):
842
790
            commands.do_recdownload(args[1:])
843
791
 
844
792
    elif command == "reconfigure":
851
799
            perform.execute("gkdebconf", root=True)
852
800
 
853
801
    elif command == "reinstall":
854
 
        if requires_args(command, args, "a list of packages"):
 
802
        if util.requires_args(command, args, "a list of packages"):
855
803
            perform.execute("apt-get --reinstall install " +\
856
804
                             perform.concat(args[1:]), root=True)
857
805
 
858
806
    elif command == "reload":
859
 
        if requires_one_arg(command, args, "name of service to " + command):
 
807
        if util.requires_one_arg(command, args, "name of service to " + command):
860
808
            perform.execute("/etc/init.d/" + args[1] + " " + command, root=True)
861
809
            # Bug#426969
862
810
            # perform.execute("invoke-rc.d " + args[1] + " " + command, root=True)
863
811
 
864
812
    elif command == "remove":
865
 
        if requires_args(command, args, "a list of packages"):
 
813
        if util.requires_args(command, args, "a list of packages"):
866
814
            perform.execute("apt-get %s remove %s" %
867
815
                            (yes, perform.concat(args[1:])), root=True)
868
816
 
869
817
    elif command == "removedepend":
870
 
        if requires_one_arg(command, args, "a single package"):
 
818
        if util.requires_one_arg(command, args, "a single package"):
871
819
            #print changes.get_dependents("libclan2-mikmod")
872
820
            # Bug#579419 - this is more efficient than do_removedepend 
873
821
            perform.execute("apt-get remove --auto-remove " + args[1], root=True)
874
822
            #commands.do_removedepend(args[1])
875
823
            
876
824
    elif command == "removeorphans":
877
 
        if requires_no_args(command, args):
878
 
            if requires_package("deborphan", "/usr/bin/deborphan"):
 
825
        if util.requires_no_args(command, args):
 
826
            if util.requires_package("deborphan", "/usr/bin/deborphan"):
879
827
                pkgs = ""
880
828
                for p in perform.execute("deborphan", pipe=True):
881
829
                    pkgs += " " + p.strip()
883
831
                    perform.execute("apt-get remove" + pkgs, root=True)
884
832
 
885
833
    elif command in ["repackage", "package"]:
886
 
        if requires_one_arg(command, args, "name of an installed package"):
887
 
            if requires_package("dpkg-repack", "/usr/bin/dpkg-repack"):
 
834
        if util.requires_one_arg(command, args, "name of an installed package"):
 
835
            if util.requires_package("dpkg-repack", "/usr/bin/dpkg-repack"):
888
836
                perform.execute("dpkg-repack " + args[1], root=True)
889
837
 
890
838
    elif command == "reset":
891
 
        if requires_no_args(command, args):
 
839
        if util.requires_no_args(command, args):
892
840
            changes.reset_files()
893
841
 
894
842
    elif command == "restart":
895
 
        if requires_one_arg(command, args, "name of service to " + command):
 
843
        if util.requires_one_arg(command, args, "name of service to " + command):
896
844
            perform.execute("/etc/init.d/" + args[1] + " " + command, root=True)
897
845
            # Bug#426969
898
846
            # perform.execute("invoke-rc.d " + args[1] + " " + command, root=True)
899
847
 
900
848
    elif command == "rpminstall":
901
 
        if requires_one_arg(command, args,
 
849
        if util.requires_one_arg(command, args,
902
850
        "a Red Hat package file name (.rpm)"):
903
851
            perform.execute("alien --to-deb --install " + args[1], root=True)
904
852
 
905
853
    elif command in ["rpmtodeb", "rpm2deb"]:
906
 
        if requires_one_arg(command, args,
 
854
        if util.requires_one_arg(command, args,
907
855
        "a Red Hat package file name (.rpm)"):
908
856
            perform.execute("alien -d " + args[1], root=True)
909
857
 
910
858
    elif command == "search":
911
859
        # Note that this uses a regular expression, thus libstdc++6
912
860
        # finds nothing but libstdc..6 does.
913
 
        if requires_args(command, args, "a list of words to search for"):
 
861
        if util.requires_args(command, args, "a list of words to search for"):
914
862
            perform.execute("apt-cache search " + perform.concat(args[1:]))
915
863
 
916
864
    elif command == "searchapt":
917
 
        if requires_one_arg(command, args, "one of stable|testing|unstable"):
918
 
            requires_package("netselect-apt", "/usr/bin/netselect-apt")
 
865
        if util.requires_one_arg(command, args, "one of stable|testing|unstable"):
 
866
            util.requires_package("netselect-apt", "/usr/bin/netselect-apt")
919
867
            perform.execute("netselect-apt " + args[1], root=True)
920
868
 
921
869
    elif command == "showdistupgrade":
922
 
        if requires_no_args(command, args):
 
870
        if util.requires_no_args(command, args):
923
871
            perform.execute("apt-get -u -s dist-upgrade", root=True)
924
872
 
925
873
    elif command == "showinstall":
926
 
        if requires_args(command, args, "a list of packages"):
 
874
        if util.requires_args(command, args, "a list of packages"):
927
875
            perform.execute("apt-get -u -s install " +
928
876
            perform.concat(args[1:]), root=True)
929
877
 
930
878
    elif command == "showremove":
931
 
        if requires_args(command, args, "a list of packages"):
 
879
        if util.requires_args(command, args, "a list of packages"):
932
880
            perform.execute("apt-get -u -s remove " + perform.concat(args[1:]),
933
881
            root=True)
934
882
 
935
883
    elif command == "showupgrade":
936
 
        if requires_no_args(command, args):
 
884
        if util.requires_no_args(command, args):
937
885
            perform.execute("apt-get -u -s upgrade", root=True)
938
886
 
939
 
    elif command == "size" or command == "sizes":
 
887
    elif command in ["size", "sizes"]:
940
888
        commands.do_size(args[1:], 0)
941
889
 
942
890
    elif command == "snapshot":
943
 
        if requires_no_args(command, args):
 
891
        if util.requires_no_args(command, args):
944
892
            commands.do_status([], True)
945
893
 
946
894
    elif command == "source":
947
 
        if requires_args(command, args, "a list of package names"):
 
895
        if util.requires_args(command, args, "a list of package names"):
948
896
            # First make sure dependencies are met
949
897
            # John V. Belmonte 04 Nov 2005 requested this not be done
950
898
            # Leave it to the user to do wajig builddepend
954
902
            perform.execute("apt-get source " + perform.concat(args[1:]))
955
903
 
956
904
    elif command == "start":
957
 
        if requires_one_arg(command, args, "name of service to " + command):
 
905
        if util.requires_one_arg(command, args, "name of service to " + command):
958
906
            perform.execute("/etc/init.d/" + args[1] + " " + command, root=True)
959
907
            # Bug#426969
960
908
            # perform.execute("invoke-rc.d " + args[1] + " " + command, root=True)
962
910
    elif command == "status":
963
911
        commands.do_status(args[1:])
964
912
 
965
 
    elif command == "statusmatch" or command == "statussearch":
966
 
        if requires_one_arg(command, args,
 
913
    elif command in ["statusmatch", "statussearch"]:
 
914
        if util.requires_one_arg(command, args,
967
915
        "a search string for the package name"):
968
916
            pkgs = map(lambda s: s.strip(),
969
917
                   commands.do_listnames(args[1:], pipe=True).readlines())
982
930
        #                    + " | xargs wajig status ")
983
931
 
984
932
    elif command == "stop":
985
 
        if requires_one_arg(command, args, "name of service to " + command):
 
933
        if util.requires_one_arg(command, args, "name of service to " + command):
986
934
            perform.execute("/etc/init.d/" + args[1] + " " + command, root=True)
987
935
            # Bug#426969
988
936
            # perform.execute("invoke-rc.d " + args[1] + " " + command, root=True)
989
937
 
990
938
    elif command == "tasksel":
991
 
        if requires_no_args(command, args):
992
 
            if requires_package("tasksel", "/usr/bin/tasksel"):
 
939
        if util.requires_no_args(command, args):
 
940
            if util.requires_package("tasksel", "/usr/bin/tasksel"):
993
941
                perform.execute("tasksel", root=True)
994
942
 
995
943
    elif command == "toupgrade":
996
 
        if requires_no_args(command, args):
 
944
        if util.requires_no_args(command, args):
997
945
            commands.do_toupgrade()
998
946
 
999
947
    # edd 03 Sep 2003  unhold patch based on hold semantics
1000
948
    elif command == "unhold":
1001
 
        if requires_args(command, args,
 
949
        if util.requires_args(command, args,
1002
950
        "a list of packages to remove from hold"):
1003
951
            commands.do_unhold(args[1:])
1004
952
        # TODO Perhaps I can use map to "execute" over each package
1005
953
 
1006
954
    elif command == "update":
1007
 
        if requires_no_args(command, args):
 
955
        if util.requires_no_args(command, args):
1008
956
            commands.do_update()
1009
957
 
1010
958
    # For testing only!
1011
959
    elif command == "updateavailable":
1012
 
        if requires_no_args(command, args):
 
960
        if util.requires_no_args(command, args):
1013
961
            changes.update_available()
1014
962
 
1015
963
    elif command in ["updatealts", "updatealternatives", "setalts",
1016
964
        "setalternatives"]:
1017
 
        if requires_one_arg(command, args, "name of alternative to update"):
 
965
        if util.requires_one_arg(command, args, "name of alternative to update"):
1018
966
            perform.execute("update-alternatives --config " + args[1], root=True)
1019
967
 
1020
968
    elif command == "updatepciids":
1021
 
        if requires_package("pciutils", "/usr/bin/update-pciids"):
1022
 
            if requires_no_args(command, args):
 
969
        if util.requires_package("pciutils", "/usr/bin/update-pciids"):
 
970
            if util.requires_no_args(command, args):
1023
971
                perform.execute("update-pciids", root=True)
1024
972
 
1025
973
    elif command == "updateusbids":
1026
 
        if requires_package("usbutils", "/usr/sbin/update-usbids"):
1027
 
            if requires_no_args(command, args):
 
974
        if util.requires_package("usbutils", "/usr/sbin/update-usbids"):
 
975
            if util.requires_no_args(command, args):
1028
976
                perform.execute("update-usbids", root=True)
1029
977
 
1030
978
    elif command == "upgrade":
1031
 
        if backup and requires_package("dpkg-repack", "/usr/bin/dpkg-repack")\
1032
 
        and requires_package("fakeroot", "/usr/bin/fakeroot"):
1033
 
            if len(args) > 1:
1034
 
                bkdir = args[1]
1035
 
            else:
1036
 
                bkdir = None
1037
 
            changes.backup_before_upgrade(bkdir)
1038
 
        elif len(args) > 1:
1039
 
            perform.execute("apt-get install " + perform.concat(args[1:]),
1040
 
                                root=True)
1041
 
        else:
 
979
        upgradable = str()
 
980
        if backup \
 
981
        and util.requires_package("dpkg-repack", "/usr/bin/dpkg-repack") \
 
982
        and util.requires_package("fakeroot", "/usr/bin/fakeroot") \
 
983
        and util.requires_no_args(command, args):
 
984
            upgradable = changes.backup_before_upgrade(backup)
 
985
        if upgradable != "quit":
1042
986
            perform.execute("apt-get %s -u upgrade" % noauth, root=True)
1043
987
 
1044
988
    elif command == "upgradesecurity":
1058
1002
            os.remove(sources_list)
1059
1003
 
1060
1004
    elif command == "verify":
1061
 
        if requires_one_arg(command, args, "a package name"):
 
1005
        if util.requires_one_arg(command, args, "a package name"):
1062
1006
            perform.execute("debsums " + args[1])
1063
1007
 
1064
 
    elif command == "version" or command == "versions":
 
1008
    elif command in ["version", "versions"]:
1065
1009
        if command == "version" and len(args) == 1:
1066
1010
            documentation.version()
1067
 
        else:
1068
 
            if requires_package("apt-show-versions",
1069
 
                                "/usr/bin/apt-show-versions"):
1070
 
                commands.versions(args[1:])
 
1011
        elif util.requires_package("apt-show-versions",
 
1012
                              "/usr/bin/apt-show-versions"):
 
1013
            commands.versions(args[1:])
 
1014
 
1071
1015
    elif command == "whatis":
1072
 
        if requires_args(command, args, "a list of package names"):
 
1016
        if util.requires_args(command, args, "a list of package names"):
1073
1017
            commands.do_describe(args[1:])
1074
1018
 
1075
1019
    elif command in ["whichpkg", "whichpackage"]:
1076
 
        if requires_one_arg(command, args,
1077
 
        "a file name (possibly with a path)"):
 
1020
        if util.requires_one_arg(command, args, "a filename (possibly with a path)") \
 
1021
        and util.requires_package("wget", "/usr/bin/wget") \
 
1022
        and util.requires_package("lynx", "/usr/bin/lynx"):
1078
1023
            commands.do_whichpkg(args[1])
1079
1024
 
1080
1025
    else:
1083
1028
        else:
1084
1029
            print "The command `" + command + "' (entered as `" + args[0] + \
1085
1030
                  "') was not recognised."
1086
 
        print "Perhaps it is not yet implemented or you misspelt the command."
1087
 
        print "Try `wajig help' for further information."
1088
 
        print "Try `wajig list-commands' for list of all commands."
 
1031
        print "Perhaps it is not yet implemented or you misspelt it."
 
1032
        print "Try 'wajig help' for further information."
1089
1033
 
1090
1034
    changes.finish_log()
1091
1035