~launchpad-pqm/launchpad/devel

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
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

# pylint: disable-msg=F0401

"""Testing infrastructure for the Launchpad application.

This module should not contain tests (but it should be tested).
"""

__metaclass__ = type
__all__ = [
    'GPGSigningContext',
    'is_security_proxied_or_harmless',
    'LaunchpadObjectFactory',
    'ObjectFactory',
    'remove_security_proxy_and_shout_at_engineer',
    ]

from contextlib import nested
from datetime import (
    datetime,
    timedelta,
    )
from email.encoders import encode_base64
from email.message import Message as EmailMessage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import (
    formatdate,
    make_msgid,
    )
from itertools import count
from operator import (
    isMappingType,
    isSequenceType,
    )
import os
from random import randint
from StringIO import StringIO
import sys
from textwrap import dedent
from threading import local
from types import InstanceType
import warnings

from bzrlib.merge_directive import MergeDirective2
from bzrlib.plugins.builder.recipe import BaseRecipeBranch
import pytz
from pytz import UTC
import simplejson
import transaction
from twisted.python.util import mergeFunctionMetadata
from zope.component import (
    ComponentLookupError,
    getUtility,
    )
from zope.security.proxy import (
    builtin_isinstance,
    Proxy,
    ProxyFactory,
    removeSecurityProxy,
    )

from canonical.config import config
from canonical.database.constants import (
    DEFAULT,
    UTC_NOW,
    )
from canonical.database.sqlbase import flush_database_updates
from canonical.launchpad.database.account import Account
from canonical.launchpad.interfaces.account import (
    AccountCreationRationale,
    AccountStatus,
    IAccountSet,
    )
from canonical.launchpad.interfaces.emailaddress import (
    EmailAddressStatus,
    IEmailAddressSet,
    )
from canonical.launchpad.interfaces.gpghandler import IGPGHandler
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
from canonical.launchpad.interfaces.lpstorm import (
    IMasterStore,
    IStore,
    )
from canonical.launchpad.interfaces.oauth import IOAuthConsumerSet
from canonical.launchpad.interfaces.temporaryblobstorage import (
    ITemporaryStorageManager,
    )
from canonical.launchpad.webapp.dbpolicy import MasterDatabasePolicy
from canonical.launchpad.webapp.interfaces import (
    DEFAULT_FLAVOR,
    IStoreSelector,
    MAIN_STORE,
    OAuthPermission,
    )
from canonical.launchpad.webapp.sorting import sorted_version_numbers
from lp.app.enums import ServiceUsage
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.archivepublisher.interfaces.publisherconfig import IPublisherConfigSet
from lp.archiveuploader.dscfile import DSCFile
from lp.archiveuploader.uploadpolicy import BuildDaemonUploadPolicy
from lp.blueprints.enums import (
    NewSpecificationDefinitionStatus,
    SpecificationDefinitionStatus,
    SpecificationPriority,
    )
from lp.blueprints.interfaces.specification import ISpecificationSet
from lp.blueprints.interfaces.sprint import ISprintSet
from lp.bugs.interfaces.bug import (
    CreateBugParams,
    IBugSet,
    )
from lp.bugs.interfaces.bugtask import BugTaskStatus
from lp.bugs.interfaces.bugtracker import (
    BugTrackerType,
    IBugTrackerSet,
    )
from lp.bugs.interfaces.bugwatch import IBugWatchSet
from lp.bugs.interfaces.cve import (
    CveStatus,
    ICveSet,
    )
from lp.buildmaster.enums import (
    BuildFarmJobType,
    BuildStatus,
    )
from lp.buildmaster.interfaces.builder import IBuilderSet
from lp.buildmaster.model.buildqueue import BuildQueue
from lp.code.enums import (
    BranchMergeProposalStatus,
    BranchSubscriptionNotificationLevel,
    BranchType,
    CodeImportMachineState,
    CodeImportResultStatus,
    CodeImportReviewStatus,
    CodeReviewNotificationLevel,
    RevisionControlSystems,
    )
from lp.code.errors import UnknownBranchTypeError
from lp.code.interfaces.branchmergequeue import IBranchMergeQueueSource
from lp.code.interfaces.branchnamespace import get_branch_namespace
from lp.code.interfaces.branchtarget import IBranchTarget
from lp.code.interfaces.codeimport import ICodeImportSet
from lp.code.interfaces.codeimportevent import ICodeImportEventSet
from lp.code.interfaces.codeimportmachine import ICodeImportMachineSet
from lp.code.interfaces.codeimportresult import ICodeImportResultSet
from lp.code.interfaces.linkedbranch import ICanHasLinkedBranch
from lp.code.interfaces.revision import IRevisionSet
from lp.code.interfaces.sourcepackagerecipe import (
    ISourcePackageRecipeSource,
    MINIMAL_RECIPE_TEXT,
    )
from lp.code.interfaces.sourcepackagerecipebuild import (
    ISourcePackageRecipeBuildSource,
    )
from lp.code.model.diff import (
    Diff,
    PreviewDiff,
    StaticDiff,
    )
from lp.code.model.recipebuild import RecipeBuildRecord
from lp.codehosting.codeimport.worker import CodeImportSourceDetails
from lp.hardwaredb.interfaces.hwdb import (
    HWSubmissionFormat,
    IHWDeviceDriverLinkSet,
    IHWSubmissionDeviceSet,
    IHWSubmissionSet,
    )
from lp.registry.enum import (
    DistroSeriesDifferenceStatus,
    DistroSeriesDifferenceType,
    )
from lp.registry.interfaces.distribution import IDistributionSet
from lp.registry.interfaces.distributionmirror import (
    MirrorContent,
    MirrorSpeed,
    )
from lp.registry.interfaces.distributionsourcepackage import (
    IDistributionSourcePackage,
    )
from lp.registry.interfaces.distroseries import IDistroSeries
from lp.registry.interfaces.distroseriesdifference import (
    IDistroSeriesDifferenceSource,
    )
from lp.registry.interfaces.distroseriesdifferencecomment import (
    IDistroSeriesDifferenceCommentSource,
    )
from lp.registry.interfaces.distroseriesparent import IDistroSeriesParentSet
from lp.registry.interfaces.gpg import (
    GPGKeyAlgorithm,
    IGPGKeySet,
    )
from lp.registry.interfaces.mailinglist import (
    IMailingListSet,
    MailingListStatus,
    )
from lp.registry.interfaces.mailinglistsubscription import (
    MailingListAutoSubscribePolicy,
    )
from lp.registry.interfaces.packaging import (
    IPackagingUtil,
    PackagingType,
    )
from lp.registry.interfaces.person import (
    IPerson,
    IPersonSet,
    PersonCreationRationale,
    TeamSubscriptionPolicy,
    )
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.poll import (
    IPollSet,
    PollAlgorithm,
    PollSecrecy,
    )
from lp.registry.interfaces.product import (
    IProductSet,
    License,
    )
from lp.registry.interfaces.productseries import IProductSeries
from lp.registry.interfaces.projectgroup import IProjectGroupSet
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.interfaces.sourcepackage import (
    ISourcePackage,
    SourcePackageFileType,
    SourcePackageUrgency,
    )
from lp.registry.interfaces.sourcepackagename import ISourcePackageNameSet
from lp.registry.interfaces.ssh import ISSHKeySet
from lp.registry.model.milestone import Milestone
from lp.registry.model.suitesourcepackage import SuiteSourcePackage
from lp.services.job.interfaces.job import SuspendJobException
from lp.services.log.logger import BufferLogger
from lp.services.mail.signedmessage import SignedMessage
from lp.services.messages.model.message import (
    Message,
    MessageChunk,
    )
from lp.services.openid.model.openididentifier import OpenIdIdentifier
from lp.services.propertycache import clear_property_cache
from lp.services.utils import AutoDecorate
from lp.services.worlddata.interfaces.country import ICountrySet
from lp.services.worlddata.interfaces.language import ILanguageSet
from lp.soyuz.adapters.overrides import SourceOverride
from lp.soyuz.adapters.packagelocation import PackageLocation
from lp.soyuz.enums import (
    ArchivePurpose,
    BinaryPackageFileType,
    BinaryPackageFormat,
    PackageDiffStatus,
    PackagePublishingPriority,
    PackagePublishingStatus,
    PackageUploadCustomFormat,
    PackageUploadStatus,
    )
from lp.soyuz.interfaces.archive import (
    default_name_by_purpose,
    IArchiveSet,
    )
from lp.soyuz.interfaces.archivepermission import IArchivePermissionSet
from lp.soyuz.interfaces.binarypackagebuild import IBinaryPackageBuildSet
from lp.soyuz.interfaces.binarypackagename import IBinaryPackageNameSet
from lp.soyuz.interfaces.component import (
    IComponent,
    IComponentSet,
    )
from lp.soyuz.interfaces.packagecopyjob import IPlainPackageCopyJobSource
from lp.soyuz.interfaces.packageset import IPackagesetSet
from lp.soyuz.interfaces.processor import IProcessorFamilySet
from lp.soyuz.interfaces.publishing import IPublishingSet
from lp.soyuz.interfaces.queue import IPackageUploadSet
from lp.soyuz.interfaces.section import ISectionSet
from lp.soyuz.model.component import ComponentSelection
from lp.soyuz.model.files import (
    BinaryPackageFile,
    SourcePackageReleaseFile,
    )
from lp.soyuz.model.packagediff import PackageDiff
from lp.soyuz.model.processor import ProcessorFamilySet
from lp.testing import (
    ANONYMOUS,
    celebrity_logged_in,
    launchpadlib_for,
    login,
    login_as,
    login_person,
    person_logged_in,
    run_with_login,
    temp_dir,
    time_counter,
    with_celebrity_logged_in,
    )
from lp.translations.enums import (
    LanguagePackType,
    RosettaImportStatus,
    )
from lp.translations.interfaces.languagepack import ILanguagePackSet
from lp.translations.interfaces.potemplate import IPOTemplateSet
from lp.translations.interfaces.side import TranslationSide
from lp.translations.interfaces.translationfileformat import (
    TranslationFileFormat,
    )
from lp.translations.interfaces.translationgroup import ITranslationGroupSet
from lp.translations.interfaces.translationmessage import (
    RosettaTranslationOrigin,
    )
from lp.translations.interfaces.translationsperson import ITranslationsPerson
from lp.translations.interfaces.translationtemplatesbuildjob import (
    ITranslationTemplatesBuildJobSource,
    )
from lp.translations.interfaces.translator import ITranslatorSet
from lp.translations.model.translationimportqueue import (
    TranslationImportQueueEntry,
    )
from lp.translations.model.translationtemplateitem import (
    TranslationTemplateItem,
    )
from lp.translations.utilities.sanitize import (
    sanitize_translations_from_webui,
    )


SPACE = ' '

DIFF = """\
=== zbqvsvrq svyr 'yvo/yc/pbqr/vagresnprf/qvss.cl'
--- yvo/yc/pbqr/vagresnprf/qvss.cl      2009-10-01 13:25:12 +0000
+++ yvo/yc/pbqr/vagresnprf/qvss.cl      2010-02-02 15:48:56 +0000
@@ -121,6 +121,10 @@
                 'Gur pbasyvpgf grkg qrfpevovat nal cngu be grkg pbasyvpgf.'),
              ernqbayl=Gehr))

+    unf_pbasyvpgf = Obby(
+        gvgyr=_('Unf pbasyvpgf'), ernqbayl=Gehr,
+        qrfpevcgvba=_('Gur cerivrjrq zretr cebqhprf pbasyvpgf.'))
+
     # Gur fpurzn sbe gur Ersrerapr trgf cngpurq va _fpurzn_pvephyne_vzcbegf.
     oenapu_zretr_cebcbfny = rkcbegrq(
         Ersrerapr(
"""


def default_master_store(func):
    """Decorator to temporarily set the default Store to the master.

    In some cases, such as in the middle of a page test story,
    we might be calling factory methods with the default Store set
    to the slave which breaks stuff. For instance, if we set an account's
    password that needs to happen on the master store and this is forced.
    However, if we then read it back the default Store has to be used.
    """

    def with_default_master_store(*args, **kw):
        try:
            store_selector = getUtility(IStoreSelector)
        except ComponentLookupError:
            # Utilities not registered. No policies.
            return func(*args, **kw)
        store_selector.push(MasterDatabasePolicy())
        try:
            return func(*args, **kw)
        finally:
            store_selector.pop()
    return mergeFunctionMetadata(func, with_default_master_store)


# We use this for default parameters where None has a specific meaning. For
# example, makeBranch(product=None) means "make a junk branch". None, because
# None means "junk branch".
_DEFAULT = object()


class GPGSigningContext:
    """A helper object to hold the fingerprint, password and mode."""

    def __init__(self, fingerprint, password='', mode=None):
        self.fingerprint = fingerprint
        self.password = password
        self.mode = mode


class ObjectFactory:
    """Factory methods for creating basic Python objects."""

    __metaclass__ = AutoDecorate(default_master_store)

    def __init__(self):
        # Initialize the unique identifier.
        self._local = local()

    def getUniqueEmailAddress(self):
        return "%s@example.com" % self.getUniqueString('email')

    def getUniqueInteger(self):
        """Return an integer unique to this factory instance.

        For each thread, this will be a series of increasing numbers, but the
        starting point will be unique per thread.
        """
        counter = getattr(self._local, 'integer', None)
        if counter is None:
            counter = count(randint(0, 1000000))
            self._local.integer = counter
        return counter.next()

    def getUniqueHexString(self, digits=None):
        """Return a unique hexadecimal string.

        :param digits: The number of digits in the string. 'None' means you
            don't care.
        :return: A hexadecimal string, with 'a'-'f' in lower case.
        """
        hex_number = '%x' % self.getUniqueInteger()
        if digits is not None:
            hex_number = hex_number.zfill(digits)
        return hex_number

    def getUniqueString(self, prefix=None):
        """Return a string unique to this factory instance.

        The string returned will always be a valid name that can be used in
        Launchpad URLs.

        :param prefix: Used as a prefix for the unique string. If
            unspecified, generates a name starting with 'unique' and
            mentioning the calling source location.
        """
        if prefix is None:
            frame = sys._getframe(2)
            source_filename = frame.f_code.co_filename
            # Dots and dashes cause trouble with some consumers of these
            # names.
            source = (
                os.path.basename(source_filename)
                .replace('_', '-')
                .replace('.', '-'))
            if source.startswith(
                    '<doctest '):
                # Like '-<doctest xx-build-summary-txt[10]>'.
                source = (source
                    .replace('<doctest ', '')
                    .replace('[', '')
                    .replace(']>', ''))
            prefix = 'unique-from-%s-line%d' % (
                source, frame.f_lineno)
        string = "%s-%s" % (prefix, self.getUniqueInteger())
        return string

    def getUniqueUnicode(self):
        return self.getUniqueString().decode('latin-1')

    def getUniqueURL(self, scheme=None, host=None):
        """Return a URL unique to this run of the test case."""
        if scheme is None:
            scheme = 'http'
        if host is None:
            host = "%s.domain.com" % self.getUniqueString('domain')
        return '%s://%s/%s' % (scheme, host, self.getUniqueString('path'))

    def getUniqueDate(self):
        """Return a unique date since January 1 2009.

        Each date returned by this function will more recent (or further into
        the future) than the previous one.
        """
        epoch = datetime(2009, 1, 1, tzinfo=pytz.UTC)
        return epoch + timedelta(minutes=self.getUniqueInteger())

    def makeCodeImportSourceDetails(self, branch_id=None, rcstype=None,
                                    url=None, cvs_root=None, cvs_module=None):
        if branch_id is None:
            branch_id = self.getUniqueInteger()
        if rcstype is None:
            rcstype = 'svn'
        if rcstype in ['svn', 'bzr-svn', 'hg']:
            assert cvs_root is cvs_module is None
            if url is None:
                url = self.getUniqueURL()
        elif rcstype == 'cvs':
            assert url is None
            if cvs_root is None:
                cvs_root = self.getUniqueString()
            if cvs_module is None:
                cvs_module = self.getUniqueString()
        elif rcstype == 'git':
            assert cvs_root is cvs_module is None
            if url is None:
                url = self.getUniqueURL(scheme='git')
        else:
            raise AssertionError("Unknown rcstype %r." % rcstype)
        return CodeImportSourceDetails(
            branch_id, rcstype, url, cvs_root, cvs_module)


