def jekyll_build(checkout_path):
    '''
    '''
    checkout_lock = checkout_path + '.jekyll-lock'
    jekyll_path = join(checkout_path, '_site')
    built_hash_file = checkout_path + '.built-hash'
    hash_file = checkout_path + '.commit-hash'

    if exists(jekyll_path) and is_fresh(jekyll_path):
        return jekyll_path
    
    with locked_file(checkout_lock):
        do_build = True
    
        if exists(built_hash_file):
            built_hash = open(built_hash_file).read().strip()
            commit_hash = open(hash_file).read().strip()
        
            if built_hash == commit_hash:
                jlogger.debug('Skipping build to ' + jekyll_path)
                do_build = False
    
        if do_build:
            jlogger.info('Building jekyll ' + jekyll_path)
            run_cmd(('jekyll', 'build'), checkout_path)
        
            if exists(hash_file):
                copyfile(hash_file, built_hash_file)
    
        touch(jekyll_path)

    return jekyll_path
Exemple #2
0
def jekyll_build(checkout_path):
    '''
    '''
    checkout_lock = checkout_path + '.jekyll-lock'
    jekyll_path = join(checkout_path, '_site')
    built_hash_file = checkout_path + '.built-hash'
    hash_file = checkout_path + '.commit-hash'

    if exists(jekyll_path) and is_fresh(jekyll_path):
        return jekyll_path

    with locked_file(checkout_lock):
        do_build = True

        if exists(built_hash_file):
            built_hash = open(built_hash_file).read().strip()
            commit_hash = open(hash_file).read().strip()

            if built_hash == commit_hash:
                jlogger.debug('Skipping build to ' + jekyll_path)
                do_build = False

        if do_build:
            jlogger.info('Building jekyll ' + jekyll_path)
            run_cmd(('jekyll', 'build'), checkout_path)

            if exists(hash_file):
                copyfile(hash_file, built_hash_file)

        touch(jekyll_path)

    return jekyll_path
def prepare_git_checkout(account, repo, ref, token):
    '''
    '''
    repo_href = 'https://github.com/%s/%s.git' % (account, repo)
    repo_path = join(getcwd(), 'repos/%s-%s' % (account, repo))
    repo_refs = 'https://api.github.com/repos/%s/%s/branches' % (account, repo)
    repo_sha = 'https://api.github.com/repos/%s/%s/commits/%s' % (account, repo, ref)
    checkout_path = join(getcwd(), 'checkouts/%s-%s-%s' % (account, repo, ref))
    checkout_lock = checkout_path + '.git-lock'
    
    if exists(checkout_path) and is_fresh(checkout_path):
        return checkout_path
    
    ref_check = OAuth2Session(github_client_id, token=token).get(repo_refs)
    
    if ref_check.status_code == 401:
        # Github wants authentication.
        raise PrivateRepoException()
    
    elif ref_check.status_code == 404:
        # This repository might not exist at all?
        raise MissingRepoException()
    
    branches = dict([(b['name'], b['commit']['sha']) for b in ref_check.json()])
    ref_sha = branches.get(ref, None)

    if ref_sha is None:
        # The ref is not a branch, but it may be a sha.
        sha_check = OAuth2Session(github_client_id, token=token).get(repo_sha)
        
        if sha_check.status_code == 200:
            # The ref must be a sha hash.
            ref_sha = sha_check.json()['sha']
        else:
            # The repository exists, but the branch does not?
            raise MissingRefException()
    
    if token:
        jlogger.debug('Adding Github credentials to environment')
        environ.update(dict(GIT_ASKPASS=join(dirname(__file__), 'askpass.py')))
        environ.update(dict(GIT_USERNAME=token['access_token'], GIT_PASSWORD=''))
    
    else:
        jlogger.debug('Clearing Github credentials from environment')
        environ.update(dict(GIT_ASKPASS='', GIT_USERNAME='', GIT_PASSWORD=''))

    with locked_file(checkout_lock):
        if not exists(repo_path):
            git_clone(repo_href, repo_path)
        else:
            git_fetch(repo_path, ref, ref_sha)

        git_checkout(repo_path, checkout_path, ref)
    
    # Make sure these are gone before we return.
    environ.update(dict(GIT_ASKPASS='', GIT_USERNAME='', GIT_PASSWORD=''))
    
    return checkout_path
def prepare_git_checkout(account, repo, ref, token):
    '''
    '''
    repo_href = 'https://github.com/%s/%s.git' % (account, repo)
    repo_path = join(getcwd(), 'repos/%s-%s' % (account, repo))
    repo_refs = 'https://api.github.com/repos/%s/%s/branches' % (account, repo)
    repo_sha = 'https://api.github.com/repos/%s/%s/commits/%s' % (account, repo, ref)
    checkout_path = join(getcwd(), 'checkouts/%s-%s-%s' % (account, repo, ref))
    checkout_lock = checkout_path + '.git-lock'
    
    if exists(checkout_path) and is_fresh(checkout_path):
        return checkout_path
    
    ref_check = OAuth2Session(github_client_id, token=token).get(repo_refs)
    
    if ref_check.status_code == 401:
        # Github wants authentication.
        raise PrivateRepoException()
    
    elif ref_check.status_code == 404:
        # This repository might not exist at all?
        raise MissingRepoException()
    
    branches = dict([(b['name'], b['commit']['sha']) for b in ref_check.json()])
    ref_sha = branches.get(ref, None)

    if ref_sha is None:
        # The ref is not a branch, but it may be a sha.
        sha_check = OAuth2Session(github_client_id, token=token).get(repo_sha)
        
        if sha_check.status_code == 200:
            # The ref must be a sha hash.
            ref_sha = sha_check.json()['sha']
        else:
            # The repository exists, but the branch does not?
            raise MissingRefException()
    
    if token:
        jlogger.debug('Adding Github credentials to environment')
        environ.update(dict(GIT_ASKPASS=join(dirname(__file__), 'askpass.py')))
        environ.update(dict(GIT_USERNAME=token['access_token'], GIT_PASSWORD=''))
    
    else:
        jlogger.debug('Clearing Github credentials from environment')
        environ.update(dict(GIT_ASKPASS='', GIT_USERNAME='', GIT_PASSWORD=''))

    with locked_file(checkout_lock):
        if not exists(repo_path):
            git_clone(repo_href, repo_path)
        else:
            git_fetch(repo_path, ref, ref_sha)

        git_checkout(repo_path, checkout_path, ref)
    
    # Make sure these are gone before we return.
    environ.update(dict(GIT_ASKPASS='', GIT_USERNAME='', GIT_PASSWORD=''))
    
    return checkout_path