This file is indexed.

/usr/sbin/koji-shadow is in koji-servers 1.10.0-1.

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

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
#!/usr/bin/python

# koji-shadow: a tool to shadow builds between koji instances
# Copyright (c) 2007-2014 Red Hat, Inc.
#
#    Koji is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation;
#    version 2.1 of the License.
#
#    This software is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this software; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:
#       Mike McLean <mikem@redhat.com>
#       Dennis Gilmore <dennis@ausil.us>

try:
    import krbV
except ImportError:
    pass
import koji
import ConfigParser
import fnmatch
import optparse
import os
import pprint
import random
import shutil
import socket   # for socket.error and socket.setdefaulttimeout
import string
import sys
import time
import urllib2
import urlgrabber.grabber as grabber
import xmlrpclib  # for ProtocolError and Fault
import rpm

# koji.fp.o keeps stalling, probably network errors...
# better to time out than to stall
socket.setdefaulttimeout(180)  #XXX - too short?


OptionParser = optparse.OptionParser
if optparse.__version__ == "1.4.1+":
    def _op_error(self, msg):
        self.print_usage(sys.stderr)
        msg = "%s: error: %s\n" % (self._get_prog_name(), msg)
        if msg:
            sys.stderr.write(msg)
        sys.exit(2)
    OptionParser.error = _op_error


def _(args):
    """Stub function for translation"""
    return args


class SubOption(object):
    """A simple container to help with tracking ConfigParser data"""
    pass

def get_options():
    """process options from command line and config file"""

    usage = _("%prog [options]")
    parser = OptionParser(usage=usage)
    parser.add_option("-c", "--config-file", metavar="FILE",
                      help=_("use alternate configuration file"))
    parser.add_option("--keytab", help=_("specify a Kerberos keytab to use"))
    parser.add_option("--principal", help=_("specify a Kerberos principal to use"))
    parser.add_option("--krbservice", help=_("the service name of the"
                                             " principal being used by the hub"))
    parser.add_option("--runas", metavar="USER",
             help=_("run as the specified user (requires special privileges)"))
    parser.add_option("--user", help=_("specify user"))
    parser.add_option("--password", help=_("specify password"))
    parser.add_option("--noauth", action="store_true", default=False,
                      help=_("do not authenticate"))
    parser.add_option("-n", "--test", action="store_true", default=False,
                      help=_("test mode"))
    parser.add_option("-d", "--debug", action="store_true", default=False,
                      help=_("show debug output"))
    parser.add_option("--first-one", action="store_true", default=False,
                      help=_("stop after scanning first build -- debugging"))
    parser.add_option("--debug-xmlrpc", action="store_true", default=False,
                      help=_("show xmlrpc debug output"))
    parser.add_option("--skip-main", action="store_true", default=False,
                      help=_("don't actually run main"))
#    parser.add_option("--tag-filter", metavar="PATTERN",
#                      help=_("limit tags for pruning"))
#    parser.add_option("--pkg-filter", metavar="PATTERN",
#                      help=_("limit packages for pruning"))
    parser.add_option("--max-jobs", type="int", default=0,
                      help=_("limit number of tasks"))
    parser.add_option("--build",
                      help=_("scan just this build"))
    parser.add_option("-s", "--server",
                      help=_("url of local XMLRPC server"))
    parser.add_option("-r", "--remote",
                      help=_("url of remote XMLRPC server"))
    parser.add_option("--prefer-new", action="store_true", default=False,
                      help=_("if there is a newer build locally prefer it for deps"))
    parser.add_option("--import-noarch-only", action="store_true", default=False,
                      help=_("Only import missing noarch builds"))
    parser.add_option("--import-noarch", action="store_true",
                      help=_("import missing noarch builds rather than rebuilding"))
    parser.add_option("--link-imports", action="store_true",
                      help=_("use 'import --link' functionality"))
    parser.add_option("--remote-topurl",
                      help=_("topurl for remote server"))
    parser.add_option("--workpath", default="/tmp/koji-shadow",
                      help=_("location to store work files"))
    parser.add_option("--auth-cert",
                      help=_("Certificate for authentication"))
    parser.add_option("--auth-ca",
                      help=_("CA certificate for authentication"))
    parser.add_option("--serverca",
                      help=_("Server CA certificate"))
    parser.add_option("--rules",
                      help=_("rules"))
    parser.add_option("--rules-greylist",
                      help=_("greylist rules"))
    parser.add_option("--rules-blacklist",
                      help=_("blacklist rules"))
    parser.add_option("--rules-ignorelist",
                      help=_("Rules: list of packages to ignore"))
    parser.add_option("--rules-excludelist",
                      help=_("Rules: list of packages to are excluded using ExcludeArch or ExclusiveArch"))
    parser.add_option("--rules-includelist",
                      help=_("Rules: list of packages to always include"))
    parser.add_option("--rules-protectlist",
                      help=_("Rules: list of package names to never replace"))
    parser.add_option("--tag-build", action="store_true", default=False,
                      help=_("tag sucessful builds into the tag we are building, default is to not tag"))
    parser.add_option("--arches",
                      help=_("arches to use when creating tags"))
    parser.add_option("--priority", type="int", default=5,
                      help=_("priority to set for submitted builds"))

    #parse once to get the config file
    (options, args) = parser.parse_args()

    defaults = parser.get_default_values()
    config = ConfigParser.ConfigParser()
    cf = getattr(options, 'config_file', None)
    if cf:
        if not os.access(cf, os.F_OK):
            parser.error(_("No such file: %s") % cf)
            assert False
    else:
        cf = '/etc/koji-shadow/koji-shadow.conf'
        if not os.access(cf, os.F_OK):
            cf = None
    if not cf:
        print "no config file"
        config = None
    else:
        config.read(cf)
        #allow config file to update defaults
        for opt in parser.option_list:
            if not opt.dest:
                continue
            name = opt.dest
            alias = ('main', name)
            if config.has_option(*alias):
                print "Using option %s from config file" % (alias,)
                if opt.action in ('store_true', 'store_false'):
                    setattr(defaults, name, config.getboolean(*alias))
                elif opt.action != 'store':
                    pass
                elif opt.type in ('int', 'long'):
                    setattr(defaults, name, config.getint(*alias))
                elif opt.type in ('float'):
                    setattr(defaults, name, config.getfloat(*alias))
                else:
                    print config.get(*alias)
                    setattr(defaults, name, config.get(*alias))
        #config file options without a cmdline equivalent
        otheropts = [
            #name, type, default
            ['keytab', None, 'string'],
            ['principal', None, 'string'],
            ['runas', None, 'string'],
            ['user', None, 'string'],
            ['password', None, 'string'],
            ['noauth', None, 'boolean'],
            ['server', None, 'string'],
            ['remote', None, 'string'],
            ['max_jobs', None, 'int'],
            ['serverca', None, 'string'],
            ['auth_cert', None, 'string'],
            ['auth_ca', None, 'string'],
            ['arches', None, 'string'],
            ]


    #parse again with updated defaults
    (options, args) = parser.parse_args(values=defaults)
    options.config = config

    return options, args

