combine_idx.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # COPYRIGHT: Openmoko Inc. 2010
  4. # LICENSE: GPL Version 3 or later
  5. # DESCRIPTION: Index file combine to build overall index
  6. # AUTHORS: Sean Moss-Pultz <sean@openmoko.com>
  7. # Christopher Hall <hsw@openmoko.com>
  8. import os, sys, re
  9. import os.path
  10. import struct
  11. import getopt
  12. import PrintLog
  13. UINT32_SIZE = 4
  14. INDEX_ITEM_SIZE = 2 * UINT32_SIZE + 1
  15. def usage(message):
  16. if None != message:
  17. print('error: {0:s}'.format(message))
  18. print('usage: {0:s} <options>'.format(os.path.basename(__file__)))
  19. print(' --help This message')
  20. print(' --verbose Enable verbose output')
  21. print(' --prefix=name Device file name portion for .idx [pedia]')
  22. exit(1)
  23. def main():
  24. global verbose
  25. global INDEX_ITEM_SIZE
  26. global UINT32_SIZE
  27. try:
  28. opts, args = getopt.getopt(sys.argv[1:], 'hvo:f:p:', ['help', 'verbose', 'out=', 'offsets=', 'prefix='])
  29. except getopt.GetoptError, err:
  30. usage(err)
  31. verbose = False
  32. in_format = 'pedia{0:d}.idx-tmp'
  33. out_name = 'pedia.idx'
  34. for opt, arg in opts:
  35. if opt in ('-v', '--verbose'):
  36. verbose = True
  37. elif opt in ('-h', '--help'):
  38. usage(None)
  39. off_name = arg
  40. elif opt in ('-p', '--prefix'):
  41. in_format = arg + '{0:d}.idx-tmp'
  42. out_name = arg + '.idx'
  43. else:
  44. usage('unhandled option: ' + opt)
  45. out = open(out_name, 'wb')
  46. article_count = 0
  47. i = 0
  48. data = {}
  49. while True:
  50. in_name = in_format.format(i)
  51. if not os.path.isfile(in_name):
  52. break
  53. if verbose:
  54. PrintLog.message('combining: {0:s}'.format(in_name))
  55. data[i] = open(in_name, 'rb').read()
  56. article_count += len(data[i]) / INDEX_ITEM_SIZE
  57. i += 1
  58. out.write(struct.pack('<I', article_count))
  59. for j in range(i):
  60. out.write(data[j])
  61. out.close()
  62. PrintLog.message('Combined {0:d} files'.format(i))
  63. # run the program
  64. if __name__ == "__main__":
  65. main()