This file is indexed.

/usr/share/pyshared/CedarBackup2/cli.py is in cedar-backup2 2.26.5-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
# -*- coding: iso-8859-1 -*-
# vim: set ft=python ts=3 sw=3 expandtab:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#              C E D A R
#          S O L U T I O N S       "Software done right."
#           S O F T W A R E
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Copyright (c) 2004-2007,2010,2015 Kenneth J. Pronovici.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# Version 2, as published by the Free Software Foundation.
#
# This program 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.
#
# Copies of the GNU General Public License are available from
# the Free Software Foundation website, http://www.gnu.org/.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Author   : Kenneth J. Pronovici <pronovic@ieee.org>
# Language : Python 2 (>= 2.7)
# Project  : Cedar Backup, release 2
# Purpose  : Provides command-line interface implementation.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

########################################################################
# Module documentation
########################################################################

"""
Provides command-line interface implementation for the cback script.

Summary
=======

   The functionality in this module encapsulates the command-line interface for
   the cback script.  The cback script itself is very short, basically just an
   invokation of one function implemented here.  That, in turn, makes it
   simpler to validate the command line interface (for instance, it's easier to
   run pychecker against a module, and unit tests are easier, too).

   The objects and functions implemented in this module are probably not useful
   to any code external to Cedar Backup.   Anyone else implementing their own
   command-line interface would have to reimplement (or at least enhance) all
   of this anyway.

Backwards Compatibility
=======================

   The command line interface has changed between Cedar Backup 1.x and Cedar
   Backup 2.x.  Some new switches have been added, and the actions have become
   simple arguments rather than switches (which is a much more standard command
   line format).  Old 1.x command lines are generally no longer valid.

@var DEFAULT_CONFIG: The default configuration file.
@var DEFAULT_LOGFILE: The default log file path.
@var DEFAULT_OWNERSHIP: Default ownership for the logfile.
@var DEFAULT_MODE: Default file permissions mode on the logfile.
@var VALID_ACTIONS: List of valid actions.
@var COMBINE_ACTIONS: List of actions which can be combined with other actions.
@var NONCOMBINE_ACTIONS: List of actions which cannot be combined with other actions.

@sort: cli, Options, DEFAULT_CONFIG, DEFAULT_LOGFILE, DEFAULT_OWNERSHIP,
       DEFAULT_MODE, VALID_ACTIONS, COMBINE_ACTIONS, NONCOMBINE_ACTIONS

@author: Kenneth J. Pronovici <pronovic@ieee.org>
"""

########################################################################
# Imported modules
########################################################################

# System modules
import sys
import os
import logging
import getopt

# Cedar Backup modules
from CedarBackup2.release import AUTHOR, EMAIL, VERSION, DATE, COPYRIGHT
from CedarBackup2.customize import customizeOverrides
from CedarBackup2.util import DirectedGraph, PathResolverSingleton
from CedarBackup2.util import sortDict, splitCommandLine, executeCommand, getFunctionReference
from CedarBackup2.util import getUidGid, encodePath, Diagnostics
from CedarBackup2.config import Config
from CedarBackup2.peer import RemotePeer
from CedarBackup2.actions.collect import executeCollect
from CedarBackup2.actions.stage import executeStage
from CedarBackup2.actions.store import executeStore
from CedarBackup2.actions.purge import executePurge
from CedarBackup2.actions.rebuild import executeRebuild
from CedarBackup2.actions.validate import executeValidate
from CedarBackup2.actions.initialize import executeInitialize


########################################################################
# Module-wide constants and variables
########################################################################

logger = logging.getLogger("CedarBackup2.log.cli")

DISK_LOG_FORMAT    = "%(asctime)s --> [%(levelname)-7s] %(message)s"
DISK_OUTPUT_FORMAT = "%(message)s"
SCREEN_LOG_FORMAT  = "%(message)s"
SCREEN_LOG_STREAM  = sys.stdout
DATE_FORMAT        = "%Y-%m-%dT%H:%M:%S %Z"

DEFAULT_CONFIG     = "/etc/cback.conf"
DEFAULT_LOGFILE    = "/var/log/cback.log"
DEFAULT_OWNERSHIP  = [ "root", "adm", ]
DEFAULT_MODE       = 0640

REBUILD_INDEX      = 0        # can't run with anything else, anyway
VALIDATE_INDEX     = 0        # can't run with anything else, anyway
INITIALIZE_INDEX   = 0        # can't run with anything else, anyway
COLLECT_INDEX      = 100
STAGE_INDEX        = 200
STORE_INDEX        = 300
PURGE_INDEX        = 400

VALID_ACTIONS      = [ "collect", "stage", "store", "purge", "rebuild", "validate", "initialize", "all", ]
COMBINE_ACTIONS    = [ "collect", "stage", "store", "purge", ]
NONCOMBINE_ACTIONS = [ "rebuild", "validate", "initialize", "all", ]

SHORT_SWITCHES     = "hVbqc:fMNl:o:m:OdsD"
LONG_SWITCHES      = [ 'help', 'version', 'verbose', 'quiet',
                       'config=', 'full', 'managed', 'managed-only',
                       'logfile=', 'owner=', 'mode=',
                       'output', 'debug', 'stack', 'diagnostics', ]


#######################################################################
# Public functions
#######################################################################

#################
# cli() function
#################

def cli():
   """
   Implements the command-line interface for the C{cback} script.

   Essentially, this is the "main routine" for the cback script.  It does all
   of the argument processing for the script, and then sets about executing the
   indicated actions.

   As a general rule, only the actions indicated on the command line will be
   executed.   We will accept any of the built-in actions and any of the
   configured extended actions (which makes action list verification a two-
   step process).

   The C{'all'} action has a special meaning: it means that the built-in set of
   actions (collect, stage, store, purge) will all be executed, in that order.
   Extended actions will be ignored as part of the C{'all'} action.

   Raised exceptions always result in an immediate return.  Otherwise, we
   generally return when all specified actions have been completed.  Actions
   are ignored if the help, version or validate flags are set.

   A different error code is returned for each type of failure:

      - C{1}: The Python interpreter version is < 2.7
      - C{2}: Error processing command-line arguments
      - C{3}: Error configuring logging
      - C{4}: Error parsing indicated configuration file
      - C{5}: Backup was interrupted with a CTRL-C or similar
      - C{6}: Error executing specified backup actions

   @note: This function contains a good amount of logging at the INFO level,
   because this is the right place to document high-level flow of control (i.e.
   what the command-line options were, what config file was being used, etc.)

   @note: We assume that anything that I{must} be seen on the screen is logged
   at the ERROR level.  Errors that occur before logging can be configured are
   written to C{sys.stderr}.

   @return: Error code as described above.
   """
   try:
      if map(int, [sys.version_info[0], sys.version_info[1]]) < [2, 7]:
         sys.stderr.write("Python 2 version 2.7 or greater required.\n")
         return 1
   except:
      # sys.version_info isn't available before 2.0
      sys.stderr.write("Python 2 version 2.7 or greater required.\n")
      return 1

   try:
      options = Options(argumentList=sys.argv[1:])
      logger.info("Specified command-line actions: %s", options.actions)
   except Exception, e:
      _usage()
      sys.stderr.write(" *** Error: %s\n" % e)
      return 2

   if options.help:
      _usage()
      return 0
   if options.version:
      _version()
      return 0
   if options.diagnostics:
      _diagnostics()
      return 0

   if options.stacktrace:
      logfile = setupLogging(options)
   else:
      try:
         logfile = setupLogging(options)
      except Exception as e:
         sys.stderr.write("Error setting up logging: %s\n" % e)
         return 3

   logger.info("Cedar Backup run started.")
   logger.info("Options were [%s]", options)
   logger.info("Logfile is [%s]", logfile)
   Diagnostics().logDiagnostics(method=logger.info)

   if options.config is None:
      logger.debug("Using default configuration file.")
      configPath = DEFAULT_CONFIG
   else:
      logger.debug("Using user-supplied configuration file.")
      configPath = options.config

   executeLocal = True
   executeManaged = False
   if options.managedOnly:
      executeLocal = False
      executeManaged = True
   if options.managed:
      executeManaged = True
   logger.debug("Execute local actions: %s", executeLocal)
   logger.debug("Execute managed actions: %s", executeManaged)

   try:
      logger.info("Configuration path is [%s]", configPath)
      config = Config(xmlPath=configPath)
      customizeOverrides(config)
      setupPathResolver(config)
      actionSet = _ActionSet(options.actions, config.extensions, config.options,
                             config.peers, executeManaged, executeLocal)
   except Exception, e:
      logger.error("Error reading or handling configuration: %s", e)
      logger.info("Cedar Backup run completed with status 4.")
      return 4

   if options.stacktrace:
      actionSet.executeActions(configPath, options, config)
   else:
      try:
         actionSet.executeActions(configPath, options, config)
      except KeyboardInterrupt:
         logger.error("Backup interrupted.")
         logger.info("Cedar Backup run completed with status 5.")
         return 5
      except Exception, e:
         logger.error("Error executing backup: %s", e)
         logger.info("Cedar Backup run completed with status 6.")
         return 6

   logger.info("Cedar Backup run completed with status 0.")
   return 0