time_units = {
    'second' : 1,
    'minute' : 60,
    'hour' : 3600,
    'day' : 86400,
    'week' : 604800,
}
time_unit_aliases = [
    #[unit, alias, alias, ...]
    ['week', 'weeks', 'wk', 'wks'],
    ['hour', 'hours', 'hr', 'hrs'],
    ['day', 'days'],
    ['minute', 'minutes', 'min', 'mins'],
    ['second', 'seconds', 'sec', 'secs', 's'],
]
def parse_duration(str):
    """Parse time duration from string, returns duration in seconds"""
    ret = 0
    n = None
    unit = None
    def parse_num(s):
        try:
            return int(s)
        except ValueError:
            pass
        try:
            return float(s)
        except ValueError:
            pass
        return None
    for x in str.split():
        if n is None:
            n = parse_num(x)
            if n is not None:
                continue
            #perhaps the unit is appended w/o a space
            for names in time_unit_aliases:
                for name in names:
                    if x.endswith(name):
                        n = parse_num(x[:-len(name)])
                        if n is None:
                            continue
                        unit = names[0]
                        # combined at end
                        break
                if unit:
                    break
            else:
                raise ValueError, "Invalid time interval: %s" % str
        if unit is None:
            x = x.lower()
            for names in time_unit_aliases:
                for name in names:
                    if x == name:
                        unit = names[0]
                        break
                if unit:
                    break
            else:
                raise ValueError, "Invalid time interval: %s" % str
        ret += n * time_units[unit]
        n = None
        unit = None
    return ret

def error(msg=None, code=1):
    if msg:
        sys.stderr.write(msg + "\n")
        sys.stderr.flush()
    sys.exit(code)

def warn(msg):
    sys.stderr.write(msg + "\n")
    sys.stderr.flush()

def ensure_connection(session):
    try:
        ret = session.getAPIVersion()
    except xmlrpclib.ProtocolError:
        error(_("Error: Unable to connect to server"))
    if ret != koji.API_VERSION:
        warn(_("WARNING: The server is at API version %d and the client is at "
               "%d" % (ret, koji.API_VERSION)))

def activate_session(session):
    """Test and login the session is applicable"""
    global options

    # convert to absolute paths
    options.auth_cert  = os.path.expanduser(options.auth_cert)
    options.auth_ca  = os.path.expanduser(options.auth_ca)
    options.serverca  = os.path.expanduser(options.serverca)

    if options.noauth:
        #skip authentication
        pass
    elif os.path.isfile(options.auth_cert):
        # authenticate using SSL client cert
        session.ssl_login(options.auth_cert, options.auth_ca, options.serverca, proxyuser=options.runas)
    elif options.user:
        #authenticate using user/password
        session.login()
    elif sys.modules.has_key('krbV'):
        try:
            if options.keytab and options.principal:
                session.krb_login(principal=options.principal, keytab=options.keytab, proxyuser=options.runas)
            else:
                session.krb_login(proxyuser=options.runas)
        except krbV.Krb5Error, e:
            error(_("Kerberos authentication failed: '%s' (%s)") % (e.args[1], e.args[0]))
        except socket.error, e:
            warn(_("Could not connect to Kerberos authentication service: '%s'") % e.args[1])
    if not options.noauth and not session.logged_in:
        error(_("Error: unable to log in"))
    ensure_connection(session)
    if options.debug:
        print "successfully connected to hub"

def _unique_path(prefix):
    """Create a unique path fragment by appending a path component
    to prefix.  The path component will consist of a string of letter and numbers
    that is unlikely to be a duplicate, but is not guaranteed to be unique."""
    # Use time() in the dirname to provide a little more information when
    # browsing the filesystem.
    # For some reason repr(time.time()) includes 4 or 5
    # more digits of precision than str(time.time())
    return '%s/%r.%s' % (prefix, time.time(),
                      ''.join([random.choice(string.ascii_letters) for i in range(8)]))


class LocalBuild(object):
    """A stand-in for substitute deps that are only available locally"""

    def __init__(self, info, tracker=None):
        self.info = info
        self.id = info['id']
        self.nvr = "%(name)s-%(version)s-%(release)s" % self.info
        self.state = 'local'


