summaryrefslogtreecommitdiff
path: root/backends/yum/yumBackend.py
blob: f800890caefe6d6ccc6f1b5c70cf2de7bb837da6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
#!/usr/bin/python
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Copyright (C) 2007-2009
#    Tim Lauridsen <timlau@fedoraproject.org>
#    Seth Vidal <skvidal@fedoraproject.org>
#    Luke Macken <lmacken@redhat.com>
#    James Bowes <jbowes@dangerouslyinc.com>
#    Robin Norwood <rnorwood@redhat.com>
#
# Copyright (C) 2007-2010
#    Richard Hughes <richard@hughsie.com>
#
# Copyright (C) 2009
#    MediaGrabber, based on the logic of pirut by Jeremy Katz <katzj@redhat.com>
#    Muayyad Alsadi <alsadi@ojuba.org>

# imports
from packagekit.backend import *
from packagekit.progress import *
from packagekit.enums import *
from packagekit.package import PackagekitPackage
import yum
from urlgrabber.progress import BaseMeter, format_number
from urlgrabber.grabber import URLGrabber, URLGrabError
from yum.rpmtrans import RPMBaseCallback
from yum.constants import *
from yum.update_md import UpdateMetadata
from yum.callbacks import *
from yum.misc import prco_tuple_to_string, unique
from yum.packages import YumLocalPackage, parsePackages
from yum.packageSack import MetaSack
import rpmUtils
import exceptions
import types
import signal
import time
import os.path
import logging
import socket
import gio

import tarfile
import tempfile
import shutil
import ConfigParser

from yumFilter import *
from yumComps import *

# Global vars
yumbase = None
progress = PackagekitProgress()  # Progress object to store the progress

MetaDataMap = {
    'repomd'        : STATUS_DOWNLOAD_REPOSITORY,
    'primary'       : STATUS_DOWNLOAD_PACKAGELIST,
    'filelists'     : STATUS_DOWNLOAD_FILELIST,
    'other'         : STATUS_DOWNLOAD_CHANGELOG,
    'comps'         : STATUS_DOWNLOAD_GROUP,
    'updateinfo'    : STATUS_DOWNLOAD_UPDATEINFO
}

StatusPercentageMap = {
    STATUS_DEP_RESOLVE : 5,
    STATUS_DOWNLOAD    : 10,
    STATUS_SIG_CHECK   : 40,
    STATUS_TEST_COMMIT : 45,
    STATUS_INSTALL     : 55,
    STATUS_CLEANUP     : 95
}

# this isn't defined in yum as it's only used in the rollback plugin
TS_REPACKAGING = 'repackaging'

# Map yum transactions with pk info enums
TransactionsInfoMap = {
    TS_UPDATE       : INFO_UPDATING,
    TS_ERASE        : INFO_REMOVING,
    TS_INSTALL      : INFO_INSTALLING,
    TS_TRUEINSTALL  : INFO_INSTALLING,
    TS_OBSOLETED    : INFO_OBSOLETING,
    TS_OBSOLETING   : INFO_INSTALLING,
    TS_UPDATED      : INFO_CLEANUP
}

# Map yum transactions with pk state enums
TransactionsStateMap = {
    TS_UPDATE       : STATUS_UPDATE,
    TS_ERASE        : STATUS_REMOVE,
    TS_INSTALL      : STATUS_INSTALL,
    TS_TRUEINSTALL  : STATUS_INSTALL,
    TS_OBSOLETED    : STATUS_OBSOLETE,
    TS_OBSOLETING   : STATUS_INSTALL,
    TS_UPDATED      : STATUS_CLEANUP,
    TS_REPACKAGING  : STATUS_REPACKAGING
}

class GPGKeyNotImported(exceptions.Exception):
    pass

def sigquit(signum, frame):
    if yumbase:
        yumbase.closeRpmDB()
        yumbase.doUnlock(YUM_PID_FILE)
    sys.exit(1)

def _to_unicode(txt, encoding='utf-8'):
    if isinstance(txt, basestring):
        if not isinstance(txt, unicode):
            txt = unicode(txt, encoding, errors='replace')
    return txt

def _get_package_ver(po):
    ''' return the a ver as epoch:version-release or version-release, if epoch=0'''
    ver = ''
    if po.epoch != '0':
        ver = "%s:%s-%s" % (po.epoch, po.version, po.release)
    elif po.release:
        ver = "%s-%s" % (po.version, po.release)
    elif po.version:
        ver = po.version
    return ver

def _format_package_id(package_id):
    """
    Convert 'hal;0.5.8;i386;fedora' to 'hal-0.5.8-fedora(i386)'
    """
    parts = package_id.split(';')
    if len(parts) != 4:
        return "incorrect package_id: %s" % package_id
    return "%s-%s(%s)%s" % (parts[0], parts[1], parts[2], parts[3])

def _format_str(text):
    """
    Convert a multi line string to a list separated by ';'
    """
    if text:
        lines = text.split('\n')
        return ";".join(lines)
    else:
        return ""

def _format_list(lst):
    """
    Convert a multi line string to a list separated by ';'
    """
    if lst:
        return ";".join(lst)
    else:
        return ""

def _getEVR(idver):
    '''
    get the e, v, r from the package id version
    '''
    cpos = idver.find(':')
    if cpos != -1:
        epoch = idver[:cpos]
        idver = idver[cpos+1:]
    else:
        epoch = '0'
    try:
        (version, release) = tuple(idver.split('-'))
    except ValueError, e:
        version = '0'
        release = '0'
    return epoch, version, release

def _truncate(text, length, etc='...'):
    if len(text) < length:
        return text
    else:
        return text[:length] + etc

def _is_development_repo(repo):
    if repo.endswith('-debuginfo'):
        return True
    if repo.endswith('-debug'):
        return True
    if repo.endswith('-development'):
        return True
    if repo.endswith('-source'):
        return True
    return False

def _format_msgs(msgs):
    if isinstance(msgs, basestring):
        msgs = msgs.split('\n')

    # yum can pass us structures (!) in the message field
    try:
        text = ";".join(msgs)
    except exceptions.TypeError, e:
        text = str(msgs)
    except Exception, e:
        text = _format_str(traceback.format_exc())

    text = _truncate(text, 1024)
    text = text.replace(";Please report this error in bugzilla", "")
    text = text.replace("Missing Dependency: ", "")
    text = text.replace(" (installed)", "")
    return text

def _get_cmdline_for_pid(pid):
    if not pid:
        return "invalid"
    cmdlines = open("/proc/%d/cmdline" % pid).read().split('\0')
    cmdline = " ".join(cmdlines).strip(' ')
    return cmdline