########################################################################
# Action-related class definition
########################################################################

####################
# _ActionItem class
####################

class _ActionItem(object):

   """
   Class representing a single action to be executed.

   This class represents a single named action to be executed, and understands
   how to execute that action.

   The built-in actions will use only the options and config values.  We also
   pass in the config path so that extension modules can re-parse configuration
   if they want to, to add in extra information.

   This class is also where pre-action and post-action hooks are executed.  An
   action item is instantiated in terms of optional pre- and post-action hook
   objects (config.ActionHook), which are then executed at the appropriate time
   (if set).

   @note: The comparison operators for this class have been implemented to only
   compare based on the index and SORT_ORDER value, and ignore all other
   values.  This is so that the action set list can be easily sorted first by
   type (_ActionItem before _ManagedActionItem) and then by index within type.

   @cvar SORT_ORDER: Defines a sort order to order properly between types.
   """

   SORT_ORDER = 0

   def __init__(self, index, name, preHooks, postHooks, function):
      """
      Default constructor.

      It's OK to pass C{None} for C{index}, C{preHooks} or C{postHooks}, but not
      for C{name}.

      @param index: Index of the item (or C{None}).
      @param name: Name of the action that is being executed.
      @param preHooks: List of pre-action hooks in terms of an C{ActionHook} object, or C{None}.
      @param postHooks: List of post-action hooks in terms of an C{ActionHook} object, or C{None}.
      @param function: Reference to function associated with item.
      """
      self.index = index
      self.name = name
      self.preHooks = preHooks
      self.postHooks = postHooks
      self.function = function

   def __cmp__(self, other):
      """
      Definition of equals operator for this class.
      The only thing we compare is the item's index.
      @param other: Other object to compare to.
      @return: -1/0/1 depending on whether self is C{<}, C{=} or C{>} other.
      """
      if other is None:
         return 1
      if self.index != other.index:
         if self.index < other.index:
            return -1
         else:
            return 1
      else:
         if self.SORT_ORDER != other.SORT_ORDER:
            if self.SORT_ORDER < other.SORT_ORDER:
               return -1
            else:
               return 1
      return 0

   def executeAction(self, configPath, options, config):
      """
      Executes the action associated with an item, including hooks.

      See class notes for more details on how the action is executed.

      @param configPath: Path to configuration file on disk.
      @param options: Command-line options to be passed to action.
      @param config: Parsed configuration to be passed to action.

      @raise Exception: If there is a problem executing the action.
      """
      logger.debug("Executing [%s] action.", self.name)
      if self.preHooks is not None:
         for hook in self.preHooks:
            self._executeHook("pre-action", hook)
      self._executeAction(configPath, options, config)
      if self.postHooks is not None:
         for hook in self.postHooks:
            self._executeHook("post-action", hook)

   def _executeAction(self, configPath, options, config):
      """
      Executes the action, specifically the function associated with the action.
      @param configPath: Path to configuration file on disk.
      @param options: Command-line options to be passed to action.
      @param config: Parsed configuration to be passed to action.
      """
      name = "%s.%s" % (self.function.__module__, self.function.__name__)
      logger.debug("Calling action function [%s], execution index [%d]", name, self.index)
      self.function(configPath, options, config)

   def _executeHook(self, type, hook):  # pylint: disable=W0622,R0201
      """
      Executes a hook command via L{util.executeCommand()}.
      @param type: String describing the type of hook, for logging.
      @param hook: Hook, in terms of a C{ActionHook} object.
      """
      fields = splitCommandLine(hook.command)
      logger.debug("Executing %s hook for action [%s]: %s", type, hook.action, fields[0:1])
      result = executeCommand(command=fields[0:1], args=fields[1:])[0]
      if result != 0:
         raise IOError("Error (%d) executing %s hook for action [%s]: %s" % (result, type, hook.action, fields[0:1]))


###########################
# _ManagedActionItem class
###########################

class _ManagedActionItem(object):

   """
   Class representing a single action to be executed on a managed peer.

   This class represents a single named action to be executed, and understands
   how to execute that action.

   Actions to be executed on a managed peer rely on peer configuration and
   on the full-backup flag.  All other configuration takes place on the remote
   peer itself.

   @note: The comparison operators for this class have been implemented to only
   compare based on the index and SORT_ORDER value, and ignore all other
   values.  This is so that the action set list can be easily sorted first by
   type (_ActionItem before _ManagedActionItem) and then by index within type.

   @cvar SORT_ORDER: Defines a sort order to order properly between types.
   """

   SORT_ORDER = 1

   def __init__(self, index, name, remotePeers):
      """
      Default constructor.

      @param index: Index of the item (or C{None}).
      @param name: Name of the action that is being executed.
      @param remotePeers: List of remote peers on which to execute the action.
      """
      self.index = index
      self.name = name
      self.remotePeers = remotePeers

   def __cmp__(self, other):
      """
      Definition of equals operator for this class.
      The only thing we compare is the item's index.
      @param other: Other object to compare to.
      @return: -1/0/1 depending on whether self is C{<}, C{=} or C{>} other.
      """
      if other is None:
         return 1
      if self.index != other.index:
         if self.index < other.index:
            return -1
         else:
            return 1
      else:
         if self.SORT_ORDER != other.SORT_ORDER:
            if self.SORT_ORDER < other.SORT_ORDER:
               return -1
            else:
               return 1
      return 0

   def executeAction(self, configPath, options, config):
      """
      Executes the managed action associated with an item.

      @note: Only options.full is actually used.  The rest of the arguments
      exist to satisfy the ActionItem iterface.

      @note: Errors here result in a message logged to ERROR, but no thrown
      exception.  The analogy is the stage action where a problem with one host
      should not kill the entire backup.  Since we're logging an error, the
      administrator will get an email.

      @param configPath: Path to configuration file on disk.
      @param options: Command-line options to be passed to action.
      @param config: Parsed configuration to be passed to action.

      @raise Exception: If there is a problem executing the action.
      """
      for peer in self.remotePeers:
         logger.debug("Executing managed action [%s] on peer [%s].", self.name, peer.name)
         try:
            peer.executeManagedAction(self.name, options.full)
         except IOError, e:
            logger.error(e)   # log the message and go on, so we don't kill the backup


###################
# _ActionSet class
###################

