Build Scripts, main script handling the github web hook

import os
import traceback
import subprocess
import re
import logging
import shutil

from import BaseCommand
from import call_command
from django.utils import timezone
from django.conf import settings

from hljs_org import lib, models

log = logging.getLogger('hljs_org.updatehljs')

def run(args):
    return subprocess.check_output(args, stderr=subprocess.STDOUT)

class Command(BaseCommand):
    help = 'Updates the site for a new version of highlight.js library'
    requires_system_checks = False

    def add_arguments(self, parser):
        parser.add_argument('version', type=str)

    def _handle(self, version):
        The actual command handling extracted into a separate method to avoid
        an extra level of indentation inside the try..except in self.handle().
        os.chdir(settings.HLJS_SOURCE)'Checking out version %s...' % version)
        run(['git', 'checkout', 'master'])
        run(['git', 'pull', '-f'])
        run(['git', 'checkout', version])'Checking version consistency within the source...')
        version_numbers = version.split('.')
        node_version = version if len(version_numbers) >= 3 else '%s.0' % version
        short_version = version if len(version_numbers) <= 2 else '.'.join(version_numbers[:2])
        assert lib.version(settings.HLJS_SOURCE) == version
        assert'"version"\s*:\s*"%s"' % node_version, open('package.json', encoding='utf-8').read()) is not None
        conf = open('docs/', encoding='utf-8').read()
        assert'version = \'%s\'' % short_version, conf) is not None
        assert'release = \'%s\'' % version, conf) is not None'Reinstalling node dependencies...')
        run(['rm', '-rf', 'node_modules'])
        run(['npm', 'install'])'Building CDN build to populate cache...')
        run(['nodejs', 'tools/build.js', '--target', 'cdn', 'none'])'Moving CDN build over to %s' % settings.HLJS_CACHE)
        if os.path.exists(settings.HLJS_CACHE):
        shutil.move('build', settings.HLJS_CACHE)'Updating CDN repo at %s' % settings.HLJS_CDN_SOURCE)
        run(['nodejs', 'tools/build.js', '--target', 'cdn', ':common'])
        run(['git', 'pull', '-f'])
        lines = run(['git', '--git-dir', os.path.join(settings.HLJS_CDN_SOURCE, '.git'), 'tag']).decode('utf-8').splitlines()
        if version in lines:
  'Tag %s already exists in the local CDN repo' % version)
            build_dir = os.path.join(settings.HLJS_CDN_SOURCE, 'build')
            if os.path.exists(build_dir):
            shutil.move(os.path.join(settings.HLJS_SOURCE, 'build'), build_dir)
            run(['git', 'add', '.'])
            run(['git', 'commit', '-m', 'Update to version %s' % version])
            run(['git', 'tag', version])
        run(['git', 'push'])
        run(['git', 'push', '--tags'])

        call_command('collectstatic', interactive=False)'Updating news entry...')
                'text':, version),

        lines = run(['npm', 'view', 'highlight.js', 'version']).decode('utf-8').splitlines()
        lines = [l for l in lines if l and not l.startswith('npm')]
        published_version = lines[0]'Published npm version is %s' % published_version)
        if published_version != node_version:
  'Publishing version %s to npm...' % node_version)
            run(['nodejs', 'tools/build.js', '--target', 'node'])
            run(['npm', 'publish', 'build'])'Update to version %s comp, builds docs and demo, called from

# -*- coding: utf-8 -*-
import os
import shutil
import subprocess
import logging

from import BaseCommand
from django.conf import settings

log = logging.getLogger('hljs_org.publishtest')

def run(args):
    return subprocess.check_output(args, stderr=subprocess.STDOUT)

def build_highlightjs(languages=[]):
    run(['nodejs', os.path.join('tools', 'build.js'),
        '--target', 'browser', '--docs',
    ] + languages)

class Command(BaseCommand):
    help = 'Publishes the demo on the Web. This requires up to date cache for building highlight.pack.js'
    requires_system_check = False

    def handle(self, *args, **options):
        # ensure we're working with the absolute static_root as we'll have to
        # chdir into highlight.js source because of how its build tool works
        static_root = os.path.abspath(settings.STATIC_ROOT)
        os.chdir(settings.HLJS_SOURCE)'Publishing test page to %s' % static_root)
        build_path = os.path.join(settings.HLJS_SOURCE, 'build')'Building full browser build...')
        build_highlightjs()'Copying highlight.pack.js...')
        shutil.copy(os.path.join(build_path, 'highlight.pack.js'), static_root)'Copying demo...')
        demo_dst = os.path.join(static_root, 'demo')
        if os.path.exists(demo_dst):
        shutil.copytree(os.path.join(build_path, 'demo'), demo_dst)'Building browser build for [%s]...' % ' '.join(settings.HLJS_SNIPPETS))
            os.path.join(build_path, 'highlight.pack.js'),
            os.path.join(static_root, '')