class PackageKitYumBackend(PackageKitBaseBackend, PackagekitPackage):

    # Packages there require a reboot
    rebootpkgs = ("kernel", "kernel-smp", "kernel-xen-hypervisor", "kernel-PAE",
              "kernel-xen0", "kernel-xenU", "kernel-xen", "kernel-xen-guest",
              "glibc", "hal", "dbus", "xen")

    def __init__(self, args, lock=True):
        signal.signal(signal.SIGQUIT, sigquit)
        PackageKitBaseBackend.__init__(self, args)
        try:
            self.yumbase = PackageKitYumBase(self)
        except PkError, e:
            self.error(e.code, e.details)

        # load the config file
        config = ConfigParser.ConfigParser()
        try:
            config.read('/etc/PackageKit/Yum.conf')
        except Exception, e:
            raise PkError(ERROR_REPO_CONFIGURATION_ERROR, "Failed to load Yum.conf: %s" % _to_unicode(e))

        # if this key does not exist, it's not fatal
        try:
            self.system_packages = config.get('Backend', 'SystemPackages').split(';')
        except ConfigParser.NoOptionError, e:
            self.system_packages = []
        except Exception, e:
            raise PkError(ERROR_REPO_CONFIGURATION_ERROR, "Failed to load Yum.conf: %s" % _to_unicode(e))
        try:
            self.infra_packages = config.get('Backend', 'InfrastructurePackages').split(';')
        except ConfigParser.NoOptionError, e:
            self.infra_packages = []
        except Exception, e:
            raise PkError(ERROR_REPO_CONFIGURATION_ERROR, "Failed to load Yum.conf: %s" % _to_unicode(e))

        # get the lock early
        if lock:
            self.doLock()

        self.package_summary_cache = {}
        self.comps = yumComps(self.yumbase)
        if not self.comps.connect():
            self.refresh_cache(True)
            if not self.comps.connect():
                self.error(ERROR_GROUP_LIST_INVALID, 'comps categories could not be loaded')

        # timeout a socket after this much time
        timeout = 15.0
        socket.setdefaulttimeout(timeout)

        # use idle bandwidth by setting congestion control algorithm to TCP Low Priority
        if self.background:
            socket.TCP_CONGESTION = 13
            try:
                sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_CONGESTION, "lp")
            except socket.error, e:
                pass

        # we only check these types
        self.transaction_sig_check_map = [TS_UPDATE, TS_INSTALL, TS_TRUEINSTALL, TS_OBSOLETING]

        # this is global so we can catch sigquit and closedown
        yumbase = self.yumbase
        try:
            self._setup_yum()
        except PkError, e:
            self.error(e.code, e.details)

    def details(self, package_id, package_license, group, desc, url, bytes):
        '''
        Send 'details' signal
        @param id: The package ID name, e.g. openoffice-clipart;2.6.22;ppc64;fedora
        @param license: The license of the package
        @param group: The enumerated group
        @param desc: The multi line package description
        @param url: The upstream project homepage
        @param bytes: The size of the package, in bytes
        convert the description to UTF before sending
        '''
        desc = _to_unicode(desc)
        PackageKitBaseBackend.details(self, package_id, package_license, group, desc, url, bytes)

    def package(self, package_id, status, summary):
        '''
        send 'package' signal
        @param info: the enumerated INFO_* string
        @param id: The package ID name, e.g. openoffice-clipart;2.6.22;ppc64;fedora
        @param summary: The package Summary
        convert the summary to UTF before sending
        '''
        summary = _to_unicode(summary)

        # maintain a dictionary of the summary text so we can use it when rpm
        # is giving up package names without summaries
        (name, idver, a, repo) = self.get_package_from_id(package_id)
        if len(summary) > 0:
            self.package_summary_cache[name] = summary
        else:
            if self.package_summary_cache.has_key(name):
                summary = self.package_summary_cache[name]

        PackageKitBaseBackend.package(self, package_id, status, summary)

    def category(self, parent_id, cat_id, name, summary, icon):
        '''
        Send 'category' signal
        parent_id : A parent id, e.g. "admin" or "" if there is no parent
        cat_id    : a unique category id, e.g. "admin;network"
        name      : a verbose category name in current locale.
        summery   : a summary of the category in current locale.
        icon      : an icon name to represent the category
        '''
        name = _to_unicode(name)
        summary = _to_unicode(summary)
        PackageKitBaseBackend.category(self, parent_id, cat_id, name, summary, icon)

    def doLock(self):
        ''' Lock Yum'''
        retries = 0
        cmdline = None
        while not self.isLocked():
            try: # Try to lock yum
                self.yumbase.doLock(YUM_PID_FILE)
                PackageKitBaseBackend.doLock(self)
                self.allow_cancel(False)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except yum.Errors.LockError, e:
                self.allow_cancel(True)
                self.status(STATUS_WAITING_FOR_LOCK)

                # get the command line of the other thing
                if not cmdline:
                    cmdline = _get_cmdline_for_pid(e.pid)

                # if it's us, kill it as it's from another instance where the daemon crashed
                if cmdline.find("yumBackend.py") != -1:
                    self.message(MESSAGE_BACKEND_ERROR, "killing pid %i, as old instance" % e.pid)
                    os.kill(e.pid, signal.SIGQUIT)

                # wait a little time, and try again
                time.sleep(2)
                retries += 1

                # give up, and print process information
                if retries > 100:
                    msg = "The other process has the command line '%s' (PID %i)" % (cmdline, e.pid)
                    self.error(ERROR_CANNOT_GET_LOCK, "Yum is locked by another application. %s" % _to_unicode(msg))

    def unLock(self):
        ''' Unlock Yum'''
        if self.isLocked():
            PackageKitBaseBackend.unLock(self)
            try:
                self.yumbase.closeRpmDB()
                self.yumbase.doUnlock(YUM_PID_FILE)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

    def _do_meta_package_search(self, filters, values):
        grps = self.comps.get_meta_packages()
        for grpid in grps:
            for value in values:
                if value in grpid:
                    self._show_meta_package(grpid, filters)

    def set_locale(self, code):
        '''
        Implement the set-locale functionality
        Needed to be implemented in a sub class
        '''
        self.lang = code

    def _do_search(self, searchlist, filters, values):
        '''
        Search for yum packages
        @param searchlist: The yum package fields to search in
        @param filters: package types to search (all, installed, available)
        @param values: key to seach for
        '''
        pkgfilter = YumFilter(filters)
        package_list = []

        # get collection objects
        if FILTER_NOT_COLLECTIONS not in filters:
            self._do_meta_package_search(filters, values)

        # return, as we only want collection objects
        if FILTER_COLLECTIONS not in filters:
            installed = []
            available = []
            try:
                res = self.yumbase.searchGenerator(searchlist, values)
                for (pkg, inst) in res:
                    if pkg.repo.id.startswith('installed'):
                        installed.append(pkg)
                    else:
                        available.append(pkg)
            except yum.Errors.RepoError, e:
                raise PkError(ERROR_NO_CACHE, "failed to use search generator: %s" %_to_unicode(e))
            except exceptions.IOError, e:
                raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            else:
                pkgfilter.add_installed(installed)
                pkgfilter.add_available(available)

                # we couldn't do this when generating the list
                package_list = pkgfilter.get_package_list()
                self._show_package_list(package_list)

    def _show_package_list(self, lst):
        for (pkg, status) in lst:
            self._show_package(pkg, status)

    def search_name(self, filters, values):
        '''
        Implement the search-name functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)

        searchlist = ['name']
        self.status(STATUS_QUERY)
        try:
            self.yumbase.doConfigSetup(errorlevel=0, debuglevel=0)# Setup Yum Config
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        try:
            self._do_search(searchlist, filters, values)
        except PkError, e:
            self.error(e.code, e.details, exit=False)

    def search_details(self, filters, values):
        '''
        Implement the search-details functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        try:
            self.yumbase.doConfigSetup(errorlevel=0, debuglevel=0)# Setup Yum Config
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)

        searchlist = ['name', 'summary', 'description', 'group']
        self.status(STATUS_QUERY)
        try:
            self._do_search(searchlist, filters, values)
        except PkError, e:
            self.error(e.code, e.details, exit=False)

    def _get_installed_from_names(self, name_list):
        found = []
        for package in name_list:
            try:
                pkgs = self.yumbase.rpmdb.searchNevra(name=package)
            except exceptions.IOError, e:
                raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            else:
                found.extend(pkgs)
        return found

    def _get_available_from_names(self, name_list):
        pkgs = None
        try:
            pkgs = self.yumbase.pkgSack.searchNames(names=name_list)
        except yum.Errors.RepoError, e:
            raise PkError(ERROR_NO_CACHE, "failed to search names: %s" %_to_unicode(e))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        return pkgs

    def _handle_repo_group_search_using_yumdb(self, repo_id, filters):
        """
        Handle the special repo groups
        """
        self.percentage(None)
        pkgfilter = YumFilter(filters)
        available = []
        installed = []

        pkgs = []
        # get installed packages that came from this repo
        try:
            pkgs = self.yumbase.rpmdb
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        for pkg in pkgs:
            if pkg.yumdb_info.get('from_repo') == repo_id:
                pkgfilter.add_installed([pkg])

        # find the correct repo
        try:
            repos = self.yumbase.repos.findRepos(repo_id)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        if len(repos) == 0:
            raise PkError(ERROR_REPO_NOT_FOUND, "cannot find repo %s" % repo_id)

        # the repo might have been disabled if it is no longer contactable
        if not repos[0].isEnabled():
            raise PkError(ERROR_PACKAGE_NOT_FOUND, '%s cannot be found as %s is disabled' % (repo_id, repos[0].id))

        # populate the sack with data
        try:
            self.yumbase.repos.populateSack(repo_id)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        for pkg in repos[0].sack:
            available.append(pkg)

        # add list to filter
        pkgfilter.add_installed(installed)
        pkgfilter.add_available(available)
        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)
        self.percentage(100)

    def _handle_repo_group_search(self, repo_id, filters):
        """
        Handle the special repo groups
        This is much slower than using _handle_repo_group_search_using_yumdb()
        as we have to resolve each package in the repo to see if it's
        installed.
        Of course, on RHEL5, there is no yumdb.
        """
        self.percentage(None)
        pkgfilter = YumFilter(filters)
        available = []
        installed = []

        # find the correct repo
        try:
            repos = self.yumbase.repos.findRepos(repo_id)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        if len(repos) == 0:
            raise PkError(ERROR_REPO_NOT_FOUND, "cannot find repo %s" % repo_id)
        print "found repos"

        # the repo might have been disabled if it is no longer contactable
        if not repos[0].isEnabled():
            raise PkError(ERROR_PACKAGE_NOT_FOUND, '%s cannot be found as %s is disabled' % (repo_id, repos[0].id))

        # populate the sack with data
        try:
            self.yumbase.repos.populateSack(repo_id)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        for pkg in repos[0].sack:
            try:
                instpo = self.yumbase.rpmdb.searchNevra(name=pkg.name, epoch=pkg.epoch, ver=pkg.ver, rel=pkg.rel, arch=pkg.arch)
            except exceptions.IOError, e:
                raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            if self._is_inst(pkg):
                installed.append(instpo[0])
            else:
                available.append(pkg)

        # add list to filter
        pkgfilter.add_installed(installed)
        pkgfilter.add_available(available)
        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)
        self.percentage(100)

    def _handle_newest(self, filters):
        """
        Handle the special newest group
        """
        self.percentage(None)
        pkgfilter = YumFilter(filters)
        pkgs = []
        try:
            ygl = self.yumbase.doPackageLists(pkgnarrow='recent')
            pkgs.extend(ygl.recent)
        except yum.Errors.RepoError, e:
            raise PkError(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            installed = []
            available = []
            for pkg in pkgs:
                try:
                    instpo = self.yumbase.rpmdb.searchNevra(name=pkg.name, epoch=pkg.epoch, ver=pkg.ver, rel=pkg.rel, arch=pkg.arch)
                except exceptions.IOError, e:
                    raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                if len(instpo) > 0:
                    installed.append(instpo[0])
                else:
                    available.append(pkg)

            # add list to filter
            pkgfilter.add_installed(installed)
            pkgfilter.add_available(available)
            package_list = pkgfilter.get_package_list()
            self._show_package_list(package_list)
            self.percentage(100)

    def _handle_collections(self, filters):
        """
        Handle the special collection group
        """
        # Fixme: Add some real code.
        self.percentage(None)
        collections = self.comps.get_meta_packages()
        if len(collections) == 0:
            raise PkError(ERROR_GROUP_LIST_INVALID, 'No groups could be found. A cache refresh should fix this.')

        pct = 20
        old_pct = -1
        step = (100.0 - pct) / len(collections)
        for col in collections:
            self._show_meta_package(col, filters)
            pct += step
            if int(pct) != int(old_pct):
                self.percentage(pct)
                old_pct = pct
        self.percentage(100)

    def _show_meta_package(self, grpid, filters):
        show_avail = FILTER_INSTALLED not in filters
        show_inst = FILTER_NOT_INSTALLED not in filters
        package_id = "%s;;;meta" % grpid
        try:
            grp = self.yumbase.comps.return_group(grpid)
        except yum.Errors.RepoError, e:
            raise PkError(ERROR_NO_CACHE, "failed to get groups from comps: %s" % _to_unicode(e))
        except yum.Errors.GroupsError, e:
            raise PkError(ERROR_GROUP_NOT_FOUND, "failed to find group '%s': %s" % (package_id, _to_unicode(e)))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            if grp:
                name = grp.nameByLang(self.lang)
                if grp.installed:
                    if show_inst:
                        self.package(package_id, INFO_COLLECTION_INSTALLED, name)
                else:
                    if show_avail:
                        self.package(package_id, INFO_COLLECTION_AVAILABLE, name)

    def search_group(self, filters, values):
        '''
        Implement the search-group functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.allow_cancel(True)
        try:
            self.yumbase.doConfigSetup(errorlevel=0, debuglevel=0)# Setup Yum Config
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        self.yumbase.conf.cache = 0 # TODO: can we just look in the cache?
        self.status(STATUS_QUERY)
        package_list = [] #we can't do emitting as found if we are post-processing
        pkgfilter = YumFilter(filters)

        # handle collections
        if GROUP_COLLECTIONS in values:
            try:
                self._handle_collections(filters)
            except PkError, e:
                self.error(e.code, e.details, exit=False)
            return

        # handle newest packages
        if GROUP_NEWEST in values:
            try:
                self._handle_newest(filters)
            except PkError, e:
                self.error(e.code, e.details, exit=False)
            return

        # handle repo groups
        if 'repo:' in values[0]:
            try:
                self._handle_repo_group_search_using_yumdb(values[0].replace('repo:',''), filters)
            except PkError, e:
                self.error(e.code, e.details, exit=False)
            return

        # for each search term
        for value in values:
            # handle dynamic groups (yum comps group)
            if value[0] == '@':
                cat_id = value[1:]
                 # get the packagelist for this group
                all_packages = self.comps.get_meta_package_list(cat_id)
            else: # this is an group_enum
                # get the packagelist for this group enum
                all_packages = self.comps.get_package_list(value)

            # group don't exits, just bail out
            if not all_packages:
                continue

            if FILTER_NOT_INSTALLED not in filters:
                try:
                    pkgfilter.add_installed(self._get_installed_from_names(all_packages))
                except PkError, e:
                    self.error(e.code, e.details, exit=False)
                    return

            if FILTER_INSTALLED not in filters:
                try:
                    pkgfilter.add_available(self._get_available_from_names(all_packages))
                except PkError, e:
                    self.error(e.code, e.details, exit=False)
                    return

        # we couldn't do this when generating the list
        package_list = pkgfilter.get_package_list()

        self.percentage(90)
        self._show_package_list(package_list)

        self.percentage(100)

    def get_packages(self, filters):
        '''
        Search for yum packages
        @param searchlist: The yum package fields to search in
        @param filters: package types to search (all, installed, available)
        '''
        self.status(STATUS_QUERY)
        self.allow_cancel(True)
        try:
            self.yumbase.doConfigSetup(errorlevel=0, debuglevel=0)# Setup Yum Config
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        self.yumbase.conf.cache = 0 # TODO: can we just look in the cache?

        package_list = [] #we can't do emitting as found if we are post-processing
        pkgfilter = YumFilter(filters)

        # Now show installed packages.
        try:
            pkgs = self.yumbase.rpmdb
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        pkgfilter.add_installed(pkgs)

        # Now show available packages.
        if FILTER_INSTALLED not in filters:
            try:
                pkgs = self.yumbase.pkgSack
            except yum.Errors.RepoError, e:
                self.error(ERROR_NO_CACHE, "failed to get package sack: %s" %_to_unicode(e), exit=False)
                return
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            else:
                pkgfilter.add_available(pkgs)

        # we couldn't do this when generating the list
        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)

    def search_file(self, filters, values):
        '''
        Implement the search-file functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_QUERY)

        #self.yumbase.conf.cache = 0 # TODO: can we just look in the cache?
        pkgfilter = YumFilter(filters)

        # Check installed for file
        if not FILTER_NOT_INSTALLED in filters:
            for value in values:
                try:
                    pkgs = self.yumbase.rpmdb.searchFiles(value)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                pkgfilter.add_installed(pkgs)

        # Check available for file
        if not FILTER_INSTALLED in filters:
            for value in values:
                try:
                    # we don't need the filelists as we're not globbing
                    pkgs = self.yumbase.pkgSack.searchFiles(value)
                except yum.Errors.RepoError, e:
                    self.error(ERROR_NO_CACHE, "failed to search sack: %s" %_to_unicode(e), exit=False)
                    return
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                else:
                    pkgfilter.add_available(pkgs)

        # we couldn't do this when generating the list
        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)

    def _get_provides_query(self, provides_type, value):
        # gets a list of provides

        # old standard
        if value.startswith("gstreamer0.10("):
            return [ value ]

        # new standard
        if provides_type == PROVIDES_CODEC:
            return [ "gstreamer0.10(%s)" % value ]
        if provides_type == PROVIDES_FONT:
            return [ "font(%s)" % value ]
        if provides_type == PROVIDES_MIMETYPE:
            return [ "mimehandler(%s)" % value ]
        if provides_type == PROVIDES_POSTSCRIPT_DRIVER:
            return [ "postscriptdriver(%s)" % value ]
        if provides_type == PROVIDES_PLASMA_SERVICE:
            # We need to allow the Plasma version to be specified.
            if value.startswith("plasma"):
                return [ value ]
            # For compatibility, we default to plasma4.
            return [ "plasma4(%s)" % value ]
        if provides_type == PROVIDES_ANY:
            provides = []
            provides.append(self._get_provides_query(PROVIDES_CODEC, value)[0])
            provides.append(self._get_provides_query(PROVIDES_FONT, value)[0])
            provides.append(self._get_provides_query(PROVIDES_MIMETYPE, value)[0])
            provides.append(self._get_provides_query(PROVIDES_POSTSCRIPT_DRIVER, value)[0])
            provides.append(self._get_provides_query(PROVIDES_PLASMA_SERVICE, value)[0])
            provides.append(value)
            return provides

        # not supported
        raise PkError(ERROR_NOT_SUPPORTED, "this backend does not support '%s' provides" % provides_type)

    def what_provides(self, filters, provides_type, values):
        '''
        Implement the what-provides functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_QUERY)
        values_provides = []

        pkgfilter = YumFilter(filters)

        try:
            for value in values:
                provides = self._get_provides_query(provides_type, value)
                for provide in provides:
                    values_provides.append(provide)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
        else:
            # there may be multiple provide strings
            for provide in values_provides:
                # Check installed packages for provide
                try:
                    pkgs = self.yumbase.rpmdb.searchProvides(provide)
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                else:
                    pkgfilter.add_installed(pkgs)

                    if not FILTER_INSTALLED in filters:
                        # Check available packages for provide
                        try:
                            pkgs = self.yumbase.pkgSack.searchProvides(provide)
                        except yum.Errors.RepoError, e:
                            self.error(ERROR_NO_CACHE, "failed to get provides for sack: %s" %_to_unicode(e), exit=False)
                            return
                        except exceptions.IOError, e:
                            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                        except Exception, e:
                            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                        else:
                            pkgfilter.add_available(pkgs)

                    # we couldn't do this when generating the list
                    package_list = pkgfilter.get_package_list()
                    self._show_package_list(package_list)

    def get_categories(self):
        '''
        Implement the get-categories functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.percentage(None)
        self.status(STATUS_QUERY)
        self.allow_cancel(True)
        cats = []
        try:
            cats = self.yumbase.comps.categories
        except yum.Errors.RepoError, e:
            self.error(ERROR_NO_CACHE, "failed to get comps list: %s" %_to_unicode(e), exit=False)
        except yum.Errors.GroupsError, e:
            self.error(ERROR_GROUP_LIST_INVALID, "Failed to get groups list: %s" %_to_unicode(e), exit=False)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            if len(cats) == 0:
                self.error(ERROR_GROUP_LIST_INVALID, "no comps categories", exit=False)
                return
            for cat in cats:
                cat_id = cat.categoryid
                # yum >= 3.2.10
                # name = cat.nameByLang(self.lang)
                # summary = cat.descriptionByLang(self.lang)
                name = _to_unicode(cat.name)
                summary = _to_unicode(cat.description)
                fn = "/usr/share/pixmaps/comps/%s.png" % cat_id
                if os.access(fn, os.R_OK):
                    icon = cat_id
                else:
                    icon = "image-missing"
                self.category("", cat_id, name, summary, icon)
                self._get_groups(cat_id)

        # also add the repo category objects
        self.category("", 'repo:', 'Software Sources', 'Packages from specific software sources', 'base-system')
        try:
            repos = self.yumbase.repos.repos.values()
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            return
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            return
        for repo in repos:
            if repo.isEnabled():
                self.category("repo:", "repo:" + repo.id, repo.name, 'Packages from ' + repo.name, 'base-system')

    def _get_groups(self, cat_id):
        '''
        Implement the get-collections functionality
        '''
        self.status(STATUS_QUERY)
        self.allow_cancel(True)
        if cat_id:
            cats = [cat_id]
        else:
            cats =  [cat.categoryid for cat in self.yumbase.comps.categories]
        for cat in cats:
            grps = []
            for grp_id in self.comps.get_groups(cat):
                try:
                    grp = self.yumbase.comps.return_group(grp_id)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                if grp:
                    grps.append(grp)
            for grp in sorted(grps):
                grp_id = grp.groupid
                cat_id_name = "@%s" % (grp_id)
                name = grp.nameByLang(self.lang)
                summary = grp.descriptionByLang(self.lang)
                icon = "image-missing"
                fn = "/usr/share/pixmaps/comps/%s.png" % grp_id
                if os.access(fn, os.R_OK):
                    icon = grp_id
                else:
                    fn = "/usr/share/pixmaps/comps/%s.png" % cat_id
                    if os.access(fn, os.R_OK):
                        icon = cat_id
                self.category(cat, cat_id_name, name, summary, icon)

    def download_packages(self, directory, package_ids):
        '''
        Implement the download-packages functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.status(STATUS_DOWNLOAD)
        percentage = 0
        bump = 100 / len(package_ids)

        # download each package
        for package_id in package_ids:
            self.percentage(percentage)
            try:
                pkg, inst = self._findPackage(package_id)
            except PkError, e:
                self.error(e.code, e.details, exit=True)
                return
            # if we couldn't map package_id -> pkg
            if not pkg:
                self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, "Could not find the package %s" % package_id)
                continue

            n, a, e, v, r = pkg.pkgtup
            try:
                packs = self.yumbase.pkgSack.searchNevra(n, e, v, r, a)
            except yum.Errors.RepoError, e:
                self.error(ERROR_NO_CACHE, "failed to search package sack: %s" %_to_unicode(e), exit=False)
                return
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

            # if we couldn't map package_id -> pkg
            if len(packs) == 0:
                self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, "Could not find a match for package %s" % package_id)
                continue

            # choose the first entry, as the same NEVRA package in multiple repos is fine
            pkg_download = packs[0]
            self._show_package(pkg_download, INFO_DOWNLOADING)
            try:
                repo = self.yumbase.repos.getRepo(pkg_download.repoid)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            remote = pkg_download.returnSimple('relativepath')
            if directory:
                local = os.path.basename(remote)
                if not os.path.exists(directory):
                    self.error(ERROR_PACKAGE_DOWNLOAD_FAILED, "No destination directory exists", exit=False)
                    return
                local = os.path.join(directory, local)
                if (os.path.exists(local) and os.path.getsize(local) == int(pkg_download.returnSimple('packagesize'))):
                    self.error(ERROR_PACKAGE_DOWNLOAD_FAILED, "Package already exists as %s" % local, exit=False)
                    return
            # Disable cache otherwise things won't download
            repo.cache = 0

            #  set the localpath we want
            if directory:
                pkg_download.localpath = local
            try:
                path = repo.getPackage(pkg_download)

                # emit the file we downloaded
                package_id_tmp = self._pkg_to_id(pkg_download)
                self.files(package_id_tmp, path)

            except IOError, e:
                self.error(ERROR_PACKAGE_DOWNLOAD_FAILED, "Cannot write to file", exit=False)
                return
            percentage += bump

        # in case we don't sum to 100
        self.percentage(100)

    def _is_meta_package(self, package_id):
        grp = None
        if len(package_id.split(';')) > 1:
            # Split up the id
            (name, idver, a, repo) = self.get_package_from_id(package_id)
            isGroup = False
            if repo == 'meta':
                if name[0] == '@':
                    name = name[1:]
                try:
                    grp = self.yumbase.comps.return_group(name)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                isGroup = True
            elif name[0] == '@':
                try:
                    grp = self.yumbase.comps.return_group(name[1:])
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                isGroup = True
            if isGroup and not grp:
                self.error(ERROR_GROUP_NOT_FOUND, "group %s does not exist (searched for %s)" % (name, _format_package_id (package_id)))
                return
        return grp

    def _findPackage(self, package_id):
        '''
        find a package based on a package id (name;version;arch;repoid)
        '''
        # Bailout if meta packages, just to be sure
        if self._is_meta_package(package_id):
            return None, False

        # is this an real id?
        if len(package_id.split(';')) <= 1:
            raise PkError(ERROR_PACKAGE_ID_INVALID, "package_id '%s' cannot be parsed" % _format_package_id(package_id))

        # Split up the id
        (n, idver, a, repo) = self.get_package_from_id(package_id)
        # get e, v, r from package id version
        e, v, r = _getEVR(idver)

        if repo.startswith('installed'):
            # search the rpmdb for the nevra
            try:
                pkgs = self.yumbase.rpmdb.searchNevra(name=n, epoch=e, ver=v, rel=r, arch=a)
            except exceptions.IOError, e:
                raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            # if the package is found, then return it (do not have to match the repo_id)
            if len(pkgs) != 0:
                return pkgs[0], True

        # find the correct repo, and don't use yb.pkgSack.searchNevra as it
        # searches all repos and takes 66ms
        try:
            repos = self.yumbase.repos.findRepos(repo)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        if len(repos) == 0:
            raise PkError(ERROR_REPO_NOT_FOUND, "cannot find repo %s" % repo)

        # the repo might have been disabled if it is no longer contactable
        if not repos[0].isEnabled():
            raise PkError(ERROR_PACKAGE_NOT_FOUND, '%s cannot be found as %s is disabled' % (_format_package_id(package_id), repos[0].id))

        # populate the sack with data
        try:
            self.yumbase.repos.populateSack(repo)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # search the pkgSack for the nevra
        try:
            pkgs = repos[0].sack.searchNevra(name=n, epoch=e, ver=v, rel=r, arch=a)
        except yum.Errors.RepoError, e:
            raise PkError(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # nothing found
        if len(pkgs) == 0:
            return None, False

        # multiple entries
        if len(pkgs) > 1:
            self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, "more than one package match for %s" % _format_package_id(package_id))

        # return first entry
        return pkgs[0], False

    def get_requires(self, filters, package_ids, recursive):
        '''
        Print a list of requires for a given package
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)

        percentage = 0
        bump = 100 / len(package_ids)
        deps_list = []
        resolve_list = []

        for package_id in package_ids:
            self.percentage(percentage)
            grp = self._is_meta_package(package_id)
            if grp:
                if not grp.installed:
                    self.error(ERROR_PACKAGE_NOT_INSTALLED, "The Group %s is not installed" % grp.groupid)
                else:
                    try:
                        txmbrs = self.yumbase.groupRemove(grp.groupid)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                    for txmbr in self.yumbase.tsInfo:
                        deps_list.append(txmbr.po)
            else:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    if e.code == ERROR_PACKAGE_NOT_FOUND:
                        self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, e.details)
                        continue
                    self.error(e.code, e.details, exit=True)
                    return
                # This simulates the removal of the package
                if inst and pkg:
                    resolve_list.append(pkg)
                    try:
                        txmbrs = self.yumbase.remove(po=pkg)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            percentage += bump

        # do the depsolve to pull in deps
        if len(self.yumbase.tsInfo) > 0  and recursive:
            try:
                rc, msgs =  self.yumbase.buildTransaction()
            except yum.Errors.RepoError, e:
                self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            if rc != 2:
                self.error(ERROR_DEP_RESOLUTION_FAILED, _format_msgs(msgs))
            else:
                for txmbr in self.yumbase.tsInfo:
                    if txmbr.po not in deps_list:
                        deps_list.append(txmbr.po)

        # remove any of the original names
        for pkg in resolve_list:
            if pkg in deps_list:
                deps_list.remove(pkg)

        # each unique name, emit
        for pkg in deps_list:
            package_id = self._pkg_to_id(pkg)
            self.package(package_id, INFO_INSTALLED, pkg.summary)
        self.percentage(100)

    def _is_inst(self, pkg):
        # search only for requested arch
        try:
            ret = self.yumbase.rpmdb.installed(po=pkg)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        return ret

    def _is_inst_arch(self, pkg):
        # search for a requested arch first
        ret = self._is_inst(pkg)
        if ret:
            return True

        # then fallback to i686 if i386
        if pkg.arch == 'i386':
            pkg.arch = 'i686'
            ret = self._is_inst(pkg)
            pkg.arch = 'i386'
        return ret

    def _installable(self, pkg, ematch=False):

        """check if the package is reasonably installable, true/false"""

        try:
            exactarchlist = self.yumbase.conf.exactarchlist
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        # we look through each returned possibility and rule out the
        # ones that we obviously can't use

        if self._is_inst_arch(pkg):
            return False

        # everything installed that matches the name
        try:
            installedByKey = self.yumbase.rpmdb.searchNevra(name=pkg.name, arch=pkg.arch)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        comparable = []
        for instpo in installedByKey:
            if rpmUtils.arch.isMultiLibArch(instpo.arch) == rpmUtils.arch.isMultiLibArch(pkg.arch):
                comparable.append(instpo)
            else:
                continue

        # go through each package
        if len(comparable) > 0:
            for instpo in comparable:
                if pkg.EVR > instpo.EVR: # we're newer - this is an update, pass to them
                    if instpo.name in exactarchlist:
                        if pkg.arch == instpo.arch:
                            return True
                    else:
                        return True

                elif pkg.EVR == instpo.EVR: # same, ignore
                    return False

                elif pkg.EVR < instpo.EVR: # lesser, check if the pkgtup is an exactmatch
                                   # if so then add it to be installed
                                   # if it can be multiply installed
                                   # this is where we could handle setting
                                   # it to be an 'oldpackage' revert.

                    if ematch:
                        try:
                            ret = self.yumbase.allowedMultipleInstalls(pkg)
                        except exceptions.IOError, e:
                            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                        except Exception, e:
                            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                        if ret:
                            return True

        else: # we've not got any installed that match n or n+a
            return True

        return False

    def _get_best_pkg_from_list(self, pkglist):
        '''
        Gets best dep package from a list
        '''
        best = None

        # first try and find the highest EVR package that is already installed
        for pkgi in pkglist:
            n, a, e, v, r = pkgi.pkgtup
            try:
                pkgs = self.yumbase.rpmdb.searchNevra(name=n, epoch=e, ver=v, arch=a)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            for pkg in pkgs:
                if best:
                    if pkg.EVR > best.EVR:
                        best = pkg
                else:
                    best = pkg

        # then give up and see if there's one available
        if not best:
            for pkg in pkglist:
                if best:
                    if pkg.EVR > best.EVR:
                        best = pkg
                else:
                    best = pkg
        return best

    def _get_best_depends(self, pkgs, recursive):
        ''' Gets the best deps for a package
        @param pkgs: a list of package objects
        @param recursive: if we recurse
        @return: a list for yum package object providing the dependencies
        '''
        deps_list = []

        # get the dep list
        try:
            results = self.yumbase.findDeps(pkgs)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        require_list = []
        recursive_list = []

        # get the list of deps for each package
        for pkg in results.keys():
            for req in results[pkg].keys():
                reqlist = results[pkg][req]
                if not reqlist: #  Unsatisfied dependency
                    self.error(ERROR_DEP_RESOLUTION_FAILED, "the (%s) requirement could not be resolved" % prco_tuple_to_string(req), exit=False)
                    break
                require_list.append(reqlist)

        # for each list, find the best backage using a metric
        for reqlist in require_list:
            pkg = self._get_best_pkg_from_list(reqlist)
            if pkg not in pkgs:
                deps_list.append(pkg)
                if recursive and not self._is_inst(pkg):
                    recursive_list.append(pkg)

        # if the package is to be downloaded, also find its deps
        if len(recursive_list) > 0:
            pkgsdeps = self._get_best_depends(recursive_list, True)
            for pkg in pkgsdeps:
                if pkg not in pkgs:
                    deps_list.append(pkg)

        return deps_list

    def _get_group_packages(self, grp):
        '''
        Get the packages there will be installed when a comps group
        is installed
        '''
        if not grp.installed:
            try:
                txmbrs = self.yumbase.selectGroup(grp.groupid)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            try:
                txmbrs = self.yumbase.groupRemove(grp.groupid)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        pkgs = []
        for t in txmbrs:
            pkgs.append(t.po)
        if not grp.installed:
            try:
                self.yumbase.deselectGroup(grp.groupid)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            try:
                self.yumbase.groupUnremove(grp.groupid)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        return pkgs

    def _get_depends_not_installed(self, filters, package_ids, recursive):
        '''
        Gets the deps that are not installed, optimisation of get_depends
        using a yum transaction
        Returns a list of pkgs.
        '''
        percentage = 0
        bump = 100 / len(package_ids)
        deps_list = []
        resolve_list = []

        for package_id in package_ids:
            self.percentage(percentage)
            grp = self._is_meta_package(package_id)
            if grp:
                if grp.installed:
                    self.error(ERROR_PACKAGE_ALREADY_INSTALLED, "The Group %s is already installed" % grp.groupid)
                else:
                    try:
                        txmbrs = self.yumbase.selectGroup(grp.groupid)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                    for txmbr in self.yumbase.tsInfo:
                        deps_list.append(txmbr.po)
                    # unselect what we previously selected
                    try:
                        self.yumbase.deselectGroup(grp.groupid)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            else:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    self.error(e.code, e.details, exit=True)
                    return
                # This simulates the addition of the package
                if not inst and pkg:
                    resolve_list.append(pkg)
                    try:
                        txmbrs = self.yumbase.install(po=pkg)
                    except yum.Errors.RepoError, e:
                        self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            percentage += bump

        if len(self.yumbase.tsInfo) > 0 and recursive:
            try:
                rc, msgs =  self.yumbase.buildTransaction()
            except yum.Errors.RepoError, e:
                self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            if rc != 2:
                self.error(ERROR_DEP_RESOLUTION_FAILED, _format_msgs(msgs))
            else:
                for txmbr in self.yumbase.tsInfo:
                    if txmbr.po not in deps_list:
                        deps_list.append(txmbr.po)

        # make unique list
        deps_list = unique(deps_list)

        # remove any of the packages we passed in
        for package_id in package_ids:
            try:
                pkg, inst = self._findPackage(package_id)
            except PkError, e:
                self.error(e.code, e.details, exit=True)
                return
            if pkg in deps_list:
                deps_list.remove(pkg)

        # remove any that are already installed
        for pkg in deps_list:
            if self._is_inst(pkg):
                deps_list.remove(pkg)

        return deps_list

    def get_depends(self, filters, package_ids, recursive):
        '''
        Print a list of depends for a given package
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)
        pkgfilter = YumFilter(filters)

        # before we do an install we do ~installed + recursive true,
        # which we can emulate quicker by doing a transaction, but not
        # executing it
        if FILTER_NOT_INSTALLED in filters and recursive:
            pkgs = self._get_depends_not_installed (filters, package_ids, recursive)
            pkgfilter.add_available(pkgs)
            package_list = pkgfilter.get_package_list()
            self._show_package_list(package_list)
            self.percentage(100)
            return

        percentage = 0
        bump = 100 / len(package_ids)
        deps_list = []
        resolve_list = []
        grp_pkgs = []

        # resolve each package_id to a pkg object
        for package_id in package_ids:
            self.percentage(percentage)
            grp = self._is_meta_package(package_id)
            if grp:
                pkgs = self._get_group_packages(grp)
                grp_pkgs.extend(pkgs)
            else:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    if e.code == ERROR_PACKAGE_NOT_FOUND:
                        self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, e.details)
                        continue
                    self.error(e.code, e.details, exit=True)
                    return
                if pkg:
                    resolve_list.append(pkg)
                else:
                    self.error(ERROR_PACKAGE_NOT_FOUND, 'Package %s was not found' % package_id)
                    break
            percentage += bump

        if grp_pkgs:
            resolve_list.extend(grp_pkgs)
        # get the best deps -- doing recursive is VERY slow
        deps_list = self._get_best_depends(resolve_list, recursive)

        # make unique list
        deps_list = unique(deps_list)

        # If packages comes from a group, then we show them along with deps.
        if grp_pkgs:
            deps_list.extend(grp_pkgs)

        # add to correct lists
        for pkg in deps_list:
            if self._is_inst(pkg):
                pkgfilter.add_installed([pkg])
            else:
                pkgfilter.add_available([pkg])

        # we couldn't do this when generating the list
        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)
        self.percentage(100)

    def _is_package_repo_signed(self, pkg):
        '''
        Finds out if the repo that contains the package is signed
        '''
        signed = False
        try:
            repo = self.yumbase.repos.getRepo(pkg.repoid)
            signed = repo.gpgcheck
        except yum.Errors.RepoError, e:
            raise PkError(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        return signed

    def _set_only_trusted(self, only_trusted):
        # if only_trusted is true, it means that we will only install/update
        # signed files and fail on unsigned ones

        if hasattr(self.yumbase, "_override_sigchecks"):
            # _override_sigchecks logic is reversed
            override_sigchecks = not only_trusted

            self.yumbase._override_sigchecks = override_sigchecks

            for repo in self.yumbase.repos.listEnabled():
                repo._override_sigchecks = override_sigchecks

        for attrname in ("gpgcheck", "repo_gpgcheck", "localpkg_gpgcheck"):
            if hasattr(self.yumbase.conf, attrname):
                setattr(self.yumbase.conf, attrname, only_trusted)

            for repo in self.yumbase.repos.listEnabled():
                if hasattr(repo, attrname):
                    setattr(repo, attrname, only_trusted)


    def update_system(self, only_trusted):
        '''
        Implement the update-system functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(0)
        self.status(STATUS_RUNNING)

        self._set_only_trusted(only_trusted)

        self.yumbase.conf.throttle = "60%" # Set bandwidth throttle to 60%
                                           # to avoid taking all the system's bandwidth.
        try:
            txmbr = self.yumbase.update() # Add all updates to Transaction
        except yum.Errors.RepoError, e:
            self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e), exit=False)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            if txmbr:
                # check all the packages in the transaction if only-trusted
                for t in txmbr:
                    # ignore transactions that do not have to be checked, e.g. obsoleted
                    if t.output_state not in self.transaction_sig_check_map:
                        continue
                    pkg = t.po
                    try:
                        signed = self._is_package_repo_signed(pkg)
                    except PkError, e:
                        self.error(e.code, e.details, exit=False)
                        return
                    if signed:
                        continue
                    if only_trusted:
                        self.error(ERROR_CANNOT_UPDATE_REPO_UNSIGNED, "The package %s will not be updated from unsigned repo %s" % (pkg.name, pkg.repoid), exit=False)
                        return
                    self._show_package(pkg, INFO_UNTRUSTED)
                try:
                    self._runYumTransaction(allow_skip_broken=True, only_simulate=False)
                except PkError, e:
                    self.error(e.code, e.details, exit=False)
            else:
                self.error(ERROR_NO_PACKAGES_TO_UPDATE, "Nothing to do", exit=False)
                return

    def refresh_cache(self, force):
        '''
        Implement the refresh_cache functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.allow_cancel(True)
        self.percentage(0)
        self.status(STATUS_REFRESH_CACHE)

        # we are working offline
        if not self.has_network:
            self.error(ERROR_NO_NETWORK, "cannot refresh cache when offline", exit=False)
            return

        pct = 0
        try:
            if len(self.yumbase.repos.listEnabled()) == 0:
                self.percentage(100)
                return

            #work out the slice for each one
            bump = (95/len(self.yumbase.repos.listEnabled()))/2

            for repo in self.yumbase.repos.listEnabled():

                # emit details for UI
                self.repo_detail(repo.id, repo.name, True)

                # is physical media
                if repo.mediaid:
                    continue
                repo.metadata_expire = 0
                self.yumbase.repos.populateSack(which=[repo.id], mdtype='metadata', cacheonly=1)
                pct += bump
                self.percentage(pct)
                self.yumbase.repos.populateSack(which=[repo.id], mdtype='filelists', cacheonly=1)
                pct += bump
                self.percentage(pct)

            self.percentage(95)
            # Setup categories/groups
            try:
                self.yumbase.doGroupSetup()
            except yum.Errors.GroupsError, e:
                pass
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            #we might have a rounding error
            self.percentage(100)

        except yum.Errors.RepoError, e:
            message = _format_msgs(e.value)
            if message.find ("No more mirrors to try") != -1:
                self.error(ERROR_NO_MORE_MIRRORS_TO_TRY, message, exit=False)
            else:
                self.error(ERROR_REPO_CONFIGURATION_ERROR, message, exit=False)
        except yum.Errors.YumBaseError, e:
            self.error(ERROR_UNKNOWN, "cannot refresh cache: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            # update the comps groups too
            self.comps.refresh()

    def resolve(self, filters, packages):
        '''
        Implement the resolve functionality
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.allow_cancel(True)
        self.percentage(None)
        try:
            self.yumbase.doConfigSetup(errorlevel=0, debuglevel=0)# Setup Yum Config
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        self.yumbase.conf.cache = 0 # TODO: can we just look in the cache?
        self.status(STATUS_QUERY)

        pkgfilter = YumFilter(filters)
        package_list = []

        # OR search
        for package in packages:
            # Get installed packages
            if FILTER_NOT_INSTALLED not in filters:
                try:
                    pkgs = self.yumbase.rpmdb.searchNevra(name=package)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                else:
                    pkgfilter.add_installed(pkgs)

            # Get available packages
            if FILTER_INSTALLED not in filters:
                try:
                    pkgs = self.yumbase.pkgSack.returnNewestByName(name=package)
                except yum.Errors.PackageSackError, e:
                    # no package of this name found, which is okay
                    pass
                except yum.Errors.RepoError, e:
                    self.error(ERROR_NO_CACHE, "failed to return newest by package sack: %s" %_to_unicode(e), exit=False)
                    return
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                else:
                    pkgfilter.add_available(pkgs)

        # is this a metapackage (a group)
        for package_id in packages:
            if package_id[0] == '@':
                grps = self.comps.get_meta_packages()
                for grpid in grps:
                    if grpid == package_id[1:]:

                        # create virtual package
                        pkg = yum.packages.PackageObject()
                        pkg.name = package_id[1:]
                        pkg.version = ''
                        pkg.release = ''
                        pkg.arch = ''
                        pkg.epoch = '0'
                        pkg.repo = 'meta'

                        # get the name and the installed status
                        try:
                            grp = self.yumbase.comps.return_group(grpid)
                        except yum.Errors.RepoError, e:
                            raise PkError(ERROR_NO_CACHE, "failed to get groups from comps: %s" % _to_unicode(e))
                        except yum.Errors.GroupsError, e:
                            raise PkError(ERROR_GROUP_NOT_FOUND, "failed to find group '%s': %s" % (package_id, _to_unicode(e)))
                        except exceptions.IOError, e:
                            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                        except Exception, e:
                            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                        pkg.summary = grp.name
                        if grp.installed:
                            pkgfilter.add_installed([pkg])
                        else:
                            pkgfilter.add_available([pkg])

        # we couldn't do this when generating the list
        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)

    def install_packages(self, only_trusted, inst_files):
        self._install_packages(only_trusted, inst_files)

    def simulate_install_packages(self, inst_files):
        self._install_packages(False, inst_files, True)

    def _install_packages(self, only_trusted, package_ids, simulate=False):
        '''
        Implement the install-packages functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(False)
        self.percentage(0)
        self.status(STATUS_RUNNING)
        txmbrs = []

        self._set_only_trusted(only_trusted)

        for package_id in package_ids:
            grp = self._is_meta_package(package_id)
            if grp:
                if grp.installed:
                    self.error(ERROR_PACKAGE_ALREADY_INSTALLED, "This Group %s is already installed" % grp.groupid, exit=False)
                    return
                try:
                    # I'm not sure why we have to deselectGroup() before we selectGroup(), but if we don't
                    # then selectGroup returns no packages. I've already made sure that any selectGroup
                    # invokations do deselectGroup, so I'm not sure what's going on...
                    self.yumbase.deselectGroup(grp.groupid)
                    txmbr = self.yumbase.selectGroup(grp.groupid)
                    if not txmbr:
                        self.error(ERROR_GROUP_NOT_FOUND, "No packages were found in the %s group for %s." % (grp.groupid, _format_package_id(package_id)))
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                txmbrs.extend(txmbr)
            else:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    self.error(e.code, e.details, exit=True)
                    return
                if pkg and not inst:
                    txmbr = self.yumbase.install(po=pkg)
                    txmbrs.extend(txmbr)
                if inst:
                    self.error(ERROR_PACKAGE_ALREADY_INSTALLED, "The package %s is already installed" % pkg.name, exit=False)
                    return
        if txmbrs:
            for t in txmbrs:
                pkg = t.po
                # ignore transactions that do not have to be checked, e.g. obsoleted
                if t.output_state not in self.transaction_sig_check_map:
                    continue
                try:
                    signed = self._is_package_repo_signed(pkg)
                except PkError, e:
                    self.error(e.code, e.details, exit=False)
                    return
                if signed:
                    continue
                if only_trusted:
                    self.error(ERROR_CANNOT_INSTALL_REPO_UNSIGNED, "The package %s will not be installed from unsigned repo %s" % (pkg.name, pkg.repoid), exit=False)
                    return
                self._show_package(pkg, INFO_UNTRUSTED)
            try:
                self._runYumTransaction(only_simulate=simulate)
            except PkError, e:
                self.error(e.code, e.details, exit=False)
        else:
            self.error(ERROR_ALL_PACKAGES_ALREADY_INSTALLED, "The packages are already all installed", exit=False)

    def _checkForNewer(self, po):
        pkgs = None
        try:
            pkgs = self.yumbase.pkgSack.returnNewestByName(name=po.name)
        except yum.Errors.PackageSackError:
            pass
        except yum.Errors.RepoError, e:
            pass
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        if pkgs:
            newest = pkgs[0]
            if newest.EVR > po.EVR:
                self.message(MESSAGE_NEWER_PACKAGE_EXISTS, "A newer version of %s is available online." % po.name)

    def install_files(self, only_trusted, inst_files):
        self._install_files(only_trusted, inst_files)

    def simulate_install_files(self, inst_files):
        self._install_files(False, inst_files, True)

    def _install_files(self, only_trusted, inst_files, simulate=False):
        '''
        Implement the install-files functionality
        Install the package containing the inst_file file
        Needed to be implemented in a sub class
        '''
        for inst_file in inst_files:
            if inst_file.endswith('.src.rpm'):
                self.error(ERROR_CANNOT_INSTALL_SOURCE_PACKAGE, 'Backend will not install a src rpm file', exit=False)
                return

        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(False)
        self.percentage(0)
        self.status(STATUS_RUNNING)

        # check we have at least one file
        if len(inst_files) == 0:
            self.error(ERROR_FILE_NOT_FOUND, 'no files specified to install', exit=False)
            return

        # check that the files still exist
        for inst_file in inst_files:
            if not os.path.exists(inst_file):
                self.error(ERROR_FILE_NOT_FOUND, '%s could not be found' % inst_file, exit=False)
                return

        # process these first
        tempdir = tempfile.mkdtemp()
        inst_packs = []

        for inst_file in inst_files:
            if inst_file.endswith('.rpm'):
                continue
            elif inst_file.endswith('.servicepack'):
                inst_packs.append(inst_file)
            else:
                self.error(ERROR_INVALID_PACKAGE_FILE, 'Only rpm files and packs are supported', exit=False)
                return

        # decompress and add the contents of any .servicepack files
        for inst_pack in inst_packs:
            inst_files.remove(inst_pack)
            pack = tarfile.TarFile(name = inst_pack, mode = "r")
            members = pack.getnames()
            for mem in members:
                pack.extract(mem, path = tempdir)
            files = os.listdir(tempdir)

            # find the metadata file
            packtype = 'unknown'
            for fn in files:
                if fn == "metadata.conf":
                    config = ConfigParser.ConfigParser()
                    config.read(os.path.join(tempdir, fn))
                    if config.has_option('PackageKit Service Pack', 'type'):
                        packtype = config.get('PackageKit Service Pack', 'type')
                    break

            # we only support update and install
            if packtype != 'install' and packtype != 'update':
                self.error(ERROR_INVALID_PACKAGE_FILE, 'no support for type %s' % packtype, exit=False)
                return

            # add the file if it's an install, or update if installed
            for fn in files:
                if fn.endswith('.rpm'):
                    inst_file = os.path.join(tempdir, fn)
                    try:
                        # read the file
                        pkg = YumLocalPackage(ts=self.yumbase.rpmdb.readOnlyTS(), filename=inst_file)
                        pkgs_local = self.yumbase.rpmdb.searchNevra(name=pkg.name)
                    except yum.Errors.MiscError:
                        self.error(ERROR_INVALID_PACKAGE_FILE, "%s does not appear to be a valid package." % inst_file)
                    except yum.Errors.YumBaseError, e:
                        self.error(ERROR_INVALID_PACKAGE_FILE, 'Package could not be decompressed')
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except:
                        self.error(ERROR_UNKNOWN, "Failed to open local file -- please report")
                    else:
                        # trying to install package that already exists
                        if len(pkgs_local) == 1 and pkgs_local[0].EVR == pkg.EVR:
                            self.message(MESSAGE_PACKAGE_ALREADY_INSTALLED, '%s is already installed and the latest version' % pkg.name)

                        # trying to install package older than already exists
                        elif len(pkgs_local) == 1 and pkgs_local[0].EVR > pkg.EVR:
                            self.message(MESSAGE_PACKAGE_ALREADY_INSTALLED, 'a newer version of %s is already installed' % pkg.name)

                        # only update if installed
                        elif packtype == 'update':
                            if len(pkgs_local) > 0:
                                inst_files.append(inst_file)

                        # only install if we passed the checks above
                        elif packtype == 'install':
                            inst_files.append(inst_file)

        if len(inst_files) == 0:
            # More than one pkg to be installed, all of them already installed
            self.error(ERROR_ALL_PACKAGES_ALREADY_INSTALLED,
                       'All of the specified packages have already been installed')

        self._set_only_trusted(only_trusted or simulate)

        # self.yumbase.installLocal fails for unsigned packages when self.yumbase.conf.gpgcheck = 1
        # This means we don't run runYumTransaction, and don't get the GPG failure in
        # PackageKitYumBase(_checkSignatures) -- so we check here
        for inst_file in inst_files:
            try:
                po = YumLocalPackage(ts=self.yumbase.rpmdb.readOnlyTS(), filename=inst_file)
            except yum.Errors.MiscError:
                self.error(ERROR_INVALID_PACKAGE_FILE, "%s does not appear to be a valid package." % inst_file, exit=False)
                return
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            try:
                self.yumbase._checkSignatures([po], None)
            except yum.Errors.YumGPGCheckError, e:
                if only_trusted:
                    self.error(ERROR_MISSING_GPG_SIGNATURE, _to_unicode(e), exit=False)
                    return
                self._show_package(po, INFO_UNTRUSTED)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # common checks copied from yum
        for inst_file in inst_files:
            if not self._check_local_file(inst_file):
                return

        txmbrs = []
        try:
            for inst_file in inst_files:
                try:
                    txmbr = self.yumbase.installLocal(inst_file)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                if txmbr:
                    txmbrs.extend(txmbr)
                    self._checkForNewer(txmbr[0].po)
                    # Added the package to the transaction set
                else:
                    self.error(ERROR_LOCAL_INSTALL_FAILED, "Can't install %s as no transaction" % _to_unicode(inst_file))
            if len(self.yumbase.tsInfo) == 0:
                self.error(ERROR_LOCAL_INSTALL_FAILED, "Can't install %s" % " or ".join(inst_files), exit=False)
                return

            try:
                self._runYumTransaction(only_simulate=simulate)
            except PkError, e:
                self.error(e.code, e.details, exit=False)
                return

        except yum.Errors.InstallError, e:
            self.error(ERROR_LOCAL_INSTALL_FAILED, _to_unicode(e))
        except (yum.Errors.RepoError, yum.Errors.PackageSackError, IOError):
            # We might not be able to connect to the internet to get
            # repository metadata, or the package might not exist.
            # Try again, (temporarily) disabling repos first.
            try:
                for repo in self.yumbase.repos.listEnabled():
                    repo.disable()

                for inst_file in inst_files:
                    try:
                        txmbr = self.yumbase.installLocal(inst_file)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                    if txmbr:
                        txmbrs.extend(txmbr)
                        if len(self.yumbase.tsInfo) > 0:
                            if not self.yumbase.tsInfo.pkgSack:
                                self.yumbase.tsInfo.pkgSack = MetaSack()
                            try:
                                self._runYumTransaction(only_simulate=simulate)
                            except PkError, e:
                                self.error(e.code, e.details, exit=False)
                                return
                    else:
                        self.error(ERROR_LOCAL_INSTALL_FAILED, "Can't install %s" % inst_file)
            except yum.Errors.InstallError, e:
                self.error(ERROR_LOCAL_INSTALL_FAILED, _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        shutil.rmtree(tempdir)

    def _check_local_file(self, pkg):
        """
        Duplicates some of the checks that yumbase.installLocal would
        do, so we can get decent error reporting.
        """
        po = None
        try:
            po = YumLocalPackage(ts=self.yumbase.rpmdb.readOnlyTS(), filename=pkg)
        except yum.Errors.MiscError:
            self.error(ERROR_INVALID_PACKAGE_FILE, "%s does not appear to be a valid package." % pkg, exit=False)
            return False
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            return False
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            return False

        # check if wrong arch
        suitable_archs = rpmUtils.arch.getArchList()
        if po.arch not in suitable_archs:
            self.error(ERROR_INCOMPATIBLE_ARCHITECTURE, "Package %s has incompatible architecture %s. Valid architectures are %s" % (pkg, po.arch, suitable_archs), exit=False)
            return False

        # check already installed
        if self._is_inst_arch(po):
            self.error(ERROR_PACKAGE_ALREADY_INSTALLED, "The package %s is already installed" % str(po), exit=False)
            return False

        # check if excluded
        if len(self.yumbase.conf.exclude) > 0:
            exactmatch, matched, unmatched = parsePackages([po], self.yumbase.conf.exclude, casematch=1)
            if po in exactmatch + matched:
                self.error(ERROR_PACKAGE_INSTALL_BLOCKED, "Installation of %s is excluded by yum configuration." % pkg, exit=False)
                return False

        return True

    def update_packages(self, only_trusted, package_ids):
        self._update_packages(only_trusted, package_ids)

    def simulate_update_packages(self, package_ids):
        self._update_packages(False, package_ids, True)

    def _update_packages(self, only_trusted, package_ids, simulate=False):
        '''
        Implement the install functionality
        This will only work with yum 3.2.4 or higher
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(False)
        self.percentage(0)
        self.status(STATUS_RUNNING)

        self._set_only_trusted(only_trusted)

        txmbrs = []
        try:
            for package_id in package_ids:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    if e.code == ERROR_PACKAGE_NOT_FOUND:
                        self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, e.details)
                        package_ids.remove(package_id)
                        continue
                    self.error(e.code, e.details, exit=True)
                    return
                if pkg:
                    try:
                        txmbr = self.yumbase.update(po=pkg)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                    if not txmbr:
                        self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, "could not add package update for %s: %s" % (_format_package_id(package_id), pkg))
                    else:
                        txmbrs.extend(txmbr)
        except yum.Errors.RepoError, e:
            self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e), exit=False)
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            if txmbrs:
                for t in txmbrs:
                    # ignore transactions that do not have to be checked, e.g. obsoleted
                    if t.output_state not in self.transaction_sig_check_map:
                        continue
                    pkg = t.po
                    try:
                        signed = self._is_package_repo_signed(pkg)
                    except PkError, e:
                        self.error(e.code, e.details, exit=False)
                        return
                    if signed:
                        continue
                    if only_trusted:
                        self.error(ERROR_CANNOT_UPDATE_REPO_UNSIGNED, "The package %s will not be updated from unsigned repo %s" % (pkg.name, pkg.repoid), exit=False)
                        return
                    self._show_package(pkg, INFO_UNTRUSTED)
                try:
                    self._runYumTransaction(allow_skip_broken=True, only_simulate=simulate)
                except PkError, e:
                    self.error(e.code, e.details, exit=False)
            else:
                self.error(ERROR_TRANSACTION_ERROR, "No transaction to process", exit=False)

    def _check_for_reboot(self):
        md = self.updateMetadata
        for txmbr in self.yumbase.tsInfo:
            pkg = txmbr.po
            # check if package is in reboot list or flagged with reboot_suggested
            # in the update metadata and is installed/updated etc
            notice = md.get_notice((pkg.name, pkg.version, pkg.release))
            if (pkg.name in self.rebootpkgs \
                or (notice and notice.get_metadata().has_key('reboot_suggested') and notice['reboot_suggested'])):
                self.require_restart(RESTART_SYSTEM, self._pkg_to_id(pkg))

    def _runYumTransaction(self, allow_remove_deps=None, allow_skip_broken=False, only_simulate=False):
        '''
        Run the yum Transaction
        This will only work with yum 3.2.4 or higher
        '''
        message = ''
        try:
            self.yumbase.conf.skip_broken = 0
            rc, msgs = self.yumbase.buildTransaction()
            message = _format_msgs(msgs)
        except yum.Errors.RepoError, e:
            raise PkError(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
        except yum.Errors.PackageSackError, e:
            raise PkError(ERROR_PACKAGE_DATABASE_CHANGED, _to_unicode(e))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # if return value is 1 (error), try again with skip-broken if allowed
        if allow_skip_broken and rc == 1:
            try:
                self.yumbase.conf.skip_broken = 1
                rc, msgs = self.yumbase.buildTransaction()
                message += " : %s" % _format_msgs(msgs)
            except yum.Errors.RepoError, e:
                raise PkError(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
            except exceptions.IOError, e:
                raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # test did succeed
        self._check_for_reboot()
        if allow_remove_deps == False:
            if len(self.yumbase.tsInfo) > 1:
                retmsg = 'package could not be removed, as other packages depend on it'
                raise PkError(ERROR_DEP_RESOLUTION_FAILED, retmsg)

        # we did not succeed
        if rc != 2:
            if message.find ("is needed by") != -1:
                raise PkError(ERROR_DEP_RESOLUTION_FAILED, message)
            if message.find ("empty transaction") != -1:
                raise PkError(ERROR_NO_PACKAGES_TO_UPDATE, message)
            raise PkError(ERROR_TRANSACTION_ERROR, message)

        # abort now we have the package list
        if only_simulate:
            package_list = []
            for txmbr in self.yumbase.tsInfo:
                if txmbr.output_state in TransactionsInfoMap.keys():
                    info = TransactionsInfoMap[txmbr.output_state]
                    package_list.append((txmbr.po, info))

            self.percentage(90)
            self._show_package_list(package_list)
            self.percentage(100)
            return

        try:
            rpmDisplay = PackageKitCallback(self)
            callback = ProcessTransPackageKitCallback(self)
            self.yumbase.processTransaction(callback=callback,
                                  rpmDisplay=rpmDisplay)
        except yum.Errors.YumDownloadError, ye:
            raise PkError(ERROR_PACKAGE_DOWNLOAD_FAILED, _format_msgs(ye.value))
        except yum.Errors.YumGPGCheckError, ye:
            raise PkError(ERROR_BAD_GPG_SIGNATURE, _format_msgs(ye.value))
        except GPGKeyNotImported, e:
            keyData = self.yumbase.missingGPGKey
            if not keyData:
                raise PkError(ERROR_BAD_GPG_SIGNATURE, "GPG key not imported, and no GPG information was found.")
            package_id = self._pkg_to_id(keyData['po'])
            fingerprint = keyData['fingerprint']()
            hex_fingerprint = "%02x" * len(fingerprint) % tuple(map(ord, fingerprint))
            # Borrowed from http://mail.python.org/pipermail/python-list/2000-September/053490.html

            self.repo_signature_required(package_id,
                                         keyData['po'].repoid,
                                         keyData['keyurl'].replace("file://", ""),
                                         keyData['userid'],
                                         keyData['hexkeyid'],
                                         hex_fingerprint,
                                         time.ctime(keyData['timestamp']),
                                         'gpg')
            raise PkError(ERROR_GPG_FAILURE, "GPG key %s required" % keyData['hexkeyid'])
        except yum.Errors.YumBaseError, ye:
            message = _format_msgs(ye.value)
            if message.find ("conflicts with file") != -1:
                raise PkError(ERROR_FILE_CONFLICTS, message)
            if message.find ("rpm_check_debug vs depsolve") != -1:
                raise PkError(ERROR_PACKAGE_CONFLICTS, message)
            else:
                raise PkError(ERROR_TRANSACTION_ERROR, message)
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

    def remove_packages(self, allowdep, autoremove, package_ids):
        self._remove_packages(allowdep, autoremove, package_ids)

    def simulate_remove_packages(self, package_ids):
        self._remove_packages(True, False, package_ids, True)

    def _remove_packages(self, allowdep, autoremove, package_ids, simulate=False):
        '''
        Implement the remove functionality
        Needed to be implemented in a sub class
        '''
        # TODO: use autoremove
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(False)
        self.percentage(0)
        self.status(STATUS_RUNNING)

        if autoremove:
            self.message(MESSAGE_PARAMETER_INVALID, "the yum backend does not support autoremove")

        txmbrs = []
        for package_id in package_ids:
            grp = self._is_meta_package(package_id)
            if grp:
                if not grp.installed:
                    self.error(ERROR_PACKAGE_NOT_INSTALLED, "This Group %s is not installed" % grp.groupid)
                try:
                    txmbr = self.yumbase.groupRemove(grp.groupid)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                txmbrs.extend(txmbr)
            else:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    self.error(e.code, e.details, exit=True)
                    return
                if pkg and inst:
                    try:
                        txmbr = self.yumbase.remove(po=pkg)
                    except exceptions.IOError, e:
                        self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                    except Exception, e:
                        self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                    txmbrs.extend(txmbr)
                if pkg and not inst:
                    self.error(ERROR_PACKAGE_NOT_INSTALLED, "The package %s is not installed" % pkg.name)
        if txmbrs:
            # check to find any system packages
            try:
                rc, msgs =  self.yumbase.buildTransaction()
            except yum.Errors.RepoError, e:
                self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except Exception, e:
                self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            if rc != 2:
                self.error(ERROR_DEP_RESOLUTION_FAILED, _format_msgs(msgs))
            else:
                for txmbr in self.yumbase.tsInfo:
                    pkg = txmbr.po
                    if pkg.name in self.system_packages:
                        self.error(ERROR_CANNOT_REMOVE_SYSTEM_PACKAGE, "The package %s is essential to correct operation and cannot be removed using this tool." % pkg.name, exit=False)
                        return
            try:
                if not allowdep:
                    self._runYumTransaction(allow_remove_deps=False, only_simulate=simulate)
                else:
                    self._runYumTransaction(allow_remove_deps=True, only_simulate=simulate)
            except PkError, e:
                self.error(e.code, e.details, exit=False)
        else:
            msg = "The following packages failed to be removed: %s" % str(package_ids)
            self.error(ERROR_PACKAGE_NOT_INSTALLED, msg, exit=False)

    def _get_category(self, groupid):
        cat_id = self.comps.get_category(groupid)
        if self.yumbase.comps._categories.has_key(cat_id):
            return self.yumbase.comps._categories[cat_id]
        else:
            return None

    def get_details(self, package_ids):
        '''
        Print a detailed details for a given package
        '''
        try:
            self._check_init(lazy_cache=True)
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)

        for package_id in package_ids:
            grp = self._is_meta_package(package_id)
            if grp:
                package_id = "%s;;;meta" % grp.groupid
                desc = grp.descriptionByLang(self.lang)
                desc = desc.replace('\n\n', ';')
                desc = desc.replace('\n', ' ')
                group = GROUP_COLLECTIONS
                pkgs = self._get_group_packages(grp)
                size = 0
                for pkg in pkgs:
                    size = size + pkg.size
                self.details(package_id, "", group, desc, "", size)

            else:
                try:
                    pkg, inst = self._findPackage(package_id)
                except PkError, e:
                    if e.code == ERROR_PACKAGE_NOT_FOUND:
                        self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, e.details)
                        continue
                    self.error(e.code, e.details, exit=True)
                    return
                if pkg:
                    self._show_details_pkg(pkg)
                else:
                    self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, 'Package %s was not found' % _format_package_id(package_id))
                    continue

    def _show_details_pkg(self, pkg):

        pkgver = _get_package_ver(pkg)
        package_id = self.get_package_id(pkg.name, pkgver, pkg.arch, pkg.repo)
        desc = _to_unicode(pkg.description)
        url = _to_unicode(pkg.url)
        license = _to_unicode(pkg.license)

        # some RPM's (especially from google) have no description
        if desc:
            desc = desc.replace('\n', ';')
            desc = desc.replace('\t', ' ')
        else:
            desc = ''

        # if we are remote and in the cache, our size is zero
        size = pkg.size
        if not pkg.repo.id.startswith('installed') and pkg.verifyLocalPkg():
            size = 0

        group = self.comps.get_group(pkg.name)
        self.details(package_id, license, group, desc, url, size)

    def get_files(self, package_ids):
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)

        for package_id in package_ids:
            try:
                pkg, inst = self._findPackage(package_id)
            except PkError, e:
                self.error(e.code, e.details, exit=True)
                return
            if pkg:
                files = pkg.returnFileEntries('dir')
                files.extend(pkg.returnFileEntries()) # regular files
                file_list = ";".join(files)
                self.files(package_id, file_list)
            else:
                self.error(ERROR_PACKAGE_NOT_FOUND, 'Package %s was not found' % package_id)

    def _pkg_to_id(self, pkg):
        pkgver = _get_package_ver(pkg)
        repo = str(pkg.repo)
        if repo.startswith('/'):
            repo = "local"
        # can we add data from the yumdb
        if repo == 'installed':
            repo_tmp = pkg.yumdb_info.get('from_repo')
            if repo_tmp:
                repo = 'installed:' + repo_tmp
        package_id = self.get_package_id(pkg.name, pkgver, pkg.arch, repo)
        return package_id

    def _show_package(self, pkg, status):
        '''  Show info about package'''
        package_id = self._pkg_to_id(pkg)
        self.package(package_id, status, pkg.summary)

    def get_distro_upgrades(self):
        '''
        Implement the get-distro-upgrades functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_QUERY)

        # if we're RHEL, then we don't have preupgrade
        if not os.path.exists('/usr/share/preupgrade/releases.list'):
            return

        # parse the releases file
        config = ConfigParser.ConfigParser()
        config.read('/usr/share/preupgrade/releases.list')

        # find the newest release
        newest = None
        last_version = 0
        for section in config.sections():
            # we only care about stable versions
            if config.has_option(section, 'stable') and config.getboolean(section, 'stable'):
                version = config.getfloat(section, 'version')
                if (version > last_version):
                    newest = section
                    last_version = version

        # got no valid data
        if not newest:
            self.error(ERROR_FAILED_CONFIG_PARSING, "could not get latest distro data")

        # are we already on the latest version
        try:
            present_version = float(self.yumbase.conf.yumvar['releasever'])
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        if (present_version >= last_version):
            return

        # if we have an upgrade candidate then pass back data to daemon
        tok = newest.split(" ")
        name = "%s-%s" % (tok[0].lower(), tok[1])
        self.distro_upgrade(DISTRO_UPGRADE_STABLE, name, newest)

    def _get_status(self, notice):
        ut = notice['type']
        if ut == 'security':
            return INFO_SECURITY
        elif ut == 'bugfix':
            return INFO_BUGFIX
        elif ut == 'enhancement':
            return INFO_ENHANCEMENT
        elif ut == 'newpackage':
            return INFO_ENHANCEMENT
        else:
            self.message(MESSAGE_BACKEND_ERROR, "status unrecognised, please report in bugzilla: %s" % ut)
            return INFO_NORMAL

    def get_updates(self, filters):
        '''
        Implement the get-updates functionality
        @param filters: package types to show
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)

        # yum 'helpfully' keeps an array of updates available
        self.yumbase.up = None

        # clear the package sack so we can get new updates
        self.yumbase.pkgSack = None

        package_list = []
        pkgfilter = YumFilter(filters)
        pkgs = []
        try:
            ygl = self.yumbase.doPackageLists(pkgnarrow='updates')
            pkgs.extend(ygl.updates)
            ygl = self.yumbase.doPackageLists(pkgnarrow='obsoletes')
            pkgs.extend(ygl.obsoletes)
        except yum.Errors.RepoError, e:
            self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # some packages should be updated before the others
        infra_pkgs = []
        for pkg in pkgs:
            if pkg.name in self.infra_packages or pkg.name.partition('-')[0] in self.infra_packages:
                infra_pkgs.append(pkg)
        if len(infra_pkgs) > 0:
            if len(infra_pkgs) < len(pkgs):
                msg = []
                for pkg in infra_pkgs:
                    msg.append(pkg.name)
                self.message(MESSAGE_OTHER_UPDATES_HELD_BACK, "Infrastructure packages take priority. " \
                             "The packages '%s' will be updated before other packages" % msg)
            pkgs = infra_pkgs

        # get the list of installed updates as this is needed for get_applicable_notices()
        installed_dict = {}
        for pkgtup_updated, pkgtup_installed in self.yumbase.up.getUpdatesTuples():
            installed_dict[pkgtup_installed[0]] = pkgtup_installed

        md = self.updateMetadata
        for pkg in unique(pkgs):
            if pkgfilter._filter_base(pkg):
                # we pre-get the ChangeLog data so that the changes file is
                # downloaded at GetUpdates time, not when we open the GUI
                # get each element of the ChangeLog
                try:
                    changelog = pkg.returnChangelog()
                except yum.Errors.RepoError, e:
                    self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

                # fall back to this if there is no installed update or there is no metadata
                status = INFO_NORMAL

                # Get info about package in updates info (using the installed update of this name)
                if installed_dict.has_key(pkg.name):
                    pkgtup = installed_dict[pkg.name]
                    notices = md.get_applicable_notices(pkgtup)
                    if notices:
                        for (pkgtup, notice) in notices:
                            status = self._get_status(notice)
                            if status == INFO_SECURITY:
                                break
                pkgfilter.add_custom(pkg, status)

        package_list = pkgfilter.get_package_list()
        self._show_package_list(package_list)

    def repo_enable(self, repoid, enable):
        '''
        Implement the repo-enable functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.status(STATUS_INFO)
        try:
            repo = self.yumbase.repos.getRepo(repoid)
            if not enable:
                if repo.isEnabled():
                    repo.disablePersistent()
            else:
                if not repo.isEnabled():
                    repo.enablePersistent()
                    if repoid.find ("rawhide") != -1:
                        warning = "These packages are untested and still under development." \
                                  "This repository is used for development of new releases.\n\n" \
                                  "This repository can see significant daily turnover and major " \
                                  "functionality changes which cause unexpected problems with " \
                                  "other development packages.\n" \
                                  "Please use these packages if you want to work with the " \
                                  "Fedora developers by testing these new development packages.\n\n" \
                                  "If this is not correct, please disable the %s software source." % repoid
                        self.message(MESSAGE_REPO_FOR_DEVELOPERS_ONLY, warning.replace("\n", ";"))
        except yum.Errors.RepoError, e:
            self.error(ERROR_REPO_NOT_FOUND, _to_unicode(e))
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        # update the comps groups too
        self.comps.refresh()

    def get_repo_list(self, filters):
        '''
        Implement the get-repo-list functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.status(STATUS_INFO)

        try:
            repos = self.yumbase.repos.repos.values()
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            return
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
            return
        for repo in repos:
            if not FILTER_NOT_DEVELOPMENT in filters or not _is_development_repo(repo.id):
                enabled = repo.isEnabled()
                self.repo_detail(repo.id, repo.name, enabled)

    def _get_obsoleted(self, name):
        try:
            # make sure yum doesn't explode in some internal fit of rage
            self.yumbase.up.doObsoletes()
            obsoletes = self.yumbase.up.getObsoletesTuples(newest=1)
            for (obsoleting, installed) in obsoletes:
                if obsoleting[0] == name:
                    pkg =  self.yumbase.rpmdb.searchPkgTuple(installed)[0]
                    return self._pkg_to_id(pkg)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            pass # no obsolete data - fd#17528
        return ""

    def _get_updated(self, pkg):
        try:
            pkgs = self.yumbase.rpmdb.searchNevra(name=pkg.name, arch=pkg.arch)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        if pkgs:
            return self._pkg_to_id(pkgs[0])
        else:
            return ""

    def _get_update_metadata(self):
        if not self._updateMetadata:
            self._updateMetadata = UpdateMetadata()
            for repo in self.yumbase.repos.listEnabled():
                try:
                    self._updateMetadata.add(repo)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    pass # No updateinfo.xml.gz in repo
        return self._updateMetadata

    _updateMetadata = None
    updateMetadata = property(fget=_get_update_metadata)

    def _get_update_extras(self, pkg):
        md = self.updateMetadata
        notice = md.get_notice((pkg.name, pkg.version, pkg.release))
        urls = {'bugzilla':[], 'cve' : [], 'vendor': []}
        if notice:
            # Update Details
            desc = notice['description']
            if desc:
                desc = desc.replace("\t", " ")

            # add link to bohdi if available
            if notice['from'].find('updates@fedoraproject.org') != -1:
                if notice['update_id']:
                    releasever = self.yumbase.conf.yumvar['releasever']
                    href = "https://admin.fedoraproject.org/updates/F%s/%s" % (releasever, notice['update_id'])
                    title = "%s Update %s" % (notice['release'], notice['update_id'])
                    urls['vendor'].append("%s;%s" % (href, title))

            # Update References (Bugzilla, CVE ...)
            refs = notice['references']
            if refs:
                for ref in refs:
                    typ = ref['type']
                    href = ref['href']
                    title = ref['title'] or ""

                    # Description can sometimes have ';' in them, and we use that as the delimiter
                    title = title.replace(";", ", ")

                    if href:
                        if typ in ('bugzilla', 'cve'):
                            urls[typ].append("%s;%s" % (href, title))
                        else:
                            urls['vendor'].append("%s;%s" % (href, title))

            # other interesting data:
            changelog = ''
            state = notice['status'] or ''
            issued = notice['issued'] or ''
            updated = notice['updated'] or ''
            if updated == issued:
                updated = ''

            # Reboot flag
            if notice.get_metadata().has_key('reboot_suggested') and notice['reboot_suggested']:
                reboot = 'system'
            else:
                reboot = 'none'
            return _format_str(desc), urls, reboot, changelog, state, issued, updated
        else:
            return "", urls, "none", '', '', '', ''

    def get_update_detail(self, package_ids):
        '''
        Implement the get-update_detail functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)
        for package_id in package_ids:
            try:
                pkg, inst = self._findPackage(package_id)
            except PkError, e:
                if e.code == ERROR_PACKAGE_NOT_FOUND:
                    self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, e.details)
                    continue
                self.error(e.code, e.details, exit=True)
                return
            if pkg == None:
                self.message(MESSAGE_COULD_NOT_FIND_PACKAGE, "could not find %s" % _format_package_id(package_id))
                continue
            update = self._get_updated(pkg)
            obsolete = self._get_obsoleted(pkg.name)
            desc, urls, reboot, changelog, state, issued, updated = self._get_update_extras(pkg)

            # the metadata stores broken ISO8601 formatted values
            issued = issued.replace(" ", "T")
            updated = updated.replace(" ", "T")

            # extract the changelog for the local package
            if len(changelog) == 0:

                # get the current installed version of the package
                instpkg = None
                try:
                    instpkgs = self.yumbase.rpmdb.searchNevra(name=pkg.name)
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                if len(instpkgs) == 1:
                    instpkg = instpkgs[0]

                # get each element of the ChangeLog
                try:
                    changes = pkg.returnChangelog()
                except yum.Errors.RepoError, e:
                    self.error(ERROR_REPO_NOT_AVAILABLE, _to_unicode(e))
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                for change in changes:

                    # ensure change has require number of fields
                    if len(change) != 3:
                        changelog += ";*Could not parse change element:* '%s';" % str(change)
                        continue

                    # get version number from "Seth Vidal <skvidal at fedoraproject.org> - 3:3.2.20-1"
                    header = _to_unicode(change[1])
                    version = header.rsplit(' ', 1)

                    # is older than what we have already?
                    if instpkg:
                        evr = ('0', '0', '0')
                        try:
                            evr = _getEVR(version[1])
                        except Exception, e:
                            pass
                        if evr == ('0', '0', '0'):
                            changelog += ";*Could not parse header:* '%s', *expected*: 'Firstname Lastname <email@account.com> - version-release';" % header
                        rc = rpmUtils.miscutils.compareEVR((instpkg.epoch, instpkg.version, instpkg.release.split('.')[0]), evr)
                        if rc >= 0:
                            break

                    gmtime = time.gmtime(change[0])
                    time_str = "%i-%02i-%02i" % (gmtime[0], gmtime[1], gmtime[2])
                    body = _to_unicode(change[2].replace("\t", " "))
                    changelog += _format_str('**' + time_str + '** ' + header + '\n' + body + '\n\n')

            cve_url = _format_list(urls['cve'])
            bz_url = _format_list(urls['bugzilla'])
            vendor_url = _format_list(urls['vendor'])
            self.update_detail(package_id, update, obsolete, vendor_url, bz_url, cve_url, reboot, desc, changelog, state, issued, updated)

    def repo_set_data(self, repoid, parameter, value):
        '''
        Implement the repo-set-data functionality
        '''
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        # Get the repo
        try:
            repo = self.yumbase.repos.getRepo(repoid)
        except yum.Errors.RepoError, e:
            self.error(ERROR_REPO_NOT_FOUND, "repo '%s' cannot be found in list" % repoid, exit=False)
        except exceptions.IOError, e:
            self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
        else:
            if not repo:
                self.error(ERROR_REPO_NOT_FOUND, 'repo %s not found' % repoid, exit=False)
                return
            repo.cfg.set(repoid, parameter, value)
            try:
                repo.cfg.write(file(repo.repofile, 'w'))
            except IOError, e:
                self.error(ERROR_CANNOT_WRITE_REPO_CONFIG, _to_unicode(e))

    def install_signature(self, sigtype, key_id, package_id):
        try:
            self._check_init()
        except PkError, e:
            self.error(e.code, e.details, exit=False)
            return
        self.yumbase.conf.cache = 0 # Allow new files
        self.allow_cancel(True)
        self.percentage(None)
        self.status(STATUS_INFO)
        if package_id.startswith(';;;'): #This is a repo signature
            repoid = package_id.split(';')[-1]
            repo = self.yumbase.repos.getRepo(repoid)
            if repo:
                try:
                    self.yumbase.repos.doSetup(thisrepo=repoid)
                    self.yumbase.getKeyForRepo(repo, callback = lambda x: True)
                except yum.Errors.YumBaseError, e:
                    self.error(ERROR_UNKNOWN, "cannot install signature: %s" % str(e))
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_GPG_FAILURE, "Error importing GPG Key for the %s repository: %s" % (repo, str(e)))
        else: # This is a package signature
            try:
                pkg, inst = self._findPackage(package_id)
            except PkError, e:
                self.error(e.code, e.details, exit=True)
                return
            if pkg:
                try:
                    self.yumbase.getKeyForPackage(pkg, askcb = lambda x, y, z: True)
                except yum.Errors.YumBaseError, e:
                    self.error(ERROR_UNKNOWN, "cannot install signature: %s" % str(e))
                except exceptions.IOError, e:
                    self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
                except Exception, e:
                    self.error(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))
                except:
                    self.error(ERROR_GPG_FAILURE, "Error importing GPG Key for %s" % pkg)


    def _check_init(self, lazy_cache=False):
        '''Just does the caching tweaks'''

        # this entire section can be cancelled
        self.allow_cancel(True)

        # clear previous transaction data
        self.yumbase._tsInfo = None

        # we are working offline
        if not self.has_network:
            for repo in self.yumbase.repos.listEnabled():
                repo.metadata_expire = -1  # never refresh
            self.yumbase.conf.cache = 1

        # choose a good default if the client didn't specify a timeout
        if self.cache_age == 0:
            self.cache_age = 60 * 60 * 24  # 24 hours
        for repo in self.yumbase.repos.listEnabled():
            # is physical media
            if repo.mediaid:
                continue
            repo.metadata_expire = self.cache_age

        # disable repos that are not contactable
        for repo in self.yumbase.repos.listEnabled():
            try:
                if not repo.mediaid:
                    repo.repoXML
                else:
                    root = self.yumbase._media_find_root(repo.mediaid)
                    if not root:
                        self.yumbase.repos.disableRepo(repo.id)
                        self.message(MESSAGE_REPO_METADATA_DOWNLOAD_FAILED,
                                     "Could not contact media source '%s', so it will be disabled" % repo.id)
            except exceptions.IOError, e:
                self.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except yum.Errors.RepoError, e:
                self.yumbase.repos.disableRepo(repo.id)
                self.message(MESSAGE_REPO_METADATA_DOWNLOAD_FAILED, "Could not contact source '%s', so it will be disabled" % repo.id)

        # should we suggest yum-complete-transaction?
        unfinished = yum.misc.find_unfinished_transactions(yumlibpath=self.yumbase.conf.persistdir)
        if unfinished and not lazy_cache:
            raise PkError(ERROR_FAILED_INITIALIZATION, 'There are unfinished transactions remaining. Please run yum-complete-transaction as root.')

        # default to 100% unless method overrides
        self.yumbase.conf.throttle = "90%"

    def _setup_yum(self):
        try:
            # setup Yum Config
            self.yumbase.doConfigSetup(errorlevel=-1, debuglevel=-1)
        except Exception, e:
            raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

        self.yumbase.rpmdb.auto_close = True
        self.dnlCallback = DownloadCallback(self, showNames=True)
        self.yumbase.repos.setProgressBar(self.dnlCallback)

class DownloadCallback(BaseMeter):
    """ Customized version of urlgrabber.progress.BaseMeter class """
    def __init__(self, base, showNames = False):
        BaseMeter.__init__(self)
        self.base = base
        self.percent_start = 0
        self.saved_pkgs = None
        self.number_packages = 0
        self.download_package_number = 0

    def setPackages(self, new_pkgs, percent_start, percent_length):
        self.saved_pkgs = new_pkgs
        self.number_packages = float(len(self.saved_pkgs))
        self.percent_start = percent_start

    def _getPackage(self, name):

        # no name
        if not name:
            return

        # no download data
        if not self.saved_pkgs:
            return None

        # split into name, version, release
        # for yum, name is:
        #  - gnote-0.1.2-2.fc11.i586.rpm
        # and for Presto:
        #  - gnote-0.1.1-4.fc11_0.1.2-2.fc11.i586.drpm
        sections = name.rsplit('-', 2)
        if len(sections) < 3:
            return None

        # we need to search the saved packages for a match and then return the pkg
        for pkg in self.saved_pkgs:
            if sections[0] == pkg.name:
                return pkg

        # nothing matched
        return None

    def update(self, amount_read, now=None):
        BaseMeter.update(self, amount_read, now)

    def _do_start(self, now=None):
        name = self._getName()
        if not name:
            return
        self.updateProgress(name, 0.0, "", "")

    def _do_update(self, amount_read, now=None):

        fread = format_number(amount_read)
        name = self._getName()
        if self.size is None:
            # Elapsed time
            etime = self.re.elapsed_time()
            frac = 0.0
            self.updateProgress(name, frac, fread, '')
        else:
            # Remaining time
            rtime = self.re.remaining_time()
            frac = self.re.fraction_read()
            self.updateProgress(name, frac, fread, '')

    def _do_end(self, amount_read, now=None):

        total_size = format_number(amount_read)
        name = self._getName()
        if not name:
            return
        self.updateProgress(name, 1.0, total_size, '')

    def _getName(self):
        '''
        Get the name of the package being downloaded
        '''
        return self.basename

    def updateProgress(self, name, frac, fread, ftime):
        '''
         Update the progressbar (Overload in child class)
        @param name: filename
        @param frac: Progress fracment (0 -> 1)
        @param fread: formated string containing BytesRead
        @param ftime: formated string containing remaining or elapsed time
        '''

        val = int(frac*100)

        # new package
        if val == 0 and name:
            pkg = self._getPackage(name)
            if pkg: # show package to download
                self.base._show_package(pkg, INFO_DOWNLOADING)
            else:
                for key in MetaDataMap.keys():
                    if key in name:
                        typ = MetaDataMap[key]
                        self.base.status(typ)
                        break

        # package finished
        if val == 100 and name:
            pkg = self._getPackage(name)
            if pkg:
                self.base._show_package(pkg, INFO_FINISHED)

        # set sub-percentage
        self.base.sub_percentage(val)

        # refine percentage with subpercentage
        pct_start = StatusPercentageMap[STATUS_DOWNLOAD]
        pct_end = StatusPercentageMap[STATUS_SIG_CHECK]

        if self.number_packages > 0:
            div = (pct_end - pct_start) / self.number_packages
            pct = pct_start + (div * self.download_package_number) + ((div / 100.0) * val)
            self.base.percentage(pct)

        # keep track of how many we downloaded
        if val == 100:
            self.download_package_number += 1

class PackageKitCallback(RPMBaseCallback):
    def __init__(self, base):
        RPMBaseCallback.__init__(self)
        self.base = base
        self.curpkg = None
        self.percent_start = 0
        self.percent_length = 0

    def _showName(self, status):
        # curpkg is a yum package object or simple string of the package name
        if type(self.curpkg) in types.StringTypes:
            package_id = self.base.get_package_id(self.curpkg, '', '', '')
            # we don't know the summary text
            self.base.package(package_id, status, "")
        else:
            # local file shouldn't put the path in the package_id
            repo_id = _to_unicode(self.curpkg.repo.id)
            if repo_id.find("/") != -1:
                repo_id = 'local'

            pkgver = _get_package_ver(self.curpkg)
            package_id = self.base.get_package_id(self.curpkg.name, pkgver, self.curpkg.arch, repo_id)
            self.base.package(package_id, status, self.curpkg.summary)

    def event(self, package, action, te_current, te_total, ts_current, ts_total):

        if str(package) != str(self.curpkg):
            self.curpkg = package
            try:
                self.base.status(TransactionsStateMap[action])
                self._showName(TransactionsInfoMap[action])
            except exceptions.IOError, e:
                self.base.error(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
            except exceptions.KeyError, e:
                self.base.message(MESSAGE_BACKEND_ERROR, "The constant '%s' was unknown, please report. details: %s" % (action, _to_unicode(e)))

        # do subpercentage
        if te_total > 0:
            val = (te_current*100L)/te_total
            self.base.sub_percentage(val)

        # find out the offset
        pct_start = StatusPercentageMap[STATUS_INSTALL]

        # do percentage
        if ts_total > 0:
            div = (100 - pct_start) / ts_total
            pct = div * (ts_current - 1) + pct_start + ((div / 100.0) * val)
            self.base.percentage(pct)

    def errorlog(self, msg):
        # grrrrrrrr
        pass

class ProcessTransPackageKitCallback:
    def __init__(self, base):
        self.base = base

    def event(self, state, data=None):

        if state == PT_DOWNLOAD:        # Start Downloading
            self.base.allow_cancel(True)
            pct_start = StatusPercentageMap[STATUS_DOWNLOAD]
            self.base.percentage(pct_start)
            self.base.status(STATUS_DOWNLOAD)
        elif state == PT_DOWNLOAD_PKGS:   # Packages to download
            self.base.dnlCallback.setPackages(data, 10, 30)
        elif state == PT_GPGCHECK:
            pct_start = StatusPercentageMap[STATUS_SIG_CHECK]
            self.base.percentage(pct_start)
            self.base.status(STATUS_SIG_CHECK)
        elif state == PT_TEST_TRANS:
            pct_start = StatusPercentageMap[STATUS_TEST_COMMIT]
            self.base.allow_cancel(False)
            self.base.percentage(pct_start)
            self.base.status(STATUS_TEST_COMMIT)
        elif state == PT_TRANSACTION:
            pct_start = StatusPercentageMap[STATUS_INSTALL]
            self.base.allow_cancel(False)
            self.base.percentage(pct_start)
        else:
            self.base.message(MESSAGE_BACKEND_ERROR, "unhandled transaction state: %s" % state)

class DepSolveCallback(object):

    # takes a PackageKitBackend so we can call StatusChanged on it.
    # That's kind of hurky.
    def __init__(self, backend):
        self.started = False
        self.backend = backend

    def start(self):
        if not self.started:
            self.backend.status(STATUS_DEP_RESOLVE)
            pct_start = StatusPercentageMap[STATUS_DEP_RESOLVE]
            self.backend.percentage(pct_start)

    # Be lazy and not define the others explicitly
    def _do_nothing(self, *args, **kwargs):
        pass

    def __getattr__(self, x):
        return self._do_nothing

class PackageKitYumBase(yum.YumBase):
    """
    Subclass of YumBase.  Needed so we can overload _checkSignatures
    and nab the gpg sig data
    """

    def __init__(self, backend):
        yum.YumBase.__init__(self)

        # YumBase.run_with_package_names is used to record which packages are
        # involved in executing a transaction
        if hasattr(self, 'run_with_package_names'):
            self.run_with_package_names.add('PackageKit-yum')

        # load the config file
        config = ConfigParser.ConfigParser()
        try:
            config.read('/etc/PackageKit/Yum.conf')
            disabled_plugins = config.get('Backend', 'DisabledPlugins').split(';')
        except ConfigParser.NoOptionError, e:
            disabled_plugins = []
        except Exception, e:
            raise PkError(ERROR_REPO_CONFIGURATION_ERROR, "Failed to load Yum.conf: %s" % _to_unicode(e))
        disabled_plugins.append('refresh-packagekit')

        # disable the PackageKit plugin when running under PackageKit
        try:
            pc = self.preconf
            pc.disabled_plugins = disabled_plugins
        except yum.Errors.ConfigError, e:
            raise PkError(ERROR_REPO_CONFIGURATION_ERROR, _to_unicode(e))
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except ValueError, e:
            raise PkError(ERROR_FAILED_CONFIG_PARSING, _to_unicode(e))

        # setup to use LANG for descriptions
        yum.misc.setup_locale(override_time=True, override_codecs=False)

        self.missingGPGKey = None
        self.dsCallback = DepSolveCallback(backend)
        self.backend = backend
        self.mediagrabber = self.MediaGrabber

        # Enable new callback mode on yum versions that support it
        self.use_txmbr_in_callback = True

        # setup Repo GPG support callbacks
        #
        # self.preconf may or may not exist at this point...
        # What we think is happening is that it's going:
        #
        #    PK.init => yum._getRepos => yum.conf =>
        #    plugins *.init_hook => RHN.init => conduit.getRepos() =>
        #    yum._getRepos
        #
        # ..at which point we try to setup from prerepoconf twice, and
        # the second time it fails.
        try:
            if hasattr(self, 'prerepoconf'):
                self.prerepoconf.confirm_func = self._repo_gpg_confirm
                self.prerepoconf.gpg_import_func = self._repo_gpg_import
            else:
                self.repos.confirm_func = self._repo_gpg_confirm
                self.repos.gpg_import_func = self._repo_gpg_import
        except exceptions.IOError, e:
            raise PkError(ERROR_NO_SPACE_ON_DEVICE, "Disk error: %s" % _to_unicode(e))
        except Exception, e:
            # helpfully, yum gives us TypeError when it can't open the rpmdb
            if str(e).find('rpmdb open failed') != -1:
                raise PkError(ERROR_FAILED_INITIALIZATION, _format_str(traceback.format_exc()))
            else:
                raise PkError(ERROR_INTERNAL_ERROR, _format_str(traceback.format_exc()))

    def _media_find_root(self, media_id):
        """ returns the root "/media/Fedora Extras" or None """

        # search all the disks
        vm = gio.volume_monitor_get()
        mounts = vm.get_mounts()
        for mount in mounts:
            # is it mounted
            root = mount.get_root().get_path()

            # is it a media disc
            discinfo = "%s/.discinfo" % root
            if not os.path.exists(discinfo):
                continue

            # get the contents
            f = open(discinfo, "r")
            lines = f.readlines()
            f.close()

            # not enough lines to be a valid .discinfo
            if len(lines) < 3:
                continue

            # check this is the right disk
            media_id_tmp = lines[0].strip()
            if cmp(media_id_tmp, media_id) != 0:
                continue

            return root

        # nothing remaining
        return None

    def MediaGrabber(self, *args, **kwargs):
        """
        Handle physical media.
        """
        root = self._media_find_root(kwargs["mediaid"])
        if root:
            # the actual copying is done by URLGrabber
            ug = URLGrabber(checkfunc = kwargs["checkfunc"])
            try:
                ug.urlgrab("%s/%s" % (root, kwargs["relative"]),
                           kwargs["local"], text=kwargs["text"],
                           range=kwargs["range"], copy_local=1)
            except (IOError, URLGrabError), e:
                pass

        # we have to send a message to the client
        if not root:
            name = "%s Volume #%s" % (kwargs["name"], kwargs["discnum"])
            self.backend.media_change_required(MEDIA_TYPE_DISC, name, name)
            self.backend.error(ERROR_MEDIA_CHANGE_REQUIRED,
                               "Insert media labeled '%s' or disable media repos" % name,
                               exit=False)
            raise yum.Errors.MediaError, "The disc was not inserted"

        # yay
        return kwargs["local"]

    def _repo_gpg_confirm(self, keyData):
        """ Confirm Repo GPG signature import """
        if not keyData:
            self.backend.error(ERROR_BAD_GPG_SIGNATURE,
                       "GPG key not imported, and no GPG information was found.")
        repo = keyData['repo']
        fingerprint = keyData['fingerprint']()
        hex_fingerprint = "%02x" * len(fingerprint) % tuple(map(ord, fingerprint))
        # Borrowed from http://mail.python.org/pipermail/python-list/2000-September/053490.html

        self.backend.repo_signature_required(";;;%s" % repo.id,
                                     repo.id,
                                     keyData['keyurl'].replace("file://", ""),
                                     keyData['userid'],
                                     keyData['hexkeyid'],
                                     hex_fingerprint,
                                     time.ctime(keyData['timestamp']),
                                     'gpg')
        self.backend.error(ERROR_GPG_FAILURE, "GPG key %s required" % keyData['hexkeyid'])

    def _repo_gpg_import(self, repo, confirm):
        """ Repo GPG signature importer"""
        self.getKeyForRepo(repo, callback=confirm)

    def _checkSignatures(self, pkgs, callback):
        ''' The the signatures of the downloaded packages '''
        # This can be overloaded by a subclass.

        for po in pkgs:
            result, errmsg = self.sigCheckPkg(po)
            if result == 0:
                # verified ok, or verify not required
                continue
            elif result == 1:
                # verify failed but installation of the correct GPG key might help
                self.getKeyForPackage(po, fullaskcb=self._fullAskForGPGKeyImport)
            else:
                # fatal GPG verification error
                raise yum.Errors.YumGPGCheckError, errmsg
        return 0

    def _fullAskForGPGKeyImport(self, data):
        self.missingGPGKey = data

        raise GPGKeyNotImported()

    def _askForGPGKeyImport(self, po, userid, hexkeyid):
        '''
        Ask for GPGKeyImport
        '''
        # TODO: Add code here to send the RepoSignatureRequired signal
        return False

def main():
    backend = PackageKitYumBackend('', lock=True)
    backend.dispatcher(sys.argv[1:])

if __name__ == "__main__":
    main()