class _ActionSet(object):

   """
   Class representing a set of local actions to be executed.

   This class does four different things.  First, it ensures that the actions
   specified on the command-line are sensible.  The command-line can only list
   either built-in actions or extended actions specified in configuration.
   Also, certain actions (in L{NONCOMBINE_ACTIONS}) cannot be combined with
   other actions.

   Second, the class enforces an execution order on the specified actions.  Any
   time actions are combined on the command line (either built-in actions or
   extended actions), we must make sure they get executed in a sensible order.

   Third, the class ensures that any pre-action or post-action hooks are
   scheduled and executed appropriately.  Hooks are configured by building a
   dictionary mapping between hook action name and command.  Pre-action hooks
   are executed immediately before their associated action, and post-action
   hooks are executed immediately after their associated action.

   Finally, the class properly interleaves local and managed actions so that
   the same action gets executed first locally and then on managed peers.

   @sort: __init__, executeActions
   """

   def __init__(self, actions, extensions, options, peers, managed, local):
      """
      Constructor for the C{_ActionSet} class.

      This is kind of ugly, because the constructor has to set up a lot of data
      before being able to do anything useful.  The following data structures
      are initialized based on the input:

         - C{extensionNames}: List of extensions available in configuration
         - C{preHookMap}: Mapping from action name to list of C{PreActionHook}
         - C{postHookMap}: Mapping from action name to list of C{PostActionHook}
         - C{functionMap}: Mapping from action name to Python function
         - C{indexMap}: Mapping from action name to execution index
         - C{peerMap}: Mapping from action name to set of C{RemotePeer}
         - C{actionMap}: Mapping from action name to C{_ActionItem}

      Once these data structures are set up, the command line is validated to
      make sure only valid actions have been requested, and in a sensible
      combination.  Then, all of the data is used to build C{self.actionSet},
      the set action items to be executed by C{executeActions()}.  This list
      might contain either C{_ActionItem} or C{_ManagedActionItem}.

      @param actions: Names of actions specified on the command-line.
      @param extensions: Extended action configuration (i.e. config.extensions)
      @param options: Options configuration (i.e. config.options)
      @param peers: Peers configuration (i.e. config.peers)
      @param managed: Whether to include managed actions in the set
      @param local: Whether to include local actions in the set

      @raise ValueError: If one of the specified actions is invalid.
      """
      extensionNames = _ActionSet._deriveExtensionNames(extensions)
      (preHookMap, postHookMap) = _ActionSet._buildHookMaps(options.hooks)
      functionMap = _ActionSet._buildFunctionMap(extensions)
      indexMap = _ActionSet._buildIndexMap(extensions)
      peerMap = _ActionSet._buildPeerMap(options, peers)
      actionMap = _ActionSet._buildActionMap(managed, local, extensionNames, functionMap,
                                             indexMap, preHookMap, postHookMap, peerMap)
      _ActionSet._validateActions(actions, extensionNames)
      self.actionSet = _ActionSet._buildActionSet(actions, actionMap)

   @staticmethod
   def _deriveExtensionNames(extensions):
      """
      Builds a list of extended actions that are available in configuration.
      @param extensions: Extended action configuration (i.e. config.extensions)
      @return: List of extended action names.
      """
      extensionNames = []
      if extensions is not None and extensions.actions is not None:
         for action in extensions.actions:
            extensionNames.append(action.name)
      return extensionNames

   @staticmethod
   def _buildHookMaps(hooks):
      """
      Build two mappings from action name to configured C{ActionHook}.
      @param hooks: List of pre- and post-action hooks (i.e. config.options.hooks)
      @return: Tuple of (pre hook dictionary, post hook dictionary).
      """
      preHookMap = {}
      postHookMap = {}
      if hooks is not None:
         for hook in hooks:
            if hook.before:
               if not hook.action in preHookMap:
                  preHookMap[hook.action] = []
               preHookMap[hook.action].append(hook)
            elif hook.after:
               if not hook.action in postHookMap:
                  postHookMap[hook.action] = []
               postHookMap[hook.action].append(hook)
      return (preHookMap, postHookMap)

   @staticmethod
   def _buildFunctionMap(extensions):
      """
      Builds a mapping from named action to action function.
      @param extensions: Extended action configuration (i.e. config.extensions)
      @return: Dictionary mapping action to function.
      """
      functionMap = {}
      functionMap['rebuild'] = executeRebuild
      functionMap['validate'] = executeValidate
      functionMap['initialize'] = executeInitialize
      functionMap['collect'] = executeCollect
      functionMap['stage'] = executeStage
      functionMap['store'] = executeStore
      functionMap['purge'] = executePurge
      if extensions is not None and extensions.actions is not None:
         for action in extensions.actions:
            functionMap[action.name] = getFunctionReference(action.module, action.function)
      return functionMap

   @staticmethod
   def _buildIndexMap(extensions):
      """
      Builds a mapping from action name to proper execution index.

      If extensions configuration is C{None}, or there are no configured
      extended actions, the ordering dictionary will only include the built-in
      actions and their standard indices.

      Otherwise, if the extensions order mode is C{None} or C{"index"}, actions
      will scheduled by explicit index; and if the extensions order mode is
      C{"dependency"}, actions will be scheduled using a dependency graph.

      @param extensions: Extended action configuration (i.e. config.extensions)

      @return: Dictionary mapping action name to integer execution index.
      """
      indexMap = {}
      if extensions is None or extensions.actions is None or extensions.actions == []:
         logger.info("Action ordering will use 'index' order mode.")
         indexMap['rebuild'] = REBUILD_INDEX
         indexMap['validate'] = VALIDATE_INDEX
         indexMap['initialize'] = INITIALIZE_INDEX
         indexMap['collect'] = COLLECT_INDEX
         indexMap['stage'] = STAGE_INDEX
         indexMap['store'] = STORE_INDEX
         indexMap['purge'] = PURGE_INDEX
         logger.debug("Completed filling in action indices for built-in actions.")
         logger.info("Action order will be: %s", sortDict(indexMap))
      else:
         if extensions.orderMode is None or extensions.orderMode == "index":
            logger.info("Action ordering will use 'index' order mode.")
            indexMap['rebuild'] = REBUILD_INDEX
            indexMap['validate'] = VALIDATE_INDEX
            indexMap['initialize'] = INITIALIZE_INDEX
            indexMap['collect'] = COLLECT_INDEX
            indexMap['stage'] = STAGE_INDEX
            indexMap['store'] = STORE_INDEX
            indexMap['purge'] = PURGE_INDEX
            logger.debug("Completed filling in action indices for built-in actions.")
            for action in extensions.actions:
               indexMap[action.name] = action.index
            logger.debug("Completed filling in action indices for extended actions.")
            logger.info("Action order will be: %s", sortDict(indexMap))
         else:
            logger.info("Action ordering will use 'dependency' order mode.")
            graph = DirectedGraph("dependencies")
            graph.createVertex("rebuild")
            graph.createVertex("validate")
            graph.createVertex("initialize")
            graph.createVertex("collect")
            graph.createVertex("stage")
            graph.createVertex("store")
            graph.createVertex("purge")
            for action in extensions.actions:
               graph.createVertex(action.name)
            graph.createEdge("collect", "stage")   # Collect must run before stage, store or purge
            graph.createEdge("collect", "store")
            graph.createEdge("collect", "purge")
            graph.createEdge("stage", "store")     # Stage must run before store or purge
            graph.createEdge("stage", "purge")
            graph.createEdge("store", "purge")     # Store must run before purge
            for action in extensions.actions:
               if action.dependencies.beforeList is not None:
                  for vertex in action.dependencies.beforeList:
                     try:
                        graph.createEdge(action.name, vertex)   # actions that this action must be run before
                     except ValueError:
                        logger.error("Dependency [%s] on extension [%s] is unknown.", vertex, action.name)
                        raise ValueError("Unable to determine proper action order due to invalid dependency.")
               if action.dependencies.afterList is not None:
                  for vertex in action.dependencies.afterList:
                     try:
                        graph.createEdge(vertex, action.name)   # actions that this action must be run after
                     except ValueError:
                        logger.error("Dependency [%s] on extension [%s] is unknown.", vertex, action.name)
                        raise ValueError("Unable to determine proper action order due to invalid dependency.")
            try:
               ordering = graph.topologicalSort()
               indexMap = dict([(ordering[i], i+1) for i in range(0, len(ordering))])
               logger.info("Action order will be: %s", ordering)
            except ValueError:
               logger.error("Unable to determine proper action order due to dependency recursion.")
               logger.error("Extensions configuration is invalid (check for loops).")
               raise ValueError("Unable to determine proper action order due to dependency recursion.")
      return indexMap

   @staticmethod
   def _buildActionMap(managed, local, extensionNames, functionMap, indexMap, preHookMap, postHookMap, peerMap):
      """
      Builds a mapping from action name to list of action items.

      We build either C{_ActionItem} or C{_ManagedActionItem} objects here.

      In most cases, the mapping from action name to C{_ActionItem} is 1:1.
      The exception is the "all" action, which is a special case.  However, a
      list is returned in all cases, just for consistency later.  Each
      C{_ActionItem} will be created with a proper function reference and index
      value for execution ordering.

      The mapping from action name to C{_ManagedActionItem} is always 1:1.
      Each managed action item contains a list of peers which the action should
      be executed.

      @param managed: Whether to include managed actions in the set
      @param local: Whether to include local actions in the set
      @param extensionNames: List of valid extended action names
      @param functionMap: Dictionary mapping action name to Python function
      @param indexMap: Dictionary mapping action name to integer execution index
      @param preHookMap: Dictionary mapping action name to pre hooks (if any) for the action
      @param postHookMap: Dictionary mapping action name to post hooks (if any) for the action
      @param peerMap: Dictionary mapping action name to list of remote peers on which to execute the action

      @return: Dictionary mapping action name to list of C{_ActionItem} objects.
      """
      actionMap = {}
      for name in extensionNames + VALID_ACTIONS:
         if name != 'all': # do this one later
            function = functionMap[name]
            index = indexMap[name]
            actionMap[name] = []
            if local:
               (preHooks, postHooks) = _ActionSet._deriveHooks(name, preHookMap, postHookMap)
               actionMap[name].append(_ActionItem(index, name, preHooks, postHooks, function))
            if managed:
               if name in peerMap:
                  actionMap[name].append(_ManagedActionItem(index, name, peerMap[name]))
      actionMap['all'] = actionMap['collect'] + actionMap['stage'] + actionMap['store'] + actionMap['purge']
      return actionMap

   @staticmethod
   def _buildPeerMap(options, peers):
      """
      Build a mapping from action name to list of remote peers.

      There will be one entry in the mapping for each managed action.  If there
      are no managed peers, the mapping will be empty.  Only managed actions
      will be listed in the mapping.

      @param options: Option configuration (i.e. config.options)
      @param peers: Peers configuration (i.e. config.peers)
      """
      peerMap = {}
      if peers is not None:
         if peers.remotePeers is not None:
            for peer in peers.remotePeers:
               if peer.managed:
                  remoteUser = _ActionSet._getRemoteUser(options, peer)
                  rshCommand = _ActionSet._getRshCommand(options, peer)
                  cbackCommand = _ActionSet._getCbackCommand(options, peer)
                  managedActions = _ActionSet._getManagedActions(options, peer)
                  remotePeer = RemotePeer(peer.name, None, options.workingDir, remoteUser, None,
                                          options.backupUser, rshCommand, cbackCommand)
                  if managedActions is not None:
                     for managedAction in managedActions:
                        if managedAction in peerMap:
                           if remotePeer not in peerMap[managedAction]:
                              peerMap[managedAction].append(remotePeer)
                        else:
                           peerMap[managedAction] = [ remotePeer, ]
      return peerMap

   @staticmethod
   def _deriveHooks(action, preHookDict, postHookDict):
      """
      Derive pre- and post-action hooks, if any, associated with named action.
      @param action: Name of action to look up
      @param preHookDict: Dictionary mapping pre-action hooks to action name
      @param postHookDict: Dictionary mapping post-action hooks to action name
      @return Tuple (preHooks, postHooks) per mapping, with None values if there is no hook.
      """
      preHooks = None
      postHooks = None
      if preHookDict.has_key(action):
         preHooks = preHookDict[action]
      if postHookDict.has_key(action):
         postHooks = postHookDict[action]
      return (preHooks, postHooks)

   @staticmethod
   def _validateActions(actions, extensionNames):
      """
      Validate that the set of specified actions is sensible.

      Any specified action must either be a built-in action or must be among
      the extended actions defined in configuration.  The actions from within
      L{NONCOMBINE_ACTIONS} may not be combined with other actions.

      @param actions: Names of actions specified on the command-line.
      @param extensionNames: Names of extensions specified in configuration.

      @raise ValueError: If one or more configured actions are not valid.
      """
      if actions is None or actions == []:
         raise ValueError("No actions specified.")
      for action in actions:
         if action not in VALID_ACTIONS and action not in extensionNames:
            raise ValueError("Action [%s] is not a valid action or extended action." % action)
      for action in NONCOMBINE_ACTIONS:
         if action in actions and actions != [ action, ]:
            raise ValueError("Action [%s] may not be combined with other actions." % action)

   @staticmethod
   def _buildActionSet(actions, actionMap):
      """
      Build set of actions to be executed.

      The set of actions is built in the proper order, so C{executeActions} can
      spin through the set without thinking about it.  Since we've already validated
      that the set of actions is sensible, we don't take any precautions here to
      make sure things are combined properly.  If the action is listed, it will
      be "scheduled" for execution.

      @param actions: Names of actions specified on the command-line.
      @param actionMap: Dictionary mapping action name to C{_ActionItem} object.

      @return: Set of action items in proper order.
      """
      actionSet = []
      for action in actions:
         actionSet.extend(actionMap[action])
      actionSet.sort()  # sort the actions in order by index
      return actionSet

   def executeActions(self, configPath, options, config):
      """
      Executes all actions and extended actions, in the proper order.

      Each action (whether built-in or extension) is executed in an identical
      manner.  The built-in actions will use only the options and config
      values.  We also pass in the config path so that extension modules can
      re-parse configuration if they want to, to add in extra information.

      @param configPath: Path to configuration file on disk.
      @param options: Command-line options to be passed to action functions.
      @param config: Parsed configuration to be passed to action functions.

      @raise Exception: If there is a problem executing the actions.
      """
      logger.debug("Executing local actions.")
      for actionItem in self.actionSet:
         actionItem.executeAction(configPath, options, config)

   @staticmethod
   def _getRemoteUser(options, remotePeer):
      """
      Gets the remote user associated with a remote peer.
      Use peer's if possible, otherwise take from options section.
      @param options: OptionsConfig object, as from config.options
      @param remotePeer: Configuration-style remote peer object.
      @return: Name of remote user associated with remote peer.
      """
      if remotePeer.remoteUser is None:
         return options.backupUser
      return remotePeer.remoteUser

   @staticmethod
   def _getRshCommand(options, remotePeer):
      """
      Gets the RSH command associated with a remote peer.
      Use peer's if possible, otherwise take from options section.
      @param options: OptionsConfig object, as from config.options
      @param remotePeer: Configuration-style remote peer object.
      @return: RSH command associated with remote peer.
      """
      if remotePeer.rshCommand is None:
         return options.rshCommand
      return remotePeer.rshCommand

   @staticmethod
   def _getCbackCommand(options, remotePeer):
      """
      Gets the cback command associated with a remote peer.
      Use peer's if possible, otherwise take from options section.
      @param options: OptionsConfig object, as from config.options
      @param remotePeer: Configuration-style remote peer object.
      @return: cback command associated with remote peer.
      """
      if remotePeer.cbackCommand is None:
         return options.cbackCommand
      return remotePeer.cbackCommand

   @staticmethod
   def _getManagedActions(options, remotePeer):
      """
      Gets the managed actions list associated with a remote peer.
      Use peer's if possible, otherwise take from options section.
      @param options: OptionsConfig object, as from config.options
      @param remotePeer: Configuration-style remote peer object.
      @return: Set of managed actions associated with remote peer.
      """
      if remotePeer.managedActions is None:
         return options.managedActions
      return remotePeer.managedActions


