Permalink
Browse files

working on it

  • Loading branch information...
mortenvp committed May 15, 2018
1 parent 12e9804 commit 3cdd081ddff86bb19b1c022e20004f8fcff7a0a6
@@ -1,90 +1,77 @@
from . import command
import git_sphinx_build.commandline
import git_sphinx_build.repository
import git_sphinx_build.git
from . import git_url_parser
from . import cache
from . import run_error
from . import sphinx
from . import sphinx_config
from . import sphinx_environment
from . import virtualenv
from . import tasks
from . import build_info
import click
import tempfile
import hashlib
import os
import logging
import json
import git_sphinx_build.factory
import git_sphinx_build.build_info
@click.command()
@click.argument('repository')
@click.option('--clone_path')
@click.option('--build_path')
@click.option('--data_path')
@click.option('--output_path')
#@click.option('--remote_only')
def cli(repository, clone_path, build_path):
def cli(repository, data_path, output_path):
logging.basicConfig(filename='git_sphinx_build.log', level=logging.DEBUG)
log = logging.getLogger(__name__)
log = logging.getLogger('git_sphinx_build.main')
git = git_sphinx_build.git.GitRun(git_binary='git', runner=command.run)
parser = git_url_parser.GitUrlParser()
# Resolve the repository
factory = git_sphinx_build.factory.resolve_factory(
data_path=data_path)
venv = virtualenv.VirtualEnv.from_git(
git=git, clone_path=os.path.join(clone_path, 'local-virtualenv'),
log=log)
git_repository = factory.build(name='git_repository')
git_repository.clone(repository=repository)
venv = virtualenv.NameToPathAdapter(
virtualenv=venv,
virtualenv_root_path=os.path.join(clone_path, 'build_environements'))
# Instantiate the cache
cache_factory = git_sphinx_build.factory.cache_factory(
data_path=data_path, unique_name=git_repository.unique_name)
environment = sphinx_environment.SphinxEnvironment(
prompt=git_sphinx_build.commandline.Prompt(), virtualenv=venv)
cache = cache_factory.build(name='cache')
config = sphinx_config.SphinxConfig()
# Build the documentation
factory = git_sphinx_build.factory.build_factory(
data_path=data_path, output_path=output_path,
git_repository=git_repository, cache=cache)
spnx = sphinx.Sphinx(sphinx_config=config,
sphinx_environment=environment,
prompt=git_sphinx_build.commandline.Prompt())
versions = {
'output_path': output_path,
'builds': []
}
repo = git_sphinx_build.repository.Repository(
git=git, git_url_parser=parser, log="ok")
with cache:
repo.clone(repository=repository, clone_path=clone_path)
task_generator = factory.build(name='task_generator')
with cache.Cache(cache_path=build_path, unique_name=repo.unique_name) as cas:
tasks = task_generator.tasks()
workingtree_generator = tasks.WorkingtreeGenerator(
repository=repo,
build_path=build_path, sphinx=spnx)
for task in tasks:
git_branch_generator = tasks.GitBranchGenerator(
repository=repo,
build_path=build_path, sphinx=spnx, git=git, cache=cas)
try:
build_info = git_sphinx_build.build_info.BuildInfo()
task.run(build_info=build_info)
# git_tag_generator = tasks.GitTagGenerator(
# repository=repo,
# build_path=build_path, sphinx=sphinx, git=git, cache=cas)
version = {
'slug': build_info.slug,
'type': build_info.type,
'path': build_info.output_path
}
task_generator = tasks.TaskFactory()
task_generator.add_generator(generator=workingtree_generator)
task_generator.add_generator(generator=git_branch_generator)
# task_generator.add_generator(generator=git_tag_generator)
versions['builds'].append(version)
tsk = task_generator.tasks()
except RuntimeError as re:
log.debug(re)
for task in tsk:
with open('versions.json', 'w') as versions_json:
try:
bi = build_info.BuildInfo()
result = task.run(build_info=bi)
print(bi)
json.dump(versions, versions_json, indent=2, sort_keys=True,
separators=(',', ': '))
except RuntimeError as re:
log.debug(re)
# If needed push the docs
if __name__ == "__main__":
@@ -62,165 +62,4 @@ def update(self, sha1, path):
self.cache[sha1] = path
class SphinxVirtualEnv(object):
def __init__(self, runner):
pass
class WorkingtreeTask(object):
def __init__(self, workingtree_path, build_path, sphinx):
self.workingtree_path = workingtree_path
self.build_path = build_path
self.sphinx = sphinx
def run():
output_path = os.path.join(self.build_path, 'workingtree')
self.sphinx.build(source_path=self.workingtree_path,
output_path=output_path, cwd=self.build_path)
return {'type': 'workingtree', 'slug': 'workingtree',
'path': output_path}
class WorkingtreeGenerator(object):
def __init__(self, repository, build_path, sphinx):
self.repository = repository
self.build_path = build_path
self.sphinx = sphinx
def tasks(self):
if self.repository.workingtree_path:
task = WorkingtreeTask(
workingtree_path=self.repository.workingtree_path,
build_path=self.build_path,
sphinx=self.sphinx)
return [task]
else:
return []
class GitTask(object):
def __init__(self, checkout_type, checkout, repository_path, build_path,
sphinx, git, cache):
self.checkout_type = checkout_type
self.checkout = checkout
self.repository_path = repository_path
self.build_path = build_path
self.sphinx = sphinx
self.git = git
self.cache = cache
def run(self):
cwd = self.repository_path
# https://stackoverflow.com/a/8888015/1717320
self.git.reset(branch=self.checkout, hard=True, cwd=cwd)
output_path = os.path.join(
self.build_path, self.checkout_type, self.checkout)
sha1 = self.git.current_commit(cwd=cwd)
if self.cache.match(sha1=sha1):
path = self.cache.path(sha1=sha1)
if path != output_path:
shutil.copytree(src=path, dst=output_path)
else:
self.sphinx.build(source_path=self.repository_path,
output_path=output_path, cwd=self.build_path)
self.cache.update(sha1=sha1, path=output_path)
return {'type': self.checkout_type, 'slug': self.checkout,
'path': output_path}
class GitBranchGenerator(object):
def __init__(self, repository, build_path,
sphinx, git, cache):
self.repository = repository
self.build_path = build_path
self.sphinx = sphinx
self.git = git
self.cache = cache
def tasks(self):
tasks = []
for branch in self.repository.branches():
task = GitTask(checkout_type='branch', checkout=branch,
repository_path=self.repository.repository_path,
build_path=self.build_path, sphinx=self.sphinx,
git=self.git, cache=self.cache)
tasks.append(task)
return tasks
class GitTagGenerator(object):
def __init__(self, repository, build_path,
sphinx, git, cache):
self.repository = repository
self.build_path = build_path
self.sphinx = sphinx
self.git = git
self.cache = cache
def tasks(self):
tasks = []
for tag in self.repository.tags():
task = GitTask(checkout_type='tag', checkout=tag,
repository_path=self.repository.repository_path,
build_path=self.build_path, sphinx=self.sphinx,
git=self.git, cache=self.cache)
tasks.append(task)
return tasks
class TaskFactory(object):
def __init__(self):
self.generators = []
def add_generator(self, generator):
self.generators.append(generator)
def tasks(self):
tasks = []
for generator in self.generators:
generator_tasks = generator.tasks()
tasks += generator_tasks
return tasks
#
Oops, something went wrong.

0 comments on commit 3cdd081

Please sign in to comment.