2017-08-30 05:52:24 +00:00
|
|
|
"""TODO"""
|
2017-08-30 06:27:09 +00:00
|
|
|
import os
|
2017-08-31 06:19:29 +00:00
|
|
|
import re
|
2017-08-30 05:52:24 +00:00
|
|
|
from os.path import expanduser
|
2017-08-30 06:27:09 +00:00
|
|
|
from os.path import exists as path_exists
|
|
|
|
from os.path import join as path_join
|
2017-08-29 07:33:03 +00:00
|
|
|
|
|
|
|
import click
|
2017-08-30 05:52:24 +00:00
|
|
|
import pygit2
|
2017-08-30 07:07:33 +00:00
|
|
|
from click import ClickException
|
2017-08-30 05:52:24 +00:00
|
|
|
from click.types import Path
|
2017-09-01 04:40:53 +00:00
|
|
|
|
|
|
|
from .compilation import (
|
|
|
|
compile_wiki
|
|
|
|
)
|
2017-08-30 05:52:24 +00:00
|
|
|
|
|
|
|
# TODO support reading from env
|
|
|
|
SITE_NAME = 'tilde.town'
|
|
|
|
PUBLISH_PATH = '/var/www/{site_name}/wiki'.format(site_name=SITE_NAME)
|
|
|
|
PREVIEW_PATH = expanduser('~/public_html/wiki')
|
|
|
|
LOCAL_REPOSITORY_PATH = expanduser('~/wiki')
|
|
|
|
REPOSITORY_PATH = '/wiki'
|
|
|
|
|
2017-08-30 07:07:33 +00:00
|
|
|
DEFAULT_PATH_KWARGS = dict(
|
2017-08-30 05:52:24 +00:00
|
|
|
exists=True,
|
|
|
|
writable=True,
|
|
|
|
readable=True,
|
|
|
|
file_okay=False,
|
2017-08-30 06:27:09 +00:00
|
|
|
dir_okay=True)
|
|
|
|
|
|
|
|
class GitRepo(Path):
|
|
|
|
name = 'git repository'
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
path = super().convert(value, param, ctx)
|
|
|
|
if not path_exists(path_join(path, '.git')):
|
|
|
|
self.fail('No .git directory found in {}'.format(path))
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
class WikiRepo(GitRepo):
|
|
|
|
name = 'wiki repository'
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
|
|
|
path = super().convert(value, param, ctx)
|
|
|
|
|
2017-08-31 06:33:39 +00:00
|
|
|
# TODO check for header.md and footer.md
|
2017-08-30 06:27:09 +00:00
|
|
|
if not path_exists(path_join(path, 'src/articles')):
|
|
|
|
self.fail(
|
|
|
|
'{} does not appear to be a wiki repository; missing src/articles.'.format(
|
|
|
|
path))
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
2017-08-30 05:52:24 +00:00
|
|
|
|
|
|
|
class Config:
|
|
|
|
def __init__(self):
|
|
|
|
self.site_name = SITE_NAME
|
|
|
|
self.publish_path = PUBLISH_PATH
|
|
|
|
self.preview_path = PREVIEW_PATH
|
|
|
|
self.local_repo_path = LOCAL_REPOSITORY_PATH
|
|
|
|
self.repo_path = REPOSITORY_PATH
|
|
|
|
|
|
|
|
pass_config = click.make_pass_decorator(Config, ensure=True)
|
|
|
|
|
|
|
|
@click.group()
|
|
|
|
@click.option('--site-name', default=SITE_NAME, help='The root domain of the wiki.')
|
|
|
|
@click.option('--publish-path',
|
|
|
|
default=PUBLISH_PATH,
|
|
|
|
help='System level path to wiki for publishing.',
|
2017-08-30 07:07:33 +00:00
|
|
|
type=Path(**DEFAULT_PATH_KWARGS))
|
2017-08-30 05:52:24 +00:00
|
|
|
@click.option('--repo-path',
|
|
|
|
default=REPOSITORY_PATH,
|
|
|
|
help='Path to your clone of the shared git repository.',
|
2017-08-30 07:07:33 +00:00
|
|
|
type=WikiRepo(**DEFAULT_PATH_KWARGS))
|
2017-08-30 05:52:24 +00:00
|
|
|
@pass_config
|
2017-08-30 07:07:33 +00:00
|
|
|
def main(config, site_name, publish_path, repo_path):
|
2017-08-30 06:27:09 +00:00
|
|
|
# TODO click does not appear to call expanduser on things. it'd be nice to
|
|
|
|
# opt into that with the Path type. Should click be patched? Or should we
|
|
|
|
# use a custom Path type?
|
2017-08-30 05:52:24 +00:00
|
|
|
config.site_name = site_name
|
|
|
|
config.publish_path = publish_path
|
|
|
|
config.repo_path = repo_path
|
2017-08-29 07:33:03 +00:00
|
|
|
|
2017-08-30 05:52:24 +00:00
|
|
|
@main.command()
|
2017-08-30 07:07:33 +00:00
|
|
|
@click.option('--local-repo-path',
|
|
|
|
default=LOCAL_REPOSITORY_PATH,
|
|
|
|
help='Path to shared wiki git repository.',
|
|
|
|
type=Path(file_okay=False))
|
|
|
|
@click.option('--preview-path',
|
|
|
|
default=PREVIEW_PATH,
|
|
|
|
help='Local path to wiki for previewing.',
|
|
|
|
type=Path(file_okay=False))
|
2017-08-30 05:52:24 +00:00
|
|
|
@pass_config
|
2017-08-30 07:07:33 +00:00
|
|
|
def init(config, local_repo_path, preview_path):
|
|
|
|
"""
|
|
|
|
This command, `wiki init`, does the following:
|
|
|
|
- clones REPOSITORY_PATH to LOCAL_REPOSITORY_PATH
|
|
|
|
- creates PREVIEW_PATH
|
|
|
|
- calls the preview command
|
|
|
|
"""
|
|
|
|
if path_exists(path_join(local_repo_path)):
|
|
|
|
raise ClickException(
|
|
|
|
'{} already exists. Have you already run wiki init?'.format(
|
|
|
|
local_repo_path))
|
|
|
|
|
|
|
|
if path_exists(path_join(preview_path)):
|
|
|
|
raise ClickException(
|
|
|
|
'{} already exists. Have you already run wiki init?'.format(
|
|
|
|
preview_path))
|
2017-08-31 06:19:29 +00:00
|
|
|
click.echo('Cloning {} to {}...'.format(config.repo_path, local_repo_path))
|
|
|
|
pygit2.clone_repository(config.repo_path, local_repo_path)
|
|
|
|
click.echo('Creating {}...'.format(preview_path))
|
|
|
|
os.makedirs(preview_path)
|
2017-09-01 05:09:33 +00:00
|
|
|
click.echo('Compiling wiki preview for the first time...')
|
|
|
|
_preview(config, preview_path, local_repo_path)
|
2017-08-31 06:19:29 +00:00
|
|
|
click.echo('~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~')
|
2017-09-01 05:09:33 +00:00
|
|
|
click.echo("Congrats, you are ready to contribute to {}'s wiki!".format(
|
|
|
|
config.site_name
|
|
|
|
))
|
2017-08-30 05:52:24 +00:00
|
|
|
|
|
|
|
@main.command()
|
2017-08-30 07:07:33 +00:00
|
|
|
@click.option('--local-repo-path',
|
|
|
|
default=LOCAL_REPOSITORY_PATH,
|
|
|
|
help='Path to shared wiki git repository.',
|
|
|
|
type=WikiRepo(**DEFAULT_PATH_KWARGS))
|
|
|
|
@click.option('--preview-path',
|
|
|
|
default=PREVIEW_PATH,
|
|
|
|
help='Local path to wiki for previewing.',
|
|
|
|
type=Path(**DEFAULT_PATH_KWARGS))
|
2017-08-30 05:52:24 +00:00
|
|
|
@pass_config
|
2017-08-31 06:19:29 +00:00
|
|
|
def preview(config, preview_path, local_repo_path):
|
|
|
|
preview_prompt = 'This will wipe everything at {}. Proceed?'
|
|
|
|
click.confirm(
|
|
|
|
preview_prompt.format(preview_path),
|
|
|
|
abort=True)
|
2017-09-01 05:09:33 +00:00
|
|
|
_preview(config, preview_path, local_repo_path)
|
|
|
|
|
|
|
|
def _preview(config, preview_path, local_repo_path):
|
2017-08-31 06:19:29 +00:00
|
|
|
compile_wiki(local_repo_path, preview_path)
|
2017-09-01 05:09:33 +00:00
|
|
|
click.echo('Your wiki preview is ready! navigate to ~{}/wiki'.format(
|
|
|
|
os.getlogin()))
|
2017-08-30 05:52:24 +00:00
|
|
|
|
|
|
|
@main.command()
|
2017-08-30 07:07:33 +00:00
|
|
|
@click.option('--local-repo-path',
|
|
|
|
default=LOCAL_REPOSITORY_PATH,
|
|
|
|
help='Path to shared wiki git repository.',
|
|
|
|
type=WikiRepo(**DEFAULT_PATH_KWARGS))
|
2017-08-30 05:52:24 +00:00
|
|
|
@pass_config
|
2017-08-30 07:07:33 +00:00
|
|
|
def publish(config, local_repo_path):
|
2017-08-30 05:52:24 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@main.command()
|
|
|
|
@pass_config
|
|
|
|
def get(config):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@main.command()
|
2017-08-30 07:07:33 +00:00
|
|
|
@click.option('--local-repo-path',
|
|
|
|
default=LOCAL_REPOSITORY_PATH,
|
|
|
|
help='Path to shared wiki git repository.',
|
|
|
|
type=WikiRepo(**DEFAULT_PATH_KWARGS))
|
2017-08-30 05:52:24 +00:00
|
|
|
@pass_config
|
2017-08-30 07:07:33 +00:00
|
|
|
def reset(config, local_repo_path):
|
2017-08-30 05:52:24 +00:00
|
|
|
raise NotImplementedError()
|