#######################################################################
# Utility functions
#######################################################################

####################
# _usage() function
####################

def _usage(fd=sys.stderr):
   """
   Prints usage information for the cback script.
   @param fd: File descriptor used to print information.
   @note: The C{fd} is used rather than C{print} to facilitate unit testing.
   """
   fd.write("\n")
   fd.write(" Usage: cback [switches] action(s)\n")
   fd.write("\n")
   fd.write(" The following switches are accepted:\n")
   fd.write("\n")
   fd.write("   -h, --help         Display this usage/help listing\n")
   fd.write("   -V, --version      Display version information\n")
   fd.write("   -b, --verbose      Print verbose output as well as logging to disk\n")
   fd.write("   -q, --quiet        Run quietly (display no output to the screen)\n")
   fd.write("   -c, --config       Path to config file (default: %s)\n" % DEFAULT_CONFIG)
   fd.write("   -f, --full         Perform a full backup, regardless of configuration\n")
   fd.write("   -M, --managed      Include managed clients when executing actions\n")
   fd.write("   -N, --managed-only Include ONLY managed clients when executing actions\n")
   fd.write("   -l, --logfile      Path to logfile (default: %s)\n" % DEFAULT_LOGFILE)
   fd.write("   -o, --owner        Logfile ownership, user:group (default: %s:%s)\n" % (DEFAULT_OWNERSHIP[0], DEFAULT_OWNERSHIP[1]))
   fd.write("   -m, --mode         Octal logfile permissions mode (default: %o)\n" % DEFAULT_MODE)
   fd.write("   -O, --output       Record some sub-command (i.e. cdrecord) output to the log\n")
   fd.write("   -d, --debug        Write debugging information to the log (implies --output)\n")
   fd.write("   -s, --stack        Dump a Python stack trace instead of swallowing exceptions\n") # exactly 80 characters in width!
   fd.write("   -D, --diagnostics  Print runtime diagnostics to the screen and exit\n")
   fd.write("\n")
   fd.write(" The following actions may be specified:\n")
   fd.write("\n")
   fd.write("   all                Take all normal actions (collect, stage, store, purge)\n")
   fd.write("   collect            Take the collect action\n")
   fd.write("   stage              Take the stage action\n")
   fd.write("   store              Take the store action\n")
   fd.write("   purge              Take the purge action\n")
   fd.write("   rebuild            Rebuild \"this week's\" disc if possible\n")
   fd.write("   validate           Validate configuration only\n")
   fd.write("   initialize         Initialize media for use with Cedar Backup\n")
   fd.write("\n")
   fd.write(" You may also specify extended actions that have been defined in\n")
   fd.write(" configuration.\n")
   fd.write("\n")
   fd.write(" You must specify at least one action to take.  More than one of\n")
   fd.write(" the \"collect\", \"stage\", \"store\" or \"purge\" actions and/or\n")
   fd.write(" extended actions may be specified in any arbitrary order; they\n")
   fd.write(" will be executed in a sensible order.  The \"all\", \"rebuild\",\n")
   fd.write(" \"validate\", and \"initialize\" actions may not be combined with\n")
   fd.write(" other actions.\n")
   fd.write("\n")


