/usr/lib/python2.7/dist-packages/pybitweb/db.py is in pybit-web 1.0.0-2.1.
This file is owned by root:root, with mode 0o664.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 | # pybit-web
# Copyright 2012:
#
# Nick Davidson <nicholas.davidson@gmail.com>,
# Simon Haswell <maxcady78@hotmail.co.uk>,
# Neil Williams <codehelp@debian.org>,
# James Bennet <github@james-bennet.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import psycopg2.extras
import cgi
import math
import re
import logging
from pybit.models import Arch,Dist,Format,Status,Suite,BuildD,Job,Package,PackageInstance,SuiteArch,JobHistory, ClientMessage, checkValue, Transport,\
BuildEnv, BuildEnvSuiteArch,Blacklist
def remove_nasties(nastystring):
try:
if isinstance(nastystring, basestring):
escaped_string = cgi.escape(nastystring,True) # Escapes <, > , &, and "
#print "Escaped the string " + nastystring + " to " + escaped_string
return escaped_string
else:
#print "Not escaping: " + str(nastystring) + " as it is not a string."
return nastystring;
except Exception as e:
raise Exception("Error escaping string: " + str(nastystring) + str(e))
return None
class Database(object):
conn = None
# CONSTANTs
limit_low = 5.0
limit_high = 10.0;
#<<<<<<<< General database functions >>>>>>>>
#Constructor, connects on initialisation.
def __init__(self, settings):
self.settings = settings
self.log = logging.getLogger("db" )
if (('debug' in self.settings) and ( self.settings['debug'])) :
self.log.setLevel( logging.DEBUG )
self.log.debug("DB constructor called.")
self.connect()
#Deconstructor, disconnects on disposal.
def __del__(self):
self.disconnect()
#Connects to DB using settings loaded from file.
def connect(self):
# for catbells
if (checkValue('password',self.settings)):
if (checkValue('hostname',self.settings) and checkValue('port',self.settings)):
# remote with password
self.log.debug("REMOTE WITH PASSWORD")
self.conn = psycopg2.connect(database=self.settings['databasename'],
user=self.settings['user'], host=self.settings['hostname'],
port=self.settings['port'], password=self.settings['password'])
else:
# local with password
self.log.debug("LOCAL WITH PASSWORD")
self.conn = psycopg2.connect(database=self.settings['databasename'],
user=self.settings['user'], password=self.settings['password'])
else:
if (checkValue('hostname',self.settings) and checkValue('port',self.settings)):
# remote without password
self.log.debug("REMOTE WITHOUT PASSWORD")
self.conn = psycopg2.connect(database=self.settings['databasename'],user=self.settings['user'], host=self.settings['hostname'],port=self.settings['port'])
else:
# local without password
self.log.debug("LOCAL WITHOUT PASSWORD")
self.conn = psycopg2.connect(database=self.settings['databasename'],
user=self.settings['user'])
#Called by deconstructor
def disconnect(self):
try:
if self.conn:
self.conn.commit()
self.conn.close()
return True
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error disconnecting from database. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return False
#<<<<<<<< NEW CODE >>>>>>>>
def log_buildRequest(self,build_request_obj):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into buildrequest(job,method,uri,vcs_id) VALUES (%s,%s,%s,%s) RETURNING id",(remove_nasties(build_request_obj.job.id),remove_nasties(build_request_obj.transport.method),remove_nasties(build_request_obj.transport.uri),remove_nasties(build_request_obj.transport.vcs_id)))
res = cur.fetchall()
self.conn.commit()
new_id = res[0]['id']
cur.close()
return new_id
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error logging build. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_jobTransportDetails(self, jobid):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT * FROM buildrequest WHERE job = %s",(remove_nasties(jobid),))
res = cur.fetchall()
self.conn.commit()
# Get transport details so we can check out the same source to retry a job.
transport = Transport(None,res[0]['method'],res[0]['uri'],res[0]['vcs_id'])
cur.close()
return transport
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error logging build. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
#<<<<<<<< Lookup table queries >>>>>>>>
# Do we care about update or delete?
def count_arches(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM arch AS num_arches")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving arches count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_arches(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,name FROM arch ORDER BY name LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,name FROM arch ORDER BY name")
res = cur.fetchall()
self.conn.commit()
arches = []
for i in res:
arches.append(Arch(i['id'],i['name']))
cur.close()
return arches
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving arches list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_arch_id(self,arch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM arch WHERE id=%s",(arch_id,))
res = cur.fetchall()
self.conn.commit()
arch = Arch(res[0]['id'],res[0]['name'])
cur.close()
return arch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving arch with id:" + str(arch_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_arch_byname(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM arch WHERE name=%s",(name,))
res = cur.fetchall()
self.conn.commit()
arches = []
for i in res:
arches.append(Arch(i['id'],i['name']))
cur.close()
return arches
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving arch by name:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_arch(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into arch(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
arch = Arch(res[0]['id'],name)
cur.close()
return arch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding arch:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_arch(self,arch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM arch WHERE id=%s RETURNING id",(arch_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == arch_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting arch with id: %s. Database error code: %s - Details: %s.",str(arch_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_suitearches(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM suitearches AS num_suitearches")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suitearches count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_suitearches(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,suite_id,arch_id,master_weight FROM suitearches ORDER BY master_weight DESC")
res = cur.fetchall()
self.conn.commit()
suite_arches = []
for i in res:
suite_arches.append(SuiteArch(i['id'],self.get_suite_id(i['suite_id']),self.get_arch_id(i['arch_id']),i['master_weight']))
cur.close()
return suite_arches
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suite arches list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_suitearch_id(self,suitearch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,suite_id,arch_id,master_weight FROM suitearches WHERE id=%s",(suitearch_id,))
res = cur.fetchall()
self.conn.commit()
suitearch = SuiteArch(res[0]['id'],self.get_suite_id(res[0]['suite_id']),self.get_arch_id(res[0]['arch_id']),res[0]['master_weight'])
cur.close()
return suitearch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suite arch with id:" + str(suitearch_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_suitearch_by_suite_name(self,suite,arch):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,suite_id,arch_id,master_weight FROM suitearches WHERE suite.id=%s, arch.id=%s",(suite.id,arch.id))
res = cur.fetchall()
self.conn.commit()
suitearch = SuiteArch(res[0]['id'],self.get_suite_id(res[0]['suite_id']),self.get_arch_id(res[0]['arch_id']),res[0]['master_weight'])
cur.close()
return suitearch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suite arch with suite and arch:. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_suitearch(self,suite_id,arch_id,master_weight = 0):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into suitearches(suite_id,arch_id,master_weight) VALUES (%s, %s, %s) RETURNING id",(remove_nasties(suite_id),remove_nasties(arch_id),remove_nasties(master_weight)))
res = cur.fetchall()
self.conn.commit()
suitearch = SuiteArch(res[0]['id'],self.get_suite_id(suite_id),self.get_arch_id(arch_id),master_weight)
cur.close()
return suitearch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding suite arch:" + suite_id + arch_id + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_suitearch(self,suitearch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM suitearches WHERE id=%s RETURNING id",(suitearch_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == suitearch_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting suitearch with id: %s. Database error code: %s - Details: %s.",str(suitearch_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_buildenv_suitearches(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM buildenvsuitearch AS num_buildenv_suitearches")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv suitearch count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_buildenv_suitearches(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,buildenv_id,suitearch_id FROM buildenvsuitearch")
res = cur.fetchall()
self.conn.commit()
buildenv_suitearches = []
for i in res:
buildenv_suitearches.append(BuildEnvSuiteArch(i['id'],self.get_build_env_id(i['buildenv_id']),self.get_suitearch_id(i['suitearch_id'])))
cur.close()
return buildenv_suitearches
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv suitearch list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_buildenv_suitearch_id(self,buildenv_suitearch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,buildenv_id,suitearch_id FROM buildenvsuitearch WHERE id=%s",(buildenv_suitearch_id,))
res = cur.fetchall()
self.conn.commit()
buildenv_suitearch = BuildEnvSuiteArch(res[0]['id'],self.get_build_env_id(res[0]['buildenv_id']),self.get_suitearch_id(res[0]['suitearch_id']))
cur.close()
return buildenv_suitearch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv suitearch with id:" + str(buildenv_suitearch_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_buildenv_suitearch(self,buildenv_id,suitearch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into buildenvsuitearch(buildenv_id,suitearch_id) VALUES (%s, %s) RETURNING id",(remove_nasties(buildenv_id),remove_nasties(suitearch_id)))
res = cur.fetchall()
self.conn.commit()
buildenv_suitearch = BuildEnvSuiteArch(res[0]['id'],self.get_build_env_id(buildenv_id),self.get_suitearch_id(suitearch_id))
cur.close()
return buildenv_suitearch
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding buildenv suitearch:" + buildenv_id + suitearch_id + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_buildenv_suitearch(self,buildenv_suitearch_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM buildenvsuitearch WHERE id=%s RETURNING id",(buildenv_suitearch_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == buildenv_suitearch_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting buildenvsuitearch with id: %s. Database error code: %s - Details: %s.",str(buildenv_suitearch_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_dists(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM distribution AS num_dists")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving distributions count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_dists(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,name FROM distribution ORDER BY name LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,name FROM distribution ORDER BY name")
res = cur.fetchall()
self.conn.commit()
dists = []
for i in res:
dists.append(Dist(i['id'],i['name']))
cur.close()
return dists
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving dist list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_dist_id(self,dist_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM distribution WHERE id=%s",(dist_id,))
res = cur.fetchall()
self.conn.commit()
dist = Dist(res[0]['id'],res[0]['name'])
cur.close()
return dist
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving dist with id:" + str(dist_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_dist_byname(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM distribution WHERE name=%s",(name,))
res = cur.fetchall()
self.conn.commit()
dists = []
for i in res:
dists.append(Dist(i['id'],i['name']))
cur.close()
return dists
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving dist by name:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_dist(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into distribution(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
dist = Dist(res[0]['id'],name)
cur.close()
return dist
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding dist:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_dist(self,dist_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM distribution WHERE id=%s RETURNING id",(dist_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == dist_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting dist with id: %s. Database error code: %s - Details: %s.",str(dist_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_formats(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM format AS num_formats")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving formats count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_formats(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,name FROM format ORDER BY name LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,name FROM format ORDER BY name")
res = cur.fetchall()
self.conn.commit()
formats = []
for i in res:
formats.append(Format(i['id'],i['name']))
cur.close()
return formats
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving formats list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_format_id(self,format_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM format WHERE id=%s",(format_id,))
res = cur.fetchall()
self.conn.commit()
ret_format = Format(res[0]['id'],res[0]['name'])
cur.close()
return ret_format
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving format with id:" + str(format_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_format_byname(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM format WHERE name=%s",(name,))
res = cur.fetchall()
formats = []
for i in res:
formats.append(Format(i['id'],i['name']))
cur.close()
return formats
except psycopg2.Error as e:
raise Exception("Error retrieving format by name:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_format(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into format(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
ret_format = Format(res[0]['id'],name)
cur.close()
return ret_format
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding format:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_format(self,format_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM format WHERE id=%s RETURNING id",(format_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == format_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting format with id: %s. Database error code: %s - Details: %s.",str(format_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_statuses(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM status AS num_statuses")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages); # ALWAYS round up.
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving statuses count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_statuses(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,name FROM status ORDER BY name LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,name FROM status ORDER BY name")
res = cur.fetchall()
self.conn.commit()
statuses = []
for i in res:
statuses.append(Status(i['id'],i['name']))
cur.close()
return statuses
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving status list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_status_id(self,status_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM status WHERE id=%s",(status_id,))
res = cur.fetchall()
self.conn.commit()
status = Status(res[0]['id'],res[0]['name'])
cur.close()
return status
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving status with id:" + str(status_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_status(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into status(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
status = Status(res[0]['id'],name)
cur.close()
return status
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error add status:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_status(self,status_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM status WHERE id=%s RETURNING id",(status_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == status_id:
cur.close()
return True
else:
cur.cloes()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting status with id: %s. Database error code: %s - Details: %s.",str(status_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_suites(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM suite AS num_suites")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suites count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_suites(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,name FROM suite ORDER BY name LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,name FROM suite ORDER BY name")
res = cur.fetchall()
self.conn.commit()
suites = []
for i in res:
suites.append(Suite(i['id'],i['name']))
cur.close()
return suites
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suite list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_suite_id(self,suite_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM suite WHERE id=%s",(suite_id,))
res = cur.fetchall()
self.conn.commit()
suite = Suite(res[0]['id'],res[0]['name'])
cur.close()
return suite
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suite with id:" + str(suite_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_suite_byname(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM suite WHERE name=%s",(name,))
res = cur.fetchall()
self.conn.commit()
suites = []
for i in res:
suites.append(Suite(i['id'],i['name']))
cur.close()
return suites
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving suite with name:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_suite(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into suite(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
suite = Suite(res[0]['id'],name)
cur.close()
return suite
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding suite:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_suite(self,suite_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM suite WHERE id=%s RETURNING id",(suite_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == suite_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting suite with id: %s. Database error code: %s - Details: %s.",str(suite_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def count_build_envs(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM buildenv AS num_build_envs")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_build_envs(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,name FROM buildenv ORDER BY name LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,name FROM buildenv ORDER BY name")
res = cur.fetchall()
self.conn.commit()
build_envs = []
for i in res:
build_envs.append(BuildEnv(i['id'],i['name']))
cur.close()
return build_envs
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_build_env_id(self,build_env_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM buildenv WHERE id=%s",(build_env_id,))
res = cur.fetchall()
self.conn.commit()
if res:
build_env = BuildEnv(res[0]['id'],res[0]['name'])
else:
build_env = None
cur.close()
return build_env
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv with id:" + str(build_env_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_build_env_byname(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM buildenv WHERE name=%s",(name,))
res = cur.fetchall()
self.conn.commit()
build_envs = []
for i in res:
build_envs.append(BuildEnv(i['id'],i['name']))
cur.close()
return build_envs
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildenv with name:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_build_env(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT INTO buildenv(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
build_env = BuildEnv(res[0]['id'],name)
cur.close()
return build_env
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding build_env:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_build_env(self,build_env_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM buildenv WHERE id=%s RETURNING id",(build_env_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == build_env_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting buildenv with id: %s. Database error code: %s - Details: %s.",str(build_env_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
#<<<<<<<< BuildD related database functions >>>>>>>>
def count_buildclients(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM buildclients AS num_buildclients")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_high;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildclients count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_buildclients(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
# CONSTANT
offset = (page -1) * self.limit_high;
cur.execute("SELECT id,name FROM buildclients ORDER BY name LIMIT %s OFFSET %s", (self.limit_high,offset,))
else:
cur.execute("SELECT id,name FROM buildclients ORDER BY name")
res = cur.fetchall()
self.conn.commit()
build_clients = []
for i in res:
build_clients.append(BuildD(i['id'],i['name']))
cur.close()
return build_clients
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildd list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_buildd_id(self,buildd_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name FROM buildclients WHERE id=%s",(buildd_id,))
res = cur.fetchall()
self.conn.commit()
if (res):
buildd = BuildD(res[0]['id'],res[0]['name'])
cur.close()
return buildd
else:
return None
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving buildd with id:" + str(buildd_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_buildclient(self,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into buildclients(name) VALUES (%s) RETURNING id",(remove_nasties(name),))
res = cur.fetchall()
self.conn.commit()
buildd = BuildD(res[0]['id'],name)
cur.close()
return buildd
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding buildd:" + name + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_buildclient(self,buildclient_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM buildclients WHERE id=%s RETURNING id",(buildclient_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == buildclient_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting buildd with id: %s. Database error code: %s - Details: %s.",str(buildclient_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def get_buildd_jobs(self,buildclient_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT job.id AS job_id,packageinstance_id,buildclients.id AS buildclients_id FROM buildclients,job WHERE buildclients.id=%s AND buildclients.id = job.buildclient_id ORDER BY job.id",(buildclient_id,))
res = cur.fetchall()
self.conn.commit()
jobs = []
for i in res:
packageinstance = self.get_packageinstance_id(i['packageinstance_id'])
jobs.append(Job(i['job_id'],packageinstance,buildclient_id))
cur.close()
return jobs
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving jobs on buildd with id:" + str(buildclient_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
#<<<<<<<< Job related database functions >>>>>>>>
# UPDATE queries?
def get_job(self,job_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,packageinstance_id,buildclient_id FROM job WHERE id=%s",(job_id,))
res = cur.fetchall()
self.conn.commit()
packageinstance = self.get_packageinstance_id(res[0]['packageinstance_id'])
buildclient = self.get_buildd_id(res[0]['buildclient_id']) if res[0]['buildclient_id'] else None
job = Job(res[0]['id'],packageinstance,buildclient)
cur.close()
return job
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving job with id:" + str(job_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_jobs(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
# CONSTANT
offset = (page -1) * self.limit_high;
cur.execute("SELECT id,packageinstance_id,buildclient_id FROM job ORDER BY id LIMIT %s OFFSET %s", (self.limit_high,offset,))
else:
cur.execute("SELECT id,packageinstance_id,buildclient_id FROM job ORDER BY id")
res = cur.fetchall()
self.conn.commit()
jobs = []
for i in res:
packageinstance = self.get_packageinstance_id(i['packageinstance_id'])
buildclient = self.get_buildd_id(i['buildclient_id']) if i['buildclient_id'] else None
jobs.append(Job(i['id'],packageinstance,buildclient))
cur.close()
return jobs
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving jobs list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_jobs_by_status(self,status):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("WITH latest_status AS (SELECT DISTINCT ON (job_id) job_id, status.name FROM jobstatus LEFT JOIN status ON status_id=status.id ORDER BY job_id, time DESC) SELECT job_id, name FROM latest_status WHERE name=%s",(status,));
res = cur.fetchall()
self.conn.commit()
jobs = []
for i in res:
jobs.append(self.get_job(i['job_id']))
cur.close()
return jobs
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving jobs list with status:" + status + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_unfinished_jobs(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("WITH latest_status AS (SELECT DISTINCT ON (job_id) job_id, status.name FROM jobstatus LEFT JOIN status ON status_id=status.id ORDER BY job_id, time DESC) SELECT job_id, name FROM latest_status WHERE name!='Uploaded' AND name!='Done' AND name!='Cancelled' ORDER BY job_id");
res = cur.fetchall()
self.conn.commit()
jobs = []
for i in res:
jobs.append(self.get_job(i['job_id']))
cur.close()
return jobs
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving unfinished jobs. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_job_statuses(self,job_id):
#gets job status *history*
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT job.id AS job_id, status.name AS status, buildclients.name AS buildclient, jobstatus.time AS time FROM job LEFT JOIN jobstatus ON job.id=jobstatus.job_id LEFT JOIN status ON jobstatus.status_id=status.id LEFT JOIN buildclients ON buildclients.id=job.buildclient_id WHERE job.id = %s ORDER BY time",(job_id,));
res = cur.fetchall()
self.conn.commit()
jobstatuses = []
for i in res:
jobstatuses.append(JobHistory(i['job_id'],i['status'],i['buildclient'],i['time']))
cur.close()
return jobstatuses
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving job status with:" + str(job_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_job_status(self, jobid, status, client=None):
try:
self.log.debug("put_job_status: %s %s %s", jobid, status, client)
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT INTO jobstatus (job_id, status_id) VALUES (%s, (SELECT id FROM status WHERE name=%s))",
(remove_nasties(jobid),remove_nasties(status),))
if client is not None and client != "":
#insert the client if it doesn't already exist.
cur.execute("INSERT INTO buildclients(name) SELECT name FROM buildclients UNION VALUES(%s) EXCEPT SELECT name FROM buildclients",
(remove_nasties(client),))
cur.execute("UPDATE job SET buildclient_id=(SELECT id FROM buildclients WHERE name=%s) WHERE id=%s",
(remove_nasties(client),remove_nasties(jobid)))
self.conn.commit()
cur.close()
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error setting job status. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_job(self,job_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("WITH latest_status AS (SELECT DISTINCT ON (job_id) job_id, status.name FROM jobstatus LEFT JOIN status ON status_id=status.id WHERE job_id=%s ORDER BY job_id, time DESC) SELECT job_id, name FROM latest_status WHERE name!='Building'",(job_id,))
res = cur.fetchall()
self.conn.commit()
if len(res) > 0:
cur.execute("DELETE FROM jobstatus WHERE job_id=%s RETURNING id",(job_id,))
self.conn.commit()
cur.execute("DELETE FROM job WHERE id=%s RETURNING id",(job_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == job_id:
cur.close()
return True
else:
cur.close()
return False
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting job with id: %s. Database error code: %s - Details: %s.",str(job_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def put_job(self,packageinstance,buildclient):
try:
if buildclient:
buildclient_id = remove_nasties(buildclient.id)
else:
buildclient_id = None
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT INTO job (packageinstance_id,buildclient_id) VALUES (%s, %s) RETURNING id",(remove_nasties(packageinstance.id),(buildclient_id)))
res = cur.fetchall()
job_id = res[0]['id']
if job_id is not None:
cur.execute("INSERT INTO jobstatus (job_id, status_id) VALUES (%s, (SELECT id FROM status WHERE status.name=%s))",
(remove_nasties(job_id), remove_nasties(ClientMessage.waiting)))
self.conn.commit()
else:
self.conn.rollback()
job = Job(job_id,packageinstance,buildclient)
cur.close()
return job
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding job. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
#<<<<<<<< Package related database functions >>>>>>>>
# UPDATE queries?
def count_packages(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM package AS num_packages")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_high;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving packages count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_packages(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
# CONSTANT
offset = (page -1) * self.limit_high;
cur.execute("SELECT id,version,name FROM package ORDER BY name,id LIMIT %s OFFSET %s", (self.limit_high,offset,))
else:
cur.execute("SELECT id,version,name FROM package ORDER BY name,id")
res = cur.fetchall()
self.conn.commit()
packages = []
for i in res:
packages.append(Package(i['id'],i['version'],i['name']))
cur.close()
return packages
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving packages list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_packagenames(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT DISTINCT (name), name FROM package GROUP BY name ORDER BY name") # We only care about a unique list of names
res = cur.fetchall()
self.conn.commit()
packages = []
for i in res:
packages.append(Package(None,None,i['name'])) # TODO: these may actually be useful to have still.
cur.close()
return packages
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving packages list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_packages_byname(self, name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,version,name FROM package WHERE name=%s",(name,))
res = cur.fetchall()
self.conn.commit()
packages = []
for i in res:
packages.append(Package(i['id'],i['version'],i['name']))
cur.close()
return packages
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package with name:" + str(name) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_package_id(self,package_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,version,name FROM package WHERE id=%s",(package_id,))
res = cur.fetchall()
self.conn.commit()
package = Package(res[0]['id'],res[0]['version'],res[0]['name'])
cur.close()
return package
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package with id:" + str(package_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_package_byvalues(self,name,version):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,name,version FROM package WHERE name=%s AND version=%s",(name,version))
res = cur.fetchall()
self.conn.commit()
packages = []
for i in res:
packages.append(Package(i['id'],i['version'],i['name']))
cur.close()
return packages
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package by values:" + name + version + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_package(self,version,name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into package(version,name) VALUES (%s, %s) RETURNING id",(remove_nasties(version),remove_nasties(name)))
res = cur.fetchall()
self.conn.commit()
package = Package(res[0]['id'],version,name)
cur.close()
return package
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding package:" + name + version + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_package(self,package_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM package WHERE id=%s RETURNING id",(package_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == package_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting package with id: %s. Database error code: %s - Details: %s.",str(package_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
#<<<<<<<<< Packageinstance related Queries >>>>>>>
def get_packageinstance_id(self,packageinstance_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master FROM packageinstance WHERE id=%s",(packageinstance_id,))
res = cur.fetchall()
self.conn.commit()
package = self.get_package_id(res[0]['package_id'])
build_env = self.get_build_env_id(res[0]['buildenv_id'])
arch = self.get_arch_id(res[0]['arch_id'])
suite = self.get_suite_id(res[0]['suite_id'])
dist = self.get_dist_id(res[0]['dist_id'])
pkg_format = self.get_format_id(res[0]['format_id'])
p_i = PackageInstance(res[0]['id'],package,arch,build_env,suite,dist,pkg_format,res[0]['master'])
cur.close()
return p_i
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package instance with:" + str(packageinstance_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def count_packageinstances(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM packageinstance AS num_packageinstances")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_high;
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving packageinstances count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_packageinstances(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
# CONSTANT
offset = (page -1) * self.limit_high;
cur.execute("SELECT id,package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master FROM packageinstance ORDER BY id LIMIT %s OFFSET %s", (self.limit_high,offset,))
else:
cur.execute("SELECT id,package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master FROM packageinstance ORDER BY id")
res = cur.fetchall()
self.conn.commit()
packageinstances = []
for i in res:
packageinstances.append(self.get_packageinstance_id(i['id']))
cur.close()
return packageinstances
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package instances list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_packageinstances_byname(self, name):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT packageinstance.id AS id,package.id AS package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master FROM packageinstance,package WHERE packageinstance.package_id = package.id AND name = %s ORDER BY package_id, id",(name,))
res = cur.fetchall()
self.conn.commit()
packageinstances = []
for i in res:
packageinstances.append(self.get_packageinstance_id(i['id']))
cur.close()
return packageinstances
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package instances by name. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_packageinstance_byvalues(self,package,build_env,arch,suite,dist,pkg_format):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if build_env:
cur.execute("SELECT id,package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master FROM packageinstance WHERE package_id=%s AND buildenv_id=%s AND arch_id=%s AND suite_id=%s AND dist_id=%s AND format_id=%s",(package.id,build_env.id,arch.id,suite.id,dist.id,pkg_format.id))
else:
cur.execute("SELECT id,package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master FROM packageinstance WHERE package_id=%s AND buildenv_id IS NULL AND arch_id=%s AND suite_id=%s AND dist_id=%s AND format_id=%s",(package.id,arch.id,suite.id,dist.id,pkg_format.id))
res = cur.fetchall()
self.conn.commit()
packageinstances = []
for i in res:
packageinstances.append(self.get_packageinstance_id(i['id']))
cur.close()
return packageinstances
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package instance by value. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_packageinstance(self,package,build_env,arch,suite,dist,pkg_format,master):
try:
# The buildenv_id field in the DB is allowed to be null.
# We may be passed a None build_env object and must handle this.
if build_env:
build_env_id = build_env.id
else:
build_env_id = None;
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into packageinstance(package_id,buildenv_id,arch_id,suite_id,dist_id,format_id,master) VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING id",(remove_nasties(package.id),remove_nasties(build_env_id),remove_nasties(arch.id),remove_nasties(suite.id),remove_nasties(dist.id),remove_nasties(pkg_format.id),remove_nasties(master)))
self.conn.commit()
res = cur.fetchall()
self.conn.commit()
p_i = PackageInstance(res[0]['id'],package,arch,build_env,suite,dist,pkg_format,master)
cur.close()
return p_i
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding package instance:" + str(package.id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def update_packageinstance_masterflag(self,packageinstance_id,master):
try:
if master == 1:
master = True
elif master == 0:
master = False
else:
return None;
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("UPDATE packageinstance SET master=%s WHERE id=%s",(remove_nasties(master),remove_nasties(packageinstance_id)))
self.conn.commit()
self.conn.commit()
cur.close()
return
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error updating package instance master flag:" + str(packageinstance_id) + " to " + str(master) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_packageinstance(self,packageinstance_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM packageinstance WHERE id=%s RETURNING id",(packageinstance_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == packageinstance_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting package instance with id: %s. Database error code: %s - Details: %s.",str(packageinstance_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
def check_specific_packageinstance_exists(self,build_env,arch,package,distribution,pkg_format,suite):
try:
if arch and distribution and pkg_format and package and suite:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if build_env:
cur.execute("SELECT id FROM packageinstance WHERE buildenv_id=%s AND arch_id=%s AND dist_id=%s AND format_id=%s AND package_id=%s AND suite_id=%s",(build_env.id,arch.id,distribution.id,pkg_format.id,package.id,suite.id))
else:
cur.execute("SELECT id FROM packageinstance WHERE buildenv_id IS NULL AND arch_id=%s AND dist_id=%s AND format_id=%s AND package_id=%s AND suite_id=%s",(arch.id,distribution.id,pkg_format.id,package.id,suite.id))
res = cur.fetchall()
self.conn.commit()
if len(res) > 0:
#Found specific package instance
cur.close()
return True
else:
# doesnt exist
#Cannot find specific package instance
cur.close()
return False
else:
#Error finding specific package instance
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error checking package instance exists. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
#<<<<<<<<< Report Queries >>>>>>>
def check_package_has_unfinished_jobs(self, package_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("WITH latest_status AS (SELECT DISTINCT ON (job_id) job_id, status.name FROM jobstatus LEFT JOIN status ON status_id=status.id ORDER BY job_id, time DESC) SELECT job_id, name, package_id FROM latest_status LEFT JOIN job ON latest_status.job_id=job.id LEFT JOIN packageinstance ON packageinstance_id=packageinstance.id WHERE package_id=%s AND name NOT IN ('Done', 'Uploaded', 'Cancelled')",(package_id,));
res = cur.fetchall()
self.conn.commit()
if res and len(res) > 0:
return True
else:
return False
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error check package has unfinished jobs. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_report_package_instance(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT packageinstance.id, suite.name AS suite, package.name AS package, package.version AS version, arch.name AS arch, packageinstance.buildenv_id AS buildenv_id, format.name AS format, distribution.name AS dist, packageinstance.master AS master FROM packageinstance LEFT JOIN arch ON arch.id=arch_id LEFT JOIN suite ON suite.id=suite_id LEFT JOIN distribution ON distribution.id=dist_id LEFT JOIN package ON package_id=package.id LEFT JOIN format ON format_id=format.id")
res = cur.fetchall()
self.conn.commit()
package_instances = []
for i in res :
package_instances.append(PackageInstance(i['id'], i['package'], i['arch'], i['buildenv_id'], i['suite'], i['dist'], i['format'], i['master']))
cur.close()
return package_instances
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving package instance list. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_supported_architectures(self,suite) :
try:
if suite :
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT arch.id, arch.name, suitearches.master_weight FROM suite LEFT JOIN suitearches ON suite.id=suite_id LEFT JOIN arch ON arch_id = arch.id WHERE suite.name=%s ORDER BY master_weight DESC, random()",[suite])
res = cur.fetchall()
self.conn.commit()
arch_list = []
for i in res :
arch_list.append(i['name'])
cur.close()
return arch_list
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving supported architectures for:" + suite + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_supported_build_environments(self,suite) :
try:
if suite :
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT DISTINCT ON (buildenv.id) buildenv.id AS buildenv_id FROM suitearches LEFT JOIN buildenvsuitearch ON suitearches.id=suitearch_id LEFT JOIN buildenv ON buildenvsuitearch.buildenv_id=buildenv.id WHERE suitearches.suite_id=(SELECT id FROM suite WHERE name=%s)",(remove_nasties(suite),))
res = cur.fetchall()
self.conn.commit()
env_list = []
for i in res :
build_env = self.get_build_env_id(i['buildenv_id'])
#self.log.debug("SUITE (%s) HAS SUPPORTED BUILD ENV:%s",suite,build_env.name)
env_list.append(build_env)
cur.close()
return env_list
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving supported build environments for:" + suite + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_supported_build_env_suite_arches(self,suite) :
try:
if suite :
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT buildenv.id AS buildenv_id, suitearches.id AS suitearch_id, suitearches.master_weight AS suitearch_master_weight, buildenvsuitearch.id AS buildenvsuitearch_id FROM suitearches LEFT JOIN buildenvsuitearch ON suitearches.id=suitearch_id LEFT JOIN buildenv ON buildenvsuitearch.buildenv_id=buildenv.id WHERE suitearches.suite_id=(SELECT id FROM suite WHERE name=%s) ORDER BY buildenv_id, suitearch_master_weight DESC",(remove_nasties(suite),))
res = cur.fetchall()
self.conn.commit()
build_env_suite_arch_list = []
for i in res :
suitearch = self.get_suitearch_id(i['suitearch_id'])
build_env = self.get_build_env_id(i['buildenv_id'])
if not build_env :
buildenvsuitearch = BuildEnvSuiteArch(i['buildenvsuitearch_id'],None,suitearch)
else :
buildenvsuitearch = BuildEnvSuiteArch(i['buildenvsuitearch_id'],build_env,suitearch)
build_env_suite_arch_list.append(buildenvsuitearch)
cur.close()
return build_env_suite_arch_list
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving supported build environments for:" + suite + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
# Note: True = failed, false = Ok. Should probably be renamed isInBlacklist() or similar.
def check_blacklist(self,field,value):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,field,regex FROM blacklist WHERE field=%s",[field])
res = cur.fetchall()
self.conn.commit()
for i in res:
# Check passed in value using i.regex - Search() or Match() ?
match = re.search(i['regex'], value) # An invalid regexp will throw an exception here. Valid regexp is i.e: name field and (.*-dev) or vcs_uri field and (.*/users/*)
if match is not None:
self.log.debug("BLACKLISTED! %s matches %s : %s", str(i['regex']), str(field), str(value))
cur.close()
return True
else:
cur.close()
return False
cur.close()
return False # If no results, that is fine too.
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error checking blacklist. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def count_blacklist(self):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT COUNT(*) FROM blacklist AS num_blacklist")
res = cur.fetchall()
self.conn.commit()
cur.close()
if res[0][0]:
pages = res[0][0] / self.limit_low;
else:
pages = 1
return math.ceil(pages);
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving blacklist count. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_blacklist(self,page=None):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if page:
offset = (page -1) * self.limit_low;
cur.execute("SELECT id,field,regex FROM blacklist ORDER BY field LIMIT %s OFFSET %s", (self.limit_low,offset,))
else:
cur.execute("SELECT id,field,regex FROM blacklist ORDER BY field")
res = cur.fetchall()
self.conn.commit()
blacklist = []
for i in res:
blacklist.append(Blacklist(i['id'],i['field'],i['regex']))
cur.close()
return blacklist
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving blacklist. Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def get_blacklist_id(self,blacklist_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("SELECT id,field,regex FROM blacklist WHERE id=%s",(blacklist_id,))
res = cur.fetchall()
self.conn.commit()
blacklist = Blacklist(res[0]['id'],res[0]['field'],res['0']['regex'])
cur.close()
return blacklist
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error retrieving blacklist rule with id:" + str(blacklist_id) + ". Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def put_blacklist(self,field,regex):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("INSERT into blacklist(field,regex) VALUES (%s,%s) RETURNING id",(remove_nasties(field),remove_nasties(regex)))
res = cur.fetchall()
self.conn.commit()
blacklist = Blacklist(res[0]['id'],field,regex)
cur.close()
return blacklist
except psycopg2.Error as e:
self.conn.rollback()
raise Exception("Error adding blacklist rule:" + " .Database error code: " + str(e.pgcode) + " - Details: " + str(e.pgerror))
return None
def delete_blacklist(self,blacklist_id):
try:
cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("DELETE FROM blacklist WHERE id=%s RETURNING id",(blacklist_id,))
res = cur.fetchall()
self.conn.commit()
if res[0]['id'] == blacklist_id:
cur.close()
return True
else:
cur.close()
return False
except psycopg2.Error as e:
self.conn.rollback()
self.log.debug("Error deleting blacklist with id: %s. Database error code: %s - Details: %s.",str(blacklist_id),str(e.pgcode),str(e.pgerror))
return e.pgcode
|