core/script/update_mdi.py

68 lines
1.9 KiB
Python
Raw Normal View History

2015-11-03 08:20:20 +00:00
#!/usr/bin/env python3
2016-03-09 10:15:04 +00:00
"""Download the latest Polymer v1 iconset for materialdesignicons.com."""
2016-05-15 23:38:05 +00:00
import gzip
2015-11-03 08:20:20 +00:00
import os
import re
import requests
import sys
from fingerprint_frontend import fingerprint
2015-11-03 08:20:20 +00:00
GETTING_STARTED_URL = ('https://raw.githubusercontent.com/Templarian/'
'MaterialDesign/master/site/getting-started.savvy')
DOWNLOAD_LINK = re.compile(r'(/api/download/polymer/v1/([A-Z0-9-]{36}))')
START_ICONSET = '<iron-iconset-svg'
OUTPUT_BASE = os.path.join('homeassistant', 'components', 'frontend')
ICONSET_OUTPUT = os.path.join(OUTPUT_BASE, 'www_static', 'mdi.html')
2016-05-15 23:38:05 +00:00
ICONSET_OUTPUT_GZ = os.path.join(OUTPUT_BASE, 'www_static', 'mdi.html.gz')
2015-11-03 08:20:20 +00:00
def get_remote_version():
2016-03-09 10:15:04 +00:00
"""Get current version and download link."""
2015-11-03 08:20:20 +00:00
gs_page = requests.get(GETTING_STARTED_URL).text
mdi_download = re.search(DOWNLOAD_LINK, gs_page)
if not mdi_download:
print("Unable to find download link")
sys.exit()
return 'https://materialdesignicons.com' + mdi_download.group(1)
2015-11-03 08:20:20 +00:00
def clean_component(source):
2016-03-09 10:15:04 +00:00
"""Clean component."""
2015-11-03 08:20:20 +00:00
return source[source.index(START_ICONSET):]
def write_component(source):
2016-03-09 10:15:04 +00:00
"""Write component."""
2015-11-03 08:20:20 +00:00
with open(ICONSET_OUTPUT, 'w') as outp:
print('Writing icons to', ICONSET_OUTPUT)
outp.write(source)
2016-05-15 23:38:05 +00:00
with gzip.open(ICONSET_OUTPUT_GZ, 'wb') as outp:
print('Writing icons gz to', ICONSET_OUTPUT_GZ)
outp.write(source.encode('utf-8'))
2015-11-03 08:20:20 +00:00
def main():
2016-03-09 10:15:04 +00:00
"""Main section of the script."""
2015-11-03 08:20:20 +00:00
# All scripts should have their current work dir set to project root
if os.path.basename(os.getcwd()) == 'script':
os.chdir('..')
print("materialdesignicons.com icon updater")
remote_url = get_remote_version()
2015-12-05 21:20:00 +00:00
source = clean_component(requests.get(remote_url).text)
write_component(source)
fingerprint()
2015-11-03 08:20:20 +00:00
print('Updated to latest version')
if __name__ == '__main__':
main()