######################
# _version() function
######################

def _version(fd=sys.stdout):
   """
   Prints version information for the cback script.
   @param fd: File descriptor used to print information.
   @note: The C{fd} is used rather than C{print} to facilitate unit testing.
   """
   fd.write("\n")
   fd.write(" Cedar Backup version %s, released %s.\n" % (VERSION, DATE))
   fd.write("\n")
   fd.write(" Copyright (c) %s %s <%s>.\n" % (COPYRIGHT, AUTHOR, EMAIL))
   fd.write(" See CREDITS for a list of included code and other contributors.\n")
   fd.write(" This is free software; there is NO warranty.  See the\n")
   fd.write(" GNU General Public License version 2 for copying conditions.\n")
   fd.write("\n")
   fd.write(" Use the --help option for usage information.\n")
   fd.write("\n")


##########################
# _diagnostics() function
##########################

def _diagnostics(fd=sys.stdout):
   """
   Prints runtime diagnostics information.
   @param fd: File descriptor used to print information.
   @note: The C{fd} is used rather than C{print} to facilitate unit testing.
   """
   fd.write("\n")
   fd.write("Diagnostics:\n")
   fd.write("\n")
   Diagnostics().printDiagnostics(fd=fd, prefix="   ")
   fd.write("\n")


##########################
# setupLogging() function
##########################

def setupLogging(options):
   """
   Set up logging based on command-line options.

   There are two kinds of logging: flow logging and output logging.  Output
   logging contains information about system commands executed by Cedar Backup,
   for instance the calls to C{mkisofs} or C{mount}, etc.  Flow logging
   contains error and informational messages used to understand program flow.
   Flow log messages and output log messages are written to two different
   loggers target (C{CedarBackup2.log} and C{CedarBackup2.output}).  Flow log
   messages are written at the ERROR, INFO and DEBUG log levels, while output
   log messages are generally only written at the INFO log level.

   By default, output logging is disabled.  When the C{options.output} or
   C{options.debug} flags are set, output logging will be written to the
   configured logfile.  Output logging is never written to the screen.

   By default, flow logging is enabled at the ERROR level to the screen and at
   the INFO level to the configured logfile.  If the C{options.quiet} flag is
   set, flow logging is enabled at the INFO level to the configured logfile
   only (i.e. no output will be sent to the screen).  If the C{options.verbose}
   flag is set, flow logging is enabled at the INFO level to both the screen
   and the configured logfile.  If the C{options.debug} flag is set, flow
   logging is enabled at the DEBUG level to both the screen and the configured
   logfile.

   @param options: Command-line options.
   @type options: L{Options} object

   @return: Path to logfile on disk.
   """
   logfile = _setupLogfile(options)
   _setupFlowLogging(logfile, options)
   _setupOutputLogging(logfile, options)
   return logfile

def _setupLogfile(options):
   """
   Sets up and creates logfile as needed.

   If the logfile already exists on disk, it will be left as-is, under the
   assumption that it was created with appropriate ownership and permissions.
   If the logfile does not exist on disk, it will be created as an empty file.
   Ownership and permissions will remain at their defaults unless user/group
   and/or mode are set in the options.  We ignore errors setting the indicated
   user and group.

   @note: This function is vulnerable to a race condition.  If the log file
   does not exist when the function is run, it will attempt to create the file
   as safely as possible (using C{O_CREAT}).  If two processes attempt to
   create the file at the same time, then one of them will fail.  In practice,
   this shouldn't really be a problem, but it might happen occassionally if two
   instances of cback run concurrently or if cback collides with logrotate or
   something.

   @param options: Command-line options.

   @return: Path to logfile on disk.
   """
   if options.logfile is None:
      logfile = DEFAULT_LOGFILE
   else:
      logfile = options.logfile
   if not os.path.exists(logfile):
      mode = DEFAULT_MODE if options.mode is None else options.mode
      orig = os.umask(0) # Per os.open(), "When computing mode, the current umask value is first masked out"
      try:
         fd = os.open(logfile, os.O_RDWR|os.O_CREAT|os.O_APPEND, mode)
         with os.fdopen(fd, "a+") as f:
            f.write("")
      finally:
         os.umask(orig)
      try:
         if options.owner is None or len(options.owner) < 2:
            (uid, gid) = getUidGid(DEFAULT_OWNERSHIP[0], DEFAULT_OWNERSHIP[1])
         else:
            (uid, gid) = getUidGid(options.owner[0], options.owner[1])
         os.chown(logfile, uid, gid)
      except: pass
   return logfile

def _setupFlowLogging(logfile, options):
   """
   Sets up flow logging.
   @param logfile: Path to logfile on disk.
   @param options: Command-line options.
   """
   flowLogger = logging.getLogger("CedarBackup2.log")
   flowLogger.setLevel(logging.DEBUG)    # let the logger see all messages
   _setupDiskFlowLogging(flowLogger, logfile, options)
   _setupScreenFlowLogging(flowLogger, options)

