|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import json |
| 5 | +import urllib |
| 6 | +import urllib2 |
| 7 | +import tempfile |
| 8 | +import zipfile |
| 9 | +import glob |
| 10 | +import subprocess |
| 11 | +import getopt |
| 12 | +import sys |
| 13 | +import shutil |
| 14 | +import os |
| 15 | +import string |
| 16 | + |
| 17 | +FIRST_RELEASE_ID=3084382 |
| 18 | +DOCS_PATH = 'docs' |
| 19 | +DOCS_BRANCHES = [ |
| 20 | + { 'tag': 'develop', 'description': 'develop branch (development)', 'path': 'develop' }, |
| 21 | + { 'tag': 'master', 'description': 'master branch (stable)', 'path': 'stable' } |
| 22 | +] |
| 23 | +CONFIGURATORS_PATH = 'configurators' |
| 24 | +CONFIGURATORS_VERSIONS = [ |
| 25 | + { 'title': 'v2', 'description': 'For Homie v2.x.x', 'path': 'v2', 'url': 'https://github.com/marvinroger/homie-esp8266-setup/raw/gh-pages/ui_bundle.html' }, |
| 26 | + { 'title': 'v1', 'description': 'For Homie v1.x.x', 'path': 'v1', 'file': '/configurator_v1.html' } |
| 27 | +] |
| 28 | + |
| 29 | +current_dir = os.path.dirname(__file__) |
| 30 | +output_dir = getopt.getopt(sys.argv[1:], 'o:')[0][0][1] |
| 31 | +github_releases = json.load(urllib2.urlopen('https://api.github.com/repos/marvinroger/homie-esp8266/releases')) |
| 32 | + |
| 33 | +def generate_docs(data): |
| 34 | + print('Generating docs for ' + data['tag'] + ' (' + data['description'] + ') at /' + data['path'] + '...') |
| 35 | + zip_url = 'https://github.com/marvinroger/homie-esp8266/archive/' + data['tag'] + '.zip' |
| 36 | + zip_path = tempfile.mkstemp()[1] |
| 37 | + urllib.urlretrieve(zip_url, zip_path) |
| 38 | + |
| 39 | + zip_file = zipfile.ZipFile(zip_path, 'r') |
| 40 | + unzip_path = tempfile.mkdtemp() |
| 41 | + zip_file.extractall(unzip_path) |
| 42 | + src_path = glob.glob(unzip_path + '/*')[0] |
| 43 | + |
| 44 | + if not os.path.isfile(src_path + '/mkdocs.yml'): shutil.copy(current_dir + '/mkdocs.default.yml', src_path + '/mkdocs.yml') |
| 45 | + |
| 46 | + subprocess.call(['mkdocs', 'build'], cwd=src_path) |
| 47 | + shutil.copytree(src_path + '/site', output_dir + '/' + DOCS_PATH + '/' + data['path']) |
| 48 | + print('Done.') |
| 49 | + |
| 50 | +def generate_configurators(data): |
| 51 | + print('Generating configurator for ' + data['title'] + ' (' + data['description'] + ') at /' + data['path'] + '...') |
| 52 | + file_path = None |
| 53 | + if 'file' in data: |
| 54 | + file_path = current_dir + data['file'] |
| 55 | + else: # url |
| 56 | + file_path = tempfile.mkstemp()[1] |
| 57 | + urllib.urlretrieve(data['url'], file_path) |
| 58 | + |
| 59 | + prefix_output = output_dir + '/' + CONFIGURATORS_PATH + '/' + data['path'] |
| 60 | + try: |
| 61 | + os.makedirs(prefix_output) |
| 62 | + except: |
| 63 | + pass |
| 64 | + |
| 65 | + shutil.copy(file_path, prefix_output + '/index.html') |
| 66 | + |
| 67 | + print('Done.') |
| 68 | + |
| 69 | +shutil.rmtree(output_dir, ignore_errors=True) |
| 70 | + |
| 71 | +# Generate docs |
| 72 | + |
| 73 | +generated_docs = [] |
| 74 | + |
| 75 | +# Generate docs for branches |
| 76 | + |
| 77 | +for branch in DOCS_BRANCHES: |
| 78 | + generated_docs.append(branch) |
| 79 | + generate_docs(branch) |
| 80 | + |
| 81 | +# Generate docs for releases |
| 82 | + |
| 83 | +for release in github_releases: |
| 84 | + if (release['id'] < FIRST_RELEASE_ID): continue |
| 85 | + |
| 86 | + tag_name = release['tag_name'] |
| 87 | + version = tag_name[1:] |
| 88 | + description = 'release ' + version |
| 89 | + |
| 90 | + data = { |
| 91 | + 'tag': tag_name, |
| 92 | + 'description': description, |
| 93 | + 'path': version |
| 94 | + } |
| 95 | + |
| 96 | + generated_docs.append(data) |
| 97 | + generate_docs(data) |
| 98 | + |
| 99 | +# Generate documentation html |
| 100 | + |
| 101 | +documentation_html = '<ul>' |
| 102 | +for documentation_data in generated_docs: |
| 103 | + documentation_html += '<li><a href="' + DOCS_PATH + '/' + documentation_data['path'] + '"># ' + documentation_data['tag'] + ' <span class="description">' + documentation_data['description'] + '</span></a></li>' |
| 104 | +documentation_html += '</ul>' |
| 105 | + |
| 106 | +# Generate configurators |
| 107 | + |
| 108 | +generated_configurators = [] |
| 109 | + |
| 110 | +for version in CONFIGURATORS_VERSIONS: |
| 111 | + generated_configurators.append(version) |
| 112 | + generate_configurators(version) |
| 113 | + |
| 114 | +# Generate configurators html |
| 115 | + |
| 116 | +configurators_html = '<ul>' |
| 117 | +for configurator_data in generated_configurators: |
| 118 | + configurators_html += '<li><a href="' + CONFIGURATORS_PATH + '/' + configurator_data['path'] + '"># ' + configurator_data['title'] + ' <span class="description">' + configurator_data['description'] + '</span></a></li>' |
| 119 | +configurators_html += '</ul>' |
| 120 | + |
| 121 | +# Generate index |
| 122 | + |
| 123 | +docs_index_template_file = open(current_dir + '/docs_index_template.html') |
| 124 | +docs_index_template_html = docs_index_template_file.read() |
| 125 | +docs_index_template = string.Template(docs_index_template_html) |
| 126 | +docs_index = docs_index_template.substitute(documentation_html=documentation_html, configurators_html=configurators_html) |
| 127 | + |
| 128 | +docs_index_file = open(output_dir + '/index.html', 'w') |
| 129 | +docs_index_file.write(docs_index) |
| 130 | +docs_index_file.close() |
0 commit comments