combine_idx.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. def usage(message):
  13. if None != message:
  14. print('error: {0:s}'.format(message))
  15. print('usage: {0:s} <options>'.format(os.path.basename(__file__)))
  16. print(' --help This message')
  17. print(' --verbose Enable verbose output')
  18. print(' --prefix=name Device file name portion for .idx [pedia]')
  19. exit(1)
  20. def main():
  21. global verbose
  22. try:
  23. opts, args = getopt.getopt(sys.argv[1:], 'hvo:f:p:', ['help', 'verbose', 'out=', 'offsets=', 'prefix='])
  24. except getopt.GetoptError, err:
  25. usage(err)
  26. verbose = False
  27. in_format = 'pedia{0:d}.idx-tmp'
  28. out_name = 'pedia.idx'
  29. for opt, arg in opts:
  30. if opt in ('-v', '--verbose'):
  31. verbose = True
  32. elif opt in ('-h', '--help'):
  33. usage(None)
  34. off_name = arg
  35. elif opt in ('-p', '--prefix'):
  36. in_format = arg + '{0:d}.idx-tmp'
  37. out_name = arg + '.idx'
  38. else:
  39. usage('unhandled option: ' + opt)
  40. out = open(out_name, 'wb')
  41. article_count = 0
  42. i = 0
  43. data = {}
  44. while True:
  45. in_name = in_format.format(i)
  46. if not os.path.isfile(in_name):
  47. break
  48. if verbose:
  49. print('combining: {0:s}'.format(in_name))
  50. data[i] = open(in_name, 'rb').read()
  51. article_count += len(data[i]) / 12 # sizeof(struct)
  52. i += 1
  53. out.write(struct.pack('I', article_count))
  54. for j in range(i):
  55. out.write(data[j])
  56. out.close()
  57. # run the program
  58. if __name__ == "__main__":
  59. main()