Permalink
Browse files

Working on it

  • Loading branch information...
mortenvp committed May 14, 2018
1 parent ecd144e commit 12e9804fdbcb78464a36850d3af3bc0b1e0fd628
@@ -1,2 +0,0 @@
from command import run
from git_run import GitRun
@@ -1,6 +1,7 @@
from . import command
from . import commandline
from . import git_run
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
@@ -18,67 +19,6 @@
import logging
class Repository(object):
def __init__(self, git, git_url_parser, log):
self.git = git
self.git_url_parser = git_url_parser
self.log = log
self.git_info = None
self.git_url = None
# Path to the local working tree (if one exists)
self.workingtree_path = None
# Path to where this repository is cloned
self.repository_path = None
# A unique name computed based on the git URL
self.unique_name = None
def clone(self, repository, clone_path):
assert repository
assert os.path.isdir(clone_path)
clone_path = os.path.abspath(os.path.expanduser(clone_path))
# Get the URL to the repository
if os.path.isdir(repository):
self.workingtree_path = repository
git_url = self.git.remote_origin_url(cwd=repository)
else:
git_url = repository
# Compute the clone path
git_info = self.git_url_parser.parse(url=git_url)
url_hash = hashlib.sha1(git_url.encode('utf-8')).hexdigest()[:6]
self.unique_name = git_info.name + '-' + url_hash
self.repository_path = os.path.join(clone_path, self.unique_name)
# Get the updates
if os.path.isdir(self.repository_path):
self.git.fetch(cwd=self.repository_path)
else:
self.git.clone(repository=git_url,
directory=self.repository_path, cwd=clone_path)
def tags(self):
return self.git.tags(cwd=self.repository_path)
def branches(self):
_, remote = self.git.branch(cwd=self.repository_path, remote=True)
# Remote branches look like origin/master, origin/some_branch etc.
# We flatten this.
return remote
@click.command()
@click.argument('repository')
@click.option('--clone_path')
@@ -90,7 +30,7 @@ def cli(repository, clone_path, build_path):
log = logging.getLogger(__name__)
git = git_run.GitRun(git_binary='git', runner=command.run)
git = git_sphinx_build.git.GitRun(git_binary='git', runner=command.run)
parser = git_url_parser.GitUrlParser()
venv = virtualenv.VirtualEnv.from_git(
@@ -102,15 +42,16 @@ def cli(repository, clone_path, build_path):
virtualenv_root_path=os.path.join(clone_path, 'build_environements'))
environment = sphinx_environment.SphinxEnvironment(
prompt=commandline.Prompt(), virtualenv=venv)
prompt=git_sphinx_build.commandline.Prompt(), virtualenv=venv)
config = sphinx_config.SphinxConfig()
spnx = sphinx.Sphinx(sphinx_config=config,
sphinx_environment=environment,
prompt=commandline.Prompt())
prompt=git_sphinx_build.commandline.Prompt())
repo = Repository(git=git, git_url_parser=parser, log="ok")
repo = git_sphinx_build.repository.Repository(
git=git, git_url_parser=parser, log="ok")
repo.clone(repository=repository, clone_path=clone_path)
@@ -0,0 +1,26 @@
#! /usr/bin/env python
# encoding: utf-8
import os
import git_sphinx_build.info
class ApplicationInfo(git_sphinx_build.info.Info):
""" Stores information about a specific build.
Information is added by different steps in the build process in a
write once fashion. Such that we avoid accidental overwrites.
"""
valid_keys = {
"data_path": "",
"clone_path": "",
"virtualenv_root_path": ""
}
def _check_key(self, attribute):
if attribute not in ApplicationInfo.valid_keys.keys():
raise AttributeError("Invalid attribute key {} valid {}".format(
attribute, ApplicationInfo.valid_keys.keys()))
@@ -3,8 +3,10 @@
import os
import git_sphinx_build.info
class BuildInfo(object):
class BuildInfo(git_sphinx_build.info.Info):
""" Stores information about a specific build.
Information is added by different steps in the build process in a
@@ -29,44 +31,6 @@ class BuildInfo(object):
"type": "The type of build"
}
def __init__(self):
# Because we override __setattr__ we have to call the
# __setattr__ in the base to avoid recursion
object.__setattr__(self, 'info', {})
def __getattr__(self, attribute):
""" Return the value corresponding to the attribute.
:param attribute: The name of the attribute to return as a string.
:return: The attribute value, if the attribute does not exist
return None
"""
self._check_key(attribute=attribute)
if attribute in self.info:
return self.info[attribute]
else:
raise AttributeError("No key {} in BuildInfo".format(attribute))
def __setattr__(self, attribute, value):
""" Sets a dependency attribute.
:param attribute: The name of the attribute as a string
:param value: The value of the attribute
"""
self._check_key(attribute=attribute)
if attribute in self.info:
raise AttributeError("Attribute {} read-only.".format(attribute))
else:
self.info[attribute] = value
def __contains__(self, attribute):
""" Checks if the attribute is available.
:return: True if the attribute is available otherwise False
"""
self._check_key(attribute=attribute)
return (attribute in self.info)
def _check_key(self, attribute):
if attribute not in BuildInfo.valid_keys.keys():
raise AttributeError("Invalid attribute key {} valid {}".format(
@@ -19,7 +19,7 @@ def __init__(self, cache_path, unique_name):
"""
# Read the "cache" from json
filename = unique_name + '.json'
filename = 'cache-' + unique_name + '.json'
cache_path = os.path.abspath(os.path.expanduser(cache_path))
self.filepath = os.path.join(cache_path, filename)
@@ -60,3 +60,167 @@ def update(self, sha1, path):
assert os.path.isdir(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 12e9804

Please sign in to comment.