class BareLaunchpadObjectFactory(ObjectFactory):
    """Factory methods for creating Launchpad objects.

    All the factory methods should be callable with no parameters.
    When this is done, the returned object should have unique references
    for any other required objects.
    """

    def loginAsAnyone(self):
        """Log in as an arbitrary person.

        If you want to log in as a celebrity, including admins, see
        `lp.testing.login_celebrity`.
        """
        login(ANONYMOUS)
        person = self.makePerson()
        login_as(person)
        return person

    @with_celebrity_logged_in('admin')
    def makeAdministrator(self, name=None, email=None, password=None):
        user = self.makePerson(name=name,
                               email=email,
                               password=password)
        administrators = getUtility(ILaunchpadCelebrities).admin
        administrators.addMember(user, administrators.teamowner)
        return user

    def makeRegistryExpert(self, name=None, email='expert@example.com',
                           password='test'):
        from lp.testing.sampledata import ADMIN_EMAIL
        login(ADMIN_EMAIL)
        user = self.makePerson(name=name,
                               email=email,
                               password=password)
        registry_team = getUtility(ILaunchpadCelebrities).registry_experts
        registry_team.addMember(user, registry_team.teamowner)
        return user

    def makeCopyArchiveLocation(self, distribution=None, owner=None,
        name=None, enabled=True):
        """Create and return a new arbitrary location for copy packages."""
        copy_archive = self.makeArchive(distribution, owner, name,
                                        ArchivePurpose.COPY, enabled)

        distribution = copy_archive.distribution
        distroseries = distribution.currentseries
        pocket = PackagePublishingPocket.RELEASE

        location = PackageLocation(copy_archive, distribution, distroseries,
            pocket)
        return ProxyFactory(location)

    def makeAccount(self, displayname=None, email=None, password=None,
                    status=AccountStatus.ACTIVE,
                    rationale=AccountCreationRationale.UNKNOWN):
        """Create and return a new Account."""
        if displayname is None:
            displayname = self.getUniqueString('displayname')
        account = getUtility(IAccountSet).new(
            rationale, displayname, password=password)
        removeSecurityProxy(account).status = status
        if email is None:
            email = self.getUniqueEmailAddress()
        email_status = EmailAddressStatus.PREFERRED
        if status != AccountStatus.ACTIVE:
            email_status = EmailAddressStatus.NEW
        email = self.makeEmail(
            email, person=None, account=account, email_status=email_status)
        self.makeOpenIdIdentifier(account)
        return account

    def makeOpenIdIdentifier(self, account, identifier=None):
        """Attach an OpenIdIdentifier to an Account."""
        # Unfortunately, there are many tests connecting as many
        # different database users that expect to be able to create
        # working accounts using these factory methods. The stored
        # procedure provides a work around and avoids us having to
        # grant INSERT rights to these database users and avoids the
        # security problems that would cause. The stored procedure
        # ensures that there is at least one OpenId Identifier attached
        # to the account that can be used to login. If the OpenId
        # Identifier needed to be created, it will not be usable in the
        # production environments so access to execute this stored
        # procedure cannot be used to compromise accounts.
        IMasterStore(OpenIdIdentifier).execute(
            "SELECT add_test_openid_identifier(%s)", (account.id, ))

    def makeGPGKey(self, owner):
        """Give 'owner' a crappy GPG key for the purposes of testing."""
        key_id = self.getUniqueHexString(digits=8).upper()
        fingerprint = key_id + 'A' * 32
        return getUtility(IGPGKeySet).new(
            owner.id,
            keyid=key_id,
            fingerprint=fingerprint,
            keysize=self.getUniqueInteger(),
            algorithm=GPGKeyAlgorithm.R,
            active=True,
            can_encrypt=False)

    def makePerson(
        self, email=None, name=None, password=None,
        email_address_status=None, hide_email_addresses=False,
        displayname=None, time_zone=None, latitude=None, longitude=None,
        selfgenerated_bugnotifications=False, member_of=()):
        """Create and return a new, arbitrary Person.

        :param email: The email address for the new person.
        :param name: The name for the new person.
        :param password: The password for the person.
            This password can be used in setupBrowser in combination
            with the email address to create a browser for this new
            person.
        :param email_address_status: If specified, the status of the email
            address is set to the email_address_status.
        :param displayname: The display name to use for the person.
        :param hide_email_addresses: Whether or not to hide the person's email
            address(es) from other users.
        :param time_zone: This person's time zone, as a string.
        :param latitude: This person's latitude, as a float.
        :param longitude: This person's longitude, as a float.
        :param selfgenerated_bugnotifications: Receive own bugmail.
        """
        if email is None:
            email = self.getUniqueEmailAddress()
        if name is None:
            name = self.getUniqueString('person-name')
        if password is None:
            password = self.getUniqueString('password')
        # By default, make the email address preferred.
        if (email_address_status is None
                or email_address_status == EmailAddressStatus.VALIDATED):
            email_address_status = EmailAddressStatus.PREFERRED
        # Set the password to test in order to allow people that have
        # been created this way can be logged in.
        person, email = getUtility(IPersonSet).createPersonAndEmail(
            email, rationale=PersonCreationRationale.UNKNOWN, name=name,
            password=password, displayname=displayname,
            hide_email_addresses=hide_email_addresses)
        naked_person = removeSecurityProxy(person)
        naked_person._password_cleartext_cached = password

        assert person.password is not None, (
            'Password not set. Wrong default auth Store?')

        if (time_zone is not None or latitude is not None or
            longitude is not None):
            naked_person.setLocation(latitude, longitude, time_zone, person)

        # Make sure the non-security-proxied object is not returned.
        del naked_person

        if selfgenerated_bugnotifications:
            # Set it explicitely only when True because the default
            # is False.
            person.selfgenerated_bugnotifications = True

        # To make the person someone valid in Launchpad, validate the
        # email.
        if email_address_status == EmailAddressStatus.PREFERRED:
            account = IMasterStore(Account).get(
                Account, person.accountID)
            account.status = AccountStatus.ACTIVE
            person.validateAndEnsurePreferredEmail(email)

        removeSecurityProxy(email).status = email_address_status

        self.makeOpenIdIdentifier(person.account)

        for team in member_of:
            with person_logged_in(team.teamowner):
                team.addMember(person, team.teamowner)

        # Ensure updated ValidPersonCache
        flush_database_updates()
        return person

    def makePersonByName(self, first_name, set_preferred_email=True,
                         use_default_autosubscribe_policy=False):
        """Create a new person with the given first name.

        The person will be given two email addresses, with the 'long form'
        (e.g. anne.person@example.com) as the preferred address.  Return
        the new person object.

        The person will also have their mailing list auto-subscription
        policy set to 'NEVER' unless 'use_default_autosubscribe_policy' is
        set to True. (This requires the Launchpad.Edit permission).  This
        is useful for testing, where we often want precise control over
        when a person gets subscribed to a mailing list.

        :param first_name: First name of the person, capitalized.
        :type first_name: string
        :param set_preferred_email: Flag specifying whether
            <name>.person@example.com should be set as the user's
            preferred email address.
        :type set_preferred_email: bool
        :param use_default_autosubscribe_policy: Flag specifying whether
            the person's `mailing_list_auto_subscribe_policy` should be set.
        :type use_default_autosubscribe_policy: bool
        :return: The newly created person.
        :rtype: `IPerson`
        """
        variable_name = first_name.lower()
        full_name = first_name + ' Person'
        # E.g. firstname.person@example.com will be an alternative address.
        preferred_address = variable_name + '.person@example.com'
        # E.g. aperson@example.org will be the preferred address.
        alternative_address = variable_name[0] + 'person@example.org'
        person, email = getUtility(IPersonSet).createPersonAndEmail(
            preferred_address,
            PersonCreationRationale.OWNER_CREATED_LAUNCHPAD,
            name=variable_name, displayname=full_name)
        if set_preferred_email:
            # setPreferredEmail no longer activates the account
            # automatically.
            account = IMasterStore(Account).get(Account, person.accountID)
            account.activate(
                "Activated by factory.makePersonByName",
                password='foo',
                preferred_email=email)
            person.setPreferredEmail(email)

        if not use_default_autosubscribe_policy:
            # Shut off list auto-subscription so that we have direct control
            # over subscriptions in the doctests.
            with person_logged_in(person):
                person.mailing_list_auto_subscribe_policy = (
                    MailingListAutoSubscribePolicy.NEVER)
        account = IMasterStore(Account).get(Account, person.accountID)
        getUtility(IEmailAddressSet).new(
            alternative_address, person, EmailAddressStatus.VALIDATED,
            account)
        return person

    def makeEmail(self, address, person, account=None, email_status=None):
        """Create a new email address for a person.

        :param address: The email address to create.
        :type address: string
        :param person: The person to assign the email address to.
        :type person: `IPerson`
        :param account: The account to assign the email address to.  Will use
            the given person's account if None is provided.
        :type person: `IAccount`
        :param email_status: The default status of the email address,
            if given.  If not given, `EmailAddressStatus.VALIDATED`
            will be used.
        :type email_status: `EmailAddressStatus`
        :return: The newly created email address.
        :rtype: `IEmailAddress`
        """
        if email_status is None:
            email_status = EmailAddressStatus.VALIDATED
        if account is None:
            account = person.account
        return getUtility(IEmailAddressSet).new(
            address, person, email_status, account)

    def makeTeam(self, owner=None, displayname=None, email=None, name=None,
                 subscription_policy=TeamSubscriptionPolicy.OPEN,
                 visibility=None, members=None):
        """Create and return a new, arbitrary Team.

        :param owner: The person or person name to use as the team's owner.
            If not given, a person will be auto-generated.
        :type owner: `IPerson` or string
        :param displayname: The team's display name.  If not given we'll use
            the auto-generated name.
        :type string:
        :param email: The email address to use as the team's contact address.
        :type email: string
        :param subscription_policy: The subscription policy of the team.
        :type subscription_policy: `TeamSubscriptionPolicy`
        :param visibility: The team's visibility. If it's None, the default
            (public) will be used.
        :type visibility: `PersonVisibility`
        :param members: People or teams to be added to the new team
        :type members: An iterable of objects implementing IPerson
        :return: The new team
        :rtype: `ITeam`
        """
        if owner is None:
            owner = self.makePerson()
        elif isinstance(owner, basestring):
            owner = getUtility(IPersonSet).getByName(owner)
        else:
            pass
        if name is None:
            name = self.getUniqueString('team-name')
        if displayname is None:
            displayname = SPACE.join(
                word.capitalize() for word in name.split('-'))
        team = getUtility(IPersonSet).newTeam(
            owner, name, displayname, subscriptionpolicy=subscription_policy)
        if visibility is not None:
            # Visibility is normally restricted to launchpad.Commercial, so
            # removing the security proxy as we don't care here.
            removeSecurityProxy(team).visibility = visibility
        if email is not None:
            team.setContactAddress(
                getUtility(IEmailAddressSet).new(email, team))
        if members is not None:
            naked_team = removeSecurityProxy(team)
            for member in members:
                naked_team.addMember(member, owner)
        return team

    def makePoll(self, team, name, title, proposition,
                 poll_type=PollAlgorithm.SIMPLE):
        """Create a new poll which starts tomorrow and lasts for a week."""
        dateopens = datetime.now(pytz.UTC) + timedelta(days=1)
        datecloses = dateopens + timedelta(days=7)
        return getUtility(IPollSet).new(
            team, name, title, proposition, dateopens, datecloses,
            PollSecrecy.SECRET, allowspoilt=True,
            poll_type=poll_type)

    def makeTranslationGroup(self, owner=None, name=None, title=None,
                             summary=None, url=None):
        """Create a new, arbitrary `TranslationGroup`."""
        if owner is None:
            owner = self.makePerson()
        if name is None:
            name = self.getUniqueString("translationgroup")
        if title is None:
            title = self.getUniqueString("title")
        if summary is None:
            summary = self.getUniqueString("summary")
        return getUtility(ITranslationGroupSet).new(
            name, title, summary, url, owner)

    def makeTranslator(self, language_code=None, group=None, person=None,
                       license=True, language=None):
        """Create a new, arbitrary `Translator`."""
        assert language_code is None or language is None, (
            "Please specifiy only one of language_code and language.")
        if language_code is None:
            if language is None:
                language = self.makeLanguage()
            language_code = language.code
        else:
            language = getUtility(ILanguageSet).getLanguageByCode(
                language_code)
            if language is None:
                language = self.makeLanguage(language_code=language_code)

        if group is None:
            group = self.makeTranslationGroup()
        if person is None:
            person = self.makePerson()
        tx_person = ITranslationsPerson(person)
        insecure_tx_person = removeSecurityProxy(tx_person)
        insecure_tx_person.translations_relicensing_agreement = license
        return getUtility(ITranslatorSet).new(group, language, person)

    def makeMilestone(self, product=None, distribution=None,
                      productseries=None, name=None):
        if product is None and distribution is None and productseries is None:
            product = self.makeProduct()
        if distribution is None:
            if productseries is not None:
                product = productseries.product
            else:
                productseries = self.makeProductSeries(product=product)
            distroseries = None
        else:
            distroseries = self.makeDistroRelease(distribution=distribution)
        if name is None:
            name = self.getUniqueString()
        return ProxyFactory(
            Milestone(product=product, distribution=distribution,
                      productseries=productseries, distroseries=distroseries,
                      name=name))

    def makeProcessor(self, family=None, name=None, title=None,
                      description=None):
        """Create a new processor.

        :param family: Family of the processor
        :param name: Name of the processor
        :param title: Optional title
        :param description: Optional description
        :return: A `IProcessor`
        """
        if name is None:
            name = self.getUniqueString()
        if family is None:
            family = self.makeProcessorFamily()
        if title is None:
            title = "The %s processor" % name
        if description is None:
            description = "The %s and processor and compatible processors"
        return family.addProcessor(name, title, description)

    def makeProcessorFamily(self, name=None, title=None, description=None,
                            restricted=False):
        """Create a new processor family.

        :param name: Name of the family (e.g. x86)
        :param title: Optional title of the family
        :param description: Optional extended description
        :param restricted: Whether the processor family is restricted
        :return: A `IProcessorFamily`
        """
        if name is None:
            name = self.getUniqueString()
        if description is None:
            description = "Description of the %s processor family" % name
        if title is None:
            title = "%s and compatible processors." % name
        family = getUtility(IProcessorFamilySet).new(name, title, description,
            restricted=restricted)
        # Make sure there's at least one processor in the family, so that
        # other things can have a default processor.
        self.makeProcessor(family=family)
        return family

    def makeProductRelease(self, milestone=None, product=None,
                           productseries=None):
        if milestone is None:
            milestone = self.makeMilestone(product=product,
                                           productseries=productseries)
        with person_logged_in(milestone.productseries.product.owner):
            release = milestone.createProductRelease(
                milestone.product.owner, datetime.now(pytz.UTC))
        return release

    def makeProductReleaseFile(self, signed=True,
                               product=None, productseries=None,
                               milestone=None,
                               release=None,
                               description="test file"):
        signature_filename = None
        signature_content = None
        if signed:
            signature_filename = 'test.txt.asc'
            signature_content = '123'
        if release is None:
            release = self.makeProductRelease(product=product,
                                              productseries=productseries,
                                              milestone=milestone)
        with person_logged_in(release.milestone.product.owner):
            release_file = release.addReleaseFile(
                'test.txt', 'test', 'text/plain',
                uploader=release.milestone.product.owner,
                signature_filename=signature_filename,
                signature_content=signature_content,
                description=description)
        return release_file

    def makeProduct(
        self, name=None, project=None, displayname=None,
        licenses=None, owner=None, registrant=None,
        title=None, summary=None, official_malone=None,
        translations_usage=None, bug_supervisor=None,
        driver=None):
        """Create and return a new, arbitrary Product."""
        if owner is None:
            owner = self.makePerson()
        if name is None:
            name = self.getUniqueString('product-name')
        if displayname is None:
            if name is None:
                displayname = self.getUniqueString('displayname')
            else:
                displayname = name.capitalize()
        if licenses is None:
            licenses = [License.GNU_GPL_V2]
        if title is None:
            title = self.getUniqueString('title')
        if summary is None:
            summary = self.getUniqueString('summary')
        product = getUtility(IProductSet).createProduct(
            owner,
            name,
            displayname,
            title,
            summary,
            self.getUniqueString('description'),
            licenses=licenses,
            project=project,
            registrant=registrant)
        naked_product = removeSecurityProxy(product)
        if official_malone is not None:
            naked_product.official_malone = official_malone
        if translations_usage is not None:
            naked_product.translations_usage = translations_usage
        if bug_supervisor is not None:
            naked_product.bug_supervisor = bug_supervisor
        if driver is not None:
            naked_product.driver = driver
        return product

    def makeProductSeries(self, product=None, name=None, owner=None,
                          summary=None, date_created=None, branch=None):
        """Create a new, arbitrary ProductSeries.

        :param branch: If supplied, the branch to set as
            ProductSeries.branch.
        :param date_created: If supplied, the date the series is created.
        :param name: If supplied, the name of the series.
        :param owner: If supplied, the owner of the series.
        :param product: If supplied, the series is created for this product.
            Otherwise, a new product is created.
        :param summary: If supplied, the product series summary.
        """
        if product is None:
            product = self.makeProduct()
        if owner is None:
            owner = product.owner
        if name is None:
            name = self.getUniqueString()
        if summary is None:
            summary = self.getUniqueString()
        # We don't want to login() as the person used to create the product,
        # so we remove the security proxy before creating the series.
        naked_product = removeSecurityProxy(product)
        series = naked_product.newSeries(
            owner=owner, name=name, summary=summary, branch=branch)
        if date_created is not None:
            series.datecreated = date_created
        return ProxyFactory(series)

    def makeProject(self, name=None, displayname=None, title=None,
                    homepageurl=None, summary=None, owner=None,
                    description=None):
        """Create and return a new, arbitrary ProjectGroup."""
        if owner is None:
            owner = self.makePerson()
        if name is None:
            name = self.getUniqueString('project-name')
        if displayname is None:
            displayname = self.getUniqueString('displayname')
        if summary is None:
            summary = self.getUniqueString('summary')
        if description is None:
            description = self.getUniqueString('description')
        if title is None:
            title = self.getUniqueString('title')
        return getUtility(IProjectGroupSet).new(
            name=name,
            displayname=displayname,
            title=title,
            homepageurl=homepageurl,
            summary=summary,
            description=description,
            owner=owner)

    def makeSprint(self, title=None, name=None):
        """Make a sprint."""
        if title is None:
            title = self.getUniqueString('title')
        owner = self.makePerson()
        if name is None:
            name = self.getUniqueString('name')
        time_starts = datetime(2009, 1, 1, tzinfo=pytz.UTC)
        time_ends = datetime(2009, 1, 2, tzinfo=pytz.UTC)
        time_zone = 'UTC'
        summary = self.getUniqueString('summary')
        return getUtility(ISprintSet).new(
            owner=owner, name=name, title=title, time_zone=time_zone,
            time_starts=time_starts, time_ends=time_ends, summary=summary)

    def makeBranch(self, branch_type=None, owner=None,
                   name=None, product=_DEFAULT, url=_DEFAULT, registrant=None,
                   private=False, stacked_on=None, sourcepackage=None,
                   reviewer=None, **optional_branch_args):
        """Create and return a new, arbitrary Branch of the given type.

        Any parameters for `IBranchNamespace.createBranch` can be specified to
        override the default ones.
        """
        if branch_type is None:
            branch_type = BranchType.HOSTED
        if owner is None:
            owner = self.makePerson()
        if name is None:
            name = self.getUniqueString('branch')

        if sourcepackage is None:
            if product is _DEFAULT:
                product = self.makeProduct()
            sourcepackagename = None
            distroseries = None
        else:
            assert product is _DEFAULT, (
                "Passed source package AND product details")
            product = None
            sourcepackagename = sourcepackage.sourcepackagename
            distroseries = sourcepackage.distroseries

        if registrant is None:
            if owner.is_team:
                registrant = owner.teamowner
            else:
                registrant = owner

        if branch_type in (BranchType.HOSTED, BranchType.IMPORTED):
            url = None
        elif branch_type in (BranchType.MIRRORED, BranchType.REMOTE):
            if url is _DEFAULT:
                url = self.getUniqueURL()
        else:
            raise UnknownBranchTypeError(
                'Unrecognized branch type: %r' % (branch_type, ))

        namespace = get_branch_namespace(
            owner, product=product, distroseries=distroseries,
            sourcepackagename=sourcepackagename)
        branch = namespace.createBranch(
            branch_type=branch_type, name=name, registrant=registrant,
            url=url, **optional_branch_args)
        if private:
            removeSecurityProxy(branch).private = True
        if stacked_on is not None:
            removeSecurityProxy(branch).stacked_on = stacked_on
        if reviewer is not None:
            removeSecurityProxy(branch).reviewer = reviewer
        return branch

    def makePackagingLink(self, productseries=None, sourcepackagename=None,
                          distroseries=None, packaging_type=None, owner=None,
                          sourcepackage=None, in_ubuntu=False):
        assert sourcepackage is None or (
            distroseries is None and sourcepackagename is None), (
            "Specify either a sourcepackage or a "
            "distroseries/sourcepackagename pair")
        if productseries is None:
            productseries = self.makeProduct().development_focus
        if sourcepackage is not None:
            distroseries = sourcepackage.distroseries
            sourcepackagename = sourcepackage.sourcepackagename
        else:
            make_sourcepackagename = (
                sourcepackagename is None or
                isinstance(sourcepackagename, str))
            if make_sourcepackagename:
                sourcepackagename = self.makeSourcePackageName(
                    sourcepackagename)
            if distroseries is None:
                if in_ubuntu:
                    distroseries = self.makeUbuntuDistroSeries()
                else:
                    distroseries = self.makeDistroSeries()
        if packaging_type is None:
            packaging_type = PackagingType.PRIME
        if owner is None:
            owner = self.makePerson()
        return getUtility(IPackagingUtil).createPackaging(
            productseries=productseries,
            sourcepackagename=sourcepackagename,
            distroseries=distroseries,
            packaging=packaging_type,
            owner=owner)

    def makePackageBranch(self, sourcepackage=None, distroseries=None,
                          sourcepackagename=None, **kwargs):
        """Make a package branch on an arbitrary package.

        See `makeBranch` for more information on arguments.

        You can pass in either `sourcepackage` or one or both of
        `distroseries` and `sourcepackagename`, but not combinations or all of
        them.
        """
        assert not(sourcepackage is not None and distroseries is not None), (
            "Don't pass in both sourcepackage and distroseries")
        assert not(sourcepackage is not None
                   and sourcepackagename is not None), (
            "Don't pass in both sourcepackage and sourcepackagename")
        if sourcepackage is None:
            sourcepackage = self.makeSourcePackage(
                sourcepackagename=sourcepackagename,
                distroseries=distroseries)
        return self.makeBranch(sourcepackage=sourcepackage, **kwargs)

    def makePersonalBranch(self, owner=None, **kwargs):
        """Make a personal branch on an arbitrary person.

        See `makeBranch` for more information on arguments.
        """
        if owner is None:
            owner = self.makePerson()
        return self.makeBranch(
            owner=owner, product=None, sourcepackage=None, **kwargs)

    def makeProductBranch(self, product=None, **kwargs):
        """Make a product branch on an arbitrary product.

        See `makeBranch` for more information on arguments.
        """
        if product is None:
            product = self.makeProduct()
        return self.makeBranch(product=product, **kwargs)

    def makeAnyBranch(self, **kwargs):
        """Make a branch without caring about its container.

        See `makeBranch` for more information on arguments.
        """
        return self.makeProductBranch(**kwargs)

    def makeBranchTargetBranch(self, target, branch_type=BranchType.HOSTED,
                               name=None, owner=None, creator=None):
        """Create a branch in a BranchTarget."""
        if name is None:
            name = self.getUniqueString('branch')
        if owner is None:
            owner = self.makePerson()
        if creator is None:
            creator = owner
        namespace = target.getNamespace(owner)
        return namespace.createBranch(branch_type, name, creator)

    def makeBranchMergeQueue(self, registrant=None, owner=None, name=None,
                             description=None, configuration=None,
                             branches=None):
        """Create a BranchMergeQueue."""
        if name is None:
            name = unicode(self.getUniqueString('queue'))
        if owner is None:
            owner = self.makePerson()
        if registrant is None:
            registrant = self.makePerson()
        if description is None:
            description = unicode(self.getUniqueString('queue-description'))
        if configuration is None:
            configuration = unicode(simplejson.dumps({
                self.getUniqueString('key'): self.getUniqueString('value')}))

        queue = getUtility(IBranchMergeQueueSource).new(
            name, owner, registrant, description, configuration, branches)
        return queue

    def makeRelatedBranchesForSourcePackage(self, sourcepackage=None,
                                            **kwargs):
        """Create some branches associated with a sourcepackage."""

        reference_branch = self.makePackageBranch(sourcepackage=sourcepackage)
        return self.makeRelatedBranches(
                reference_branch=reference_branch, **kwargs)

    def makeRelatedBranchesForProduct(self, product=None, **kwargs):
        """Create some branches associated with a product."""

        reference_branch = self.makeProductBranch(product=product)
        return self.makeRelatedBranches(
                reference_branch=reference_branch, **kwargs)

    def makeRelatedBranches(self, reference_branch=None,
                            with_series_branches=True,
                            with_package_branches=True,
                            with_private_branches=False):
        """Create some branches associated with a reference branch.
        The other branches are:
          - series branches: a set of branches associated with product
            series of the same product as the reference branch.
          - package branches: a set of branches associated with packagesource
            entities of the same product as the reference branch or the same
            sourcepackage depending on what type of branch it is.

        If no reference branch is supplied, create one.

        Returns: a tuple consisting of
        (reference_branch, related_series_branches, related_package_branches)

        """
        related_series_branch_info = []
        related_package_branch_info = []
        # Make the base_branch if required and find the product if one exists.
        naked_product = None
        if reference_branch is None:
            naked_product = removeSecurityProxy(self.makeProduct())
            # Create the 'source' branch ie the base branch of a recipe.
            reference_branch = self.makeProductBranch(
                                            name="reference_branch",
                                            product=naked_product)
        elif reference_branch.product is not None:
            naked_product = removeSecurityProxy(reference_branch.product)

        related_branch_owner = self.makePerson()
        # Only branches related to products have related series branches.
        if with_series_branches and naked_product is not None:
            series_branch_info = []

            # Add some product series
            def makeSeriesBranch(name, is_private=False):
                branch = self.makeBranch(
                    name=name,
                    product=naked_product, owner=related_branch_owner,
                    private=is_private)
                series = self.makeProductSeries(
                    product=naked_product, branch=branch)
                return branch, series
            for x in range(4):
                is_private = x == 0 and with_private_branches
                (branch, series) = makeSeriesBranch(
                        name=("series_branch_%s" % x), is_private=is_private)
                if not is_private:
                    series_branch_info.append((branch, series))

            # Sort them
            related_series_branch_info = sorted_version_numbers(
                    series_branch_info, key=lambda branch_info: (
                        getattr(branch_info[1], 'name')))

            # Add a development branch at the start of the list.
            naked_product.development_focus.name = 'trunk'
            devel_branch = self.makeProductBranch(
                product=naked_product, name='trunk_branch',
                owner=related_branch_owner)
            linked_branch = ICanHasLinkedBranch(naked_product)
            linked_branch.setBranch(devel_branch)
            related_series_branch_info.insert(0,
                    (devel_branch, naked_product.development_focus))

        if with_package_branches:
            # Create related package branches if the base_branch is
            # associated with a product.
            if naked_product is not None:

                def makePackageBranch(name, is_private=False):
                    distro = self.makeDistribution()
                    distroseries = self.makeDistroSeries(
                        distribution=distro)
                    sourcepackagename = self.makeSourcePackageName()

                    suitesourcepackage = self.makeSuiteSourcePackage(
                        sourcepackagename=sourcepackagename,
                        distroseries=distroseries,
                        pocket=PackagePublishingPocket.RELEASE)
                    naked_sourcepackage = removeSecurityProxy(
                        suitesourcepackage)

                    branch = self.makePackageBranch(
                        name=name, owner=related_branch_owner,
                        sourcepackagename=sourcepackagename,
                        distroseries=distroseries, private=is_private)
                    linked_branch = ICanHasLinkedBranch(naked_sourcepackage)
                    with celebrity_logged_in('admin'):
                        linked_branch.setBranch(branch, related_branch_owner)

                    series = self.makeProductSeries(product=naked_product)
                    self.makePackagingLink(
                        distroseries=distroseries, productseries=series,
                        sourcepackagename=sourcepackagename)
                    return branch, distroseries

                for x in range(5):
                    is_private = x == 0 and with_private_branches
                    branch, distroseries = makePackageBranch(
                            name=("product_package_branch_%s" % x),
                            is_private=is_private)
                    if not is_private:
                        related_package_branch_info.append(
                                (branch, distroseries))

            # Create related package branches if the base_branch is
            # associated with a sourcepackage.
            if reference_branch.sourcepackage is not None:
                distroseries = reference_branch.sourcepackage.distroseries
                for pocket in [
                        PackagePublishingPocket.RELEASE,
                        PackagePublishingPocket.UPDATES,
                        ]:
                    branch = self.makePackageBranch(
                            name="package_branch_%s" % pocket.name,
                            distroseries=distroseries)
                    with celebrity_logged_in('admin'):
                        reference_branch.sourcepackage.setBranch(
                            pocket, branch,
                            related_branch_owner)

                    related_package_branch_info.append(
                            (branch, distroseries))

            related_package_branch_info = sorted_version_numbers(
                    related_package_branch_info, key=lambda branch_info: (
                        getattr(branch_info[1], 'name')))

        return (
            reference_branch,
            related_series_branch_info,
            related_package_branch_info)

    def enableDefaultStackingForProduct(self, product, branch=None):
        """Give 'product' a default stacked-on branch.

        :param product: The product to give a default stacked-on branch to.
        :param branch: The branch that should be the default stacked-on
            branch.  If not supplied, a fresh branch will be created.
        """
        if branch is None:
            branch = self.makeBranch(product=product)
        # We just remove the security proxies to be able to change the objects
        # here.
        removeSecurityProxy(branch).branchChanged(
            '', 'rev1', None, None, None)
        naked_series = removeSecurityProxy(product.development_focus)
        naked_series.branch = branch
        return branch

    def enableDefaultStackingForPackage(self, package, branch):
        """Give 'package' a default stacked-on branch.

        :param package: The package to give a default stacked-on branch to.
        :param branch: The branch that should be the default stacked-on
            branch.
        """
        # We just remove the security proxies to be able to change the branch
        # here.
        removeSecurityProxy(branch).branchChanged(
            '', 'rev1', None, None, None)
        with person_logged_in(package.distribution.owner):
            package.development_version.setBranch(
                PackagePublishingPocket.RELEASE, branch,
                package.distribution.owner)
        return branch

    def makeBranchMergeProposal(self, target_branch=None, registrant=None,
                                set_state=None, prerequisite_branch=None,
                                product=None, review_diff=None,
                                initial_comment=None, source_branch=None,
                                preview_diff=None, date_created=None,
                                description=None, reviewer=None):
        """Create a proposal to merge based on anonymous branches."""
        if target_branch is not None:
            target = target_branch.target
        elif source_branch is not None:
            target = source_branch.target
        elif prerequisite_branch is not None:
            target = prerequisite_branch.target
        else:
            # Create a target product branch, and use that target.  This is
            # needed to make sure we get a branch target that has the needed
            # security proxy.
            target_branch = self.makeProductBranch(product)
            target = target_branch.target

        # Fall back to initial_comment for description.
        if description is None:
            description = initial_comment

        if target_branch is None:
            target_branch = self.makeBranchTargetBranch(target)
        if source_branch is None:
            source_branch = self.makeBranchTargetBranch(target)
        if registrant is None:
            registrant = self.makePerson()
        review_requests = []
        if reviewer is not None:
            review_requests.append((reviewer, None))
        proposal = source_branch.addLandingTarget(
            registrant, target_branch, review_requests=review_requests,
            prerequisite_branch=prerequisite_branch, review_diff=review_diff,
            description=description, date_created=date_created)

        unsafe_proposal = removeSecurityProxy(proposal)
        if preview_diff is not None:
            unsafe_proposal.preview_diff = preview_diff
        if (set_state is None or
            set_state == BranchMergeProposalStatus.WORK_IN_PROGRESS):
            # The initial state is work in progress, so do nothing.
            pass
        elif set_state == BranchMergeProposalStatus.NEEDS_REVIEW:
            unsafe_proposal.requestReview()
        elif set_state == BranchMergeProposalStatus.CODE_APPROVED:
            unsafe_proposal.approveBranch(
                proposal.target_branch.owner, 'some_revision')
        elif set_state == BranchMergeProposalStatus.REJECTED:
            unsafe_proposal.rejectBranch(
                proposal.target_branch.owner, 'some_revision')
        elif set_state == BranchMergeProposalStatus.MERGED:
            unsafe_proposal.markAsMerged()
        elif set_state == BranchMergeProposalStatus.MERGE_FAILED:
            unsafe_proposal.setStatus(set_state, proposal.target_branch.owner)
        elif set_state == BranchMergeProposalStatus.QUEUED:
            unsafe_proposal.commit_message = self.getUniqueString(
                'commit message')
            unsafe_proposal.enqueue(
                proposal.target_branch.owner, 'some_revision')
        elif set_state == BranchMergeProposalStatus.SUPERSEDED:
            unsafe_proposal.resubmit(proposal.registrant)
        else:
            raise AssertionError('Unknown status: %s' % set_state)

        return proposal

    def makeBranchSubscription(self, branch=None, person=None,
                               subscribed_by=None):
        """Create a BranchSubscription."""
        if branch is None:
            branch = self.makeBranch()
        if person is None:
            person = self.makePerson()
        if subscribed_by is None:
            subscribed_by = person
        return branch.subscribe(person,
            BranchSubscriptionNotificationLevel.NOEMAIL, None,
            CodeReviewNotificationLevel.NOEMAIL, subscribed_by)

    def makeDiff(self, diff_text=DIFF):
        return ProxyFactory(
            Diff.fromFile(StringIO(diff_text), len(diff_text)))

    def makePreviewDiff(self, conflicts=u''):
        diff = self.makeDiff()
        bmp = self.makeBranchMergeProposal()
        preview_diff = PreviewDiff()
        preview_diff.branch_merge_proposal = bmp
        preview_diff.conflicts = conflicts
        preview_diff.diff = diff
        preview_diff.source_revision_id = self.getUniqueUnicode()
        preview_diff.target_revision_id = self.getUniqueUnicode()
        return preview_diff

    def makeIncrementalDiff(self, merge_proposal=None, old_revision=None,
                            new_revision=None):
        diff = self.makeDiff()
        if merge_proposal is None:
            source_branch = self.makeBranch()
        else:
            source_branch = merge_proposal.source_branch

        def make_revision(parent=None):
            sequence = source_branch.revision_history.count() + 1
            if parent is None:
                parent_ids = []
            else:
                parent_ids = [parent.revision_id]
            branch_revision = self.makeBranchRevision(
                source_branch, sequence=sequence,
                revision_date=self.getUniqueDate(), parent_ids=parent_ids)
            return branch_revision.revision
        if old_revision is None:
            old_revision = make_revision()
        if merge_proposal is None:
            merge_proposal = self.makeBranchMergeProposal(
                date_created=self.getUniqueDate(),
                source_branch=source_branch)
        if new_revision is None:
            new_revision = make_revision(old_revision)
        return merge_proposal.generateIncrementalDiff(
            old_revision, new_revision, diff)

    def makeStaticDiff(self):
        return StaticDiff.acquireFromText(
            self.getUniqueUnicode(), self.getUniqueUnicode(),
            self.getUniqueString())

    def makeRevision(self, author=None, revision_date=None, parent_ids=None,
                     rev_id=None, log_body=None, date_created=None):
        """Create a single `Revision`."""
        if author is None:
            author = self.getUniqueString('author')
        elif IPerson.providedBy(author):
            author = removeSecurityProxy(author).preferredemail.email
        if revision_date is None:
            revision_date = datetime.now(pytz.UTC)
        if parent_ids is None:
            parent_ids = []
        if rev_id is None:
            rev_id = self.getUniqueString('revision-id')
        if log_body is None:
            log_body = self.getUniqueString('log-body')
        return getUtility(IRevisionSet).new(
            revision_id=rev_id, log_body=log_body,
            revision_date=revision_date, revision_author=author,
            parent_ids=parent_ids, properties={},
            _date_created=date_created)

    def makeRevisionsForBranch(self, branch, count=5, author=None,
                               date_generator=None):
        """Add `count` revisions to the revision history of `branch`.

        :param branch: The branch to add the revisions to.
        :param count: The number of revisions to add.
        :param author: A string for the author name.
        :param date_generator: A `time_counter` instance, defaults to starting
                               from 1-Jan-2007 if not set.
        """
        if date_generator is None:
            date_generator = time_counter(
                datetime(2007, 1, 1, tzinfo=pytz.UTC),
                delta=timedelta(days=1))
        sequence = branch.revision_count
        parent = branch.getTipRevision()
        if parent is None:
            parent_ids = []
        else:
            parent_ids = [parent.revision_id]

        revision_set = getUtility(IRevisionSet)
        if author is None:
            author = self.getUniqueString('author')
        for index in range(count):
            revision = revision_set.new(
                revision_id=self.getUniqueString('revision-id'),
                log_body=self.getUniqueString('log-body'),
                revision_date=date_generator.next(),
                revision_author=author,
                parent_ids=parent_ids,
                properties={})
            sequence += 1
            branch.createBranchRevision(sequence, revision)
            parent = revision
            parent_ids = [parent.revision_id]
        if branch.branch_type not in (BranchType.REMOTE, BranchType.HOSTED):
            branch.startMirroring()
        removeSecurityProxy(branch).branchChanged(
            '', parent.revision_id, None, None, None)
        branch.updateScannedDetails(parent, sequence)

    def makeBranchRevision(self, branch, revision_id=None, sequence=None,
                           parent_ids=None, revision_date=None):
        revision = self.makeRevision(
            rev_id=revision_id, parent_ids=parent_ids,
            revision_date=revision_date)
        return branch.createBranchRevision(sequence, revision)

    def makeBug(self, product=None, owner=None, bug_watch_url=None,
                private=False, date_closed=None, title=None,
                date_created=None, description=None, comment=None,
                status=None, distribution=None, milestone=None, series=None,
                tags=None, sourcepackagename=None):
        """Create and return a new, arbitrary Bug.

        The bug returned uses default values where possible. See
        `IBugSet.new` for more information.

        :param product: If the product is not set, and if the parameter
            distribution, milestone, and series are not set, a product
            is created and this is used as the primary bug target.
        :param owner: The reporter of the bug. If not set, one is created.
        :param bug_watch_url: If specified, create a bug watch pointing
            to this URL.
        :param distribution: If set, the distribution is used as the
            default bug target.
        :param milestone: If set, the milestone.target must match the product
            or distribution parameters, or the those parameters must be None.
        :param series: If set, the series.product must match the product
            parameter, or the series.distribution must match the distribution
            parameter, or the those parameters must be None.
        :param tags: If set, the tags to be added with the bug.
        :param distribution: If set, the sourcepackagename is used as the
            default bug target.
        At least one of the parameters distribution and product must be
        None, otherwise, an assertion error will be raised.
        """
        if product is None and distribution is None:
            if milestone is not None:
                # One of these will be None.
                product = milestone.product
                distribution = milestone.distribution
            elif series is not None:
                if IProductSeries.providedBy(series):
                    product = series.product
                else:
                    distribution = series.distribution
            else:
                product = self.makeProduct()
        if owner is None:
            owner = self.makePerson()
        if title is None:
            title = self.getUniqueString('bug-title')
        if comment is None:
            comment = self.getUniqueString()
        if sourcepackagename is not None:
            self.makeSourcePackagePublishingHistory(
                distroseries=distribution.currentseries,
                sourcepackagename=sourcepackagename)
        create_bug_params = CreateBugParams(
            owner, title, comment=comment, private=private,
            datecreated=date_created, description=description,
            status=status, tags=tags)
        create_bug_params.setBugTarget(
            product=product, distribution=distribution,
            sourcepackagename=sourcepackagename)
        bug = getUtility(IBugSet).createBug(create_bug_params)
        if bug_watch_url is not None:
            # fromText() creates a bug watch associated with the bug.
            getUtility(IBugWatchSet).fromText(bug_watch_url, bug, owner)
        bugtask = bug.default_bugtask
        if date_closed is not None:
            bugtask.transitionToStatus(
                BugTaskStatus.FIXRELEASED, owner, when=date_closed)
        if milestone is not None:
            bugtask.transitionToMilestone(milestone, milestone.target.owner)
        if series is not None:
            with person_logged_in(owner):
                task = bug.addTask(owner, series)
                task.transitionToStatus(status, owner)

        return bug

    def makeBugTask(self, bug=None, target=None, owner=None, publish=True):
        """Create and return a bug task.

        If the bug is already targeted to the given target, the existing
        bug task is returned.

        :param bug: The `IBug` the bug tasks should be part of. If None,
            one will be created.
        :param target: The `IBugTarget`, to which the bug will be
            targeted to.
        """
        if bug is None:
            bug = self.makeBug()
        if target is None:
            target = self.makeProduct()
        existing_bugtask = bug.getBugTask(target)
        if existing_bugtask is not None:
            return existing_bugtask

        if owner is None:
            owner = self.makePerson()

        prerequisite_target = None
        if IProductSeries.providedBy(target):
            # We can't have a series task without a product task.
            prerequisite_target = target.product
        if IDistroSeries.providedBy(target):
            # We can't have a series task without a distribution task.
            prerequisite_target = target.distribution
        if ISourcePackage.providedBy(target):
            # We can't have a series task without a distribution task.
            prerequisite_target = target.distribution_sourcepackage
            if publish:
                self.makeSourcePackagePublishingHistory(
                    distroseries=target.distroseries,
                    sourcepackagename=target.sourcepackagename)
        if IDistributionSourcePackage.providedBy(target):
            if publish:
                self.makeSourcePackagePublishingHistory(
                    distroseries=target.distribution.currentseries,
                    sourcepackagename=target.sourcepackagename)
        if prerequisite_target is not None:
            prerequisite = bug.getBugTask(prerequisite_target)
            if prerequisite is None:
                self.makeBugTask(bug, prerequisite_target, publish=publish)

        return removeSecurityProxy(bug).addTask(owner, target)

    def makeBugNomination(self, bug=None, target=None):
        """Create and return a BugNomination.

        Will create a non-series task if it does not already exist.

        :param bug: The `IBug` the nomination should be for. If None,
            one will be created.
        :param target: The `IProductSeries`, `IDistroSeries` or
            `ISourcePackage` to nominate for.
        """
        if ISourcePackage.providedBy(target):
            non_series = target.distribution_sourcepackage
            series = target.distroseries
        else:
            non_series = target.parent
            series = target
        with celebrity_logged_in('admin'):
            bug = self.makeBugTask(bug=bug, target=non_series).bug
            nomination = bug.addNomination(
                getUtility(ILaunchpadCelebrities).admin, series)
        return nomination

    def makeBugTracker(self, base_url=None, bugtrackertype=None, title=None,
                       name=None):
        """Make a new bug tracker."""
        owner = self.makePerson()

        if base_url is None:
            base_url = 'http://%s.example.com/' % self.getUniqueString()
        if bugtrackertype is None:
            bugtrackertype = BugTrackerType.BUGZILLA

        return getUtility(IBugTrackerSet).ensureBugTracker(
            base_url, owner, bugtrackertype, title=title, name=name)

    def makeBugTrackerWithWatches(self, base_url=None, count=2):
        """Make a new bug tracker with some watches."""
        bug_tracker = self.makeBugTracker(base_url=base_url)
        bug_watches = [
            self.makeBugWatch(bugtracker=bug_tracker)
            for i in range(count)]
        return (bug_tracker, bug_watches)

    def makeBugTrackerComponentGroup(self, name=None, bug_tracker=None):
        """Make a new bug tracker component group."""
        if name is None:
            name = self.getUniqueUnicode()
        if bug_tracker is None:
            bug_tracker = self.makeBugTracker()

        component_group = bug_tracker.addRemoteComponentGroup(name)
        return component_group

    def makeBugTrackerComponent(self, name=None, component_group=None,
                                custom=None):
        """Make a new bug tracker component."""
        if name is None:
            name = self.getUniqueUnicode()
        if component_group is None:
            component_group = self.makeBugTrackerComponentGroup()
        if custom is None:
            custom = False
        if custom:
            component = component_group.addCustomComponent(name)
        else:
            component = component_group.addComponent(name)
        return component

    def makeBugWatch(self, remote_bug=None, bugtracker=None, bug=None,
                     owner=None, bug_task=None):
        """Make a new bug watch."""
        if remote_bug is None:
            remote_bug = self.getUniqueInteger()

        if bugtracker is None:
            bugtracker = self.makeBugTracker()

        if bug_task is not None:
            # If someone passes a value for bug *and* a value for
            # bug_task then the bug value will get clobbered, but that
            # doesn't matter since the bug should be the one that the
            # bug task belongs to anyway (unless they're having a crazy
            # moment, in which case we're saving them from themselves).
            bug = bug_task.bug
        elif bug is None:
            bug = self.makeBug()

        if owner is None:
            owner = self.makePerson()

        bug_watch = getUtility(IBugWatchSet).createBugWatch(
            bug, owner, bugtracker, str(remote_bug))
        if bug_task is not None:
            bug_task.bugwatch = bug_watch

        # You need to be an admin to set next_check on a BugWatch.
        def set_next_check(bug_watch):
            bug_watch.next_check = datetime.now(pytz.timezone('UTC'))

        person = getUtility(IPersonSet).getByName('name16')
        run_with_login(person, set_next_check, bug_watch)
        return bug_watch

    def makeBugComment(self, bug=None, owner=None, subject=None, body=None,
                       bug_watch=None):
        """Create and return a new bug comment.

        :param bug: An `IBug` or a bug ID or name, or None, in which
            case a new bug is created.
        :param owner: An `IPerson`, or None, in which case a new
            person is created.
        :param subject: An `IMessage` or a string, or None, in which
            case a new message will be generated.
        :param body: An `IMessage` or a string, or None, in which
            case a new message will be generated.
        :param bug_watch: An `IBugWatch`, which will be used to set the
            new comment's bugwatch attribute.
        :return: An `IBugMessage`.
        """
        if bug is None:
            bug = self.makeBug()
        elif isinstance(bug, (int, long, basestring)):
            bug = getUtility(IBugSet).getByNameOrID(str(bug))
        if owner is None:
            owner = self.makePerson()
        if subject is None:
            subject = self.getUniqueString()
        if body is None:
            body = self.getUniqueString()
        return bug.newMessage(owner=owner, subject=subject,
                              content=body, parent=None, bugwatch=bug_watch,
                              remote_comment_id=None)

    def makeBugAttachment(self, bug=None, owner=None, data=None,
                          comment=None, filename=None, content_type=None,
                          description=None, is_patch=_DEFAULT):
        """Create and return a new bug attachment.

        :param bug: An `IBug` or a bug ID or name, or None, in which
            case a new bug is created.
        :param owner: An `IPerson`, or None, in which case a new
            person is created.
        :param data: A file-like object or a string, or None, in which
            case a unique string will be used.
        :param comment: An `IMessage` or a string, or None, in which
            case a new message will be generated.
        :param filename: A string, or None, in which case a unique
            string will be used.
        :param content_type: The MIME-type of this file.
        :param description: The description of the attachment.
        :param is_patch: If true, this attachment is a patch.
        :return: An `IBugAttachment`.
        """
        if bug is None:
            bug = self.makeBug()
        elif isinstance(bug, (int, long, basestring)):
            bug = getUtility(IBugSet).getByNameOrID(str(bug))
        if owner is None:
            owner = self.makePerson()
        if data is None:
            data = self.getUniqueString()
        if description is None:
            description = self.getUniqueString()
        if comment is None:
            comment = self.getUniqueString()
        if filename is None:
            filename = self.getUniqueString()
        # If the default value of is_patch when creating a new
        # BugAttachment should ever change, we don't want to interfere
        # with that.  So, we only override it if our caller explicitly
        # passed it.
        other_params = {}
        if is_patch is not _DEFAULT:
            other_params['is_patch'] = is_patch
        return bug.addAttachment(
            owner, data, comment, filename, content_type=content_type,
            description=description, **other_params)

    def makeSignedMessage(self, msgid=None, body=None, subject=None,
            attachment_contents=None, force_transfer_encoding=False,
            email_address=None, signing_context=None, to_address=None):
        """Return an ISignedMessage.

        :param msgid: An rfc2822 message-id.
        :param body: The body of the message.
        :param attachment_contents: The contents of an attachment.
        :param force_transfer_encoding: If True, ensure a transfer encoding is
            used.
        :param email_address: The address the mail is from.
        :param signing_context: A GPGSigningContext instance containing the
            gpg key to sign with.  If None, the message is unsigned.  The
            context also contains the password and gpg signing mode.
        """
        mail = SignedMessage()
        if email_address is None:
            person = self.makePerson()
            email_address = removeSecurityProxy(person).preferredemail.email
        mail['From'] = email_address
        if to_address is None:
            to_address = removeSecurityProxy(
                self.makePerson()).preferredemail.email
        mail['To'] = to_address
        if subject is None:
            subject = self.getUniqueString('subject')
        mail['Subject'] = subject
        if msgid is None:
            msgid = self.makeUniqueRFC822MsgId()
        if body is None:
            body = self.getUniqueString('body')
        charset = 'ascii'
        try:
            body = body.encode(charset)
        except UnicodeEncodeError:
            charset = 'utf-8'
            body = body.encode(charset)
        mail['Message-Id'] = msgid
        mail['Date'] = formatdate()
        if signing_context is not None:
            gpghandler = getUtility(IGPGHandler)
            body = gpghandler.signContent(
                body, signing_context.fingerprint,
                signing_context.password, signing_context.mode)
            assert body is not None
        if attachment_contents is None:
            mail.set_payload(body)
            body_part = mail
        else:
            body_part = EmailMessage()
            body_part.set_payload(body)
            mail.attach(body_part)
            attach_part = EmailMessage()
            attach_part.set_payload(attachment_contents)
            attach_part['Content-type'] = 'application/octet-stream'
            if force_transfer_encoding:
                encode_base64(attach_part)
            mail.attach(attach_part)
            mail['Content-type'] = 'multipart/mixed'
        body_part['Content-type'] = 'text/plain'
        if force_transfer_encoding:
            encode_base64(body_part)
        body_part.set_charset(charset)
        mail.parsed_string = mail.as_string()
        return mail

    def makeSpecification(self, product=None, title=None, distribution=None,
                          name=None, summary=None, owner=None,
                          status=NewSpecificationDefinitionStatus.NEW,
                          implementation_status=None, goal=None, specurl=None,
                          assignee=None, drafter=None, approver=None,
                          priority=None, whiteboard=None, milestone=None):
        """Create and return a new, arbitrary Blueprint.

        :param product: The product to make the blueprint on.  If one is
            not specified, an arbitrary product is created.
        """
        if distribution is None and product is None:
            product = self.makeProduct()
        if name is None:
            name = self.getUniqueString('name')
        if summary is None:
            summary = self.getUniqueString('summary')
        if title is None:
            title = self.getUniqueString('title')
        if owner is None:
            owner = self.makePerson()
        if priority is None:
            priority = SpecificationPriority.UNDEFINED
        status_names = NewSpecificationDefinitionStatus.items.mapping.keys()
        if status.name in status_names:
            definition_status = status
        else:
            # This is to satisfy life cycle requirements.
            definition_status = NewSpecificationDefinitionStatus.NEW
        spec = getUtility(ISpecificationSet).new(
            name=name,
            title=title,
            specurl=None,
            summary=summary,
            definition_status=definition_status,
            whiteboard=whiteboard,
            owner=owner,
            assignee=assignee,
            drafter=drafter,
            approver=approver,
            product=product,
            distribution=distribution,
            priority=priority)
        naked_spec = removeSecurityProxy(spec)
        if status.name not in status_names:
            # Set the closed status after the status has a sane initial state.
            naked_spec.definition_status = status
        if status == SpecificationDefinitionStatus.OBSOLETE:
            # This is to satisfy a DB constraint of obsolete specs.
            naked_spec.completer = owner
            naked_spec.date_completed = datetime.now(pytz.UTC)
        naked_spec.specurl = specurl
        naked_spec.milestone = milestone
        if goal is not None:
            naked_spec.proposeGoal(goal, spec.target.owner)
        if implementation_status is not None:
            naked_spec.implementation_status = implementation_status
            naked_spec.updateLifecycleStatus(owner)
        return spec

    makeBlueprint = makeSpecification

    def makeQuestion(self, target=None, title=None,
                     owner=None, description=None):
        """Create and return a new, arbitrary Question.

        :param target: The IQuestionTarget to make the question on. If one is
            not specified, an arbitrary product is created.
        :param title: The question title. If one is not provided, an
            arbitrary title is created.
        :param owner: The owner of the question. If one is not provided, the
            question target owner will be used.
        """
        if target is None:
            target = self.makeProduct()
        if title is None:
            title = self.getUniqueString('title')
        if owner is None:
            owner = target.owner
        if description is None:
            description = self.getUniqueString('description')
        with person_logged_in(target.owner):
            question = target.newQuestion(
                owner=owner, title=title, description=description)
        return question

    def makeFAQ(self, target=None, title=None):
        """Create and return a new, arbitrary FAQ.

        :param target: The IFAQTarget to make the FAQ on. If one is
            not specified, an arbitrary product is created.
        :param title: The FAQ title. If one is not provided, an
            arbitrary title is created.
        """
        if target is None:
            target = self.makeProduct()
        if title is None:
            title = self.getUniqueString('title')
        return target.newFAQ(
            owner=target.owner, title=title, content='content')

    def makePackageCodeImport(self, sourcepackage=None, **kwargs):
        """Make a code import targetting a sourcepackage."""
        if sourcepackage is None:
            sourcepackage = self.makeSourcePackage()
        target = IBranchTarget(sourcepackage)
        return self.makeCodeImport(target=target, **kwargs)

    def makeProductCodeImport(self, product=None, **kwargs):
        """Make a code import targetting a product."""
        if product is None:
            product = self.makeProduct()
        target = IBranchTarget(product)
        return self.makeCodeImport(target=target, **kwargs)

    def makeCodeImport(self, svn_branch_url=None, cvs_root=None,
                       cvs_module=None, target=None, branch_name=None,
                       git_repo_url=None, hg_repo_url=None, registrant=None,
                       rcs_type=None, review_status=None):
        """Create and return a new, arbitrary code import.

        The type of code import will be inferred from the source details
        passed in, but defaults to a Subversion import from an arbitrary
        unique URL.
        """
        if (svn_branch_url is cvs_root is cvs_module is git_repo_url is
            hg_repo_url is None):
            svn_branch_url = self.getUniqueURL()

        if target is None:
            target = IBranchTarget(self.makeProduct())
        if branch_name is None:
            branch_name = self.getUniqueString('name')
        if registrant is None:
            registrant = self.makePerson()

        code_import_set = getUtility(ICodeImportSet)
        if svn_branch_url is not None:
            if rcs_type is None:
                rcs_type = RevisionControlSystems.SVN
            else:
                assert rcs_type in (RevisionControlSystems.SVN,
                                    RevisionControlSystems.BZR_SVN)
            code_import = code_import_set.new(
                registrant, target, branch_name, rcs_type=rcs_type,
                url=svn_branch_url)
        elif git_repo_url is not None:
            assert rcs_type in (None, RevisionControlSystems.GIT)
            code_import = code_import_set.new(
                registrant, target, branch_name,
                rcs_type=RevisionControlSystems.GIT,
                url=git_repo_url)
        elif hg_repo_url is not None:
            code_import = code_import_set.new(
                registrant, target, branch_name,
                rcs_type=RevisionControlSystems.HG,
                url=hg_repo_url)
        else:
            assert rcs_type in (None, RevisionControlSystems.CVS)
            code_import = code_import_set.new(
                registrant, target, branch_name,
                rcs_type=RevisionControlSystems.CVS,
                cvs_root=cvs_root, cvs_module=cvs_module)
        if review_status:
            removeSecurityProxy(code_import).review_status = review_status
        return code_import

    def makeChangelog(self, spn=None, versions=[]):
        """Create and return a LFA of a valid Debian-style changelog."""
        if spn is None:
            spn = self.getUniqueString()
        changelog = ''
        for version in versions:
            entry = dedent('''
            %s (%s) unstable; urgency=low

              * %s.

             -- Foo Bar <foo@example.com>  Tue, 01 Jan 1970 01:50:41 +0000

            ''' % (spn, version, version))
            changelog += entry
        return self.makeLibraryFileAlias(content=changelog)

    def makeCodeImportEvent(self):
        """Create and return a CodeImportEvent."""
        code_import = self.makeCodeImport()
        person = self.makePerson()
        code_import_event_set = getUtility(ICodeImportEventSet)
        return code_import_event_set.newCreate(code_import, person)

    def makeCodeImportJob(self, code_import=None):
        """Create and return a new code import job for the given import.

        This implies setting the import's review_status to REVIEWED.
        """
        if code_import is None:
            code_import = self.makeCodeImport()
        code_import.updateFromData(
            {'review_status': CodeImportReviewStatus.REVIEWED},
            code_import.registrant)
        return code_import.import_job

    def makeCodeImportMachine(self, set_online=False, hostname=None):
        """Return a new CodeImportMachine.

        The machine will be in the OFFLINE state."""
        if hostname is None:
            hostname = self.getUniqueString('machine-')
        if set_online:
            state = CodeImportMachineState.ONLINE
        else:
            state = CodeImportMachineState.OFFLINE
        machine = getUtility(ICodeImportMachineSet).new(hostname, state)
        return machine

    def makeCodeImportResult(self, code_import=None, result_status=None,
                             date_started=None, date_finished=None,
                             log_excerpt=None, log_alias=None, machine=None):
        """Create and return a new CodeImportResult."""
        if code_import is None:
            code_import = self.makeCodeImport()
        if machine is None:
            machine = self.makeCodeImportMachine()
        requesting_user = None
        if log_excerpt is None:
            log_excerpt = self.getUniqueString()
        if result_status is None:
            result_status = CodeImportResultStatus.FAILURE
        if date_finished is None:
            # If a date_started is specified, then base the finish time
            # on that.
            if date_started is None:
                date_finished = time_counter().next()
            else:
                date_finished = date_started + timedelta(hours=4)
        if date_started is None:
            date_started = date_finished - timedelta(hours=4)
        if log_alias is None:
            log_alias = self.makeLibraryFileAlias()
        return getUtility(ICodeImportResultSet).new(
            code_import, machine, requesting_user, log_excerpt, log_alias,
            result_status, date_started, date_finished)

    def makeCodeReviewComment(self, sender=None, subject=None, body=None,
                              vote=None, vote_tag=None, parent=None,
                              merge_proposal=None, date_created=DEFAULT):
        if sender is None:
            sender = self.makePerson()
        if subject is None:
            subject = self.getUniqueString('subject')
        if body is None:
            body = self.getUniqueString('content')
        if merge_proposal is None:
            if parent:
                merge_proposal = parent.branch_merge_proposal
            else:
                merge_proposal = self.makeBranchMergeProposal(
                    registrant=sender)
        return merge_proposal.createComment(
            sender, subject, body, vote, vote_tag, parent,
            _date_created=date_created)

    def makeCodeReviewVoteReference(self):
        bmp = removeSecurityProxy(self.makeBranchMergeProposal())
        candidate = self.makePerson()
        return bmp.nominateReviewer(candidate, bmp.registrant)

    def makeMessage(self, subject=None, content=None, parent=None,
                    owner=None):
        if subject is None:
            subject = self.getUniqueString()
        if content is None:
            content = self.getUniqueString()
        if owner is None:
            owner = self.makePerson()
        rfc822msgid = self.makeUniqueRFC822MsgId()
        message = Message(rfc822msgid=rfc822msgid, subject=subject,
            owner=owner, parent=parent)
        MessageChunk(message=message, sequence=1, content=content)
        return message

    def makeLanguage(self, language_code=None, name=None, pluralforms=None,
                     plural_expression=None):
        """Makes a language given the language_code and name."""
        if language_code is None:
            language_code = self.getUniqueString('lang')
        if name is None:
            name = "Language %s" % language_code
        if plural_expression is None and pluralforms is not None:
            # If the number of plural forms is known, the language
            # should also have a plural expression and vice versa.
            plural_expression = 'n %% %d' % pluralforms

        language_set = getUtility(ILanguageSet)
        return language_set.createLanguage(
            language_code, name, pluralforms=pluralforms,
            pluralexpression=plural_expression)

    def makeLanguagePack(self, distroseries=None, languagepack_type=None):
        """Create a language pack."""
        if distroseries is None:
            distroseries = self.makeUbuntuDistroSeries()
        if languagepack_type is None:
            languagepack_type = LanguagePackType.FULL
        return getUtility(ILanguagePackSet).addLanguagePack(
            distroseries, self.makeLibraryFileAlias(), languagepack_type)

    def makeLibraryFileAlias(self, filename=None, content=None,
                             content_type='text/plain', restricted=False,
                             expires=None):
        """Make a library file, and return the alias."""
        if filename is None:
            filename = self.getUniqueString('filename')
        if content is None:
            content = self.getUniqueString()
        library_file_alias_set = getUtility(ILibraryFileAliasSet)
        library_file_alias = library_file_alias_set.create(
            filename, len(content), StringIO(content), content_type,
            expires=expires, restricted=restricted)
        return library_file_alias

    def makeDistribution(self, name=None, displayname=None, owner=None,
                         registrant=None, members=None, title=None,
                         aliases=None, bug_supervisor=None,
                         publish_root_dir=None, publish_base_url=None,
                         publish_copy_base_url=None, no_pubconf=False):
        """Make a new distribution."""
        if name is None:
            name = self.getUniqueString(prefix="distribution")
        if displayname is None:
            displayname = name.capitalize()
        if title is None:
            title = self.getUniqueString()
        description = self.getUniqueString()
        summary = self.getUniqueString()
        domainname = self.getUniqueString()
        if registrant is None:
            registrant = self.makePerson()
        if owner is None:
            owner = self.makePerson()
        if members is None:
            members = self.makeTeam(owner)
        distro = getUtility(IDistributionSet).new(
            name, displayname, title, description, summary, domainname,
            members, owner, registrant)
        if aliases is not None:
            removeSecurityProxy(distro).setAliases(aliases)
        if bug_supervisor is not None:
            naked_distro = removeSecurityProxy(distro)
            naked_distro.bug_supervisor = bug_supervisor
        if not no_pubconf:
            self.makePublisherConfig(
                distro, publish_root_dir, publish_base_url,
                publish_copy_base_url)
        return distro

    def makeDistroRelease(self, distribution=None, version=None,
                          status=SeriesStatus.DEVELOPMENT,
                          previous_series=None, name=None, displayname=None,
                          registrant=None):
        """Make a new distro release."""
        if distribution is None:
            distribution = self.makeDistribution()
        if name is None:
            name = self.getUniqueString(prefix="distroseries")
        if displayname is None:
            displayname = name.capitalize()
        if version is None:
            version = "%s.0" % self.getUniqueInteger()
        if registrant is None:
            registrant = distribution.owner

        # We don't want to login() as the person used to create the product,
        # so we remove the security proxy before creating the series.
        naked_distribution = removeSecurityProxy(distribution)
        series = naked_distribution.newSeries(
            version=version,
            name=name,
            displayname=displayname,
            title=self.getUniqueString(), summary=self.getUniqueString(),
            description=self.getUniqueString(),
            previous_series=previous_series, registrant=registrant)
        series.status = status

        return ProxyFactory(series)

    def makeUbuntuDistroRelease(self, version=None,
                                status=SeriesStatus.DEVELOPMENT,
                                previous_series=None, name=None,
                                displayname=None):
        """Short cut to use the celebrity 'ubuntu' as the distribution."""
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        return self.makeDistroRelease(
            ubuntu, version, status, previous_series, name, displayname)

    # Most people think of distro releases as distro series.
    makeDistroSeries = makeDistroRelease
    makeUbuntuDistroSeries = makeUbuntuDistroRelease

    def makeDistroSeriesDifference(
        self, derived_series=None, source_package_name_str=None,
        versions=None,
        difference_type=DistroSeriesDifferenceType.DIFFERENT_VERSIONS,
        status=DistroSeriesDifferenceStatus.NEEDS_ATTENTION,
        changelogs=None, set_base_version=False, parent_series=None):
        """Create a new distro series source package difference."""
        if derived_series is None:
            dsp = self.makeDistroSeriesParent(
                parent_series=parent_series)
            derived_series = dsp.derived_series
            parent_series = dsp.parent_series
        else:
            if parent_series is None:
                dsp = getUtility(IDistroSeriesParentSet).getByDerivedSeries(
                    derived_series)
                if dsp.count() == 0:
                    new_dsp = self.makeDistroSeriesParent(
                        derived_series=derived_series,
                        parent_series=parent_series)
                    parent_series = new_dsp.parent_series
                else:
                    parent_series = dsp[0].parent_series

        if source_package_name_str is None:
            source_package_name_str = self.getUniqueString('src-name')

        source_package_name = self.getOrMakeSourcePackageName(
            source_package_name_str)

        if versions is None:
            versions = {}
        if changelogs is None:
            changelogs = {}

        base_version = versions.get('base')
        if base_version is not None:
            for series in [derived_series, parent_series]:
                spr = self.makeSourcePackageRelease(
                    sourcepackagename=source_package_name,
                    version=base_version)
                self.makeSourcePackagePublishingHistory(
                    distroseries=series, sourcepackagerelease=spr,
                    status=PackagePublishingStatus.SUPERSEDED)

        if difference_type is not (
            DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES):
            spr = self.makeSourcePackageRelease(
                sourcepackagename=source_package_name,
                version=versions.get('derived'),
                changelog=changelogs.get('derived'))
            self.makeSourcePackagePublishingHistory(
                distroseries=derived_series, sourcepackagerelease=spr,
                status=PackagePublishingStatus.PUBLISHED)

        if difference_type is not (
            DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES):
            spr = self.makeSourcePackageRelease(
                sourcepackagename=source_package_name,
                version=versions.get('parent'),
                changelog=changelogs.get('parent'))
            self.makeSourcePackagePublishingHistory(
                distroseries=parent_series,
                sourcepackagerelease=spr,
                status=PackagePublishingStatus.PUBLISHED)

        diff = getUtility(IDistroSeriesDifferenceSource).new(
            derived_series, source_package_name, parent_series)

        removeSecurityProxy(diff).status = status

        if set_base_version:
            version = versions.get('base', "%s.0" % self.getUniqueInteger())
            removeSecurityProxy(diff).base_version = version

        # We clear the cache on the diff, returning the object as if it
        # was just loaded from the store.
        clear_property_cache(diff)
        return diff

    def makeDistroSeriesDifferenceComment(
        self, distro_series_difference=None, owner=None, comment=None):
        """Create a new distro series difference comment."""
        if distro_series_difference is None:
            distro_series_difference = self.makeDistroSeriesDifference()
        if owner is None:
            owner = self.makePerson()
        if comment is None:
            comment = self.getUniqueString('dsdcomment')

        return getUtility(IDistroSeriesDifferenceCommentSource).new(
            distro_series_difference, owner, comment)

    def makeDistroSeriesParent(self, derived_series=None, parent_series=None,
                               initialized=False, is_overlay=False,
                               pocket=None, component=None):
        if parent_series is None:
            parent_series = self.makeDistroSeries()
        if derived_series is None:
            derived_series = self.makeDistroSeries()
        return getUtility(IDistroSeriesParentSet).new(
            derived_series, parent_series, initialized, is_overlay, pocket,
            component)

    def makeDistroArchSeries(self, distroseries=None,
                             architecturetag=None, processorfamily=None,
                             official=True, owner=None,
                             supports_virtualized=False, enabled=True):
        """Create a new distroarchseries"""

        if distroseries is None:
            distroseries = self.makeDistroRelease()
        if processorfamily is None:
            processorfamily = self.makeProcessorFamily()
        if owner is None:
            owner = self.makePerson()
        # XXX: architecturetag & processorfamily are tightly coupled. It's
        # wrong to just make a fresh architecture tag without also making a
        # processor family to go with it (ideally with processors!)
        if architecturetag is None:
            architecturetag = self.getUniqueString('arch')
        return distroseries.newArch(
            architecturetag, processorfamily, official, owner,
            supports_virtualized, enabled)

    def makeComponent(self, name=None):
        """Make a new `IComponent`."""
        if name is None:
            name = self.getUniqueString()
        return getUtility(IComponentSet).ensure(name)

    def makeComponentSelection(self, distroseries=None, component=None):
        """Make a new `ComponentSelection`.

        :param distroseries: Optional `DistroSeries`.  If none is given,
            one will be created.
        :param component: Optional `Component` or a component name.  If
            none is given, one will be created.
        """
        if distroseries is None:
            distroseries = self.makeDistroSeries()

        if not IComponent.providedBy(component):
            component = self.makeComponent(component)

        return ComponentSelection(
            distroseries=distroseries, component=component)

    def makeArchive(self, distribution=None, owner=None, name=None,
                    purpose=None, enabled=True, private=False,
                    virtualized=True, description=None, displayname=None):
        """Create and return a new arbitrary archive.

        :param distribution: Supply IDistribution, defaults to a new one
            made with makeDistribution() for non-PPAs and ubuntu for PPAs.
        :param owner: Supply IPerson, defaults to a new one made with
            makePerson().
        :param name: Name of the archive, defaults to a random string.
        :param purpose: Supply ArchivePurpose, defaults to PPA.
        :param enabled: Whether the archive is enabled.
        :param private: Whether the archive is created private.
        :param virtualized: Whether the archive is virtualized.
        :param description: A description of the archive.
        """
        if purpose is None:
            purpose = ArchivePurpose.PPA
        if distribution is None:
            # See bug #568769
            if purpose == ArchivePurpose.PPA:
                distribution = getUtility(ILaunchpadCelebrities).ubuntu
            else:
                distribution = self.makeDistribution()
        if owner is None:
            owner = self.makePerson()
        if name is None:
            if purpose != ArchivePurpose.PPA:
                name = default_name_by_purpose.get(purpose)
            if name is None:
                name = self.getUniqueString()

        # Making a distribution makes an archive, and there can be only one
        # per distribution.
        if purpose == ArchivePurpose.PRIMARY:
            return distribution.main_archive

        archive = getUtility(IArchiveSet).new(
            owner=owner, purpose=purpose,
            distribution=distribution, name=name, displayname=displayname,
            enabled=enabled, require_virtualized=virtualized,
            description=description)

        if private:
            naked_archive = removeSecurityProxy(archive)
            naked_archive.private = True
            naked_archive.buildd_secret = "sekrit"

        return archive

    def makeArchiveAdmin(self, archive=None):
        """Make an Archive Admin.

        :param archive: The `IArchive`, will be auto-created if None.

        Make and return an `IPerson` who has an `ArchivePermission` to admin
        the distroseries queue.
        """
        if archive is None:
            archive = self.makeArchive()

        person = self.makePerson()
        permission_set = getUtility(IArchivePermissionSet)
        permission_set.newQueueAdmin(archive, person, 'main')
        return person

    def makeBuilder(self, processor=None, url=None, name=None, title=None,
                    description=None, owner=None, active=True,
                    virtualized=True, vm_host=None, manual=False):
        """Make a new builder for i386 virtualized builds by default.

        Note: the builder returned will not be able to actually build -
        we currently have a build slave setup for 'bob' only in the
        test environment.
        See lib/canonical/buildd/tests/buildd-slave-test.conf
        """
        if processor is None:
            processor_fam = ProcessorFamilySet().getByName('x86')
            processor = processor_fam.processors[0]
        if url is None:
            url = 'http://%s:8221/' % self.getUniqueString()
        if name is None:
            name = self.getUniqueString('builder-name')
        if title is None:
            title = self.getUniqueString('builder-title')
        if description is None:
            description = self.getUniqueString('description')
        if owner is None:
            owner = self.makePerson()

        return getUtility(IBuilderSet).new(
            processor, url, name, title, description, owner, active,
            virtualized, vm_host, manual=manual)

    def makeRecipeText(self, *branches):
        if len(branches) == 0:
            branches = (self.makeAnyBranch(), )
        base_branch = branches[0]
        other_branches = branches[1:]
        text = MINIMAL_RECIPE_TEXT % base_branch.bzr_identity
        for i, branch in enumerate(other_branches):
            text += 'merge dummy-%s %s\n' % (i, branch.bzr_identity)
        return text

    def makeRecipe(self, *branches):
        """Make a builder recipe that references `branches`.

        If no branches are passed, return a recipe text that references an
        arbitrary branch.
        """
        from bzrlib.plugins.builder.recipe import RecipeParser
        parser = RecipeParser(self.makeRecipeText(*branches))
        return parser.parse()

    def makeSourcePackageRecipeDistroseries(self, name="warty"):
        """Return a supported Distroseries to use with Source Package Recipes.

        Ew.  This uses sampledata currently, which is the ONLY reason this
        method exists: it gives us a migration path away from sampledata.
        """
        ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
        return ubuntu.getSeries(name)

    def makeSourcePackageRecipe(self, registrant=None, owner=None,
                                distroseries=None, name=None,
                                description=None, branches=(),
                                build_daily=False, daily_build_archive=None,
                                is_stale=None, recipe=None,
                                date_created=DEFAULT):
        """Make a `SourcePackageRecipe`."""
        if registrant is None:
            registrant = self.makePerson()
        if owner is None:
            owner = self.makePerson()
        if distroseries is None:
            distroseries = self.makeSourcePackageRecipeDistroseries()

        if name is None:
            name = self.getUniqueString('spr-name').decode('utf8')
        if description is None:
            description = self.getUniqueString(
                'spr-description').decode('utf8')
        if daily_build_archive is None:
            daily_build_archive = self.makeArchive(
                distribution=distroseries.distribution, owner=owner)
        if recipe is None:
            recipe = self.makeRecipeText(*branches)
        else:
            assert branches == ()
        source_package_recipe = getUtility(ISourcePackageRecipeSource).new(
            registrant, owner, name, recipe, description, [distroseries],
            daily_build_archive, build_daily, date_created)
        if is_stale is not None:
            removeSecurityProxy(source_package_recipe).is_stale = is_stale
        IStore(source_package_recipe).flush()
        return source_package_recipe

    def makeSourcePackageRecipeBuild(self, sourcepackage=None, recipe=None,
                                     requester=None, archive=None,
                                     sourcename=None, distroseries=None,
                                     pocket=None, date_created=None,
                                     status=BuildStatus.NEEDSBUILD,
                                     duration=None):
        """Make a new SourcePackageRecipeBuild."""
        if recipe is None:
            recipe = self.makeSourcePackageRecipe(name=sourcename)
        if archive is None:
            archive = self.makeArchive()
        if distroseries is None:
            distroseries = self.makeDistroSeries(
                distribution=archive.distribution)
            arch = self.makeDistroArchSeries(distroseries=distroseries)
            removeSecurityProxy(distroseries).nominatedarchindep = arch
        if requester is None:
            requester = self.makePerson()
        spr_build = getUtility(ISourcePackageRecipeBuildSource).new(
            distroseries=distroseries,
            recipe=recipe,
            archive=archive,
            requester=requester,
            pocket=pocket,
            date_created=date_created)
        removeSecurityProxy(spr_build).status = status
        if duration is not None:
            naked_sprb = removeSecurityProxy(spr_build)
            if naked_sprb.date_started is None:
                naked_sprb.date_started = spr_build.date_created
            naked_sprb.date_finished = naked_sprb.date_started + duration
        IStore(spr_build).flush()
        return spr_build

    def makeSourcePackageRecipeBuildJob(
        self, score=9876, virtualized=True, estimated_duration=64,
        sourcename=None, recipe_build=None):
        """Create a `SourcePackageRecipeBuildJob` and a `BuildQueue` for
        testing."""
        if recipe_build is None:
            recipe_build = self.makeSourcePackageRecipeBuild(
                sourcename=sourcename)
        recipe_build_job = recipe_build.makeJob()

        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
        bq = BuildQueue(
            job=recipe_build_job.job, lastscore=score,
            job_type=BuildFarmJobType.RECIPEBRANCHBUILD,
            estimated_duration=timedelta(seconds=estimated_duration),
            virtualized=virtualized)
        store.add(bq)
        return bq

    def makeRecipeBuildRecords(self, num_recent_records=1,
                               num_records_outside_epoch=0, epoch_days=30):
        """Create some recipe build records.

        A RecipeBuildRecord is a named tuple. Some records will be created
        with archive of type ArchivePurpose.PRIMARY, others with type
        ArchivePurpose.PPA. Some build records with be created with a build
        status of fully built and some will be created with the daily build
        option set to True and False. Only those records with daily build set
        to True and build status to fully built are returned.
        :param num_recent_records: the number of records within the specified
         time window.
        :param num_records_outside_epoch: the number of records outside the
         specified time window.
        :param epoch_days: the time window to use when creating records.
        """

        distroseries = self.makeDistroRelease()
        sourcepackagename = self.makeSourcePackageName()
        sourcepackage = self.makeSourcePackage(
            sourcepackagename=sourcepackagename,
            distroseries=distroseries)

        records_inside_epoch = []
        all_records = []
        for x in range(num_recent_records + num_records_outside_epoch):

            # We want some different source package names occasionally
            if not x % 3:
                sourcepackagename = self.makeSourcePackageName()
                sourcepackage = self.makeSourcePackage(
                    sourcepackagename=sourcepackagename,
                    distroseries=distroseries)

            # Ensure we have both ppa and primary archives
            if not x % 2:
                purpose = ArchivePurpose.PPA
            else:
                purpose = ArchivePurpose.PRIMARY
            archive = self.makeArchive(
                purpose=purpose, distribution=distroseries.distribution)
            # Make some daily and non-daily recipe builds.
            for daily in (True, False):
                recipeowner = self.makePerson()
                recipe = self.makeSourcePackageRecipe(
                    build_daily=daily,
                    owner=recipeowner,
                    name="Recipe_%s_%d" % (sourcepackagename.name, x),
                    daily_build_archive=archive,
                    distroseries=distroseries)
                sprb = self.makeSourcePackageRecipeBuild(
                    requester=recipeowner,
                    recipe=recipe,
                    sourcepackage=sourcepackage,
                    distroseries=distroseries)
                spr = self.makeSourcePackageRelease(
                    source_package_recipe_build=sprb,
                    sourcepackagename=sourcepackagename,
                    distroseries=distroseries, archive=archive)
                self.makeSourcePackagePublishingHistory(
                    sourcepackagerelease=spr, archive=archive,
                    distroseries=distroseries)

                # Make some complete and incomplete builds.
                for build_status in (
                    BuildStatus.FULLYBUILT,
                    BuildStatus.NEEDSBUILD,
                    ):
                    binary_build = self.makeBinaryPackageBuild(
                            source_package_release=spr)
                    naked_build = removeSecurityProxy(binary_build)
                    naked_build.queueBuild()
                    naked_build.status = build_status

                    now = datetime.now(UTC)
                    # We want some builds to be created in ascending order
                    if x < num_recent_records:
                        naked_build.date_finished = (
                            now - timedelta(
                                days=epoch_days - 1,
                                hours=-x))
                    # And others is descending order
                    else:
                        days_offset = epoch_days + 1 + x
                        naked_build.date_finished = (
                            now - timedelta(days=days_offset))

                    naked_build.date_started = (
                        naked_build.date_finished - timedelta(minutes=5))
                    rbr = RecipeBuildRecord(
                        removeSecurityProxy(sourcepackagename),
                        removeSecurityProxy(recipeowner),
                        removeSecurityProxy(archive),
                        removeSecurityProxy(recipe),
                        naked_build.date_finished.replace(tzinfo=None))

                    # Only return fully completed daily builds.
                    if daily and build_status == BuildStatus.FULLYBUILT:
                        if x < num_recent_records:
                            records_inside_epoch.append(rbr)
                        all_records.append(rbr)
        # We need to explicitly commit because if don't, the records don't
        # appear in the slave datastore.
        transaction.commit()

        # We need to ensure our results are sorted by correctly
        def _sort_records(records):
            records.sort(lambda x, y:
                cmp(x.sourcepackagename.name, y.sourcepackagename.name) or
                -cmp(x.most_recent_build_time, y.most_recent_build_time))

        _sort_records(all_records)
        _sort_records(records_inside_epoch)
        return all_records, records_inside_epoch

    def makeDscFile(self, tempdir_path=None):
        """Make a DscFile.

        :param tempdir_path: Path to a temporary directory to use.  If not
            supplied, a temp directory will be created.
        """
        filename = 'ed_0.2-20.dsc'
        contexts = []
        if tempdir_path is None:
            contexts.append(temp_dir())
        # Use nested so temp_dir is an optional context.
        with nested(*contexts) as result:
            if tempdir_path is None:
                tempdir_path = result[0]
            fullpath = os.path.join(tempdir_path, filename)
            with open(fullpath, 'w') as dsc_file:
                dsc_file.write(dedent("""\
                Format: 1.0
                Source: ed
                Version: 0.2-20
                Binary: ed
                Maintainer: James Troup <james@nocrew.org>
                Architecture: any
                Standards-Version: 3.5.8.0
                Build-Depends: dpatch
                Files:
                 ddd57463774cae9b50e70cd51221281b 185913 ed_0.2.orig.tar.gz
                 f9e1e5f13725f581919e9bfd62272a05 8506 ed_0.2-20.diff.gz
                """))

            class Changes:
                architectures = ['source']
            logger = BufferLogger()
            policy = BuildDaemonUploadPolicy()
            policy.distroseries = self.makeDistroSeries()
            policy.archive = self.makeArchive()
            policy.distro = policy.distroseries.distribution
            dsc_file = DSCFile(fullpath, 'digest', 0, 'main/editors',
                'priority', 'package', 'version', Changes, policy, logger)
            list(dsc_file.verify())
        return dsc_file

    def makeTranslationTemplatesBuildJob(self, branch=None):
        """Make a new `TranslationTemplatesBuildJob`.

        :param branch: The branch that the job should be for.  If none
            is given, one will be created.
        """
        if branch is None:
            branch = self.makeBranch()

        jobset = getUtility(ITranslationTemplatesBuildJobSource)
        return jobset.create(branch)

    def makePOTemplate(self, productseries=None, distroseries=None,
                       sourcepackagename=None, owner=None, name=None,
                       translation_domain=None, path=None,
                       copy_pofiles=True, side=None, sourcepackage=None):
        """Make a new translation template."""
        if sourcepackage is not None:
            assert distroseries is None, (
                'Cannot specify sourcepackage and distroseries')
            distroseries = sourcepackage.distroseries
            assert sourcepackagename is None, (
                'Cannot specify sourcepackage and sourcepackagename')
            sourcepackagename = sourcepackage.sourcepackagename
        if productseries is None and distroseries is None:
            if side != TranslationSide.UBUNTU:
                # No context for this template; set up a productseries.
                productseries = self.makeProductSeries(owner=owner)
                # Make it use Translations, otherwise there's little point
                # to us creating a template for it.
                naked_series = removeSecurityProxy(productseries)
                naked_series.product.translations_usage = (
                    ServiceUsage.LAUNCHPAD)
            else:
                distroseries = self.makeUbuntuDistroSeries()
        if distroseries is not None and sourcepackagename is None:
            sourcepackagename = self.makeSourcePackageName()

        templateset = getUtility(IPOTemplateSet)
        subset = templateset.getSubset(
            distroseries, sourcepackagename, productseries)

        if name is None:
            name = self.getUniqueString()
        if translation_domain is None:
            translation_domain = self.getUniqueString()

        if owner is None:
            if productseries is None:
                owner = distroseries.owner
            else:
                owner = productseries.owner

        if path is None:
            path = 'messages.pot'

        return subset.new(name, translation_domain, path, owner, copy_pofiles)

    def makePOTemplateAndPOFiles(self, language_codes, **kwargs):
        """Create a POTemplate and associated POFiles.

        Create a POTemplate for the given distroseries/sourcepackagename or
        productseries and create a POFile for each language. Returns the
        template.
        """
        template = self.makePOTemplate(**kwargs)
        for language_code in language_codes:
            self.makePOFile(language_code, template, template.owner)
        return template

    def makePOFile(self, language_code=None, potemplate=None, owner=None,
                   create_sharing=False, language=None, side=None):
        """Make a new translation file."""
        assert language_code is None or language is None, (
            "Please specifiy only one of language_code and language.")
        if language_code is None:
            if language is None:
                language = self.makeLanguage()
            language_code = language.code
        if potemplate is None:
            potemplate = self.makePOTemplate(owner=owner, side=side)
        else:
            assert side is None, 'Cannot specify both side and potemplate.'
        return potemplate.newPOFile(language_code,
                                    create_sharing=create_sharing)

    def makePOTMsgSet(self, potemplate=None, singular=None, plural=None,
                      context=None, sequence=None, commenttext=None,
                      filereferences=None, sourcecomment=None,
                      flagscomment=None):
        """Make a new `POTMsgSet` in the given template."""
        if potemplate is None:
            potemplate = self.makePOTemplate()
        if singular is None and plural is None:
            singular = self.getUniqueString()
        if sequence is None:
            sequence = self.getUniqueInteger()
        potmsgset = potemplate.createMessageSetFromText(
            singular, plural, context, sequence)
        if commenttext is not None:
            potmsgset.commenttext = commenttext
        if filereferences is not None:
            potmsgset.filereferences = filereferences
        if sourcecomment is not None:
            potmsgset.sourcecomment = sourcecomment
        if flagscomment is not None:
            potmsgset.flagscomment = flagscomment
        removeSecurityProxy(potmsgset).sync()
        return potmsgset

    def makePOFileAndPOTMsgSet(self, language_code=None, msgid=None,
                               with_plural=False, side=None):
        """Make a `POFile` with a `POTMsgSet`."""
        pofile = self.makePOFile(language_code, side=side)

        if with_plural:
            if msgid is None:
                msgid = self.getUniqueString()
            plural = self.getUniqueString()
        else:
            plural = None

        potmsgset = self.makePOTMsgSet(
            pofile.potemplate, singular=msgid, plural=plural)

        return pofile, potmsgset

    def makeTranslationsDict(self, translations=None):
        """Make sure translations are stored in a dict, e.g. {0: "foo"}.

        If translations is already dict, it is returned unchanged.
        If translations is a sequence, it is enumerated into a dict.
        If translations is None, an arbitrary single translation is created.
        """
        translations = removeSecurityProxy(translations)
        if translations is None:
            return {0: self.getUniqueString()}
        if isinstance(translations, dict):
            return translations
        assert isinstance(translations, (list, tuple)), (
                "Expecting either a dict or a sequence.")
        return dict(enumerate(translations))

    def makeSuggestion(self, pofile=None, potmsgset=None, translator=None,
                       translations=None, date_created=None):
        """Make a new suggested `TranslationMessage` in the given PO file."""
        if pofile is None:
            pofile = self.makePOFile('sr')
        if potmsgset is None:
            potmsgset = self.makePOTMsgSet(pofile.potemplate)
        if translator is None:
            translator = self.makePerson()
        translations = self.makeTranslationsDict(translations)
        translation_message = potmsgset.submitSuggestion(
            pofile, translator, translations)
        assert translation_message is not None, (
            "Cannot make suggestion on translation credits POTMsgSet.")
        if date_created is not None:
            naked_translation_message = removeSecurityProxy(
                translation_message)
            naked_translation_message.date_created = date_created
            naked_translation_message.sync()
        return translation_message

    def makeCurrentTranslationMessage(self, pofile=None, potmsgset=None,
                                      translator=None, reviewer=None,
                                      translations=None, diverged=False,
                                      current_other=False,
                                      date_created=None, date_reviewed=None,
                                      language=None, side=None,
                                      potemplate=None):
        """Create a `TranslationMessage` and make it current.

        By default the message will only be current on the side (Ubuntu
        or upstream) that `pofile` is on.

        Be careful: if the message is already translated, calling this
        method may violate database unique constraints.

        :param pofile: `POFile` to put translation in; if omitted, one
            will be created.
        :param potmsgset: `POTMsgSet` to translate; if omitted, one will
            be created (with sequence number 1).
        :param translator: `Person` who created the translation.  If
            omitted, one will be created.
        :param reviewer: `Person` who reviewed the translation.  If
            omitted, one will be created.
        :param translations: Strings to translate the `POTMsgSet`
            to.  Can be either a list, or a dict mapping plural form
            numbers to the forms' respective translations.
            If omitted, will translate to a single random string.
        :param diverged: Create a diverged message?
        :param current_other: Should the message also be current on the
            other translation side?  (Cannot be combined with `diverged`).
        :param date_created: Force a specific creation date instead of 'now'.
        :param date_reviewed: Force a specific review date instead of 'now'.
        :param language: `Language` to use for the POFile
        :param side: The `TranslationSide` this translation should be for.
        :param potemplate: If provided, the POTemplate to use when creating
            the POFile.
        """
        assert not (diverged and current_other), (
            "A diverged message can't be current on the other side.")
        assert None in (language, pofile), (
            'Cannot specify both language and pofile.')
        assert None in (side, pofile), (
            'Cannot specify both side and pofile.')
        link_potmsgset_with_potemplate = (
            (pofile is None and potemplate is None) or potmsgset is None)
        if pofile is None:
            pofile = self.makePOFile(
                language=language, side=side, potemplate=potemplate)
        else:
            assert potemplate is None, (
                'Cannot specify both pofile and potemplate')
        potemplate = pofile.potemplate
        if potmsgset is None:
            potmsgset = self.makePOTMsgSet(potemplate)
        if link_potmsgset_with_potemplate:
            # If we have a new pofile or a new potmsgset, we must link
            # the potmsgset to the pofile's potemplate.
            potmsgset.setSequence(
                pofile.potemplate, self.getUniqueInteger())
        else:
            # Otherwise it is the duty of the callsite to ensure
            # consistency.
            store = IStore(TranslationTemplateItem)
            tti_for_message_in_template = store.find(
                TranslationTemplateItem.potmsgset == potmsgset,
                TranslationTemplateItem.potemplate == pofile.potemplate).any()
            assert tti_for_message_in_template is not None
        if translator is None:
            translator = self.makePerson()
        if reviewer is None:
            reviewer = self.makePerson()
        translations = sanitize_translations_from_webui(
            potmsgset.singular_text,
            self.makeTranslationsDict(translations),
            pofile.language.pluralforms)

        if diverged:
            message = self.makeDivergedTranslationMessage(
                pofile, potmsgset, translator, reviewer,
                translations, date_created)
        else:
            message = potmsgset.setCurrentTranslation(
                pofile, translator, translations,
                RosettaTranslationOrigin.ROSETTAWEB,
                share_with_other_side=current_other)
            if date_created is not None:
                removeSecurityProxy(message).date_created = date_created

        message.markReviewed(reviewer, date_reviewed)

        return message

    def makeDivergedTranslationMessage(self, pofile=None, potmsgset=None,
                                       translator=None, reviewer=None,
                                       translations=None, date_created=None):
        """Create a diverged, current `TranslationMessage`."""
        if pofile is None:
            pofile = self.makePOFile('lt')
        if reviewer is None:
            reviewer = self.makePerson()

        message = self.makeSuggestion(
            pofile=pofile, potmsgset=potmsgset, translator=translator,
            translations=translations, date_created=date_created)
        return message.approveAsDiverged(pofile, reviewer)

    def makeTranslationImportQueueEntry(self, path=None, productseries=None,
                                        distroseries=None,
                                        sourcepackagename=None,
                                        potemplate=None, content=None,
                                        uploader=None, pofile=None,
                                        format=None, status=None,
                                        by_maintainer=False):
        """Create a `TranslationImportQueueEntry`."""
        if path is None:
            path = self.getUniqueString() + '.pot'

        for_distro = not (distroseries is None and sourcepackagename is None)
        for_project = productseries is not None
        has_template = (potemplate is not None)
        if has_template and not for_distro and not for_project:
            # Copy target from template.
            distroseries = potemplate.distroseries
            sourcepackagename = potemplate.sourcepackagename
            productseries = potemplate.productseries

        if sourcepackagename is None and distroseries is None:
            if productseries is None:
                productseries = self.makeProductSeries()
        else:
            if sourcepackagename is None:
                sourcepackagename = self.makeSourcePackageName()
            if distroseries is None:
                distroseries = self.makeDistroSeries()

        if uploader is None:
            uploader = self.makePerson()

        if content is None:
            content = self.getUniqueString()
        content_reference = getUtility(ILibraryFileAliasSet).create(
            name=os.path.basename(path), size=len(content),
            file=StringIO(content), contentType='text/plain')

        if format is None:
            format = TranslationFileFormat.PO

        if status is None:
            status = RosettaImportStatus.NEEDS_REVIEW

        return TranslationImportQueueEntry(
            path=path, productseries=productseries, distroseries=distroseries,
            sourcepackagename=sourcepackagename, importer=uploader,
            content=content_reference, status=status, format=format,
            by_maintainer=by_maintainer)

    def makeMailingList(self, team, owner):
        """Create a mailing list for the team."""
        team_list = getUtility(IMailingListSet).new(team, owner)
        team_list.startConstructing()
        team_list.transitionToStatus(MailingListStatus.ACTIVE)
        return team_list

    def makeTeamAndMailingList(
        self, team_name, owner_name,
        visibility=None,
        subscription_policy=TeamSubscriptionPolicy.OPEN):
        """Make a new active mailing list for the named team.

        :param team_name: The new team's name.
        :type team_name: string
        :param owner_name: The name of the team's owner.
        :type owner: string
        :param visibility: The team's visibility. If it's None, the default
            (public) will be used.
        :type visibility: `PersonVisibility`
        :param subscription_policy: The subscription policy of the team.
        :type subscription_policy: `TeamSubscriptionPolicy`
        :return: The new team and mailing list.
        :rtype: (`ITeam`, `IMailingList`)
        """
        owner = getUtility(IPersonSet).getByName(owner_name)
        display_name = SPACE.join(
            word.capitalize() for word in team_name.split('-'))
        team = getUtility(IPersonSet).getByName(team_name)
        if team is None:
            team = self.makeTeam(
                owner, displayname=display_name, name=team_name,
                visibility=visibility,
                subscription_policy=subscription_policy)
        team_list = self.makeMailingList(team, owner)
        return team, team_list

    def makeTeamWithMailingListSubscribers(self, team_name, super_team=None,
                                           auto_subscribe=True):
        """Create a team, mailing list, and subscribers.

        :param team_name: The name of the team to create.
        :param super_team: Make the team a member of the super_team.
        :param auto_subscribe: Automatically subscribe members to the
            mailing list.
        :return: A tuple of team and the member user.
        """
        team = self.makeTeam(name=team_name)
        member = self.makePerson()
        with celebrity_logged_in('admin'):
            if super_team is None:
                mailing_list = self.makeMailingList(team, team.teamowner)
            else:
                super_team.addMember(
                    team, reviewer=team.teamowner, force_team_add=True)
                mailing_list = super_team.mailing_list
            team.addMember(member, reviewer=team.teamowner)
            if auto_subscribe:
                mailing_list.subscribe(member)
        return team, member

    def makeMirrorProbeRecord(self, mirror):
        """Create a probe record for a mirror of a distribution."""
        log_file = StringIO()
        log_file.write("Fake probe, nothing useful here.")
        log_file.seek(0)

        library_alias = getUtility(ILibraryFileAliasSet).create(
            name='foo', size=len(log_file.getvalue()),
            file=log_file, contentType='text/plain')

        proberecord = mirror.newProbeRecord(library_alias)
        return proberecord

    def makeMirror(self, distribution, displayname=None, country=None,
                   http_url=None, ftp_url=None, rsync_url=None,
                   official_candidate=False):
        """Create a mirror for the distribution."""
        if displayname is None:
            displayname = self.getUniqueString("mirror")
        # If no URL is specified create an HTTP URL.
        if http_url is None and ftp_url is None and rsync_url is None:
            http_url = self.getUniqueURL()
        # If no country is given use Argentina.
        if country is None:
            country = getUtility(ICountrySet)['AR']

        mirror = distribution.newMirror(
            owner=distribution.owner,
            speed=MirrorSpeed.S256K,
            country=country,
            content=MirrorContent.ARCHIVE,
            displayname=displayname,
            description=None,
            http_base_url=http_url,
            ftp_base_url=ftp_url,
            rsync_base_url=rsync_url,
            official_candidate=official_candidate)
        return mirror

    def makeUniqueRFC822MsgId(self):
        """Make a unique RFC 822 message id.

        The created message id is guaranteed not to exist in the
        `Message` table already.
        """
        msg_id = make_msgid('launchpad')
        while Message.selectBy(rfc822msgid=msg_id).count() > 0:
            msg_id = make_msgid('launchpad')
        return msg_id

    def makeSourcePackageName(self, name=None):
        """Make an `ISourcePackageName`."""
        if name is None:
            name = self.getUniqueString()
        return getUtility(ISourcePackageNameSet).new(name)

    def getOrMakeSourcePackageName(self, name=None):
        """Get an existing`ISourcePackageName` or make a new one.

        This method encapsulates getOrCreateByName so that tests can be kept
        free of the getUtility(ISourcePackageNameSet) noise.
        """
        if name is None:
            return self.makeSourcePackageName()
        return getUtility(ISourcePackageNameSet).getOrCreateByName(name)

    def makeSourcePackage(self, sourcepackagename=None, distroseries=None):
        """Make an `ISourcePackage`."""
        # Make sure we have a real sourcepackagename object.
        if (sourcepackagename is None or
            isinstance(sourcepackagename, basestring)):
            sourcepackagename = self.getOrMakeSourcePackageName(
                sourcepackagename)
        if distroseries is None:
            distroseries = self.makeDistroRelease()
        return distroseries.getSourcePackage(sourcepackagename)

    def getAnySourcePackageUrgency(self):
        return SourcePackageUrgency.MEDIUM

    def makePackageUpload(self, distroseries=None, archive=None,
                          pocket=None, changes_filename=None,
                          changes_file_content=None,
                          signing_key=None, status=None,
                          package_copy_job=None):
        if archive is None:
            archive = self.makeArchive()
        if distroseries is None:
            distroseries = self.makeDistroSeries(
                distribution=archive.distribution)
        if changes_filename is None:
            changes_filename = self.getUniqueString("changesfilename")
        if changes_file_content is None:
            changes_file_content = self.getUniqueString("changesfilecontent")
        if pocket is None:
            pocket = PackagePublishingPocket.RELEASE
        package_upload = distroseries.createQueueEntry(
            pocket, archive, changes_filename, changes_file_content,
            signing_key=signing_key, package_copy_job=package_copy_job)
        if status is not None:
            naked_package_upload = removeSecurityProxy(package_upload)
            status_changers = {
                PackageUploadStatus.DONE: naked_package_upload.setDone,
                PackageUploadStatus.ACCEPTED:
                    naked_package_upload.setAccepted,
                }
            status_changers[status]()
        return package_upload

    def makeSourcePackageUpload(self, distroseries=None,
                                sourcepackagename=None):
        """Make a `PackageUpload` with a `PackageUploadSource` attached."""
        if distroseries is None:
            distroseries = self.makeDistroSeries()
        upload = self.makePackageUpload(
            distroseries=distroseries, archive=distroseries.main_archive)
        upload.addSource(self.makeSourcePackageRelease(
            sourcepackagename=sourcepackagename))
        return upload

    def makeBuildPackageUpload(self, distroseries=None,
                               binarypackagename=None):
        """Make a `PackageUpload` with a `PackageUploadBuild` attached."""
        if distroseries is None:
            distroseries = self.makeDistroSeries()
        upload = self.makePackageUpload(
            distroseries=distroseries, archive=distroseries.main_archive)
        build = self.makeBinaryPackageBuild()
        upload.addBuild(build)
        self.makeBinaryPackageRelease(
            binarypackagename=binarypackagename, build=build)
        return upload

    def makeCustomPackageUpload(self, distroseries=None, custom_type=None,
                                filename=None):
        """Make a `PackageUpload` with a `PackageUploadCustom` attached."""
        if distroseries is None:
            distroseries = self.makeDistroSeries()
        if custom_type is None:
            custom_type = PackageUploadCustomFormat.DEBIAN_INSTALLER
        upload = self.makePackageUpload(
            distroseries=distroseries, archive=distroseries.main_archive)
        file_alias = self.makeLibraryFileAlias(filename=filename)
        upload.addCustom(file_alias, custom_type)
        return upload

    def makeCopyJobPackageUpload(self, distroseries=None,
                                 sourcepackagename=None, target_pocket=None):
        """Make a `PackageUpload` with a `PackageCopyJob` attached."""
        if distroseries is None:
            distroseries = self.makeDistroSeries()
        spph = self.makeSourcePackagePublishingHistory(
            sourcepackagename=sourcepackagename)
        spr = spph.sourcepackagerelease
        job = self.makePlainPackageCopyJob(
            package_name=spr.sourcepackagename.name,
            package_version=spr.version,
            source_archive=spph.archive,
            target_pocket=target_pocket,
            target_archive=distroseries.main_archive,
            target_distroseries=distroseries)
        job.addSourceOverride(SourceOverride(
            spr.sourcepackagename, spr.component, spr.section))
        try:
            job.run()
        except SuspendJobException:
            # Expected exception.
            job.suspend()
        upload_set = getUtility(IPackageUploadSet)
        return upload_set.getByPackageCopyJobIDs([job.id]).one()

    def makeSourcePackageRelease(self, archive=None, sourcepackagename=None,
                                 distroseries=None, maintainer=None,
                                 creator=None, component=None,
                                 section_name=None, urgency=None,
                                 version=None, builddepends=None,
                                 builddependsindep=None,
                                 build_conflicts=None,
                                 build_conflicts_indep=None,
                                 architecturehintlist='all',
                                 dsc_maintainer_rfc822=None,
                                 dsc_standards_version='3.6.2',
                                 dsc_format='1.0', dsc_binaries='foo-bin',
                                 date_uploaded=UTC_NOW,
                                 source_package_recipe_build=None,
                                 dscsigningkey=None,
                                 user_defined_fields=None,
                                 changelog_entry=None,
                                 homepage=None,
                                 changelog=None):
        """Make a `SourcePackageRelease`."""
        if distroseries is None:
            if source_package_recipe_build is not None:
                distroseries = source_package_recipe_build.distroseries
            else:
                if archive is None:
                    distribution = None
                else:
                    distribution = archive.distribution
                distroseries = self.makeDistroRelease(
                    distribution=distribution)

        if archive is None:
            archive = self.makeArchive(
                distribution=distroseries.distribution,
                purpose=ArchivePurpose.PRIMARY)

        if (sourcepackagename is None or
            isinstance(sourcepackagename, basestring)):
            sourcepackagename = self.getOrMakeSourcePackageName(
                sourcepackagename)

        if component is None:
            component = self.makeComponent()

        if urgency is None:
            urgency = self.getAnySourcePackageUrgency()

        section = self.makeSection(name=section_name)

        if maintainer is None:
            maintainer = self.makePerson()

        if dsc_maintainer_rfc822 is None:
            dsc_maintainer_rfc822 = '%s <%s>' % (
                maintainer.displayname,
                removeSecurityProxy(maintainer).preferredemail.email)

        if creator is None:
            creator = self.makePerson()

        if version is None:
            version = unicode(self.getUniqueInteger()) + 'version'

        return distroseries.createUploadedSourcePackageRelease(
            sourcepackagename=sourcepackagename,
            maintainer=maintainer,
            creator=creator,
            component=component,
            section=section,
            urgency=urgency,
            version=version,
            builddepends=builddepends,
            builddependsindep=builddependsindep,
            build_conflicts=build_conflicts,
            build_conflicts_indep=build_conflicts_indep,
            architecturehintlist=architecturehintlist,
            changelog=changelog,
            changelog_entry=changelog_entry,
            dsc=None,
            copyright=self.getUniqueString(),
            dscsigningkey=dscsigningkey,
            dsc_maintainer_rfc822=dsc_maintainer_rfc822,
            dsc_standards_version=dsc_standards_version,
            dsc_format=dsc_format,
            dsc_binaries=dsc_binaries,
            archive=archive,
            dateuploaded=date_uploaded,
            source_package_recipe_build=source_package_recipe_build,
            user_defined_fields=user_defined_fields,
            homepage=homepage)

    def makeSourcePackageReleaseFile(self, sourcepackagerelease=None,
                                     library_file=None, filetype=None):
        if sourcepackagerelease is None:
            sourcepackagerelease = self.makeSourcePackageRelease()
        if library_file is None:
            library_file = self.makeLibraryFileAlias()
        if filetype is None:
            filetype = SourcePackageFileType.DSC
        return ProxyFactory(
            SourcePackageReleaseFile(
                sourcepackagerelease=sourcepackagerelease,
                libraryfile=library_file, filetype=filetype))

    def makeBinaryPackageBuild(self, source_package_release=None,
            distroarchseries=None, archive=None, builder=None,
            status=None, pocket=None, date_created=None, processor=None,
            sourcepackagename=None):
        """Create a BinaryPackageBuild.

        If archive is not supplied, the source_package_release is used
        to determine archive.
        :param source_package_release: The SourcePackageRelease this binary
            build uses as its source.
        :param sourcepackagename: when source_package_release is None, the
            sourcepackagename from which the build will come.
        :param distroarchseries: The DistroArchSeries to use.
        :param archive: The Archive to use.
        :param builder: An optional builder to assign.
        :param status: The BuildStatus for the build.
        """
        if archive is None:
            if source_package_release is None:
                archive = self.makeArchive()
            else:
                archive = source_package_release.upload_archive
        if processor is None:
            processor = self.makeProcessor()
        if distroarchseries is None:
            if source_package_release is not None:
                distroseries = source_package_release.upload_distroseries
            else:
                distroseries = self.makeDistroSeries()
            distroarchseries = self.makeDistroArchSeries(
                distroseries=distroseries,
                processorfamily=processor.family)
        if pocket is None:
            pocket = PackagePublishingPocket.RELEASE
        if source_package_release is None:
            multiverse = self.makeComponent(name='multiverse')
            source_package_release = self.makeSourcePackageRelease(
                archive, component=multiverse,
                distroseries=distroarchseries.distroseries,
                sourcepackagename=sourcepackagename)
            self.makeSourcePackagePublishingHistory(
                distroseries=source_package_release.upload_distroseries,
                archive=archive, sourcepackagerelease=source_package_release,
                pocket=pocket)
        if status is None:
            status = BuildStatus.NEEDSBUILD
        if date_created is None:
            date_created = self.getUniqueDate()
        binary_package_build = getUtility(IBinaryPackageBuildSet).new(
            source_package_release=source_package_release,
            processor=processor,
            distro_arch_series=distroarchseries,
            status=status,
            archive=archive,
            pocket=pocket,
            date_created=date_created)
        naked_build = removeSecurityProxy(binary_package_build)
        naked_build.builder = builder
        IStore(binary_package_build).flush()
        return binary_package_build

    def makeSourcePackagePublishingHistory(self, sourcepackagename=None,
                                           distroseries=None, maintainer=None,
                                           creator=None, component=None,
                                           section_name=None,
                                           urgency=None, version=None,
                                           archive=None,
                                           builddepends=None,
                                           builddependsindep=None,
                                           build_conflicts=None,
                                           build_conflicts_indep=None,
                                           architecturehintlist='all',
                                           dateremoved=None,
                                           date_uploaded=UTC_NOW,
                                           pocket=None,
                                           status=None,
                                           scheduleddeletiondate=None,
                                           dsc_standards_version='3.6.2',
                                           dsc_format='1.0',
                                           dsc_binaries='foo-bin',
                                           sourcepackagerelease=None,
                                           ancestor=None,
                                           ):
        """Make a `SourcePackagePublishingHistory`."""
        if distroseries is None:
            if archive is None:
                distribution = None
            else:
                distribution = archive.distribution
            distroseries = self.makeDistroRelease(distribution=distribution)

        if archive is None:
            archive = self.makeArchive(
                distribution=distroseries.distribution,
                purpose=ArchivePurpose.PRIMARY)

        if pocket is None:
            pocket = self.getAnyPocket()

        if status is None:
            status = PackagePublishingStatus.PENDING

        if sourcepackagerelease is None:
            sourcepackagerelease = self.makeSourcePackageRelease(
                archive=archive,
                sourcepackagename=sourcepackagename,
                distroseries=distroseries,
                maintainer=maintainer,
                creator=creator, component=component,
                section_name=section_name,
                urgency=urgency,
                version=version,
                builddepends=builddepends,
                builddependsindep=builddependsindep,
                build_conflicts=build_conflicts,
                build_conflicts_indep=build_conflicts_indep,
                architecturehintlist=architecturehintlist,
                dsc_standards_version=dsc_standards_version,
                dsc_format=dsc_format,
                dsc_binaries=dsc_binaries,
                date_uploaded=date_uploaded)

        spph = getUtility(IPublishingSet).newSourcePublication(
            archive, sourcepackagerelease, distroseries,
            sourcepackagerelease.component, sourcepackagerelease.section,
            pocket, ancestor)

        naked_spph = removeSecurityProxy(spph)
        naked_spph.status = status
        naked_spph.datecreated = date_uploaded
        naked_spph.dateremoved = dateremoved
        naked_spph.scheduleddeletiondate = scheduleddeletiondate
        if status == PackagePublishingStatus.PUBLISHED:
            naked_spph.datepublished = UTC_NOW
        return spph

    def makeBinaryPackagePublishingHistory(self, binarypackagerelease=None,
                                           binarypackagename=None,
                                           distroarchseries=None,
                                           component=None, section_name=None,
                                           priority=None, status=None,
                                           scheduleddeletiondate=None,
                                           dateremoved=None,
                                           pocket=None, archive=None,
                                           source_package_release=None,
                                           sourcepackagename=None):
        """Make a `BinaryPackagePublishingHistory`."""
        if distroarchseries is None:
            if archive is None:
                distribution = None
            else:
                distribution = archive.distribution
            distroseries = self.makeDistroSeries(distribution=distribution)
            distroarchseries = self.makeDistroArchSeries(
                distroseries=distroseries)

        if archive is None:
            archive = self.makeArchive(
                distribution=distroarchseries.distroseries.distribution,
                purpose=ArchivePurpose.PRIMARY)

        if pocket is None:
            pocket = self.getAnyPocket()

        if status is None:
            status = PackagePublishingStatus.PENDING

        if priority is None:
            priority = PackagePublishingPriority.OPTIONAL

        if binarypackagerelease is None:
            # Create a new BinaryPackageBuild and BinaryPackageRelease
            # in the same archive and suite.
            binarypackagebuild = self.makeBinaryPackageBuild(
                archive=archive, distroarchseries=distroarchseries,
                pocket=pocket, source_package_release=source_package_release,
                sourcepackagename=sourcepackagename)
            binarypackagerelease = self.makeBinaryPackageRelease(
                binarypackagename=binarypackagename,
                build=binarypackagebuild,
                component=component,
                section_name=section_name,
                priority=priority)

        bpph = getUtility(IPublishingSet).newBinaryPublication(
            archive, binarypackagerelease, distroarchseries,
            binarypackagerelease.component, binarypackagerelease.section,
            priority, pocket)
        naked_bpph = removeSecurityProxy(bpph)
        naked_bpph.status = status
        naked_bpph.dateremoved = dateremoved
        naked_bpph.scheduleddeletiondate = scheduleddeletiondate
        naked_bpph.priority = priority
        if status == PackagePublishingStatus.PUBLISHED:
            naked_bpph.datepublished = UTC_NOW
        return bpph

    def makeBinaryPackageName(self, name=None):
        """Make an `IBinaryPackageName`."""
        if name is None:
            name = self.getUniqueString("binarypackage")
        return getUtility(IBinaryPackageNameSet).new(name)

    def getOrMakeBinaryPackageName(self, name=None):
        """Get an existing `IBinaryPackageName` or make a new one.

        This method encapsulates getOrCreateByName so that tests can be kept
        free of the getUtility(IBinaryPackageNameSet) noise.
        """
        if name is None:
            return self.makeBinaryPackageName()
        return getUtility(IBinaryPackageNameSet).getOrCreateByName(name)

    def makeBinaryPackageFile(self, binarypackagerelease=None,
                              library_file=None, filetype=None):
        if binarypackagerelease is None:
            binarypackagerelease = self.makeBinaryPackageRelease()
        if library_file is None:
            library_file = self.makeLibraryFileAlias()
        if filetype is None:
            filetype = BinaryPackageFileType.DEB
        return ProxyFactory(BinaryPackageFile(
            binarypackagerelease=binarypackagerelease,
            libraryfile=library_file, filetype=filetype))

    def makeBinaryPackageRelease(self, binarypackagename=None,
                                 version=None, build=None,
                                 binpackageformat=None, component=None,
                                 section_name=None, priority=None,
                                 architecturespecific=False,
                                 summary=None, description=None,
                                 shlibdeps=None, depends=None,
                                 recommends=None, suggests=None,
                                 conflicts=None, replaces=None,
                                 provides=None, pre_depends=None,
                                 enhances=None, breaks=None,
                                 essential=False, installed_size=None,
                                 date_created=None, debug_package=None,
                                 homepage=None):
        """Make a `BinaryPackageRelease`."""
        if build is None:
            build = self.makeBinaryPackageBuild()
        if (binarypackagename is None or
            isinstance(binarypackagename, basestring)):
            binarypackagename = self.getOrMakeBinaryPackageName(
                binarypackagename)
        if version is None:
            version = build.source_package_release.version
        if binpackageformat is None:
            binpackageformat = BinaryPackageFormat.DEB
        if component is None:
            component = build.source_package_release.component
        section = build.source_package_release.section
        if priority is None:
            priority = PackagePublishingPriority.OPTIONAL
        if summary is None:
            summary = self.getUniqueString("summary")
        if description is None:
            description = self.getUniqueString("description")
        if installed_size is None:
            installed_size = self.getUniqueInteger()
        bpr = build.createBinaryPackageRelease(
                binarypackagename=binarypackagename, version=version,
                binpackageformat=binpackageformat,
                component=component, section=section, priority=priority,
                summary=summary, description=description,
                architecturespecific=architecturespecific,
                shlibdeps=shlibdeps, depends=depends, recommends=recommends,
                suggests=suggests, conflicts=conflicts, replaces=replaces,
                provides=provides, pre_depends=pre_depends,
                enhances=enhances, breaks=breaks, essential=essential,
                installedsize=installed_size, debug_package=debug_package,
                homepage=homepage)
        if date_created is not None:
            removeSecurityProxy(bpr).datecreated = date_created
        return bpr

    def makeSection(self, name=None):
        """Make a `Section`."""
        if name is None:
            name = self.getUniqueString('section')
        return getUtility(ISectionSet).ensure(name)

    def makePackageset(self, name=None, description=None, owner=None,
                       packages=(), distroseries=None, related_set=None):
        """Make an `IPackageset`."""
        if name is None:
            name = self.getUniqueString(u'package-set-name')
        if description is None:
            description = self.getUniqueString(u'package-set-description')
        if owner is None:
            person = self.getUniqueString(u'package-set-owner')
            owner = self.makePerson(name=person)
        techboard = getUtility(ILaunchpadCelebrities).ubuntu_techboard
        ps_set = getUtility(IPackagesetSet)
        package_set = run_with_login(
            techboard.teamowner,
            lambda: ps_set.new(
                name, description, owner, distroseries, related_set))
        run_with_login(owner, lambda: package_set.add(packages))
        return package_set

    def getAnyPocket(self):
        return PackagePublishingPocket.BACKPORTS

    def makeSuiteSourcePackage(self, distroseries=None,
                               sourcepackagename=None, pocket=None):
        if distroseries is None:
            distroseries = self.makeDistroRelease()
        if pocket is None:
            pocket = self.getAnyPocket()
        # Make sure we have a real sourcepackagename object.
        if (sourcepackagename is None or
            isinstance(sourcepackagename, basestring)):
            sourcepackagename = self.getOrMakeSourcePackageName(
                sourcepackagename)
        return ProxyFactory(
            SuiteSourcePackage(distroseries, pocket, sourcepackagename))

    def makeDistributionSourcePackage(self, sourcepackagename=None,
                                      distribution=None, with_db=False):
        # Make sure we have a real sourcepackagename object.
        if (sourcepackagename is None or
            isinstance(sourcepackagename, basestring)):
            sourcepackagename = self.getOrMakeSourcePackageName(
                sourcepackagename)
        if distribution is None:
            distribution = self.makeDistribution()
        package = distribution.getSourcePackage(sourcepackagename)
        if with_db:
            # Create an instance with a database record, that is normally
            # done by secondary process.
            removeSecurityProxy(package)._new(
                distribution, sourcepackagename, False)
        return package

    def makeEmailMessage(self, body=None, sender=None, to=None,
                         attachments=None, encode_attachments=False):
        """Make an email message with possible attachments.

        :param attachments: Should be an interable of tuples containing
           (filename, content-type, payload)
        """
        if sender is None:
            sender = self.makePerson()
        if body is None:
            body = self.getUniqueString('body')
        if to is None:
            to = self.getUniqueEmailAddress()

        msg = MIMEMultipart()
        msg['Message-Id'] = make_msgid('launchpad')
        msg['Date'] = formatdate()
        msg['To'] = to
        msg['From'] = removeSecurityProxy(sender).preferredemail.email
        msg['Subject'] = 'Sample'

        if attachments is None:
            msg.set_payload(body)
        else:
            msg.attach(MIMEText(body))
            for filename, content_type, payload in attachments:
                attachment = EmailMessage()
                attachment.set_payload(payload)
                attachment['Content-Type'] = content_type
                attachment['Content-Disposition'] = (
                    'attachment; filename="%s"' % filename)
                if encode_attachments:
                    encode_base64(attachment)
                msg.attach(attachment)
        return msg

    def makeBundleMergeDirectiveEmail(self, source_branch, target_branch,
                                      signing_context=None, sender=None):
        """Create a merge directive email from two bzr branches.

        :param source_branch: The source branch for the merge directive.
        :param target_branch: The target branch for the merge directive.
        :param signing_context: A GPGSigningContext instance containing the
            gpg key to sign with.  If None, the message is unsigned.  The
            context also contains the password and gpg signing mode.
        :param sender: The `Person` that is sending the email.
        """
        md = MergeDirective2.from_objects(
            source_branch.repository, source_branch.last_revision(),
            public_branch=source_branch.get_public_branch(),
            target_branch=target_branch.getInternalBzrUrl(),
            local_target_branch=target_branch.getInternalBzrUrl(), time=0,
            timezone=0)
        email = None
        if sender is not None:
            email = removeSecurityProxy(sender).preferredemail.email
        return self.makeSignedMessage(
            body='My body', subject='My subject',
            attachment_contents=''.join(md.to_lines()),
            signing_context=signing_context, email_address=email)

    def makeMergeDirective(self, source_branch=None, target_branch=None,
        source_branch_url=None, target_branch_url=None):
        """Return a bzr merge directive object.

        :param source_branch: The source database branch in the merge
            directive.
        :param target_branch: The target database branch in the merge
            directive.
        :param source_branch_url: The URL of the source for the merge
            directive.  Overrides source_branch.
        :param target_branch_url: The URL of the target for the merge
            directive.  Overrides target_branch.
        """
        from bzrlib.merge_directive import MergeDirective2
        if source_branch_url is not None:
            assert source_branch is None
        else:
            if source_branch is None:
                source_branch = self.makeAnyBranch()
            source_branch_url = (
                config.codehosting.supermirror_root +
                source_branch.unique_name)
        if target_branch_url is not None:
            assert target_branch is None
        else:
            if target_branch is None:
                target_branch = self.makeAnyBranch()
            target_branch_url = (
                config.codehosting.supermirror_root +
                target_branch.unique_name)
        return MergeDirective2(
            'revid', 'sha', 0, 0, target_branch_url,
            source_branch=source_branch_url, base_revision_id='base-revid',
            patch='')

    def makeMergeDirectiveEmail(self, body='Hi!\n', signing_context=None):
        """Create an email with a merge directive attached.

        :param body: The message body to use for the email.
        :param signing_context: A GPGSigningContext instance containing the
            gpg key to sign with.  If None, the message is unsigned.  The
            context also contains the password and gpg signing mode.
        :return: message, file_alias, source_branch, target_branch
        """
        target_branch = self.makeProductBranch()
        source_branch = self.makeProductBranch(
            product=target_branch.product)
        md = self.makeMergeDirective(source_branch, target_branch)
        message = self.makeSignedMessage(body=body,
            subject='My subject', attachment_contents=''.join(md.to_lines()),
            signing_context=signing_context)
        message_string = message.as_string()
        file_alias = getUtility(ILibraryFileAliasSet).create(
            '*', len(message_string), StringIO(message_string), '*')
        return message, file_alias, source_branch, target_branch

    def makeHWSubmission(self, date_created=None, submission_key=None,
                         emailaddress=u'test@canonical.com',
                         distroarchseries=None, private=False,
                         contactable=False, system=None,
                         submission_data=None):
        """Create a new HWSubmission."""
        if date_created is None:
            date_created = datetime.now(pytz.UTC)
        if submission_key is None:
            submission_key = self.getUniqueString('submission-key')
        if distroarchseries is None:
            distroarchseries = self.makeDistroArchSeries()
        if system is None:
            system = self.getUniqueString('system-fingerprint')
        if submission_data is None:
            sample_data_path = os.path.join(
                config.root, 'lib', 'canonical', 'launchpad', 'scripts',
                'tests', 'simple_valid_hwdb_submission.xml')
            submission_data = open(sample_data_path).read()
        filename = self.getUniqueString('submission-file')
        filesize = len(submission_data)
        raw_submission = StringIO(submission_data)
        format = HWSubmissionFormat.VERSION_1
        submission_set = getUtility(IHWSubmissionSet)

        return submission_set.createSubmission(
            date_created, format, private, contactable,
            submission_key, emailaddress, distroarchseries,
            raw_submission, filename, filesize, system)

    def makeHWSubmissionDevice(self, submission, device, driver, parent,
                               hal_device_id):
        """Create a new HWSubmissionDevice."""
        device_driver_link_set = getUtility(IHWDeviceDriverLinkSet)
        device_driver_link = device_driver_link_set.getOrCreate(
            device, driver)
        return getUtility(IHWSubmissionDeviceSet).create(
            device_driver_link, submission, parent, hal_device_id)

    def makeSSHKey(self, person=None):
        """Create a new SSHKey."""
        if person is None:
            person = self.makePerson()
        public_key = "ssh-rsa %s %s" % (
            self.getUniqueString(), self.getUniqueString())
        return getUtility(ISSHKeySet).new(person, public_key)

    def makeBlob(self, blob=None, expires=None):
        """Create a new TemporaryFileStorage BLOB."""
        if blob is None:
            blob = self.getUniqueString()
        new_uuid = getUtility(ITemporaryStorageManager).new(blob, expires)

        return getUtility(ITemporaryStorageManager).fetch(new_uuid)

    def makeLaunchpadService(self, person=None, version="devel"):
        if person is None:
            person = self.makePerson()
        from canonical.testing import BaseLayer
        launchpad = launchpadlib_for(
            "test", person, service_root=BaseLayer.appserver_root_url("api"),
            version=version)
        login_person(person)
        return launchpad

    def makePackageDiff(self, from_source=None, to_source=None,
                        requester=None, status=None, date_fulfilled=None,
                        diff_content=None, diff_filename=None):
        """Create a new `PackageDiff`."""
        if from_source is None:
            from_source = self.makeSourcePackageRelease()
        if to_source is None:
            to_source = self.makeSourcePackageRelease()
        if requester is None:
            requester = self.makePerson()
        if status is None:
            status = PackageDiffStatus.COMPLETED
        if date_fulfilled is None:
            date_fulfilled = UTC_NOW
        if diff_content is None:
            diff_content = self.getUniqueString("packagediff")
        lfa = self.makeLibraryFileAlias(
            filename=diff_filename, content=diff_content)
        return ProxyFactory(
            PackageDiff(
                requester=requester, from_source=from_source,
                to_source=to_source, date_fulfilled=date_fulfilled,
                status=status, diff_content=lfa))

    # Factory methods for OAuth tokens.
    def makeOAuthConsumer(self, key=None, secret=None):
        if key is None:
            key = self.getUniqueString("oauthconsumerkey")
        if secret is None:
            secret = ''
        return getUtility(IOAuthConsumerSet).new(key, secret)

    def makeOAuthRequestToken(self, consumer=None, date_created=None,
                              reviewed_by=None,
                              access_level=OAuthPermission.READ_PUBLIC):
        """Create a (possibly reviewed) OAuth request token."""
        if consumer is None:
            consumer = self.makeOAuthConsumer()
        token = consumer.newRequestToken()

        if reviewed_by is not None:
            # Review the token before modifying the date_created,
            # since the date_created can be used to simulate an
            # expired token.
            token.review(reviewed_by, access_level)

        if date_created is not None:
            unwrapped_token = removeSecurityProxy(token)
            unwrapped_token.date_created = date_created
        return token

    def makeOAuthAccessToken(self, consumer=None, owner=None,
                             access_level=OAuthPermission.READ_PUBLIC):
        """Create an OAuth access token."""
        if owner is None:
            owner = self.makePerson()
        request_token = self.makeOAuthRequestToken(
            consumer, reviewed_by=owner, access_level=access_level)
        return request_token.createAccessToken()

    def makeCVE(self, sequence, description=None,
                cvestate=CveStatus.CANDIDATE):
        """Create a new CVE record."""
        if description is None:
            description = self.getUniqueString()
        return getUtility(ICveSet).new(sequence, description, cvestate)

    def makePublisherConfig(self, distribution=None, root_dir=None,
                            base_url=None, copy_base_url=None):
        """Create a new `PublisherConfig` record."""
        if distribution is None:
            distribution = self.makeDistribution()
        if root_dir is None:
            root_dir = self.getUniqueUnicode()
        if base_url is None:
            base_url = self.getUniqueUnicode()
        if copy_base_url is None:
            copy_base_url = self.getUniqueUnicode()
        return getUtility(IPublisherConfigSet).new(
            distribution, root_dir, base_url, copy_base_url)

    def makePlainPackageCopyJob(
        self, package_name=None, package_version=None, source_archive=None,
        target_archive=None, target_distroseries=None, target_pocket=None):
        """Create a new `PlainPackageCopyJob`."""
        if package_name is None and package_version is None:
            package_name = self.makeSourcePackageName().name
            package_version = unicode(self.getUniqueInteger()) + 'version'
        if source_archive is None:
            source_archive = self.makeArchive()
        if target_archive is None:
            target_archive = self.makeArchive()
        if target_distroseries is None:
            target_distroseries = self.makeDistroSeries()
        if target_pocket is None:
            target_pocket = self.getAnyPocket()
        return getUtility(IPlainPackageCopyJobSource).create(
            package_name, source_archive, target_archive,
            target_distroseries, target_pocket,
            package_version=package_version)


