Exemplo n.º 1
0
def main():
    DEFAULT_GIT_REPO = '[email protected]:madebr/rescueteam.git'

    parser = argparse.ArgumentParser()
    parser.add_argument('-i', type=Path, dest='src', metavar='PATH', help='Input, path of Lektor project')
    parser.add_argument('-o', type=Path, dest='dst', metavar='PATH', help='Output, path of website')
    parser.add_argument('-f', type=str, dest='build_flags', nargs=argparse.ZERO_OR_MORE, default=None, metavar='VAR', help='Build flag(s)')
    parser.add_argument('-r', type=str, dest='repo', default=DEFAULT_GIT_REPO, metavar='REPO', help='Remote git repository (default:{})'.format(DEFAULT_GIT_REPO))
    parser.add_argument('action', choices=('build-init', 'build', 'rebuild', 'build-commit', 'build-push', ), help='Action to perform')
    ns = parser.parse_args()
    if ns.src is None:
        ns.src = Path(__file__).absolute().parent / 'lektor'
    if ns.dst is None:
        ns.dst = Path(__file__).absolute().parent / 'build'
    if ns.build_flags is None:
        ns.build_flags = [
            # 'minify',
            'webpack',
        ]

    ns.dst.mkdir(exist_ok=True)

    b = Builder(ns.src, ns.dst, ns.build_flags)
    if ns.action == 'build-init':
        r = git.Repo.init(ns.dst)
        github = r.create_remote('github', ns.repo)
        github.fetch()

        gh_pages = r.create_head('gh-pages', github.refs['gh-pages'])
        gh_pages.set_tracking_branch(github.refs['gh-pages'])
        gh_pages.checkout()
        success = True
    elif ns.action == 'build':
        r = git.Repo(str(ns.dst))
        gh_pages = r.branches['gh-pages']
        gh_pages.checkout()
        success = b.build(tiny=True)
    elif ns.action == 'rebuild':
        r = git.Repo(str(ns.dst))
        gh_pages = r.branches['gh-pages']
        gh_pages.checkout()
        b.clean()
        success = b.build()
    elif ns.action == 'build-commit':
        r = git.Repo(str(ns.dst))
        # gh_pages = r.branches['gh-pages']
        # gh_pages.checkout()
        r.git.checkout('gh-pages')
        r.git.add(all=True)
        m = time.strftime('Commit on %Y-%m-%d %H:%M:%S')
        r.index.commit(m)
        success = True
    elif ns.action == 'build-push':
        r = git.Repo(str(ns.dst))
        gh_pages = r.branches['gh-pages']
        gh_pages.checkout()

        github = r.remotes.github
        github.push()
        success = True
    else:
        success = False

    if not success:
        print('ERROR!', file=sys.stderr)

    sys.exit(0 if success else 1)