buildModels.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/python
  2. #
  3. import argparse
  4. import json
  5. import os
  6. import re
  7. # Some Data
  8. PREFIX = "gluon-ffho"
  9. VENDORS = {
  10. '8devices' : '8 Devices',
  11. 'a5' : 'A5',
  12. 'alfa-network' : 'Alfa Network',
  13. 'allnet' : 'Allnet',
  14. 'avm' : 'AVM',
  15. 'buffalo' : 'Buffalo',
  16. 'd-link' : 'D-Link',
  17. 'gl' : 'GL',
  18. 'gl-inet' : 'GL.Inet',
  19. 'lemarker' : 'Lemarker',
  20. 'linksys' : 'Linksys',
  21. 'netgear' : 'Netgear',
  22. 'ocedo' : 'Ocedo',
  23. 'onion' : 'Onion',
  24. 'openmesh' : 'Openmesh',
  25. 'raspberri-pi' : 'Raspberri Pi',
  26. 'tp-link' : 'TP-Link',
  27. 'ubiquiti' : 'Ubiquiti',
  28. 'ubnt' : 'UBNT',
  29. 'vocore' : 'Vocore',
  30. 'wd-my-net' : 'WD My Net',
  31. 'x86' : 'x86',
  32. 'zbt' : 'ZBT',
  33. 'zyxel' : 'Zyxel'
  34. }
  35. BLACKLIST_MODELS = [
  36. 'cpe510-v1.0',
  37. 'cpe510-v1.1',
  38. 'cpe520-v1.1',
  39. 'bullet-m5',
  40. 'nanostation-m5',
  41. 'nanostation-m5-xw',
  42. 'nanostation-loco-m5',
  43. 'nanostation-loco-m5-xw',
  44. 'rocket-m5',
  45. 'rocket-m5-ti',
  46. 'rocket-m5-xw'
  47. ]
  48. def longestPrefixKeyMatch(string, dictionary):
  49. maxlen = 0
  50. retval = None
  51. for key in dictionary:
  52. if string.startswith(key):
  53. if len(key) > maxlen:
  54. maxlen = len(key)
  55. retval = key
  56. return retval
  57. def findFiles(directory):
  58. retval = []
  59. searchpath = directory + "/sysupgrade/"
  60. factory = directory + "/factory/"
  61. pattern = PREFIX + "-(([0-9]+\.?)+)-(.+)-sysupgrade\.([a-z\.]+)"
  62. for f in os.listdir(searchpath):
  63. m = re.search(pattern, f)
  64. if m:
  65. # extract info from filename
  66. firmware = m.group(3)
  67. imageversion = m.group(1)
  68. imagetype = m.group(4)
  69. vendor = 'Unknown'
  70. model = 'Unknown'
  71. vendor_key = longestPrefixKeyMatch(firmware, VENDORS)
  72. if vendor_key:
  73. vendor = VENDORS[vendor_key]
  74. n = re.search(vendor_key + "-(.+)$", firmware)
  75. if n:
  76. model = n.group(1)
  77. if model in BLACKLIST_MODELS:
  78. continue
  79. has_factory = False
  80. if os.path.isfile(factory + PREFIX + "-" + imageversion + "-" + firmware + "." + imagetype):
  81. has_factory = True
  82. retval.append({'firmware' : firmware,
  83. 'imagetype' : imagetype,
  84. 'imageversion' : imageversion,
  85. 'vendor' : vendor,
  86. 'model' : model,
  87. 'factory' : has_factory
  88. })
  89. return retval
  90. def buildDictForJSON(data):
  91. retval = {}
  92. for vendor in VENDORS:
  93. retval[VENDORS[vendor]] = {}
  94. for entry in data:
  95. if entry['vendor'] == VENDORS[vendor]:
  96. retval[VENDORS[vendor]][entry['model']] = entry
  97. return retval
  98. if __name__ == "__main__":
  99. parser = argparse.ArgumentParser(description='Build models.json file.')
  100. parser.add_argument('directory', help='Directory where the firmware files are')
  101. parser.add_argument('outputfile', help='Write output to file', nargs='?')
  102. args = parser.parse_args()
  103. files = findFiles(args.directory)
  104. data = buildDictForJSON(files)
  105. if args.outputfile:
  106. with open(args.outputfile, "w") as fd:
  107. json.dump(data, fd, sort_keys=True)
  108. else:
  109. print json.dumps(data, sort_keys=True)