buildModels.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 vendorPP(vendor):
  63. if vendor in VENDORS:
  64. return VENDORS[vendor]
  65. else:
  66. return vendor
  67. def findFiles(directory):
  68. retval = {}
  69. for imtype in ['sysupgrade', 'factory', 'bootloader']:
  70. searchpath = directory + "/"+imtype+"/"
  71. if imtype == 'sysupgrade':
  72. pattern = PREFIX + "-(([0-9]+\.?)+)-(.+)-sysupgrade\.((img|bin|img.gz|ubi|chk|vdi|vmdk|tar))"
  73. elif imtype == 'bootloader':
  74. pattern = PREFIX + "-(([0-9]+\.?)+)-(.+)-bootloader\.((img|bin|img.gz|ubi|chk|vdi|vmdk|tar))"
  75. else:
  76. pattern = PREFIX + "-(([0-9]+\.?)+)-(.+)\.((img|bin|img.gz|ubi|chk|vdi|vmdk|tar))"
  77. for f in os.listdir(searchpath):
  78. m = re.search(pattern, f)
  79. if m:
  80. # extract info from filename
  81. file = m.group(0)
  82. firmware = m.group(3)
  83. imageversion = m.group(1)
  84. imagetype = m.group(4)
  85. vendor = 'Unknown'
  86. model = 'Unknown'
  87. vendor_key = longestPrefixKeyMatch(firmware, VENDORS)
  88. if vendor_key:
  89. vendor = VENDORS[vendor_key]
  90. n = re.search(vendor_key + "-(.+)$", firmware)
  91. if n:
  92. model = n.group(1)
  93. if model in BLACKLIST_MODELS:
  94. continue
  95. vendor = vendorPP(vendor)
  96. entry = {'vendor' : vendor,
  97. 'model' : model,
  98. 'file' : file
  99. }
  100. if vendor not in retval:
  101. retval.update({vendor : {}})
  102. if model not in retval[vendor]:
  103. retval[vendor].update({model : {}})
  104. retval[vendor][model].update({'modelname' : model , imtype : entry})
  105. return retval
  106. if __name__ == "__main__":
  107. parser = argparse.ArgumentParser(description='Build models.json file.')
  108. parser.add_argument('directory', help='Directory where the firmware files are')
  109. parser.add_argument('outputfile', help='Write output to file', nargs='?')
  110. args = parser.parse_args()
  111. data = findFiles(args.directory)
  112. if args.outputfile:
  113. with open(args.outputfile, "w") as fd:
  114. json.dump(data, fd, sort_keys=True)
  115. else:
  116. print json.dumps(data, sort_keys=True)