123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #!/usr/bin/python
- #
- import argparse
- import json
- import os
- import re
- # Some Data
- PREFIX = "gluon-ffho"
- VENDORS = {
- '8devices' : '8 Devices',
- 'a5' : 'A5',
- 'alfa-network' : 'Alfa Network',
- 'allnet' : 'Allnet',
- 'avm' : 'AVM',
- 'buffalo' : 'Buffalo',
- 'd-link' : 'D-Link',
- 'gl' : 'GL',
- 'gl-inet' : 'GL.Inet',
- 'lemarker' : 'Lemarker',
- 'linksys' : 'Linksys',
- 'netgear' : 'Netgear',
- 'ocedo' : 'Ocedo',
- 'onion' : 'Onion',
- 'openmesh' : 'Openmesh',
- 'raspberri-pi' : 'Raspberri Pi',
- 'tp-link' : 'TP-Link',
- 'ubiquiti' : 'Ubiquiti',
- 'ubnt' : 'UBNT',
- 'vocore' : 'Vocore',
- 'wd-my-net' : 'WD My Net',
- 'x86' : 'x86',
- 'zbt' : 'ZBT',
- 'zyxel' : 'Zyxel'
- }
- BLACKLIST_MODELS = [
- 'cpe510-v1.0',
- 'cpe510-v1.1',
- 'cpe520-v1.1',
- 'bullet-m5',
- 'nanostation-m5',
- 'nanostation-m5-xw',
- 'nanostation-loco-m5',
- 'nanostation-loco-m5-xw',
- 'rocket-m5',
- 'rocket-m5-ti',
- 'rocket-m5-xw'
- ]
- def longestPrefixKeyMatch(string, dictionary):
- maxlen = 0
- retval = None
- for key in dictionary:
- if string.startswith(key):
- if len(key) > maxlen:
- maxlen = len(key)
- retval = key
- return retval
- def findFiles(directory):
- retval = []
- searchpath = directory + "/sysupgrade/"
- factory = directory + "/factory/"
- pattern = PREFIX + "-(([0-9]+\.?)+)-(.+)-sysupgrade\.([a-z\.]+)"
- for f in os.listdir(searchpath):
- m = re.search(pattern, f)
- if m:
- # extract info from filename
- firmware = m.group(3)
- imageversion = m.group(1)
- imagetype = m.group(4)
- vendor = 'Unknown'
- model = 'Unknown'
- vendor_key = longestPrefixKeyMatch(firmware, VENDORS)
- if vendor_key:
- vendor = VENDORS[vendor_key]
- n = re.search(vendor_key + "-(.+)$", firmware)
- if n:
- model = n.group(1)
- if model in BLACKLIST_MODELS:
- continue
- has_factory = False
- if os.path.isfile(factory + PREFIX + "-" + imageversion + "-" + firmware + "." + imagetype):
- has_factory = True
- retval.append({'firmware' : firmware,
- 'imagetype' : imagetype,
- 'imageversion' : imageversion,
- 'vendor' : vendor,
- 'model' : model,
- 'factory' : has_factory
- })
- return retval
- def buildDictForJSON(data):
- retval = {}
- for vendor in VENDORS:
- retval[VENDORS[vendor]] = {}
- for entry in data:
- if entry['vendor'] == VENDORS[vendor]:
- retval[VENDORS[vendor]][entry['model']] = entry
- return retval
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description='Build models.json file.')
- parser.add_argument('directory', help='Directory where the firmware files are')
- parser.add_argument('outputfile', help='Write output to file', nargs='?')
- args = parser.parse_args()
- files = findFiles(args.directory)
- data = buildDictForJSON(files)
- if args.outputfile:
- with open(args.outputfile, "w") as fd:
- json.dump(data, fd, sort_keys=True)
- else:
- print json.dumps(data, sort_keys=True)
|