def _setupOutputLogging(logfile, options):
   """
   Sets up command output logging.
   @param logfile: Path to logfile on disk.
   @param options: Command-line options.
   """
   outputLogger = logging.getLogger("CedarBackup2.output")
   outputLogger.setLevel(logging.DEBUG)      # let the logger see all messages
   _setupDiskOutputLogging(outputLogger, logfile, options)

def _setupDiskFlowLogging(flowLogger, logfile, options):
   """
   Sets up on-disk flow logging.
   @param flowLogger: Python flow logger object.
   @param logfile: Path to logfile on disk.
   @param options: Command-line options.
   """
   formatter = logging.Formatter(fmt=DISK_LOG_FORMAT, datefmt=DATE_FORMAT)
   handler = logging.FileHandler(logfile, mode="a")
   handler.setFormatter(formatter)
   if options.debug:
      handler.setLevel(logging.DEBUG)
   else:
      handler.setLevel(logging.INFO)
   flowLogger.addHandler(handler)

def _setupScreenFlowLogging(flowLogger, options):
   """
   Sets up on-screen flow logging.
   @param flowLogger: Python flow logger object.
   @param options: Command-line options.
   """
   formatter = logging.Formatter(fmt=SCREEN_LOG_FORMAT)
   handler = logging.StreamHandler(SCREEN_LOG_STREAM)
   handler.setFormatter(formatter)
   if options.quiet:
      handler.setLevel(logging.CRITICAL)  # effectively turn it off
   elif options.verbose:
      if options.debug:
         handler.setLevel(logging.DEBUG)
      else:
         handler.setLevel(logging.INFO)
   else:
      handler.setLevel(logging.ERROR)
   flowLogger.addHandler(handler)

def _setupDiskOutputLogging(outputLogger, logfile, options):
   """
   Sets up on-disk command output logging.
   @param outputLogger: Python command output logger object.
   @param logfile: Path to logfile on disk.
   @param options: Command-line options.
   """
   formatter = logging.Formatter(fmt=DISK_OUTPUT_FORMAT, datefmt=DATE_FORMAT)
   handler = logging.FileHandler(logfile, mode="a")
   handler.setFormatter(formatter)
   if options.debug or options.output:
      handler.setLevel(logging.DEBUG)
   else:
      handler.setLevel(logging.CRITICAL)  # effectively turn it off
   outputLogger.addHandler(handler)


###############################
# setupPathResolver() function
###############################

def setupPathResolver(config):
   """
   Set up the path resolver singleton based on configuration.

   Cedar Backup's path resolver is implemented in terms of a singleton, the
   L{PathResolverSingleton} class.  This function takes options configuration,
   converts it into the dictionary form needed by the singleton, and then
   initializes the singleton.  After that, any function that needs to resolve
   the path of a command can use the singleton.

   @param config: Configuration
   @type config: L{Config} object
   """
   mapping = {}
   if config.options.overrides is not None:
      for override in config.options.overrides:
         mapping[override.command] = override.absolutePath
   singleton = PathResolverSingleton()
   singleton.fill(mapping)


#########################################################################
# Options class definition
########################################################################

