OSDN Git Service

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