FixRestrictions.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # COPYRIGHT: Openmoko Inc. 2010
  4. # LICENSE: GPL Version 3 or later
  5. # DESCRIPTION: Fix the resrictions flag bit
  6. # AUTHORS: Sean Moss-Pultz <sean@openmoko.com>
  7. # Christopher Hall <hsw@openmoko.com>
  8. import sys, os
  9. import struct
  10. import os.path
  11. import sqlite3
  12. idx_file = 'image/pedia.idx'
  13. art_file = 'work/articles.db'
  14. article_db = sqlite3.connect(art_file)
  15. article_db.execute('pragma synchronous = 0')
  16. article_db.execute('pragma temp_store = 2')
  17. article_db.execute('pragma read_uncommitted = true')
  18. article_db.execute('pragma cache_size = 20000000')
  19. article_db.execute('pragma default_cache_size = 20000000')
  20. article_db.execute('pragma journal_mode = off')
  21. index = open(idx_file, 'r+b')
  22. c = article_db.cursor()
  23. c.execute('select article_number, fnd_offset, title, restricted from articles')
  24. r_count = 0
  25. f_count = 0
  26. for article_number, fnd_offset, title, restricted in c.fetchall():
  27. article_number = int(article_number)
  28. restricted = bool(int(restricted))
  29. offset = (article_number - 1) * 12 + 4
  30. index.seek(offset)
  31. (byte,) = struct.unpack('I', index.read(4))
  32. flag = (byte & 0x80000000) != 0
  33. if flag != restricted:
  34. if restricted:
  35. r_count += 1
  36. print('Restrict[{0:d}]: {1:s}'.format(r_count, title[1:].encode('utf-8')))
  37. byte |= 0x80000000
  38. else:
  39. f_count += 1
  40. print('Free[{0:d}]: {1:s}'.format(f_count, title[1:].encode('utf-8')))
  41. byte &= 0x7fffffff
  42. index.seek(offset)
  43. index.write(struct.pack('I', byte))
  44. c.close()
  45. index.close()
  46. print('Became Restricted: {0:10d}'.format(r_count))
  47. print('Became Free: {0:10d}'.format(f_count))