class TrackedBuild(object):

    def __init__(self, build_id, child=None, tracker=None):
        self.id = build_id
        self.tracker = tracker
        self.info = remote.getBuild(build_id)
        self.nvr = "%(name)s-%(version)s-%(release)s" % self.info
        self.name = "%(name)s" % self.info
        self.epoch = "%(epoch)s" % self.info
        self.version = "%(version)s" % self.info
        self.release = "%(release)s" % self.info
        self.srpm = None
        self.rpms = None
        self.children = {}
        self.state = None
        self.order = 0
        self.substitute = None
        if child is not None:
            #children tracks the builds that were built using this one
            self.children[child] = 1
        #see if we have it
        self.rebuilt = False
        self.updateState()
        if self.state == 'missing':
            self.rpms = remote.listRPMs(self.id)
            for rinfo in self.rpms:
                if rinfo['arch'] == 'src':
                    self.srpm = rinfo
            self.getExtraArches()
            self.getDeps() #sets deps, br_tag, base, order, (maybe state)

    def updateState(self):
        """Update state from local hub

        This is intended to be called at initialization and after a missing
        build has been rebuilt"""
        ours = session.getBuild(self.nvr)
        if ours is not None:
            state = koji.BUILD_STATES[ours['state']]
            if state == 'COMPLETE':
                self.setState("common")
                if ours['task_id']:
                    self.rebuilt = True
                return
            elif state in ('FAILED', 'CANCELED'):
                #treat these as having no build
                pass
            elif state == 'BUILDING' and ours['task_id']:
                self.setState("pending")
                self.task_id = ours['task_id']
                return
            else:
                # DELETED or BUILDING(no task)
                self.setState("broken")
                return
        self.setState("missing")

    def isNoarch(self):
        if not self.rpms:
            return False
        noarch = False
        for rpminfo in self.rpms:
            if rpminfo['arch'] == 'noarch':
                #note that we've seen a noarch rpm
                noarch = True
            elif rpminfo['arch'] != 'src':
                return False
        return noarch

    def setState(self, state):
        #print "%s -> %s" % (self.nvr, state)
        if state == self.state:
            return
        if self.state is not None and self.tracker:
            del self.tracker.state_idx[self.state][self.id]
        self.state = state
        if self.tracker:
            self.tracker.state_idx.setdefault(self.state, {})[self.id] = self

    def getSource(self):
        """Get source from remote"""
        if options.remote_topurl and self.srpm:
            #download srpm from remote
            pathinfo = koji.PathInfo(options.remote_topurl)
            url = "%s/%s" % (pathinfo.build(self.info), pathinfo.rpm(self.srpm))
            print "Downloading %s" % url
            #XXX - this is not really the right place for this
            fsrc = urllib2.urlopen(url)
            fn = "/tmp/koji-shadow/%s.src.rpm" % self.nvr
            koji.ensuredir(os.path.dirname(fn))
            fdst = file(fn, 'w')
            shutil.copyfileobj(fsrc, fdst)
            fsrc.close()
            fdst.close()
            serverdir = _unique_path('koji-shadow')
            session.uploadWrapper(fn, serverdir, blocksize=65536)
            src = "%s/%s" % (serverdir, os.path.basename(fn))
            return src
        #otherwise use SCM url
        task_id = self.info['task_id']
        if task_id:
            tinfo = remote.getTaskInfo(task_id)
            if tinfo['method'] == 'build':
                try:
                    request = remote.getTaskRequest(task_id)
                    src = request[0]
                    #XXX - Move SCM class out of kojid and use it to check for scm url
                    if src.startswith('cvs:'):
                        return src
                except:
                    pass
        #otherwise fail
        return None

    def addChild(self, child):
        self.children[child] = 1

    def getExtraArches(self):
        arches = {}
        for rpminfo in self.rpms:
            arches.setdefault(rpminfo['arch'], 1)
        self.extraArches = [a for a in arches if koji.canonArch(a) != a]

    def getBuildroots(self):
        """Return a list of buildroots for remote build"""
        brs = {}
        bad = []
        for rinfo in self.rpms:
            br_id = rinfo.get('buildroot_id')
            if not br_id:
                bad.append(rinfo)
                continue
            brs[br_id] = 1
        if brs and bad:
            print "Warning: some rpms for %s lacked buildroots:" % self.nvr
            for rinfo in bad:
                print "    %(name)s-%(version)s-%(release)s.%(arch)s" % rinfo
        return brs.keys()

    def getDeps(self):
        buildroots = self.getBuildroots()
        if not buildroots:
            self.setState("noroot")
            return
        buildroots.sort()
        self.order = buildroots[-1]
        seen = {}       #used to avoid scanning the same buildroot twice
        builds = {}     #track which builds we need for a rebuild
        bases = {}      #track base install for buildroots
        tags = {}       #track buildroot tag(s)
        remote.multicall = True
        unpack = []
        for br_id in buildroots:
            if seen.has_key(br_id):
                continue
            seen[br_id] = 1
            #br_info = remote.getBuildroot(br_id, strict=True)
            remote.getBuildroot(br_id, strict=True)
            unpack.append(('br_info', br_id))
            #tags.setdefault(br_info['tag_name'], 0)
            #tags[br_info['tag_name']] += 1
            #print "."
            remote.listRPMs(componentBuildrootID=br_id)
            unpack.append(('rpmlist', br_id))
            #for rinfo in remote.listRPMs(componentBuildrootID=br_id):
            #    builds[rinfo['build_id']] = 1
            #    if not rinfo['is_update']:
            #        bases.setdefault(rinfo['name'], {})[br_id] = 1
        for (dtype, br_id), data in zip(unpack, remote.multiCall()):
            if dtype == 'br_info':
                [br_info] = data
                tags.setdefault(br_info['tag_name'], 0)
                tags[br_info['tag_name']] += 1
            elif dtype == 'rpmlist':
                [rpmlist] = data
                for rinfo in rpmlist:
                    builds[rinfo['build_id']] = 1
                    if not rinfo['is_update']:
                        bases.setdefault(rinfo['name'], {})[br_id] = 1
        # we want to record the intersection of the base sets
        # XXX - this makes some assumptions about homogeneity that, while reasonable,
        #       are not strictly required of the db.
        #       The only way I can think of to break this is if some significant tag/target
        #        changes happened during the build startup and some subtasks got the old
        #        repo and others the new one.
        base = []
        for name, brlist in bases.iteritems():
            #We want to determine for each name if that package was present
            #in /all/ the buildroots or just some.
            #Because brlist is constructed only from elements of buildroots, we
            #can simply check the length
            assert len(brlist) <= len(buildroots)
            if len(brlist) == len(buildroots):
                #each buildroot had this as a base package
                base.append(name)
        if len(tags) > 1:
            print "Warning: found multiple buildroot tags for %s: %s" % (self.nvr, tags.keys())
            counts = [(n, tag) for tag, n in tags.iteritems()]
            sort(counts)
            tag = counts[-1][1]
        else:
            tag = tags.keys()[0]
        self.deps = builds
        self.revised_deps = None #BuildTracker will set this later
        self.br_tag = tag
        self.base = base


