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-09-25 06:08:58 +00:00
|
|
|
import subprocess
|
2017-08-29 07:33:03 +00:00
|
|
|
|
|
|
|
import click
|
2017-09-24 01:16:56 +00:00
|
|
|
from click import ClickException, Abort
|
2017-08-30 05:52:24 +00:00
|
|
|
from click.types import Path
|
2017-09-12 05:34:53 +00:00
|
|
|
from shutil import rmtree
|
|
|
|
|
2017-09-24 01:34:55 +00:00
|
|
|
from .click_types import WikiRepo
|
|
|
|
from .compilation import compile_wiki
|
2017-09-12 05:34:53 +00:00
|
|
|
from . import git_wrapper as git
|
2017-09-02 23:19:34 +00:00
|
|
|
|
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-09-12 05:36:13 +00:00
|
|
|
WIPE_PROMPT = 'This will wipe everything at {}. Proceed?'
|
|
|
|
LOCK_PATH = '/tmp/tildewiki.lock'
|
2017-08-30 05:52:24 +00:00
|
|
|
|
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)
|
|
|
|
|
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
|
2017-09-02 23:19:34 +00:00
|
|
|
self.author_name = os.environ.get('LOGNAME')
|
|
|
|
|
|
|
|
@property
|
|
|
|
def author_email(self):
|
|
|
|
return '{}@{}'.format(self.author_name, self.site_name)
|
2017-08-30 05:52:24 +00:00
|
|
|
|
|
|
|
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,
|
2017-09-02 23:19:34 +00:00
|
|
|
help='Path to the shared wiki 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-09-02 23:19:34 +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
|
|
|
|
"""
|
2017-09-12 05:36:13 +00:00
|
|
|
if os.path.exists(os.path.join(local_repo_path)):
|
2017-08-30 07:07:33 +00:00
|
|
|
raise ClickException(
|
|
|
|
'{} already exists. Have you already run wiki init?'.format(
|
|
|
|
local_repo_path))
|
|
|
|
|
2017-09-12 05:36:13 +00:00
|
|
|
if os.path.exists(os.path.join(preview_path)):
|
2017-08-30 07:07:33 +00:00
|
|
|
raise ClickException(
|
|
|
|
'{} already exists. Have you already run wiki init?'.format(
|
|
|
|
preview_path))
|
2017-09-02 23:19:34 +00:00
|
|
|
|
2017-08-31 06:19:29 +00:00
|
|
|
click.echo('Cloning {} to {}...'.format(config.repo_path, local_repo_path))
|
2017-09-12 05:36:13 +00:00
|
|
|
git.create_repo(
|
2017-09-02 23:19:34 +00:00
|
|
|
config.repo_path,
|
|
|
|
config.local_repo_path,
|
|
|
|
config.author_name,
|
|
|
|
config.author_email
|
|
|
|
)
|
|
|
|
|
2017-08-31 06:19:29 +00:00
|
|
|
click.echo('Creating {}...'.format(preview_path))
|
|
|
|
os.makedirs(preview_path)
|
2017-09-02 23:19:34 +00:00
|
|
|
|
2017-09-01 05:09:33 +00:00
|
|
|
click.echo('Compiling wiki preview for the first time...')
|
2017-09-24 01:16:56 +00:00
|
|
|
_preview(preview_path, local_repo_path)
|
2017-09-02 23:19:34 +00:00
|
|
|
|
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-09-02 23:19:34 +00:00
|
|
|
@click.option('--local-repo-path', default=LOCAL_REPOSITORY_PATH,
|
|
|
|
help='Path to local clone of wiki repository.', type=WikiRepo(**DEFAULT_PATH_KWARGS))
|
2017-08-30 07:07:33 +00:00
|
|
|
@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):
|
|
|
|
click.confirm(
|
2017-09-12 05:36:13 +00:00
|
|
|
WIPE_PROMPT.format(preview_path),
|
2017-08-31 06:19:29 +00:00
|
|
|
abort=True)
|
2017-09-24 01:16:56 +00:00
|
|
|
clear_directory(preview_path)
|
|
|
|
_preview(preview_path, local_repo_path)
|
2017-09-01 05:09:33 +00:00
|
|
|
|
2017-08-30 05:52:24 +00:00
|
|
|
@main.command()
|
2017-09-02 23:19:34 +00:00
|
|
|
@click.option('--local-repo-path', default=LOCAL_REPOSITORY_PATH,
|
|
|
|
help='Path to local clone of wiki 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-09-12 05:36:13 +00:00
|
|
|
if os.path.exists(LOCK_PATH):
|
|
|
|
raise ClickException('The wiki lock file already exists. Seems like someone else is compiling.')
|
|
|
|
|
|
|
|
rm_error_paths = []
|
|
|
|
onerror = lambda f,p,e: rm_error_paths.append(p)
|
|
|
|
error = None
|
|
|
|
lockf = open(LOCK_PATH, 'w')
|
|
|
|
try:
|
|
|
|
click.echo('Committing your changes locally...')
|
|
|
|
git.make_commit(local_repo_path, config.author_name, config.author_email)
|
|
|
|
git.pull_from_origin(local_repo_path)
|
|
|
|
|
|
|
|
click.echo('Pushing your changes...')
|
2017-10-03 05:37:19 +00:00
|
|
|
git.push_hard(local_repo_path, config.repo_path)
|
2017-09-12 05:36:13 +00:00
|
|
|
|
|
|
|
click.echo('Compiling wiki to {}'.format(config.publish_path))
|
|
|
|
click.confirm(WIPE_PROMPT.format(config.publish_path), abort=True)
|
2017-09-24 01:16:56 +00:00
|
|
|
clear_directory(config.publish_path)
|
2017-09-12 05:36:13 +00:00
|
|
|
compile_wiki(config.repo_path, config.publish_path)
|
2017-09-23 08:04:40 +00:00
|
|
|
except ClickException:
|
|
|
|
raise
|
2017-09-24 01:16:56 +00:00
|
|
|
except Abort:
|
|
|
|
raise
|
2017-09-12 05:36:13 +00:00
|
|
|
except Exception as e:
|
|
|
|
error = e
|
|
|
|
finally:
|
|
|
|
lockf.close()
|
|
|
|
try:
|
|
|
|
os.remove(LOCK_PATH)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if error is not None:
|
|
|
|
raise ClickException('Failed publishing wiki. Error: {}'.format(error))
|
2017-08-30 05:52:24 +00:00
|
|
|
|
|
|
|
@main.command()
|
2017-09-25 06:08:58 +00:00
|
|
|
@click.option('--preview', help='show pages from your local wiki', is_flag=True)
|
|
|
|
@click.option('--preview-path', default=PREVIEW_PATH,
|
|
|
|
help='Local path to wiki for previewing.', type=Path(file_okay=False))
|
|
|
|
@click.argument('path')
|
2017-08-30 05:52:24 +00:00
|
|
|
@pass_config
|
2017-09-25 06:08:58 +00:00
|
|
|
def get(config, preview, preview_path, path):
|
|
|
|
read_path = config.publish_path
|
|
|
|
if preview:
|
|
|
|
read_path = preview_path
|
|
|
|
|
2017-10-03 05:37:59 +00:00
|
|
|
path = os.path.join(read_path, path)
|
|
|
|
if os.path.exists(path)\
|
|
|
|
and os.path.isdir(path)\
|
|
|
|
and os.path.exists(os.path.join(path, 'index.html')):
|
|
|
|
path = os.path.join(path, 'index.html')
|
|
|
|
elif os.path.exists(path + '.html'):
|
|
|
|
path = path + '.html'
|
|
|
|
else:
|
|
|
|
raise ClickException("Couldn't find path {}".format(path))
|
2017-09-25 06:08:58 +00:00
|
|
|
|
|
|
|
subprocess.run(['sensible-browser', path])
|
2017-08-30 05:52:24 +00:00
|
|
|
|
2017-10-03 05:37:59 +00:00
|
|
|
# TODO sync command that:
|
|
|
|
# - offers to wipe any local changes or commit them
|
|
|
|
# - pulls from origin
|
|
|
|
# - pushes to origin
|
|
|
|
|
2017-08-30 05:52:24 +00:00
|
|
|
@main.command()
|
2017-09-02 23:19:34 +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-09-25 06:42:26 +00:00
|
|
|
click.confirm("This will overwrite any changes you've made locally. Proceed?", abort=True)
|
|
|
|
git.reset_from_origin(local_repo_path)
|
2017-09-02 23:19:34 +00:00
|
|
|
|
2017-09-24 01:16:56 +00:00
|
|
|
def _preview(preview_path, local_repo_path):
|
2017-09-02 23:19:34 +00:00
|
|
|
compile_wiki(local_repo_path, preview_path)
|
|
|
|
click.echo('Your wiki preview is ready! navigate to ~{}/wiki'.format(
|
|
|
|
os.environ.get('LOGNAME')))
|
2017-09-24 01:16:56 +00:00
|
|
|
|
|
|
|
def clear_directory(path:str) -> None:
|
|
|
|
"""Given a path to a directory, deletes everything in it. Use with
|
|
|
|
caution."""
|
|
|
|
if path in ['', '/', '~', '*']:
|
|
|
|
raise ValueError('"{}" is not a valid path for clearing'.format(path))
|
|
|
|
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
raise ValueError('{} is not a directory'.format(path))
|
|
|
|
|
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
for f in files:
|
|
|
|
os.unlink(os.path.join(root, f))
|
|
|
|
for d in dirs:
|
|
|
|
rmtree(os.path.join(root, d))
|
|
|
|
|