# Some factory methods return simple Python types. We don't add
# security wrappers for them, as well as for objects created by
# other Python libraries.
unwrapped_types = frozenset((
        BaseRecipeBranch,
        DSCFile,
        InstanceType,
        MergeDirective2,
        Message,
        datetime,
        int,
        str,
        unicode,
        ))


def is_security_proxied_or_harmless(obj):
    """Check that the object is security wrapped or a harmless object."""
    if obj is None:
        return True
    if builtin_isinstance(obj, Proxy):
        return True
    if type(obj) in unwrapped_types:
        return True
    if isSequenceType(obj) or isinstance(obj, (set, frozenset)):
        return all(
            is_security_proxied_or_harmless(element)
            for element in obj)
    if isMappingType(obj):
        return all(
            (is_security_proxied_or_harmless(key) and
             is_security_proxied_or_harmless(obj[key]))
            for key in obj)
    return False


class UnproxiedFactoryMethodWarning(UserWarning):
    """Raised when someone calls an unproxied factory method."""

    def __init__(self, method_name):
        super(UnproxiedFactoryMethodWarning, self).__init__(
            "PLEASE FIX: LaunchpadObjectFactory.%s returns an "
            "unproxied object." % (method_name, ))


class ShouldThisBeUsingRemoveSecurityProxy(UserWarning):
    """Raised when there is a potentially bad call to removeSecurityProxy."""

    def __init__(self, obj):
        message = (
            "removeSecurityProxy(%r) called. Is this correct? "
            "Either call it directly or fix the test." % obj)
        super(ShouldThisBeUsingRemoveSecurityProxy, self).__init__(message)