class BuildTracker(object):

    def __init__(self):
        self.rebuild_order = 0
        self.builds = {}
        self.state_idx = {}
        self.nvr_idx = {}
        for state in ('common', 'pending', 'missing', 'broken', 'brokendeps',
                     'noroot', 'blocked', 'grey'):
            self.state_idx.setdefault(state, {})
        self.scanRules()

    def scanRules(self):
        """Reads/parses rules data from the config

        This data consists mainly of
            white/black/greylist data
            substitution data
        """
        self.blacklist = None
        self.whitelist = None
        self.greylist = None
        self.ignorelist = []
        self.excludelist = []
        self.includelist = []
        self.protectlist = []
        self.substitute_idx = {}
        self.substitutions = {}
        if options.config.has_option('rules', 'whitelist'):
            self.whitelist = options.config.get('rules', 'whitelist').split()
        if options.config.has_option('rules', 'blacklist'):
            self.blacklist = options.config.get('rules', 'blacklist').split()
        if options.config.has_option('rules', 'greylist'):
            self.greylist = options.config.get('rules', 'greylist').split()
        if options.config.has_option('rules', 'ignorelist'):
            self.ignorelist = options.config.get('rules', 'ignorelist').split()
        if options.config.has_option('rules', 'excludelist'):
            self.excludelist = options.config.get('rules', 'excludelist').split()
        if options.config.has_option('rules', 'includelist'):
            self.includelist = options.config.get('rules', 'includelist').split()
        if options.config.has_option('rules', 'protectlist'):
            self.protectlist = options.config.get('rules', 'protectlist').split()

        # merge the excludelist (script generated) to the ignorelist (manually maintained)
        self.ignorelist = self.ignorelist + self.excludelist

        if options.config.has_option('rules', 'substitutions'):
            #At present this is a simple multi-line format
            #one substitution per line
            #format:
            #  missing-build  build-to-substitute
            #TODO: allow more robust substitutions
            for line in options.config.get('rules', 'substitutions').splitlines():
                line = line.strip()
                if line[:1] == "#":
                    #skip comment
                    continue
                if not line:
                    #blank
                    continue
                data = line.split()
                if len(data) != 2:
                    raise Exception, "Bad substitution: %s" % line
                match, replace = data
                self.substitutions[match] = replace

    def checkFilter(self, build, grey=None, default=True):
        """Check build against white/black/grey lists

        Whitelisting takes precedence over blacklisting. In our case, the whitelist
        is a list of exceptions to black/greylisting.

        If the build is greylisted, returns the value specified by the 'grey' parameter

        If the build matches nothing, returns the value specified in the 'default' parameter
        """
        if self.whitelist:
            for pattern in self.whitelist:
                if fnmatch.fnmatch(build.nvr, pattern):
                    return True
        if self.blacklist:
            for pattern in self.blacklist:
                if fnmatch.fnmatch(build.nvr, pattern):
                    return False
        if self.greylist:
            for pattern in self.greylist:
                if fnmatch.fnmatch(build.nvr, pattern):
                    return grey
        return default

    def rpmvercmp(self, (e1, v1, r1), (e2, v2, r2)):
        """find out which build is newer"""
        rc = rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
        if rc == 1:
            #first evr wins
            return 1
        elif rc == 0:
            #same evr
            return 0
        else:
            #second evr wins
            return -1

    def newerBuild(self, build, tag):
        #XXX:  secondary arches need a policy to say if we have newer build localy it will be the substitute
        localBuilds = session.listTagged(tag, inherit=True, package=str(build.name))
        newer = None
        parentevr = (str(build.epoch), build.version, build.release)
        parentnvr = (str(build.name), build.version, build.release)
        for b in localBuilds:
            latestevr = (str(b['epoch']), b['version'], b['release'])
            newestRPM = self.rpmvercmp(parentevr, latestevr)
            if options.debug:
                print "remote evr: %s  \nlocal evr: %s \nResult: %s" % (parentevr, latestevr, newestRPM)
            if newestRPM == -1:
                newer = b
            else:
                break
        #the local is newer
        if newer is not None:
            info = session.getBuild("%s-%s-%s" % (str(newer['name']), newer['version'], newer['release']))
            if info:
                build = LocalBuild(info)
                self.substitute_idx[parentnvr] = build
                return build
        return None

    def getSubstitute(self, nvr):
        build = self.substitute_idx.get(nvr)
        if not build:
            #see if remote has it
            info = remote.getBuild(nvr)
            if info:
                #see if we're already tracking it
                build = self.builds.get(info['id'])
                if not build:
                    build = TrackedBuild(info['id'], tracker=self)
            else:
                #remote doesn't have it
                #see if we have it locally
                info = session.getBuild(nvr)
                if info:
                    build = LocalBuild(info)
                else:
                    build = None
            self.substitute_idx[nvr] = build
        return build

    def scanBuild(self, build_id, from_build=None, depth=0, tag=None):
        """Recursively scan a build and its dependencies"""
        #print build_id
        build = self.builds.get(build_id)
        if build:
            #already scanned
            if from_build:
                build.addChild(from_build.id)
            #There are situations where, we'll need to go forward anyway:
            # - if we were greylisted before, and depth > 0 now
            # - if we're being substituted and depth is 0
            if not (depth > 0 and build.state == 'grey') \
                    and not (depth == 0 and build.substitute):
                return build
        else:
            child_id = None
            if from_build:
                child_id = from_build.id
            build = TrackedBuild(build_id, child=child_id, tracker=self)
            self.builds[build_id] = build
        if from_build:
            tail = " (from %s)" % from_build.nvr
        else:
            tail = ""
        head = " " * depth
        for ignored in self.ignorelist:
            if (build.name == ignored) or fnmatch.fnmatch(build.name, ignored):
                print "%sIgnored Build: %s%s" % (head, build.nvr, tail)
                build.setState('ignore')
                return build
        check = self.checkFilter(build, grey=None)
        if check is None:
            #greylisted builds are ok as deps, but not primary builds
            if depth == 0:
                print "%sGreylisted build %s%s" % (head, build.nvr, tail)
                build.setState('grey')
                return build
            #get rid of 'grey' state (filter will not be checked again)
            build.updateState()
        elif not check:
            print "%sBlocked build %s%s" % (head, build.nvr, tail)
            build.setState('blocked')
            return build
        #make sure we dont have the build name protected
        if build.name not in self.protectlist:
            #check to see if a substition applies
            replace = self.substitutions.get(build.nvr)
            if replace:
                build.substitute = replace
                if depth > 0:
                    print "%sDep replaced: %s->%s" % (head, build.nvr, replace)
                    return build
            if options.prefer_new and (depth > 0) and (tag is not None) and not (build.state == "common"):
                latestBuild = self.newerBuild(build, tag)
                if latestBuild != None:
                    build.substitute = latestBuild.nvr
                    print "%sNewer build replaced: %s->%s" % (head, build.nvr, latestBuild.nvr)
                    return build
        else:
            print "%sProtected Build: %s" % (head, build.nvr)
        if build.state == "common":
            #we're good
            if build.rebuilt:
                print "%sCommon build (rebuilt) %s%s" % (head, build.nvr, tail)
            else:
                print "%sCommon build %s%s" % (head, build.nvr, tail)
        elif build.state == 'pending':
            print "%sRebuild in progress: %s%s" % (head, build.nvr, tail)
        elif build.state == "broken":
            #The build already exists locally, but is somehow invalid.
            #We should not replace it automatically. An admin can reset it
            #if that is the correct thing. A substitution might also be in order
            print "%sWarning: build exists, but is invalid: %s%s" % (head, build.nvr, tail)
        #
        #  !! Cases where importing a noarch is /not/ ok must occur
        #     before this point
        #
        elif (options.import_noarch or options.import_noarch_only) and build.isNoarch():
            self.importBuild(build, tag)
        elif options.import_noarch_only and not build.isNoarch():
            print "%sSkipping archful build: %s" % (head, build.nvr)
        elif build.state == "noroot":
            #Can't rebuild it, this is what substitutions are for
            print "%sWarning: no buildroot data for %s%s" % (head, build.nvr, tail)
        elif build.state == 'brokendeps':
            #should not be possible at this point
            print "Error: build reports brokendeps state before dep scan"
        elif build.state == "missing":
            #scan its deps
            print "%sMissing build %s%s. Scanning deps..." % (head, build.nvr, tail)
            newdeps = []
            # include extra local builds as deps.
            if self.includelist:
                for dep in self.includelist:
                    info = session.getBuild(dep)
                    if info:
                        print "%s Adding local Dep %s%s" % (head, dep, tail)
                        extradep = LocalBuild(info)
                        newdeps.append(extradep)
                    else:
                        print "%s Warning: could not find build for %s" % (head, dep)
            #don't actually set build.revised_deps until we finish the dep scan
            for dep_id in build.deps:
                dep = self.scanBuild(dep_id, from_build=build, depth=depth+1, tag=tag)
                if dep.name in self.ignorelist:
                    # we are not done dep solving yet.  but we dont want this dep in our buildroot
                    continue
                else:
                    if dep.substitute:
                        dep2 = self.getSubstitute(dep.substitute)
                        if isinstance(dep2, TrackedBuild):
                            self.scanBuild(dep2.id, from_build=build, depth=depth+1, tag=tag)
                        elif dep2 is None:
                            #dep is missing on both local and remote
                            print "%sSubstitute dep unavailable: %s" % (head, dep2.nvr)
                            #no point in continuing
                            break
                        #otherwise dep2 should be LocalBuild instance
                        newdeps.append(dep2)
                    elif dep.state in ('broken', 'brokendeps', 'noroot', 'blocked'):
                        #no point in continuing
                        build.setState('brokendeps')
                        print "%sCan't rebuild %s, %s is %s" % (head, build.nvr, dep.nvr, dep.state)
                        newdeps = None
                        break
                    else:
                        newdeps.append(dep)
                    # set rebuild order as we go
                    # we do this /after/ the recursion, so our deps have a lower order number
                    self.rebuild_order += 1
                    build.order = self.rebuild_order
            build.revised_deps = newdeps
        #scanning takes a long time, might as well start builds if we can
        self.checkJobs(tag)
        self.rebuildMissing()
        if len(self.builds) % 50 == 0:
            self.report()
        return build

    def scanTag(self, tag):
        """Scan the latest builds in a remote tag"""
        taginfo = remote.getTag(tag)
        builds = remote.listTagged(taginfo['id'], latest=True)
        for build in builds:
            for retry in xrange(10):
                try:
                    self.scanBuild(build['id'], tag=tag)
                    if options.first_one:
                        return
                except (socket.timeout, socket.error):
                    print "retry"
                    continue
                break
            else:
                print "Error: unable to scan %(name)s-%(version)s-%(release)s" % build
                continue

    def _importURL(self, url, fn):
        """Import an rpm directly from a url"""
        serverdir = _unique_path('koji-shadow')
        if options.link_imports:
            #bit of a hack, but faster than uploading
            dst = "%s/%s/%s" % (koji.pathinfo.work(), serverdir, fn)
            old_umask = os.umask(002)
            try:
                koji.ensuredir(os.path.dirname(dst))
                os.chown(os.path.dirname(dst), 48, 48) #XXX - hack
                print "Downloading %s to %s" % (url, dst)
                fsrc = urllib2.urlopen(url)
                fdst = file(fn, 'w')
                shutil.copyfileobj(fsrc, fdst)
                fsrc.close()
                fdst.close()
            finally:
                os.umask(old_umask)
        else:
            #TODO - would be possible, using uploadFile directly, to upload without writing locally.
            #for now, though, just use uploadWrapper
            koji.ensuredir(options.workpath)
            dst = "%s/%s" % (options.workpath, fn)
            print "Downloading %s to %s..." % (url, dst)
            fsrc = urllib2.urlopen(url)
            fdst = file(dst, 'w')
            shutil.copyfileobj(fsrc, fdst)
            fsrc.close()
            fdst.close()
            print "Uploading %s..." % dst
            session.uploadWrapper(dst, serverdir, blocksize=65536)
        session.importRPM(serverdir, fn)

    def importBuild(self, build, tag=None):
        '''import a build from remote hub'''
        if not build.srpm:
            print "No srpm for build %s, skipping import" % build.nvr
            #TODO - support no-src imports here
            return False
        if not options.remote_topurl:
            print "Skipping import of %s, remote_topurl not specified" % build.nvr
            return False
        pathinfo = koji.PathInfo(options.remote_topurl)
        build_url = pathinfo.build(build.info)
        url = "%s/%s" % (pathinfo.build(build.info), pathinfo.rpm(build.srpm))
        fname = "%s.src.rpm" % build.nvr
        self._importURL(url, fname)
        for rpminfo in build.rpms:
            if rpminfo['arch'] == 'src':
                #already imported above
                continue
            relpath = pathinfo.rpm(rpminfo)
            url = "%s/%s" % (build_url, relpath)
            fname = os.path.basename(relpath)
            self._importURL(url, fname)
        build.updateState()
        if options.tag_build and not tag == None:
            self.tagSuccessful(build.nvr, tag)
        return True

    def scan(self):
        """Scan based on config file"""
        to_scan = []
        alltags = remote.listTags()

    def rebuild(self, build):
        """Rebuild a remote build using closest possible buildroot"""
        #first check that we can
        if build.state != 'missing':
            print "Can't rebuild %s. state=%s" % (build.nvr, build.state)
            return
        #deps = []
        #for build_id in build.deps:
        #    dep = self.builds.get(build_id)
        #    if not dep:
        #        print "Missing dependency %i for %s. Not scanned?" % (build_id, build.nvr)
        #        return
        #    if dep.state != 'common':
        #        print "Dependency missing for %s: %s (%s)" % (build.nvr, dep.nvr, dep.state)
        #        return
        #    deps.append(dep)
        deps = build.revised_deps
        if deps is None:
            print "Can't rebuild %s" % build.nvr
            return
        if options.test:
            print "Skipping rebuild of %s (test mode)" % build.nvr
            return
        #check/create tag
        our_tag = "SHADOWBUILD-%s" % build.br_tag
        taginfo = session.getTag(our_tag)
        parents = None
        if not taginfo:
            #XXX - not sure what is best here
            #how do we pick arches?  for now just hardcoded
            #XXX this call for perms is stupid, but it's all we've got
            perm_id = None
            for data in session.getAllPerms():
                if data['name'] == 'admin':
                    perm_id = data['id']
                    break
            session.createTag(our_tag, perm=perm_id, arches=options.arches)
            taginfo = session.getTag(our_tag, strict=True)
            #we don't need a target, we trigger our own repo creation and
            #pass that repo_id to the build call
            #session.createBuildTarget(taginfo['name'], taginfo['id'], taginfo['id'])
        else:
            parents = session.getInheritanceData(taginfo['id'])
            if parents:
                print "Warning: shadow build tag has inheritance"
        #check package list
        pkgs = {}
        for pkg in session.listPackages(tagID=taginfo['id']):
            pkgs[pkg['package_name']] = pkg
        missing_pkgs = []
        for dep in deps:
            name = dep.info['name']
            if not pkgs.has_key(name):
                #guess owner
                owners = {}
                for pkg in session.listPackages(pkgID=name):
                    owners.setdefault(pkg['owner_id'], []).append(pkg)
                if owners:
                    order = [(len(v), k) for k, v in owners.iteritems()]
                    order.sort()
                    owner = order[-1][1]
                else:
                    #just use ourselves
                    owner = session.getLoggedInUser()['id']
                missing_pkgs.append((name, owner))
        #check build list
        cur_builds = {}
        for binfo in session.listTagged(taginfo['id']):
            #index by name in tagging order (latest first)
            cur_builds.setdefault(binfo['name'], []).append(binfo)
        to_untag = []
        to_tag = []
        for dep in deps:
            #XXX - assuming here that there is only one dep per 'name'
            #      may want to check that this is true
            cur_order = cur_builds.get(dep.info['name'], [])
            tagged = False
            for binfo in cur_order:
                if binfo['nvr'] == dep.nvr:
                    tagged = True
                    #may not be latest now, but it will be after we do all the untagging
                else:
                    # note that the untagging keeps older builds from piling up. In a sense
                    # we're gc-pruning this tag ourselves every pass.
                    to_untag.append(binfo)
            if not tagged:
                to_tag.append(dep)
        #TODO - "add-on" packages
        #       for handling arch-specific deps that may not show up on remote
        #       e.g. elilo or similar
        #       these extra packages should be added to tag, but not the build group
        #TODO - local extra builds
        #       a configurable mechanism to add specific local builds to the buildroot
        drop_groups = []
        build_group = None
        for group in session.getTagGroups(taginfo['id']):
            if group['name'] == 'build':
                build_group = group
            else:
                # we should have no other groups but build
                print "Warning: found stray group: %s" % group
                drop_groups.append(group['name'])
        if build_group:
            #fix build group package list based on base of build to shadow
            needed = dict([(n, 1) for n in build.base])
            current = dict([(p['package'], 1) for p in build_group['packagelist']])
            add_pkgs = [n for n in needed if not current.has_key(n)]
            drop_pkgs = [n for n in current if not needed.has_key(n)]
            #no group deps needed/allowed
            drop_deps = [(g['name'], 1) for g in build_group['grouplist']]
            if drop_deps:
                print "Warning: build group had deps: %r" % build_group
        else:
            add_pkgs = build.base
            drop_pkgs = []
            drop_deps = []
        #update package list, tagged packages, and groups in one multicall/transaction
        #(avoid useless repo regens)
        session.multicall = True
        for name, owner in missing_pkgs:
            session.packageListAdd(taginfo['id'], name, owner=owner)
        for binfo in to_untag:
            session.untagBuildBypass(taginfo['id'], binfo['id'])
        for dep in to_tag:
            session.tagBuildBypass(taginfo['id'], dep.nvr)
            #shouldn't need force here
        #set groups data
        if not build_group:
            # build group not present. add it
            session.groupListAdd(taginfo['id'], 'build', force=True)
            #using force in case group is blocked. This shouldn't be the case, but...
        for pkg_name in drop_pkgs:
            #in principal, our tag should not have inheritance, so the remove call is the right thing
            session.groupPackageListRemove(taginfo['id'], 'build', pkg_name)
        for pkg_name in add_pkgs:
            session.groupPackageListAdd(taginfo['id'], 'build', pkg_name)
            #we never add any blocks, so forcing shouldn't be required
        #TODO - adjust extra_arches for package to build
        #get event id to facilitate waiting on repo
        #       not sure if getLastEvent is good enough
        #       short of adding a new call, perhaps use getLastEvent together with event of
        #       current latest repo for tag
        session.getLastEvent()
        results = session.multiCall(strict=True)
        event_id = results[-1][0]['id']
        #TODO - verify / check results ?
        task_id = session.newRepo(our_tag, event=event_id)
        #TODO - upload src
        #       [?] use remote SCM url (if avail)?
        src = build.getSource()
        if not src:
            print "Couldn't get source for %s" % build.nvr
            return None
        #wait for repo task
        print "Waiting on newRepo task %i" % task_id
        while True:
            tinfo = session.getTaskInfo(task_id)
            tstate = koji.TASK_STATES[tinfo['state']]
            if tstate == 'CLOSED':
                break
            elif tstate in ('CANCELED', 'FAILED'):
                print "Error: failed to generate repo"
                return None
            #add a timeout?
        #TODO       ...and verify repo
        repo_id, event_id = session.getTaskResult(task_id)
        #kick off build
        task_id = session.build(src, None, opts={'repo_id': repo_id}, priority=options.priority)
        return task_id

    def report(self):
        print "-- %s --" % time.asctime()
        self.report_brief()
        for state in ('broken', 'noroot', 'blocked'):
            builds = self.state_idx[state].values()
            not_replaced = [b for b in builds if not b.substitute]
            n_replaced = len(builds) - len(not_replaced)
            print "%s: %i (+%i replaced)" % (state, len(not_replaced), n_replaced)
            if not_replaced and len(not_replaced) < 8:
                print '', ' '.join([b.nvr for b in not_replaced])
        #generate a report of the most frequent problem deps
        problem_counts = {}
        for build in self.state_idx['brokendeps'].values():
            for dep_id in build.deps:
                dep = self.builds.get(dep_id)
                if not dep:
                    #unscanned
                    #possible because we short circuit the earlier scan on problems
                    #we don't really know if this one is a problem or not, so just
                    #skip it.
                    continue
                if dep.state in ('common', 'pending', 'missing'):
                    #not a problem
                    continue
                nvr = dep.nvr
                if dep.substitute:
                    dep2 = self.getSubstitute(dep.substitute)
                    if dep2:
                        #we have a substitution, so not a problem
                        continue
                    #otherwise the substitution is the problem
                    nvr = dep.substitute
                problem_counts.setdefault(nvr, 0)
                problem_counts[nvr] += 1
        order = [(c, nvr) for (nvr, c) in problem_counts.iteritems()]
        if order:
            order.sort()
            order.reverse()
            #print top 5 problems
            print "-- top problems --"
            for (c, nvr) in order[:5]:
                print " %s (%i)" % (nvr, c)

    def report_brief(self):
        N = len(self.builds)
        states = self.state_idx.keys()
        states.sort()
        parts = ["%s: %i" % (s, len(self.state_idx[s])) for s in states]
        parts.append("total: %i" % N)
        print ' '.join(parts)

    def _print_builds(self, mylist):
        """small helper function for output"""
        for build in mylist:
            print "    %s (%s)" % (build.nvr, build.state)

    def checkJobs(self, tag=None):
        """Check outstanding jobs. Return true if anything changes"""
        ret = False
        for build_id, build in self.state_idx['pending'].items():
            #check pending builds
            if not build.task_id:
                print "No task id recorded for %s" % build.nvr
                build.updateState()
                ret = True
            info = session.getTaskInfo(build.task_id)
            if not info:
                print "No such task: %i (build %s)" % (build.task_id, build.nvr)
                build.updateState()
                ret = True
                continue
            state = koji.TASK_STATES[info['state']]
            if state in ('CANCELED', 'FAILED'):
                print "Task %i is %s (build %s)" % (build.task_id, state, build.nvr)
                #we have to set the state to broken manually (updateState will mark
                #a failed build as missing)
                build.setState('broken')
                ret = True
            elif state == 'CLOSED':
                print "Task %i complete (build %s)" % (build.task_id, build.nvr)
                if options.tag_build and not tag == None:
                    self.tagSuccessful(build.nvr, tag)
                build.updateState()
                ret = True
                if build.state != 'common':
                    print "Task %i finished, but %s still missing" \
                            % (build.task_id, build.nvr)
        return ret

    def checkBuildDeps(self, build):
        #check deps
        if build.revised_deps is None:
            #print "No revised deplist yet for %s" % build.nvr
            return False
        problem = [x for x in build.revised_deps
                         if x.state in ('broken', 'brokendeps', 'noroot', 'blocked')]
        if problem:
            print "Can't rebuild %s, missing %i deps" % (build.nvr, len(problem))
            build.setState('brokendeps')
            self._print_builds(problem)
            return False
        not_common = [x for x in build.revised_deps
                         if x.state not in ('common', 'local')]
        if not_common:
            #could be missing or still building or whatever
            #print "Still missing %i revised deps for %s" % (len(not_common), build.nvr)
            return False
        #otherwise, we should be good to rebuild
        return True

    def rebuildMissing(self):
        """Initiate rebuilds for missing builds, if possible.

        Returns True if any builds were attempted"""
        ret = False
        if options.max_jobs and len(self.state_idx['pending']) >= options.max_jobs:
            return ret
        missing = [(b.order, b.id, b) for b in self.state_idx['missing'].itervalues()]
        missing.sort()
        for order, build_id, build in missing:
            if not self.checkBuildDeps(build):
                continue
            #otherwise, we should be good to rebuild
            print "rebuild: %s" % build.nvr
            task_id = self.rebuild(build)
            ret = True
            if options.test:
                #pretend build is available
                build.setState('common')
            elif not task_id:
                #something went wrong setting up the rebuild
                print "Did not get a task for %s" % build.nvr
                build.setState('broken')
            else:
                # build might not show up as 'BUILDING' immediately, so we
                # set this state manually rather than by updateState
                build.task_id = task_id
                build.setState('pending')
            if options.max_jobs and len(self.state_idx['pending']) >= options.max_jobs:
                if options.debug:
                    print "Maximum number of jobs reached."
                break
        return ret

    def runRebuilds(self, tag=None):
        """Rebuild missing builds"""
        print "Determining rebuild order"
        #using self.state_idx to track build states
        #make sure state_idx has at least these states
        initial_avail = len(self.state_idx['common'])
        self.report_brief()
        while True:
            if (not self.state_idx['missing'] and not self.state_idx['pending']) or \
               (options.prefer_new and not self.state_idx['pending']):
                #we're done
                break
            changed1 = self.checkJobs(tag)
            changed2 = self.rebuildMissing()
            if not changed1 and not changed2:
                time.sleep(30)
                continue
            self.report_brief()
        print "Rebuilt %i builds" % (len(self.state_idx['common']) - initial_avail)

    def tagSuccessful(self, nvr, tag):
        """tag completed builds into final tags"""
        #TODO: check if there are other reasons why tagging may fail and handle them
        try:
            session.tagBuildBypass(tag, nvr)
            print "tagged %s to %s" % (nvr, tag)
        except koji.TagError:
            print "NOTICE: %s already tagged in %s" % (nvr, tag)


