-
Notifications
You must be signed in to change notification settings - Fork 4
/
cookiecutter-update.py
executable file
·84 lines (65 loc) · 2.79 KB
/
cookiecutter-update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016, Abdó Roig-Maranges <[email protected]>
# All rights reserved.
#
# This file is part of 'LaTeX Base Cookiecutter'.
import os
import sys
import json
import shutil
import subprocess
import json
from cookiecutter.main import cookiecutter
class TemporaryWorkdir():
"""Context Manager for a temporary working directory of a branch in a git repo"""
def __init__(self, path, repo, branch='master'):
self.repo = repo
self.path = path
self.branch = branch
def __enter__(self):
if not os.path.exists(os.path.join(self.repo, ".git")):
raise Exception("Not a git repository: %s" % self.repo)
if os.path.exists(self.path):
raise Exception("Temporary directory already exists: %s" % self.path)
os.makedirs(self.path)
subprocess.run(["git", "worktree", "add", "--no-checkout", self.path, self.branch],
cwd=self.repo)
def __exit__(self, type, value, traceback):
shutil.rmtree(self.path)
subprocess.run(["git", "worktree", "prune"], cwd=self.repo)
def update_template(template_url, root, branch):
"""Update template branch from a template url"""
tmpdir = os.path.join(root, ".git", "cookiecutter")
project_slug = os.path.basename(root)
config_file = os.path.join(root, ".cookiecutter.json")
tmp_workdir = os.path.join(tmpdir, project_slug)
# read context from file.
context = None
if os.path.exists(config_file):
with open(config_file, 'r') as fd:
context = json.loads(fd.read())
context['project_slug'] = project_slug
# create a template branch if necessary
if subprocess.run(["git", "rev-parse", "-q", "--verify", branch], cwd=root).returncode != 0:
firstref = subprocess.run(["git", "rev-list", "--max-parents=0", "HEAD"],
cwd=root,
stdout=subprocess.PIPE,
universal_newlines=True).stdout.strip()
subprocess.run(["git", "branch", branch, firstref])
with TemporaryWorkdir(tmp_workdir, repo=root, branch=branch):
# update the template
cookiecutter(template_url,
no_input=(context != None),
extra_context=context,
overwrite_if_exists=True,
output_dir=tmpdir)
# commit to template branch
subprocess.run(["git", "add", "-A", "."], cwd=tmp_workdir)
subprocess.run(["git", "commit", "-m", "Update template"],
cwd=tmp_workdir)
if __name__ == '__main__':
with open(sys.argv[1], 'r') as fd:
context = json.load(fd)
update_template(context['_template'], os.getcwd(), branch=sys.argv[2])