Changeset - 637ca012b778
[Not reviewed]
1 0 1
Brett Smith (brett) - 6 years ago 2018-05-04 20:05:32
brett@sfconservancy.org
policies: Extend publication script to handle any Conservancy policy.
1 file changed with 15 insertions and 5 deletions:
0 comments (0 inline, 0 general)
www/conservancy/static/projects/policies/publish-policy.py
Show inline comments
 
file renamed from www/conservancy/static/projects/policies/publish-travel-policy.py to www/conservancy/static/projects/policies/publish-policy.py
...
 
@@ -15,25 +15,25 @@ try:
 
    import markdown
 
    from markdown.extensions import tables as mdx_tables
 
    from markdown.extensions import sane_lists as mdx_sane_lists
 
    from markdown.extensions import smarty as mdx_smarty
 
    from markdown.extensions import toc as mdx_toc
 
    markdown_import_success = True
 
except ImportError:
 
    if __name__ != '__main__':
 
        raise
 
    markdown_import_success = False
 

	
 
TEMPLATE_HEADER = """{% extends "base_projects.html" %}
 
{% block subtitle %}Travel and Reimburseable Expense Policy - {% endblock %}
 
{% block subtitle %}{% endblock %}
 
{% block submenuselection %}Policies{% endblock %}
 
{% block content %}
 

	
 
"""
 

	
 
TEMPLATE_FOOTER = """
 

	
 
{% endblock %}
 
"""
 

	
 
@contextlib.contextmanager
 
def run(cmd, encoding=None, ok_exitcodes=frozenset([0]), **kwargs):
...
 
@@ -172,38 +172,38 @@ Setting any flag will always override the default behavior.
 
    parser.add_argument(
 
        '--revision', '-r',
 
        help="Revision string to version the published page. "
 
        "Default determined from the revision of the source file.",
 
    )
 
    add_parser_flag(
 
        parser, 'pull',
 
        help="Try to pull the remote tracking branch to make the checkout "
 
        "up-to-date before making changes"
 
    )
 
    add_parser_flag(
 
        parser, 'commit',
 
        help="Commit changes to the travel policy",
 
        help="Commit changes to the website repository",
 
    )
 
    parser.add_argument(
 
        '-m', dest='commit_message',
 
        default="Publish {filename} revision {revision}.",
 
        help="Message for any commit",
 
    )
 
    add_parser_flag(
 
        parser, 'push',
 
        help="Push to the remote tracking branch after committing changes",
 
    )
 
    parser.add_argument(
 
        'input_path', type=pathlib.Path,
 
        help="Path to the Conservancy travel policy Markdown source",
 
        help="Path to the Conservancy policy Markdown source",
 
    )
 
    parser.add_argument(
 
        'output_path', type=pathlib.Path,
 
        nargs='?', default=pathlib.Path(__file__).parent,
 
        help="Path to the directory to write output files",
 
    )
 

	
 
    if not markdown_import_success:
 
        parser.error("""markdown module is not installed.
 
Try `apt install python3-markdown` or `python3 -m pip install --user Markdown`.""")
 

	
 
    args = parser.parse_args(arglist)
...
 
@@ -212,25 +212,25 @@ Try `apt install python3-markdown` or `python3 -m pip install --user Markdown`."
 
        if not args.git_output.can_run():
 
            parser.error("Git operation requested but `git` not found in PATH")
 
        elif not args.git_output.is_work_tree():
 
            parser.error("Git operation requested but {} is not a working path".format(
 
                args.output_path.as_posix()))
 
    if args.revision is None:
 
        try:
 
            args.revision = GitPath(args.input_path, args.encoding, GitPath.CLEAN_ENV).last_commit()
 
        except subprocess.CalledProcessError:
 
            pass
 
        if args.revision is None:
 
            parser.error("no --revision specified and not found from input path")
 
    args.output_link_path = args.git_output.dir_path / 'conservancy-travel-policy.html'
 
    args.output_link_path = args.git_output.dir_path / args.input_path.with_suffix('.html').name
 
    args.output_file_path = args.output_link_path.with_suffix('.{}.html'.format(args.revision))
 
    return args
 

	
 
class GitOperation:
 
    def __init__(self, args):
 
        self.args = args
 
        self.git_path = args.git_output
 
        self.exitcode = None
 
        self.on_work_tree = self.git_path.can_run() and self.git_path.is_work_tree()
 

	
 
    def run(self):
 
        arg_state = getattr(self.args, self.NAME)
...
 
@@ -290,35 +290,45 @@ class GitPush(GitCommit):
 

	
 

	
 
def write_output(args):
 
    converter = markdown.Markdown(
 
        extensions=[
 
            mdx_tables.TableExtension(),
 
            mdx_sane_lists.SaneListExtension(),
 
            mdx_smarty.SmartyExtension(),
 
            mdx_toc.TocExtension(),
 
        ],
 
        output_format='html5',
 
    )
 
    header = TEMPLATE_HEADER
 
    with args.input_path.open(encoding=args.encoding) as src_file:
 
        for line in src_file:
 
            if line.startswith('# '):
 
                subtitle = line[2:].replace('Software Freedom Conservancy', '').strip()
 
                header = header.replace(
 
                    '{% block subtitle %}',
 
                    '{{% block subtitle %}}{} - '.format(subtitle),
 
                )
 
                break
 
        src_file.seek(0)
 
        body = converter.convert(src_file.read())
 
    with tempfile.NamedTemporaryFile(
 
            'w',
 
            encoding=args.encoding,
 
            dir=args.git_output.dir_path.as_posix(),
 
            suffix='.html',
 
            delete=False,
 
    ) as tmp_out:
 
        try:
 
            tmp_out.write(TEMPLATE_HEADER)
 
            tmp_out.write(header)
 
            tmp_out.write(body)
 
            tmp_out.write(TEMPLATE_FOOTER)
 
            tmp_out.flush()
 
            os.rename(tmp_out.name, str(args.output_file_path))
 
        except BaseException:
 
            os.unlink(tmp_out.name)
 
            raise
 
    if args.output_link_path.is_symlink():
 
        args.output_link_path.unlink()
 
    args.output_link_path.symlink_to(args.output_file_path.name)
 

	
 
def main(arglist=None, stdout=sys.stdout, stderr=sys.stderr):
0 comments (0 inline, 0 general)