OSDN Git Service

update fonts
[sawarabi-fonts/sawarabi-fonts.git] / script / release.py
1 #!/usr/bin/env python
2
3 import StringIO
4 import all_kanji
5 import locale
6 import new_glyphs
7 import os
8 import re
9 import shutil
10 import sys
11 import time
12 import yaml
13 from listprinter import SimpleListPrinter
14
15 class AllKanjiList:
16   def __init__(self, fontpath):
17     self.fontpath = fontpath
18     self.__listbuffer = None
19     self.__count = None
20
21   def listbuffer(self):
22     if self.__listbuffer is None:
23       self.__retrieve()
24     return self.__listbuffer
25
26   def count(self):
27     if self.__count is None:
28       self.__retrieve()
29     return self.__count
30
31   def __get_buffer(self):
32     out = StringIO.StringIO()
33     all_kanji.main(self.fontpath, out)
34     buff = out.getvalue()
35     out.close()
36     return buff
37
38   def __retrieve(self):
39     buff = self.__get_buffer()
40     pat = re.compile(r'\s([0-9]+)\s+char\(s\)$')
41     m = pat.search(buff)
42     if m:
43       self.__listbuffer = buff[0:m.start(0)]
44       self.__count = int(m.group(1))
45
46 class NewKanjiList:
47   def __init__(self, fontpath, archive):
48     self.fontpath = fontpath
49     self.archive = archive
50     self.__listbuffer = None
51     self.__count = None
52
53   def listbuffer(self):
54     if self.__listbuffer is None:
55       self.__retrieve()
56     return self.__listbuffer
57
58   def count(self):
59     if self.__count is None:
60       self.__retrieve()
61     return self.__count
62
63   def __get_buffer(self):
64     out = StringIO.StringIO()
65     p = SimpleListPrinter(delimiter=0x3001, out=out)
66     new_glyphs.print_new_glyph(self.archive, self.fontpath, p)
67     buff = out.getvalue()
68     out.close()
69     return buff
70
71   def __retrieve(self):
72     buff = self.__get_buffer()
73     pat = re.compile(r'\s([0-9]+)\s+char\(s\)$')
74     m = pat.search(buff)
75     if m:
76       self.__listbuffer = buff[0:m.start(0)]
77       self.__count = int(m.group(1))
78
79 def print_readme(template_dir, out_dir, language, all_list):
80   template_name = 'README-%s-template.txt' % language
81   template_file = os.path.join(template_dir, template_name)
82
83   out_file = os.path.join(out_dir, 'README_%s.txt' % language)
84   d = {'kanji_count': __l(int(all_list.count())),
85        'kanji_list': all_list.listbuffer(),
86        'year': time.localtime()[0],
87        }
88   __print_document(template_file, out_file, d)
89
90 def print_releasenote(template_dir, out_dir, language, all_list, new_list):
91   template_name = 'NOTE-%s-template.txt' % language
92   template_file = os.path.join(template_dir, template_name)
93
94   out_file = os.path.join(out_dir, 'RELEASE-NOTE-%s.txt' % language)
95   d = {'kanji_count': __l(int(all_list.count())),
96        'new_kanji_count': __l(int(new_list.count())),
97        'kanji_list': all_list.listbuffer(),
98        'new_kanji_list': new_list.listbuffer(),
99        }
100   __print_document(template_file, out_file, d)
101
102 def __print_document(template_file, out_file, data):
103   if not os.path.exists(template_file): return
104
105   out = open(out_file, 'w')
106   template_lines = open(template_file, 'rU').readlines()
107   for line in template_lines:
108     out.write(line % data)
109   out.close()
110
111 def __l(num):
112   return locale.format('%d', num, True)
113
114 def prepare_release_dir(font_dir_path, out_path, name, weight):
115   for ext in ('ttf', 'otf'):
116     src = os.path.join(font_dir_path, '%s-%s.%s' % (name, weight, ext))
117     if os.path.exists(src):
118       if ext == 'ttf': 
119         dest = os.path.join(out_path, name) 
120       else:
121         dest = os.path.join(out_path, '%s-%s' % (name, ext))
122       if not os.path.exists(dest): os.makedirs(dest)
123       shutil.copy(src, dest)
124
125 class ReleaseConfig:
126   def __init__(self, path):
127     if not os.path.exists(path):
128       raise 'No such file exists: {}'.format(path)
129     if not os.path.isfile(path):
130       raise 'The specified path is not a file'
131
132     self.conf = yaml.load(open(path).read())
133     self.rootdir = os.path.abspath(self.conf['rootdir'])
134
135   def get_workdir(self, style):
136     return self.__get_configurated_dir_path('workdir', style)
137
138   def get_archivedir(self, style):
139     return self.__get_configurated_dir_path('archivedir', style)
140
141   def bind_style(self, style):
142     self.style = style
143     self.workdir = self.get_workdir(style)
144     self.archivedir = self.get_archivedir(style)
145
146   def __get_configurated_dir_path(self, dirtype, style):
147     d = self.conf[dirtype]
148     rootpath = d['root'] or ''
149     stylepath = d[style]
150     return os.path.join(self.rootdir, rootpath, stylepath)
151
152   def get_fontpaths(self, style, weights=None, extensions=None):
153     subconf = self.conf['fonts'][style]
154     rootpath = subconf['root']
155     if weights != None and weights.__class__ != list: 
156       weights = [weights]
157     weights = weights or subconf['weight'].keys()
158     if extensions != None and extensions.__class__ != list: 
159       extensions = [extensions]
160     extensions = extensions or subconf['extension']
161     paths = [os.path.join(self.rootdir, rootpath, \
162                             '%s.%s' % (subconf['weight'][key], ext)) \
163                for key in weights if key != 'root' \
164                for ext in extensions]
165     return paths
166
167 class FontName:
168   def __init__(self, path):
169     if not os.path.exists(path): 
170       raise Exception('No such file exists: {}'.format(path))
171     if not os.path.isfile(path): 
172       raise Exception('The specified path is not a file: {}'.format(path))
173     self.__setup_items(path)
174
175   def __setup_items(self, path):
176     self.dir = os.path.dirname(path)
177     self.basename = os.path.basename(path)
178     familyname, style, weight = self.basename.split('.')[0].split('-')
179     self.name = '%s-%s' % (familyname, style)
180     self.familyname = familyname
181     self.style = style
182     self.weight = weight
183
184 if __name__ == '__main__':
185   scriptdir = os.path.dirname(sys.argv[0])
186   conf = ReleaseConfig(os.path.join(scriptdir, 'release.yml'))
187
188   fontpath = sys.argv[1]
189   font = FontName(fontpath)
190   conf.bind_style(font.style)
191
192   if not os.path.exists(conf.archivedir) or \
193         not os.path.isdir(conf.archivedir): 
194     raise Exception('The specified archive directory does not exits')
195   if not os.path.exists(conf.workdir): os.makedirs(conf.workdir)
196
197   archlist = os.listdir(conf.archivedir)
198   pat = re.compile(r'^%s\-%s\-[0-9]{8}\.tar\.gz$' % \
199                      (font.familyname, font.style))
200   archfiles = [nm for nm in archlist if pat.match(nm)]
201   archfiles.sort()
202   archfile = archfiles[-1]
203
204   all_list = AllKanjiList(fontpath)
205   new_list = NewKanjiList(fontpath, os.path.join(conf.archivedir, archfile))
206
207   prepare_release_dir(font.dir, conf.workdir, font.name, font.weight)
208
209   templatedir = os.path.join(scriptdir, 'templates', font.style)
210   for lang in ['ja', 'en']: 
211     for ext in ['ttf', 'otf']:
212       if ext == 'ttf':
213         dest = os.path.join(conf.workdir, font.name)
214       else:
215         dest = os.path.join(conf.workdir, '%s-%s' % (font.name, ext))
216       if os.path.exists(dest): 
217         print_readme(templatedir, dest, lang, all_list)
218
219   for lang in ['ja', 'en', 'de']: 
220     print_releasenote(templatedir, conf.workdir, lang, all_list, new_list)