class LaunchpadObjectFactory:
    """A wrapper around `BareLaunchpadObjectFactory`.

    Ensure that each object created by a `BareLaunchpadObjectFactory` method
    is either of a simple Python type or is security proxied.

    A warning message is printed to stderr if a factory method creates
    an object without a security proxy.

    Whereever you see such a warning: fix it!
    """

    def __init__(self):
        self._factory = BareLaunchpadObjectFactory()

    def __getattr__(self, name):
        attr = getattr(self._factory, name)
        if os.environ.get('LP_PROXY_WARNINGS') == '1' and callable(attr):

            def guarded_method(*args, **kw):
                result = attr(*args, **kw)
                if not is_security_proxied_or_harmless(result):
                    warnings.warn(
                        UnproxiedFactoryMethodWarning(name), stacklevel=1)
                return result
            return guarded_method
        else:
            return attr

    def __dir__(self):
        """Enumerate the attributes and methods of the wrapped object factory.

        This is especially useful for interactive users."""
        return dir(self._factory)


def remove_security_proxy_and_shout_at_engineer(obj):
    """Remove an object's security proxy and print a warning.

    A number of LaunchpadObjectFactory methods returned objects without
    a security proxy. This is now no longer possible, but a number of
    tests rely on unrestricted access to object attributes.

    This function should only be used in legacy tests which fail because
    they expect unproxied objects.
    """
    if os.environ.get('LP_PROXY_WARNINGS') == '1':
        warnings.warn(ShouldThisBeUsingRemoveSecurityProxy(obj), stacklevel=2)
    return removeSecurityProxy(obj)