Mercurial > piecrust2
comparison garcon/benchsite.py @ 642:79aefe82c6b6
cm: Move all scripts into a `garcon` package with `invoke` support.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 14 Feb 2016 22:11:24 -0800 |
parents | util/generate_benchsite.py@4a9047850657 |
children | 22cf13b86cc3 |
comparison
equal
deleted
inserted
replaced
641:35221f5fe0dd | 642:79aefe82c6b6 |
---|---|
1 import io | |
2 import os | |
3 import os.path | |
4 import string | |
5 import random | |
6 import datetime | |
7 import argparse | |
8 | |
9 | |
10 def generateWord(min_len=1, max_len=10): | |
11 length = random.randint(min_len, max_len) | |
12 word = ''.join(random.choice(string.ascii_letters) for _ in range(length)) | |
13 return word | |
14 | |
15 | |
16 def generateSentence(words): | |
17 return ' '.join([generateWord() for i in range(words)]) | |
18 | |
19 | |
20 def generateDate(): | |
21 year = random.choice(range(1995, 2015)) | |
22 month = random.choice(range(1, 13)) | |
23 day = random.choice(range(1, 29)) | |
24 hours = random.choice(range(0, 24)) | |
25 minutes = random.choice(range(0, 60)) | |
26 seconds = random.choice(range(0, 60)) | |
27 return datetime.datetime( | |
28 year, month, day, hours, minutes, seconds) | |
29 | |
30 | |
31 def generateTitleAndSlug(): | |
32 title = generateSentence(8) | |
33 slug = title.replace(' ', '-').lower() | |
34 slug = ''.join(c for c in slug if c.isalnum() or c == '-') | |
35 return title, slug | |
36 | |
37 | |
38 class BenchmarkSiteGenerator(object): | |
39 def __init__(self, out_dir): | |
40 self.out_dir = out_dir | |
41 self.all_tags = [] | |
42 | |
43 def generatePost(self): | |
44 post_info = {} | |
45 title, slug = generateTitleAndSlug() | |
46 post_info.update({ | |
47 'title': title, | |
48 'slug': slug}) | |
49 post_info['description'] = generateSentence(20) | |
50 post_info['tags'] = random.choice(self.all_tags) | |
51 post_info['datetime'] = generateDate() | |
52 | |
53 buf = io.StringIO() | |
54 with buf: | |
55 para_count = random.randint(5, 10) | |
56 for i in range(para_count): | |
57 buf.write(generateSentence(random.randint(50, 100))) | |
58 buf.write('\n\n') | |
59 post_info['text'] = buf.getvalue() | |
60 | |
61 self.writePost(post_info) | |
62 | |
63 def initialize(self): | |
64 pass | |
65 | |
66 def writePost(self, post_info): | |
67 raise NotImplementedError() | |
68 | |
69 | |
70 class PieCrustBechmarkSiteGenerator(BenchmarkSiteGenerator): | |
71 def initialize(self): | |
72 posts_dir = os.path.join(self.out_dir, 'posts') | |
73 if not os.path.isdir(posts_dir): | |
74 os.makedirs(posts_dir) | |
75 | |
76 def writePost(self, post_info): | |
77 out_dir = os.path.join(self.out_dir, 'posts') | |
78 slug = post_info['slug'] | |
79 dtstr = post_info['datetime'].strftime('%Y-%m-%d') | |
80 with open('%s/%s_%s.md' % (out_dir, dtstr, slug), 'w', | |
81 encoding='utf8') as f: | |
82 f.write('---\n') | |
83 f.write('title: %s\n' % post_info['title']) | |
84 f.write('description: %s\n' % post_info['description']) | |
85 f.write('tags: [%s]\n' % post_info['tags']) | |
86 f.write('---\n') | |
87 | |
88 para_count = random.randint(5, 10) | |
89 for i in range(para_count): | |
90 f.write(generateSentence(random.randint(50, 100))) | |
91 f.write('\n\n') | |
92 | |
93 | |
94 class OctopressBenchmarkSiteGenerator(BenchmarkSiteGenerator): | |
95 def initialize(self): | |
96 posts_dir = os.path.join(self.out_dir, 'source', '_posts') | |
97 if not os.path.isdir(posts_dir): | |
98 os.makedirs(posts_dir) | |
99 | |
100 def writePost(self, post_info): | |
101 out_dir = os.path.join(self.out_dir, 'source', '_posts') | |
102 slug = post_info['slug'] | |
103 dtstr = post_info['datetime'].strftime('%Y-%m-%d') | |
104 with open('%s/%s-%s.markdown' % (out_dir, dtstr, slug), 'w', | |
105 encoding='utf8') as f: | |
106 f.write('---\n') | |
107 f.write('layout: post\n') | |
108 f.write('title: %s\n' % post_info['title']) | |
109 f.write('date: %s 12:00\n' % dtstr) | |
110 f.write('comments: false\n') | |
111 f.write('categories: [%s]\n' % post_info['tags']) | |
112 f.write('---\n') | |
113 | |
114 para_count = random.randint(5, 10) | |
115 for i in range(para_count): | |
116 f.write(generateSentence(random.randint(50, 100))) | |
117 f.write('\n\n') | |
118 | |
119 | |
120 class MiddlemanBenchmarkSiteGenerator(BenchmarkSiteGenerator): | |
121 def initialize(self): | |
122 posts_dir = os.path.join(self.out_dir, 'source') | |
123 if not os.path.isdir(posts_dir): | |
124 os.makedirs(posts_dir) | |
125 | |
126 def writePost(self, post_info): | |
127 out_dir = os.path.join(self.out_dir, 'source') | |
128 slug = post_info['slug'] | |
129 dtstr = post_info['datetime'].strftime('%Y-%m-%d') | |
130 with open('%s/%s-%s.html.markdown' % (out_dir, dtstr, slug), 'w', | |
131 encoding='utf8') as f: | |
132 f.write('---\n') | |
133 f.write('title: %s\n' % post_info['title']) | |
134 f.write('date: %s\n' % post_info['datetime'].strftime('%Y/%m/%d')) | |
135 f.write('tags: %s\n' % post_info['tags']) | |
136 f.write('---\n') | |
137 | |
138 para_count = random.randint(5, 10) | |
139 for i in range(para_count): | |
140 f.write(generateSentence(random.randint(50, 100))) | |
141 f.write('\n\n') | |
142 | |
143 | |
144 class HugoBenchmarkSiteGenerator(BenchmarkSiteGenerator): | |
145 def initialize(self): | |
146 posts_dir = os.path.join(self.out_dir, 'content', 'post') | |
147 if not os.path.isdir(posts_dir): | |
148 os.makedirs(posts_dir) | |
149 | |
150 def writePost(self, post_info): | |
151 out_dir = os.path.join(self.out_dir, 'content', 'post') | |
152 dtstr = post_info['datetime'].strftime('%Y-%m-%d_%H-%M-%S') | |
153 post_path = os.path.join(out_dir, '%s.md' % dtstr) | |
154 with open(post_path, 'w', encoding='utf8') as f: | |
155 f.write('+++\n') | |
156 f.write('title = "%s"\n' % post_info['title']) | |
157 f.write('description = "%s"\n' % post_info['description']) | |
158 f.write('categories = [\n "%s"\n]\n' % post_info['tags']) | |
159 f.write('date = "%s"\n' % post_info['datetime'].strftime( | |
160 "%Y-%m-%d %H:%M:%S-00:00")) | |
161 f.write('slug ="%s"\n' % post_info['slug']) | |
162 f.write('+++\n') | |
163 f.write(post_info['text']) | |
164 | |
165 | |
166 generators = { | |
167 'piecrust': PieCrustBechmarkSiteGenerator, | |
168 'octopress': OctopressBenchmarkSiteGenerator, | |
169 'middleman': MiddlemanBenchmarkSiteGenerator, | |
170 'hugo': HugoBenchmarkSiteGenerator | |
171 } | |
172 | |
173 | |
174 def main(): | |
175 parser = argparse.ArgumentParser( | |
176 prog='generate_benchsite', | |
177 description=("Generates a benchmark website with placeholder " | |
178 "content suitable for testing.")) | |
179 parser.add_argument( | |
180 'engine', | |
181 help="The engine to generate the site for.", | |
182 choices=list(generators.keys())) | |
183 parser.add_argument( | |
184 'out_dir', | |
185 help="The target directory for the website.") | |
186 parser.add_argument( | |
187 '-c', '--post-count', | |
188 help="The number of posts to create.", | |
189 type=int, | |
190 default=100) | |
191 parser.add_argument( | |
192 '--tag-count', | |
193 help="The number of tags to use.", | |
194 type=int, | |
195 default=30) | |
196 | |
197 result = parser.parse_args() | |
198 generate(result.engine, result.out_dir, | |
199 post_count=result.post_count, | |
200 tag_count=result.tag_count) | |
201 | |
202 | |
203 def generate(engine, out_dir, post_count=100, tag_count=10): | |
204 print("Generating %d posts in %s..." % (post_count, out_dir)) | |
205 | |
206 if not os.path.exists(out_dir): | |
207 os.makedirs(out_dir) | |
208 | |
209 gen = generators[engine](out_dir) | |
210 gen.all_tags = [generateWord(3, 12) for _ in range(tag_count)] | |
211 gen.initialize() | |
212 | |
213 for i in range(post_count): | |
214 gen.generatePost() | |
215 | |
216 | |
217 if __name__ == '__main__': | |
218 main() | |
219 else: | |
220 from invoke import task | |
221 | |
222 @task | |
223 def genbenchsite(engine, out_dir, post_count=100, tag_count=10): | |
224 generate(engine, out_dir, | |
225 post_count=post_count, | |
226 tag_count=tag_count) | |
227 |