class Options(object):

   ######################
   # Class documentation
   ######################

   """
   Class representing command-line options for the cback script.

   The C{Options} class is a Python object representation of the command-line
   options of the cback script.

   The object representation is two-way: a command line string or a list of
   command line arguments can be used to create an C{Options} object, and then
   changes to the object can be propogated back to a list of command-line
   arguments or to a command-line string.  An C{Options} object can even be
   created from scratch programmatically (if you have a need for that).

   There are two main levels of validation in the C{Options} class.  The first
   is field-level validation.  Field-level validation comes into play when a
   given field in an object is assigned to or updated.  We use Python's
   C{property} functionality to enforce specific validations on field values,
   and in some places we even use customized list classes to enforce
   validations on list members.  You should expect to catch a C{ValueError}
   exception when making assignments to fields if you are programmatically
   filling an object.

   The second level of validation is post-completion validation.  Certain
   validations don't make sense until an object representation of options is
   fully "complete".  We don't want these validations to apply all of the time,
   because it would make building up a valid object from scratch a real pain.
   For instance, we might have to do things in the right order to keep from
   throwing exceptions, etc.

   All of these post-completion validations are encapsulated in the
   L{Options.validate} method.  This method can be called at any time by a
   client, and will always be called immediately after creating a C{Options}
   object from a command line and before exporting a C{Options} object back to
   a command line.  This way, we get acceptable ease-of-use but we also don't
   accept or emit invalid command lines.

   @note: Lists within this class are "unordered" for equality comparisons.

   @sort: __init__, __repr__, __str__, __cmp__
   """

   ##############
   # Constructor
   ##############

   def __init__(self, argumentList=None, argumentString=None, validate=True):
      """
      Initializes an options object.

      If you initialize the object without passing either C{argumentList} or
      C{argumentString}, the object will be empty and will be invalid until it
      is filled in properly.

      No reference to the original arguments is saved off by this class.  Once
      the data has been parsed (successfully or not) this original information
      is discarded.

      The argument list is assumed to be a list of arguments, not including the
      name of the command, something like C{sys.argv[1:]}.  If you pass
      C{sys.argv} instead, things are not going to work.

      The argument string will be parsed into an argument list by the
      L{util.splitCommandLine} function (see the documentation for that
      function for some important notes about its limitations).  There is an
      assumption that the resulting list will be equivalent to C{sys.argv[1:]},
      just like C{argumentList}.

      Unless the C{validate} argument is C{False}, the L{Options.validate}
      method will be called (with its default arguments) after successfully
      parsing any passed-in command line.  This validation ensures that
      appropriate actions, etc. have been specified.  Keep in mind that even if
      C{validate} is C{False}, it might not be possible to parse the passed-in
      command line, so an exception might still be raised.

      @note: The command line format is specified by the L{_usage} function.
      Call L{_usage} to see a usage statement for the cback script.

      @note: It is strongly suggested that the C{validate} option always be set
      to C{True} (the default) unless there is a specific need to read in
      invalid command line arguments.

      @param argumentList: Command line for a program.
      @type argumentList: List of arguments, i.e. C{sys.argv}

      @param argumentString: Command line for a program.
      @type argumentString: String, i.e. "cback --verbose stage store"

      @param validate: Validate the command line after parsing it.
      @type validate: Boolean true/false.

      @raise getopt.GetoptError: If the command-line arguments could not be parsed.
      @raise ValueError: If the command-line arguments are invalid.
      """
      self._help = False
      self._version = False
      self._verbose = False
      self._quiet = False
      self._config = None
      self._full = False
      self._managed = False
      self._managedOnly = False
      self._logfile = None
      self._owner = None
      self._mode = None
      self._output = False
      self._debug = False
      self._stacktrace = False
      self._diagnostics = False
      self._actions = None
      self.actions = []    # initialize to an empty list; remainder are OK
      if argumentList is not None and argumentString is not None:
         raise ValueError("Use either argumentList or argumentString, but not both.")
      if argumentString is not None:
         argumentList = splitCommandLine(argumentString)
      if argumentList is not None:
         self._parseArgumentList(argumentList)
         if validate:
            self.validate()


   #########################
   # String representations
   #########################

   def __repr__(self):
      """
      Official string representation for class instance.
      """
      return self.buildArgumentString(validate=False)

   def __str__(self):
      """
      Informal string representation for class instance.
      """
      return self.__repr__()


   #############################
   # Standard comparison method
   #############################

   def __cmp__(self, other):
      """
      Definition of equals operator for this class.
      Lists within this class are "unordered" for equality comparisons.
      @param other: Other object to compare to.
      @return: -1/0/1 depending on whether self is C{<}, C{=} or C{>} other.
      """
      if other is None:
         return 1
      if self.help != other.help:
         if self.help < other.help:
            return -1
         else:
            return 1
      if self.version != other.version:
         if self.version < other.version:
            return -1
         else:
            return 1
      if self.verbose != other.verbose:
         if self.verbose < other.verbose:
            return -1
         else:
            return 1
      if self.quiet != other.quiet:
         if self.quiet < other.quiet:
            return -1
         else:
            return 1
      if self.config != other.config:
         if self.config < other.config:
            return -1
         else:
            return 1
      if self.full != other.full:
         if self.full < other.full:
            return -1
         else:
            return 1
      if self.managed != other.managed:
         if self.managed < other.managed:
            return -1
         else:
            return 1
      if self.managedOnly != other.managedOnly:
         if self.managedOnly < other.managedOnly:
            return -1
         else:
            return 1
      if self.logfile != other.logfile:
         if self.logfile < other.logfile:
            return -1
         else:
            return 1
      if self.owner != other.owner:
         if self.owner < other.owner:
            return -1
         else:
            return 1
      if self.mode != other.mode:
         if self.mode < other.mode:
            return -1
         else:
            return 1
      if self.output != other.output:
         if self.output < other.output:
            return -1
         else:
            return 1
      if self.debug != other.debug:
         if self.debug < other.debug:
            return -1
         else:
            return 1
      if self.stacktrace != other.stacktrace:
         if self.stacktrace < other.stacktrace:
            return -1
         else:
            return 1
      if self.diagnostics != other.diagnostics:
         if self.diagnostics < other.diagnostics:
            return -1
         else:
            return 1
      if self.actions != other.actions:
         if self.actions < other.actions:
            return -1
         else:
            return 1
      return 0


   #############
   # Properties
   #############

   def _setHelp(self, value):
      """
      Property target used to set the help flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._help = True
      else:
         self._help = False

   def _getHelp(self):
      """
      Property target used to get the help flag.
      """
      return self._help

   def _setVersion(self, value):
      """
      Property target used to set the version flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._version = True
      else:
         self._version = False

   def _getVersion(self):
      """
      Property target used to get the version flag.
      """
      return self._version

   def _setVerbose(self, value):
      """
      Property target used to set the verbose flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._verbose = True
      else:
         self._verbose = False

   def _getVerbose(self):
      """
      Property target used to get the verbose flag.
      """
      return self._verbose

   def _setQuiet(self, value):
      """
      Property target used to set the quiet flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._quiet = True
      else:
         self._quiet = False

   def _getQuiet(self):
      """
      Property target used to get the quiet flag.
      """
      return self._quiet

   def _setConfig(self, value):
      """
      Property target used to set the config parameter.
      """
      if value is not None:
         if len(value) < 1:
            raise ValueError("The config parameter must be a non-empty string.")
      self._config = value

   def _getConfig(self):
      """
      Property target used to get the config parameter.
      """
      return self._config

   def _setFull(self, value):
      """
      Property target used to set the full flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._full = True
      else:
         self._full = False

   def _getFull(self):
      """
      Property target used to get the full flag.
      """
      return self._full

   def _setManaged(self, value):
      """
      Property target used to set the managed flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._managed = True
      else:
         self._managed = False

   def _getManaged(self):
      """
      Property target used to get the managed flag.
      """
      return self._managed

   def _setManagedOnly(self, value):
      """
      Property target used to set the managedOnly flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._managedOnly = True
      else:
         self._managedOnly = False

   def _getManagedOnly(self):
      """
      Property target used to get the managedOnly flag.
      """
      return self._managedOnly

   def _setLogfile(self, value):
      """
      Property target used to set the logfile parameter.
      @raise ValueError: If the value cannot be encoded properly.
      """
      if value is not None:
         if len(value) < 1:
            raise ValueError("The logfile parameter must be a non-empty string.")
      self._logfile = encodePath(value)

   def _getLogfile(self):
      """
      Property target used to get the logfile parameter.
      """
      return self._logfile

   def _setOwner(self, value):
      """
      Property target used to set the owner parameter.
      If not C{None}, the owner must be a C{(user,group)} tuple or list.
      Strings (and inherited children of strings) are explicitly disallowed.
      The value will be normalized to a tuple.
      @raise ValueError: If the value is not valid.
      """
      if value is None:
         self._owner = None
      else:
         if isinstance(value, str):
            raise ValueError("Must specify user and group tuple for owner parameter.")
         if len(value) != 2:
            raise ValueError("Must specify user and group tuple for owner parameter.")
         if len(value[0]) < 1 or len(value[1]) < 1:
            raise ValueError("User and group tuple values must be non-empty strings.")
         self._owner = (value[0], value[1])

   def _getOwner(self):
      """
      Property target used to get the owner parameter.
      The parameter is a tuple of C{(user, group)}.
      """
      return self._owner

   def _setMode(self, value):
      """
      Property target used to set the mode parameter.
      """
      if value is None:
         self._mode = None
      else:
         try:
            if isinstance(value, str):
               value = int(value, 8)
            else:
               value = int(value)
         except TypeError:
            raise ValueError("Mode must be an octal integer >= 0, i.e. 644.")
         if value < 0:
            raise ValueError("Mode must be an octal integer >= 0. i.e. 644.")
         self._mode = value

   def _getMode(self):
      """
      Property target used to get the mode parameter.
      """
      return self._mode

   def _setOutput(self, value):
      """
      Property target used to set the output flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._output = True
      else:
         self._output = False

   def _getOutput(self):
      """
      Property target used to get the output flag.
      """
      return self._output

   def _setDebug(self, value):
      """
      Property target used to set the debug flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._debug = True
      else:
         self._debug = False

   def _getDebug(self):
      """
      Property target used to get the debug flag.
      """
      return self._debug

   def _setStacktrace(self, value):
      """
      Property target used to set the stacktrace flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._stacktrace = True
      else:
         self._stacktrace = False

   def _getStacktrace(self):
      """
      Property target used to get the stacktrace flag.
      """
      return self._stacktrace

   def _setDiagnostics(self, value):
      """
      Property target used to set the diagnostics flag.
      No validations, but we normalize the value to C{True} or C{False}.
      """
      if value:
         self._diagnostics = True
      else:
         self._diagnostics = False

   def _getDiagnostics(self):
      """
      Property target used to get the diagnostics flag.
      """
      return self._diagnostics

   def _setActions(self, value):
      """
      Property target used to set the actions list.
      We don't restrict the contents of actions.  They're validated somewhere else.
      @raise ValueError: If the value is not valid.
      """
      if value is None:
         self._actions = None
      else:
         try:
            saved = self._actions
            self._actions = []
            self._actions.extend(value)
         except Exception, e:
            self._actions = saved
            raise e

   def _getActions(self):
      """
      Property target used to get the actions list.
      """
      return self._actions

   help = property(_getHelp, _setHelp, None, "Command-line help (C{-h,--help}) flag.")
   version = property(_getVersion, _setVersion, None, "Command-line version (C{-V,--version}) flag.")
   verbose = property(_getVerbose, _setVerbose, None, "Command-line verbose (C{-b,--verbose}) flag.")
   quiet = property(_getQuiet, _setQuiet, None, "Command-line quiet (C{-q,--quiet}) flag.")
   config = property(_getConfig, _setConfig, None, "Command-line configuration file (C{-c,--config}) parameter.")
   full = property(_getFull, _setFull, None, "Command-line full-backup (C{-f,--full}) flag.")
   managed = property(_getManaged, _setManaged, None, "Command-line managed (C{-M,--managed}) flag.")
   managedOnly = property(_getManagedOnly, _setManagedOnly, None, "Command-line managed-only (C{-N,--managed-only}) flag.")
   logfile = property(_getLogfile, _setLogfile, None, "Command-line logfile (C{-l,--logfile}) parameter.")
   owner = property(_getOwner, _setOwner, None, "Command-line owner (C{-o,--owner}) parameter, as tuple C{(user,group)}.")
   mode = property(_getMode, _setMode, None, "Command-line mode (C{-m,--mode}) parameter.")
   output = property(_getOutput, _setOutput, None, "Command-line output (C{-O,--output}) flag.")
   debug = property(_getDebug, _setDebug, None, "Command-line debug (C{-d,--debug}) flag.")
   stacktrace = property(_getStacktrace, _setStacktrace, None, "Command-line stacktrace (C{-s,--stack}) flag.")
   diagnostics = property(_getDiagnostics, _setDiagnostics, None, "Command-line diagnostics (C{-D,--diagnostics}) flag.")
   actions = property(_getActions, _setActions, None, "Command-line actions list.")


   ##################
   # Utility methods
   ##################

   def validate(self):
      """
      Validates command-line options represented by the object.

      Unless C{--help} or C{--version} are supplied, at least one action must
      be specified.  Other validations (as for allowed values for particular
      options) will be taken care of at assignment time by the properties
      functionality.

      @note: The command line format is specified by the L{_usage} function.
      Call L{_usage} to see a usage statement for the cback script.

      @raise ValueError: If one of the validations fails.
      """
      if not self.help and not self.version and not self.diagnostics:
         if self.actions is None or len(self.actions) == 0:
            raise ValueError("At least one action must be specified.")
      if self.managed and self.managedOnly:
         raise ValueError("The --managed and --managed-only options may not be combined.")

   def buildArgumentList(self, validate=True):
      """
      Extracts options into a list of command line arguments.

      The original order of the various arguments (if, indeed, the object was
      initialized with a command-line) is not preserved in this generated
      argument list.   Besides that, the argument list is normalized to use the
      long option names (i.e. --version rather than -V).  The resulting list
      will be suitable for passing back to the constructor in the
      C{argumentList} parameter.  Unlike L{buildArgumentString}, string
      arguments are not quoted here, because there is no need for it.

      Unless the C{validate} parameter is C{False}, the L{Options.validate}
      method will be called (with its default arguments) against the
      options before extracting the command line.  If the options are not valid,
      then an argument list will not be extracted.

      @note: It is strongly suggested that the C{validate} option always be set
      to C{True} (the default) unless there is a specific need to extract an
      invalid command line.

      @param validate: Validate the options before extracting the command line.
      @type validate: Boolean true/false.

      @return: List representation of command-line arguments.
      @raise ValueError: If options within the object are invalid.
      """
      if validate:
         self.validate()
      argumentList = []
      if self._help:
         argumentList.append("--help")
      if self.version:
         argumentList.append("--version")
      if self.verbose:
         argumentList.append("--verbose")
      if self.quiet:
         argumentList.append("--quiet")
      if self.config is not None:
         argumentList.append("--config")
         argumentList.append(self.config)
      if self.full:
         argumentList.append("--full")
      if self.managed:
         argumentList.append("--managed")
      if self.managedOnly:
         argumentList.append("--managed-only")
      if self.logfile is not None:
         argumentList.append("--logfile")
         argumentList.append(self.logfile)
      if self.owner is not None:
         argumentList.append("--owner")
         argumentList.append("%s:%s" % (self.owner[0], self.owner[1]))
      if self.mode is not None:
         argumentList.append("--mode")
         argumentList.append("%o" % self.mode)
      if self.output:
         argumentList.append("--output")
      if self.debug:
         argumentList.append("--debug")
      if self.stacktrace:
         argumentList.append("--stack")
      if self.diagnostics:
         argumentList.append("--diagnostics")
      if self.actions is not None:
         for action in self.actions:
            argumentList.append(action)
      return argumentList

   def buildArgumentString(self, validate=True):
      """
      Extracts options into a string of command-line arguments.

      The original order of the various arguments (if, indeed, the object was
      initialized with a command-line) is not preserved in this generated
      argument string.   Besides that, the argument string is normalized to use
      the long option names (i.e. --version rather than -V) and to quote all
      string arguments with double quotes (C{"}).  The resulting string will be
      suitable for passing back to the constructor in the C{argumentString}
      parameter.

      Unless the C{validate} parameter is C{False}, the L{Options.validate}
      method will be called (with its default arguments) against the options
      before extracting the command line.  If the options are not valid, then
      an argument string will not be extracted.

      @note: It is strongly suggested that the C{validate} option always be set
      to C{True} (the default) unless there is a specific need to extract an
      invalid command line.

      @param validate: Validate the options before extracting the command line.
      @type validate: Boolean true/false.

      @return: String representation of command-line arguments.
      @raise ValueError: If options within the object are invalid.
      """
      if validate:
         self.validate()
      argumentString = ""
      if self._help:
         argumentString += "--help "
      if self.version:
         argumentString += "--version "
      if self.verbose:
         argumentString += "--verbose "
      if self.quiet:
         argumentString += "--quiet "
      if self.config is not None:
         argumentString += "--config \"%s\" " % self.config
      if self.full:
         argumentString += "--full "
      if self.managed:
         argumentString += "--managed "
      if self.managedOnly:
         argumentString += "--managed-only "
      if self.logfile is not None:
         argumentString += "--logfile \"%s\" " % self.logfile
      if self.owner is not None:
         argumentString += "--owner \"%s:%s\" " % (self.owner[0], self.owner[1])
      if self.mode is not None:
         argumentString += "--mode %o " % self.mode
      if self.output:
         argumentString += "--output "
      if self.debug:
         argumentString += "--debug "
      if self.stacktrace:
         argumentString += "--stack "
      if self.diagnostics:
         argumentString += "--diagnostics "
      if self.actions is not None:
         for action in self.actions:
            argumentString +=  "\"%s\" " % action
      return argumentString

   def _parseArgumentList(self, argumentList):
      """
      Internal method to parse a list of command-line arguments.

      Most of the validation we do here has to do with whether the arguments
      can be parsed and whether any values which exist are valid.  We don't do
      any validation as to whether required elements exist or whether elements
      exist in the proper combination (instead, that's the job of the
      L{validate} method).

      For any of the options which supply parameters, if the option is
      duplicated with long and short switches (i.e. C{-l} and a C{--logfile})
      then the long switch is used.  If the same option is duplicated with the
      same switch (long or short), then the last entry on the command line is
      used.

      @param argumentList: List of arguments to a command.
      @type argumentList: List of arguments to a command, i.e. C{sys.argv[1:]}

      @raise ValueError: If the argument list cannot be successfully parsed.
      """
      switches = { }
      opts, self.actions = getopt.getopt(argumentList, SHORT_SWITCHES, LONG_SWITCHES)
      for o, a in opts:  # push the switches into a hash
         switches[o] = a
      if switches.has_key("-h") or switches.has_key("--help"):
         self.help = True
      if switches.has_key("-V") or switches.has_key("--version"):
         self.version = True
      if switches.has_key("-b") or switches.has_key("--verbose"):
         self.verbose = True
      if switches.has_key("-q") or switches.has_key("--quiet"):
         self.quiet = True
      if switches.has_key("-c"):
         self.config = switches["-c"]
      if switches.has_key("--config"):
         self.config = switches["--config"]
      if switches.has_key("-f") or switches.has_key("--full"):
         self.full = True
      if switches.has_key("-M") or switches.has_key("--managed"):
         self.managed = True
      if switches.has_key("-N") or switches.has_key("--managed-only"):
         self.managedOnly = True
      if switches.has_key("-l"):
         self.logfile = switches["-l"]
      if switches.has_key("--logfile"):
         self.logfile = switches["--logfile"]
      if switches.has_key("-o"):
         self.owner = switches["-o"].split(":", 1)
      if switches.has_key("--owner"):
         self.owner = switches["--owner"].split(":", 1)
      if switches.has_key("-m"):
         self.mode = switches["-m"]
      if switches.has_key("--mode"):
         self.mode = switches["--mode"]
      if switches.has_key("-O") or switches.has_key("--output"):
         self.output = True
      if switches.has_key("-d") or switches.has_key("--debug"):
         self.debug = True
      if switches.has_key("-s") or switches.has_key("--stack"):
         self.stacktrace = True
      if switches.has_key("-D") or switches.has_key("--diagnostics"):
         self.diagnostics = True


#########################################################################
# Main routine
########################################################################

if __name__ == "__main__":
   result = cli()
   sys.exit(result)