insert_missing_changedby.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Adds yet unknown changedby fields when this column is added to an existing
  4. # database. If everything goes well, it needs to be run only once. Data is
  5. # extracted from Filippo Giunchedi's upload-history project, get the file at
  6. # merkel:/home/filippo/upload-history/*.db.
  7. # Copyright (C) 2008 Christoph Berg <myon@debian.org>
  8. # Copyright (C) 2008 Bernd Zeimetz <bzed@debian.org>
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. ###############################################################################
  21. # /Everybody stand back/
  22. #
  23. # I know regular expressions
  24. ###############################################################################
  25. from __future__ import print_function
  26. import errno
  27. import fcntl
  28. import os
  29. import sys
  30. import time
  31. import re
  32. import apt_pkg
  33. import daklib.database
  34. import daklib.queue
  35. import daklib.utils
  36. from pysqlite2 import dbapi2 as sqlite
  37. import pysqlite2.dbapi2
  38. import psycopg2
  39. projectB = None
  40. projectBdb = None
  41. DBNAME = "uploads-queue.db"
  42. sqliteConn = None
  43. maintainer_id_cache = {}
  44. ###############################################################################
  45. def get_or_set_maintainer_id(maintainer):
  46. global maintainer_id_cache
  47. if maintainer in maintainer_id_cache:
  48. return maintainer_id_cache[maintainer]
  49. if isinstance(maintainer, basestring):
  50. if not isinstance(maintainer, unicode):
  51. try:
  52. maintainer = unicode(maintainer, 'utf-8')
  53. except:
  54. maintainer = unicode(maintainer, 'iso8859-15')
  55. maintainer = maintainer.encode('utf-8')
  56. print("%s" % maintainer)
  57. cursor = projectBdb.cursor()
  58. cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
  59. row = cursor.fetchone()
  60. if not row:
  61. cursor.execute("INSERT INTO maintainer (name) VALUES (%s)", (maintainer, ))
  62. cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
  63. row = cursor.fetchone()
  64. maintainer_id = row[0]
  65. maintainer_id_cache[maintainer] = maintainer_id
  66. cursor.close()
  67. return maintainer_id
  68. def __get_changedby__(package, version):
  69. cur = sqliteConn.cursor()
  70. cur.execute("SELECT changedby FROM uploads WHERE package=? AND version=? LIMIT 1", (package, version))
  71. res = cur.fetchone()
  72. cur.close()
  73. return res
  74. def insert():
  75. print("Adding missing changedby fields.")
  76. listcursor = projectBdb.cursor()
  77. listcursor.execute("SELECT id, source, version FROM source WHERE changedby IS NULL")
  78. row = listcursor.fetchone()
  79. while row:
  80. print(repr(row))
  81. try:
  82. res = __get_changedby__(row[1], row[2])
  83. except:
  84. sqliteConn.text_factory = str
  85. try:
  86. res = __get_changedby__(row[1], row[2])
  87. except:
  88. print('FAILED SQLITE')
  89. res = None
  90. sqliteConn.text_factory = unicode
  91. if res:
  92. changedby_id = get_or_set_maintainer_id(res[0])
  93. cur = projectBdb.cursor()
  94. cur.execute("UPDATE source SET changedby=%s WHERE id=%s" % (changedby_id, row[0]))
  95. cur.close()
  96. print(changedby_id, "(%d)" % row[0])
  97. else:
  98. print("nothing found")
  99. row = listcursor.fetchone()
  100. listcursor.close()
  101. ###############################################################################
  102. def main():
  103. global projectB, sqliteConn, projectBdb
  104. Cnf = daklib.utils.get_conf()
  105. Upload = daklib.queue.Upload(Cnf)
  106. projectB = Upload.projectB
  107. projectBdb = psycopg2.connect("dbname=%s" % Cnf["DB::Name"])
  108. sqliteConn = sqlite.connect(DBNAME)
  109. insert()
  110. projectBdb.commit()
  111. projectBdb.close()
  112. ###############################################################################
  113. if __name__ == '__main__':
  114. main()