insert_missing_changedby.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. import errno, fcntl, os, sys, time, re
  26. import apt_pkg
  27. import daklib.database
  28. import daklib.queue
  29. import daklib.utils
  30. from pysqlite2 import dbapi2 as sqlite
  31. import pysqlite2.dbapi2
  32. import psycopg2
  33. projectB = None
  34. projectBdb = None
  35. DBNAME = "uploads-queue.db"
  36. sqliteConn = None
  37. maintainer_id_cache={}
  38. ###############################################################################
  39. def get_or_set_maintainer_id (maintainer):
  40. global maintainer_id_cache
  41. if maintainer_id_cache.has_key(maintainer):
  42. return maintainer_id_cache[maintainer]
  43. if isinstance(maintainer, basestring):
  44. if not isinstance(maintainer, unicode):
  45. try:
  46. maintainer = unicode(maintainer, 'utf-8')
  47. except:
  48. maintainer = unicode(maintainer, 'iso8859-15')
  49. maintainer = maintainer.encode('utf-8')
  50. print "%s" % maintainer
  51. cursor = projectBdb.cursor()
  52. cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
  53. row = cursor.fetchone()
  54. if not row:
  55. cursor.execute("INSERT INTO maintainer (name) VALUES (%s)" , (maintainer, ))
  56. cursor.execute("SELECT id FROM maintainer WHERE name=%s", (maintainer, ))
  57. row = cursor.fetchone()
  58. maintainer_id = row[0]
  59. maintainer_id_cache[maintainer] = maintainer_id
  60. cursor.close()
  61. return maintainer_id
  62. def __get_changedby__(package, version):
  63. cur = sqliteConn.cursor()
  64. cur.execute("SELECT changedby FROM uploads WHERE package=? AND version=? LIMIT 1", (package, version))
  65. res = cur.fetchone()
  66. cur.close()
  67. return res
  68. def insert ():
  69. print "Adding missing changedby fields."
  70. listcursor = projectBdb.cursor()
  71. listcursor.execute("SELECT id, source, version FROM source WHERE changedby IS NULL")
  72. row = listcursor.fetchone()
  73. while row:
  74. print repr(row)
  75. try:
  76. res = __get_changedby__(row[1], row[2])
  77. except:
  78. sqliteConn.text_factory = str
  79. try:
  80. res = __get_changedby__(row[1], row[2])
  81. except:
  82. print 'FAILED SQLITE'
  83. res=None
  84. sqliteConn.text_factory = unicode
  85. if res:
  86. changedby_id = get_or_set_maintainer_id(res[0])
  87. cur = projectBdb.cursor()
  88. cur.execute("UPDATE source SET changedby=%s WHERE id=%s" % (changedby_id, row[0]))
  89. cur.close()
  90. print changedby_id, "(%d)" % row[0]
  91. else:
  92. print "nothing found"
  93. row = listcursor.fetchone()
  94. listcursor.close()
  95. ###############################################################################
  96. def main():
  97. global projectB, sqliteConn, projectBdb
  98. Cnf = daklib.utils.get_conf()
  99. Upload = daklib.queue.Upload(Cnf)
  100. projectB = Upload.projectB
  101. projectBdb = psycopg2.connect("dbname=%s" % Cnf["DB::Name"])
  102. sqliteConn = sqlite.connect(DBNAME)
  103. insert()
  104. projectBdb.commit()
  105. projectBdb.close()
  106. ###############################################################################
  107. if __name__ == '__main__':
  108. main()