def main(args):
    tracker = BuildTracker()
    try:
        tag = args[0]
    except IndexError:
        tag = None

    if options.build:
        binfo = remote.getBuild(options.build, strict=True)
        tracker.scanBuild(binfo['id'], tag=tag)
    else:
        if tag is None:
            print "Tag is required"
            return
        else:
            print "Working on tag %s" % (tag)
            tracker.scanTag(tag)
    tracker.report()
    tracker.runRebuilds(tag)


if __name__ == "__main__":

    options, args = get_options()

    session_opts = {}
    for k in ('user', 'password', 'krbservice', 'debug_xmlrpc', 'debug'):
        session_opts[k] = getattr(options, k)
    session = koji.ClientSession(options.server, session_opts)
    if not options.noauth:
        activate_session(session)
    #XXX - sane auth
    #XXX - config!
    remote_opts = {'anon_retry': True}
    for k in ('debug_xmlrpc', 'debug'):
        remote_opts[k] = getattr(options, k)
    remote = koji.ClientSession(options.remote, remote_opts)
    rv = 0
    try:
        rv = main(args)
        if not rv:
            rv = 0
    except KeyboardInterrupt:
        pass
    except SystemExit:
        rv = 1
    #except:
    #    if options.debug:
    #        raise
    #    else:
    #        exctype, value = sys.exc_info()[:2]
    #        rv = 1
    #        print "%s: %s" % (exctype, value)
    try:
        session.logout()
    except:
        pass
    sys.exit(rv)