def theme_update(path=None, safe=False): conf.load(path) if safe: logger.info('saving current theme') assets_bak = "%s.bak" % pathes.theme_assets_installed() templates_bak = "%s.bak" % pathes.theme_templates_installed() try: for path in [templates_bak, assets_bak]: if os.path.isdir(path): shutil.rmtree(path, ignore_errors=True) shutil.move(pathes.theme_assets_installed(), assets_bak) shutil.move(pathes.theme_templates_installed(), templates_bak) except Exception as e: logger.error('error saving existing theme files: ' + str(e)) return logger.info('updating theme files') try: path = pathes.theme_assets_source() helpers.copydir(path, pathes.theme_assets_installed()) path = pathes.theme_templates_source() helpers.copydir(path, pathes.theme_templates_installed()) except Exception as e: logger.error('error updating theme: ' + str(e))
def main(): try: dispatch(cli.parse(sys.argv[1:])) except USER_ERRORS as e: logger.error(e) except CRITICAL_ERRORS: logger.crash()
def pages(cache): """Build site pages.""" for source in cache.pages(): logger.info(_to('page', source.rel_path(), source.rel_dest())) helpers.makedirs(source.dest_dir()) try: data = _complement(source.data(), index=cache.index()) templates.render_page(data, source.dest()) except Exception as ex: logger.error('page building error: ' + str(ex)) logger.debug(traceback.format_exc())
def page(path=None, name=None, force=False, edit=False): """Create new page.""" conf.load(path) try: path = source.PageSource.create(name, force) except source.PageExistsException: logger.error('page already exists, use -f to overwrite') return logger.info('page created: ' + path) if edit: helpers.execute(conf.get('editor_cmd'), path)
def humans(cache): """Build humans.txt.""" for source in cache.assets(basename='humans.txt'): logger.info('processing ' + source.rel_path()) helpers.makedirs(source.dest_dir()) try: data = _complement({}) templates.render_file(source.path(), data, source.dest()) except Exception as ex: logger.error('humans.txt processing failed: ' + str(ex)) logger.debug(traceback.format_exc()) finally: source.processed(True)
def build(path=None, output=None): """Generate web content from source.""" conf.load(path) cache = Cache() if cache.processing_errors(): for file_name, error in cache.processing_errors(): message = "error processing source file '%s' - %s" logger.error(message % (file_name, error)) if output: conf.set('build_path', output) logger.info('build directory: ' + conf.get('build_path')) for builder in builders.order(): builder(cache)
def render_page(page_data, dest_path): """This one is tricky. It creates a dynamic template inherited from the base template, adds a 'main' block to this template with page content inside, and renders the result template to [dest_path]. Boom!""" base_template = page_data['page']['template'] + '.html' content = page_data['page']['content'] template = """{%% extends "%s" %%}{%% block main %%}%s{%% endblock %%}""" template = template % (base_template, content) try: template = env().from_string(template) html = template.render(page_data) _save(html, dest_path) except jinja2.exceptions.TemplateNotFound as e: message = "page generation failed because template was not found: %s" logger.error(message % e)
def deploy(path=None): """Deploy generated website to the remote web server.""" conf.load(path) helpers.check_build(conf.get('build_path')) logger.info('deploying website...') if not conf.get('deploy_cmd'): raise Exception('deploy command is not defined') cmd = conf.get('deploy_cmd').format(build_path=conf.get('build_path')) try: output = subprocess.check_output(cmd, shell=True) logger.debug("Command output:\n%s" % output.decode('utf-8')) logger.info('done') except subprocess.CalledProcessError as e: logger.error(e) logger.debug("Command output:\n%s" % e.output.decode('utf-8'))
def init(path=None, force=False): """Create new website.""" conf.generate(path, force) try: src = pathes.proto() existing = helpers.copydir(src, pathes.site(), force=force) if len(existing): message = "some existing files were overwritten" if force else \ "some existing files were NOT overwritten (use --force " \ "to overwrite)" logger.warn("%s\n- %s" % (message, '\n- '.join(existing))) logger.info('website created successfully, have fun!') except Exception as ex: logger.error('initialization failed: ' + str(ex)) print(str(ex))
def posts(cache): """Build blog posts and copy the latest post to the site root.""" for source in cache.posts(): logger.info(_to('post', source.rel_path(), source.rel_dest())) helpers.makedirs(source.dest_dir()) try: data = _complement(source.data()) templates.render_page(data, source.dest()) except Exception as ex: logger.error('post building error: ' + str(ex)) logger.debug(traceback.format_exc()) if conf.get('post_at_root_url'): # put the latest post at site root url last = cache.posts()[0] path = os.path.join(conf.get('build_path'), conf.get('index_page')) logger.info(_to('root', last.rel_dest(), conf.get('index_page'))) if any(cache.pages(dest=conf.get('index_page'))): logger.warn('root page will be overwritten by the latest post') try: shutil.copyfile(last.dest(), path) except FileNotFoundError: logger.error("latest post was not generated and can't be copied")