]>
Commit | Line | Data |
---|---|---|
639debb9 VB |
1 | from invoke import task |
2 | ||
3 | import os | |
4 | import sys | |
5 | import glob | |
6 | import hashlib | |
7 | import tempfile | |
8 | import shutil | |
9 | ||
10 | os.environ["PATH"] = os.path.expanduser('~/.virtualenvs/hyde/bin') \ | |
11 | + os.pathsep + os.environ["PATH"] | |
12 | ||
13 | ||
14 | def confirm(question, default=False): | |
15 | if default: | |
16 | suffix = "Y/n" | |
17 | else: | |
18 | suffix = "y/N" | |
19 | while True: | |
20 | response = input("{0} [{1}] ".format(question, suffix)) | |
21 | response = response.lower().strip() # Normalize | |
22 | # Default | |
23 | if not response: | |
24 | return default | |
25 | if response in ["y", "yes"]: | |
26 | return True | |
27 | if response in ["n", "no"]: | |
28 | return False | |
29 | err = "I didn't understand you. Please specify '(y)es' or '(n)o'." | |
30 | print(err, file=sys.stderr) | |
31 | ||
32 | ||
33 | @task | |
34 | def gen(c): | |
35 | """Generate dev content""" | |
36 | c.run('hyde -x gen') | |
37 | ||
38 | ||
39 | @task(post=[gen]) | |
40 | def regen(c): | |
41 | """Regenerate dev content""" | |
42 | c.run('rm -rf deploy') | |
43 | ||
44 | ||
45 | @task | |
46 | def serve(c): | |
47 | """Serve dev content""" | |
48 | c.run('hyde -x serve -a 0.0.0.0') | |
49 | ||
50 | ||
51 | @task | |
52 | def build(c): | |
53 | """Build production content""" | |
54 | # Generate the website from scratch | |
55 | c.run("rm -rf deploy") | |
56 | conf = "site-production.yaml" | |
3d1fe5a5 | 57 | c.run('hyde -x gen -c %s' % conf) |
639debb9 VB |
58 | |
59 | # Compute hash for media files | |
60 | with c.cd("deploy"): | |
61 | for p in ['media/js/*.js', | |
62 | 'media/css/*.css']: | |
63 | files = glob.glob("%s/%s" % (c.cwd, p)) | |
64 | for f in files: | |
65 | # Compute hash | |
66 | md5 = hashlib.md5() | |
67 | md5.update(open(f, 'rb').read()) | |
68 | md5 = md5.hexdigest()[:8] | |
69 | f = f[len(c.cwd)+1:] | |
70 | print("[+] MD5 hash for %s is %s" % (f, md5)) | |
71 | # New name | |
72 | root, ext = os.path.splitext(f) | |
73 | newname = "%s.%s%s" % (root, md5, ext) | |
74 | # Symlink | |
75 | c.run("cp %s %s" % (f, newname)) | |
76 | # Fix HTML | |
77 | c.run(r"find . -name '*.html' -type f -print0 | xargs -r0 sed -i " | |
78 | '"' | |
79 | r"s@\([\"']\)\([^\"']*\)%s\1@\1\2%s\1@g" | |
80 | '"' % (f, newname)) | |
81 | ||
82 | lldpdir = os.getcwd() | |
83 | tempdir = tempfile.mkdtemp() | |
84 | try: | |
85 | with c.cd(tempdir): | |
86 | c.run("git clone %s -b gh-pages ." % lldpdir) | |
87 | c.run("rsync -ac --exclude=.git %s/deploy/ ." % lldpdir) | |
88 | c.run("git add .") | |
89 | c.run("git diff --stat HEAD || true", pty=True) | |
90 | if confirm("More diff?", default=True): | |
5892bc76 | 91 | c.run("env GIT_PAGER=less git diff --word-diff HEAD || true", pty=True) |
639debb9 VB |
92 | if confirm("Keep?", default=True): |
93 | c.run('git commit -a -m "Update generated copy of website"') | |
94 | c.run('git push origin') | |
95 | finally: | |
96 | shutil.rmtree(tempdir) |