buildModels.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 'wbs510-v1.20',
  40. 'bullet-m5',
  41. 'nanostation-m5',
  42. 'nanostation-m5-xw',
  43. 'nanostation-loco-m5',
  44. 'nanostation-loco-m5-xw',
  45. 'rocket-m5',
  46. 'rocket-m5-ti',
  47. 'rocket-m5-xw',
  48. 'unifi-ac-mesh',
  49. 'unifi-ac-mesh-pro',
  50. 'unifiap-outdoor',
  51. 'unifiap-outdoor+'
  52. ]
  53. def longestPrefixKeyMatch(string, dictionary):
  54. maxlen = 0
  55. retval = None
  56. for key in dictionary:
  57. if string.startswith(key):
  58. if len(key) > maxlen:
  59. maxlen = len(key)
  60. retval = key
  61. return retval
  62. def findFiles(directory):
  63. retval = []
  64. searchpath = directory + "/sysupgrade/"
  65. factory = directory + "/factory/"
  66. pattern = PREFIX + "-(([0-9]+\.?)+)-(.+)-sysupgrade\.([a-z\.]+)"
  67. for f in os.listdir(searchpath):
  68. m = re.search(pattern, f)
  69. if m:
  70. # extract info from filename
  71. firmware = m.group(3)
  72. imageversion = m.group(1)
  73. imagetype = m.group(4)
  74. vendor = 'Unknown'
  75. model = 'Unknown'
  76. vendor_key = longestPrefixKeyMatch(firmware, VENDORS)
  77. if vendor_key:
  78. vendor = VENDORS[vendor_key]
  79. n = re.search(vendor_key + "-(.+)$", firmware)
  80. if n:
  81. model = n.group(1)
  82. if model in BLACKLIST_MODELS:
  83. continue
  84. has_factory = False
  85. if os.path.isfile(factory + PREFIX + "-" + imageversion + "-" + firmware + "." + imagetype):
  86. has_factory = True
  87. retval.append({'firmware' : firmware,
  88. 'imagetype' : imagetype,
  89. 'imageversion' : imageversion,
  90. 'vendor' : vendor,
  91. 'model' : model,
  92. 'factory' : has_factory
  93. })
  94. return retval
  95. def buildDictForJSON(data):
  96. retval = {}
  97. for vendor in VENDORS:
  98. retval[VENDORS[vendor]] = {}
  99. for entry in data:
  100. if entry['vendor'] == VENDORS[vendor]:
  101. retval[VENDORS[vendor]][entry['model']] = entry
  102. return retval
  103. if __name__ == "__main__":
  104. parser = argparse.ArgumentParser(description='Build models.json file.')
  105. parser.add_argument('directory', help='Directory where the firmware files are')
  106. parser.add_argument('outputfile', help='Write output to file', nargs='?')
  107. args = parser.parse_args()
  108. files = findFiles(args.directory)
  109. data = buildDictForJSON(files)
  110. if args.outputfile:
  111. with open(args.outputfile, "w") as fd:
  112. json.dump(data, fd, sort_keys=True)
  113. else:
  114. print json.dumps(data, sort_keys=True)