OSDN Git Service

implement ffmepg interlaced option.
[rec10/rec10-git.git] / rec10 / trunk / src / install.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2010 Yukikaze
5 import time
6 import os.path
7 import ConfigParser
8 import sys
9 import commands
10 import configwriter
11 import shutil
12
13 import recdblist
14 def printutf8(str):
15     print str.encode('utf-8')
16 def install():
17     version = recdblist.version
18     recdblist.printutf8(u"rec10の環境インストールを開始します。")
19     time.sleep(1)
20     recdblist.printutf8(u"環境インストール処理中・・・")
21     path = str(os.path.dirname(os.path.abspath(__file__)))
22     confp = ConfigParser.SafeConfigParser()
23     Conf = 'rec10.conf'
24     confpath=""
25     if os.path.exists(os.path.join(path,Conf)):
26         confpath=os.path.join(path,Conf)
27     elif os.path.exists(os.path.join("/etc","rec10.conf")):
28         confpath=os.path.join("/etc","rec10.conf")
29     elif os.path.exists(os.path.join("/etc/rec10","rec10.conf")):
30         confpath=os.path.join("/etc/rec10","rec10.conf")
31     if confpath!="":
32         confp.read(confpath)
33     else:
34         recdblist.printutf8(u"rec10.confが見つかりません。")
35         recdblist.printutf8(u"このinstall.pyと同じフォルダに置くか、/etc/rec10.confもしくは/etc/rec10/rec10.confにおいてください。")
36         sys.exit(1)
37     recdblist.printutf8(u"設定ファイルの読み込みを確認//設定ファイルのパスは"+confpath)
38     if confp.get('path',"recpath")=="/path of /recording":
39         recdblist.printutf8(u"録画先のフォルダを設定してください(recpath=)")
40         sys.exit(1)
41     recdblist.printutf8(u"DB処理に入ります")
42     if os.path.exists(os.path.join(path,"rec10d.py")):
43         try:
44             update_db_all()
45         except Exception, inst:
46             recdblist.printutf8(u"DB処理中にエラーが出ました。configファイルのDB設定を見直してください。")
47             recdblist.printutf8(str(type(inst)))
48             recdblist.printutf8(str(inst))
49     recdblist.printutf8(u"チャンネル設定に入ります")
50     if os.path.exists(os.path.join(path,"chlist.xml")):
51         recdblist.printutf8(u"チャンネルリストが見つかりました。||chlist.xml")
52     else:
53         recdblist.printutf8(u"チャンネルリストが見つかりません。"+os.path.join(path,"chlist.xml")+u"を作成してください。")
54         recdblist.printutf8(u"東京/名古屋/神戸のサンプルがchlist_sample_*.xmlにあるため、参考にしてください。")
55         sys.exit(1)
56     bsok=int(confp.get('env',"bs"))
57     csok=int(confp.get('env',"cs"))
58     import chdata
59     chdata.new_chdata(bsok,csok)
60     recdblist.printutf8(u"おめでとうございます 初期設定は完了しました。")
61     recdblist.printutf8(u"rec10を実行するユーザーのcrontabに"+os.path.join(path,"rec10")+u"を追加してください(5分周期が目安)")
62     import rec10d
63     rec10d.rec10db.change_installed_in_status()
64 def getpath(cmd):
65     printutf8(cmd+u"の場所を確認中")
66     txt=commands.getoutput(u"which "+cmd)
67     if len(txt.split(" "))>3:
68         printutf8(cmd+u"の場所を確認できませんでした。終了します")
69         sys.exit(1)
70     else:
71         printutf8(cmd+u" : "+txt)
72         if len(txt.splitlines()):
73             txt=txt.splitlines()[0]
74         return txt.strip()
75 def move():
76     useB=0
77     path = str(os.path.dirname(os.path.abspath(__file__)))
78     printutf8(u"rec10 ver "+recdblist.version_str+" installer.")
79     printutf8(u"rec10のインストールを開始します。")
80     maxn=16
81     configwriter.setpath(u"wine", getpath(u"wine"))
82     configwriter.setpath(u"recpt1", getpath(u"recpt1"))
83     #configwriter.setpath(u"x264", getpath(u"x264"))
84     configwriter.setpath(u"mp4box", getpath(u"MP4Box"))
85     configwriter.setpath(u"mencoder", getpath(u"mencoder"))
86     configwriter.setpath(u"ffmpeg", getpath(u"ffmpeg"))
87     configwriter.setpath(u"mkvmerge", getpath(u"mkvmerge"))
88     configwriter.setpath(u"b25", getpath(u"b25"))
89     printutf8(u"必須環境の設定が完了しました")
90     printutf8(u"個人設定に入ります")
91     if os.path.exists("/etc/rec10.conf"):
92         printutf8(u"以前の設定ファイルが見つかりました。")
93         printutf8(u"インポートしますか?")
94         useB=raw_input("[Y/n]:")
95         if useB=="N" or useB == "n":
96             useB=0
97         else:
98             useB=1
99             printutf8(u"設定ファイルのインポートを行います。")
100     printutf8(u"rec10の録画ファイルが置かれる場所を入力してください(1/"+str(maxn)+")")
101     printutf8(u"100GB程度は確保されている必要があります")
102     recpath=getConf(useB,"/etc/rec10.conf","path","recpath","path : ")
103     configwriter.setpath(u"recpath",recpath)
104     printutf8(u"DBの設定に入ります。")
105     printutf8(u"rec10が使用するMySQLのユーザーを設定してください。(2/"+str(maxn)+")")
106     mysql_user=getConf(useB,"/etc/rec10.conf","db","mysql_user","mysql_user : ")
107     configwriter.setdbpath("mysql_user", mysql_user)
108     printutf8(u"パスワードを設定してください(3/"+str(maxn)+")")
109     mysql_passwd=getConf(useB,"/etc/rec10.conf","db","mysql_passwd","mysql_passwd : ")
110     configwriter.setdbpath("mysql_passwd", mysql_passwd)
111     printutf8(u"同時録画可能数の設定に入ります")
112     printutf8(u"TE(地デジ)録画可能数(PT*だと2 白Friioだと1)(4/"+str(maxn)+")")
113     te_max=str(int(getConf(useB,"/etc/rec10.conf","env","te_max","te_max : ")))
114     configwriter.setenv("te_max", te_max)
115     printutf8(u"BS/CS110録画可能数(PT*だと2 黒Friioだと1)(5/"+str(maxn)+")")
116     bscs_max=str(int(getConf(useB,"/etc/rec10.conf","env","bscs_max","bscs_max : ")))
117     configwriter.setenv("bscs_max", bscs_max)
118     printutf8(u"同時エンコード最大数を設定してください(6/"+str(maxn)+")")
119     printutf8(u"CPUのコア数が目安です")
120     enc_max=raw_input("[2]:")
121     if enc_max=="":
122         enc_max="2"
123     else:
124         enc_max=str(int(enc_max))
125     configwriter.setenv("enc_max", enc_max)
126     printutf8(u"二カ国語放送/5.1ch放送の音声変換の設定です。\nNeroAACエンコーダーを使用しますか?(NeroAACEncを別途入手してtstoolsに入れてください。))")
127     printutf8(u"入手先:http://www.nero.com/jpn/technologies-aac-codec.html\n (7/"+str(maxn)+")")
128     audioenc=raw_input("[y/N]:  ")
129     if audioenc=="y" or audioenc=="Y":
130         configwriter.setpath("useNeroAAC", "1")
131         printutf8(u"NeroAACEncのパスを指定してください")
132         aacpath=getConf(useB,"/etc/rec10.conf","path","NeroAAC","[/usr/local/bin/neroAacEnc] : ")
133         if aacpath.replace(" ","")=="":
134             aacpath="/usr/local/bin/neroAacEnc"
135         configwriter.setpath("NeroAAC",aacpath)
136     else:
137         configwriter.setpath("useNeroAAC", "0")
138         printutf8(u"lameを使用します。")
139     printutf8(u"x264のパスを指定してください(7/"+str(maxn)+")")
140     x264path=getConf(useB,"/etc/rec10.conf","path","x264","x264 path : ")
141     if x264path.replace(" ","")=="" or not os.path.isfile(x264path):
142         x264path=getpath(u"x264")
143     configwriter.setpath("x264",x264path)
144     printutf8(u"保存する画質を教えてください(8/"+str(maxn)+")")
145     printutf8(u"x264のcrfの値です。標準だと30分で800MBほどになります")
146     crf=getConf(useB,"/etc/rec10.conf","env","crf","[24] : ")
147     if crf.replace(" ","")=="":
148         crf="24"
149     configwriter.setenv("crf", crf)
150     printutf8(u"アニメーションを保存する画質を教えてください(9/"+str(maxn)+")")
151     printutf8(u"x264のcrfの値です。標準だと30分で800MBほどになります")
152     a_crf=getConf(useB,"/etc/rec10.conf","env","animation_crf","[20] : ")
153     if a_crf.replace(" ","")=="":
154         a_crf="20"
155     configwriter.setenv("animation_crf", a_crf)
156     printutf8(u"x264の圧縮率を設定してください(10/"+str(maxn)+")")
157     printutf8(u"x264のpresetの値です。標準だとmedium、小さければ小さいほど高圧縮になります")
158     preset=getConf(useB,"/etc/rec10.conf","env","x264_preset","[4] : ")
159     if preset.replace(" ","")=="":
160         preset="4"
161     configwriter.setenv("x264_preset", preset)
162     printutf8(u"保存するコンテナの設定(11/"+str(maxn)+")")
163     printutf8(u"MP4を標準にしますか(もしくはMKVが使えます)")
164     useMP4=raw_input("[y/N]:")
165     if useMP4=="Y" or useMP4 == "y" :
166         useMP4="1"
167     else:
168         useMP4="0"
169     configwriter.setenv("make_mp4", useMP4)
170     printutf8(u"TSファイルの自動削除(12/"+str(maxn)+")")
171     printutf8(u"中間体のtsファイルを削除しますか")
172     printutf8(u"(削除するとやり直しができないため残しておくことを推奨します。)")
173     removeTS=raw_input("[y/N]:")
174     if removeTS=="Y" or removeTS == "y" :
175         removeTS="1"
176     else:
177         removeTS="0"
178     configwriter.setenv("remove_ts", removeTS)
179     printutf8(u"チャンネルの設定に入ります(13/"+str(maxn)+")")
180     printutf8(u"rec10を設置する地域を選んでください。")
181     printutf8(u"1:東京 2:神戸 3:名古屋 4:その他")
182     ch=raw_input()
183     ch=int(ch)
184     if ch==1:
185         try:
186             shutil.copy(os.path.join(path,"chlist_sample_tokyo.xml"),os.path.join(path,"chlist.xml"))
187         except:
188             printutf8(u"既存のchlis.xmlを上書きできないため、前のものを使います。")
189     elif ch==2:
190         try:
191             shutil.copy(os.path.join(path,"chlist_sample_kobe.xml"),os.path.join(path,"chlist.xml"))
192         except:
193             printutf8(u"既存のchlis.xmlを上書きできないため、前のものを使います。")
194     elif ch==3:
195         try:
196             shutil.copy(os.path.join(path,"chlist_sample_nagoya.xml"),os.path.join(path,"chlist.xml"))
197         except:
198             printutf8(u"既存のchlis.xmlを上書きできないため、前のものを使います。")
199     elif ch==4:
200         if not os.path.exists(os.path.join(path,"chlist.xml")):
201             printutf8(u"チャンネルの設定が必要です。")
202             printutf8(u"chlist_sample_*を参考にchlist.xmlを作成した後に次のステップに進んでください。")
203             time.sleep(5)
204     printutf8(u"BS放送は受信可能ですか?(14/"+str(maxn)+")")
205     bsok=raw_input("y/N:")
206     if bsok=="Y" or bsok == "y" :
207         bsok="1"
208     else:
209         bsok="0"
210     configwriter.setenv("bs", bsok)
211     printutf8(u"CS放送(スカパーe2)は受信可能ですか?(15/"+str(maxn)+")")
212     csok=raw_input("y/N:")
213     if csok=="Y" or csok == "y" :
214         csok="1"
215         printutf8(u"標準パックの番組は入力してありますがそれ以外は追加でchlist_cs.xmlに追加する必要があります。")
216     else:
217         csok="0"
218     configwriter.setenv("cs", csok)
219
220     printutf8(u"自機識別オプション(16/"+str(maxn)+")")
221     printutf8(u"本rec10の識別名を入力してください")
222     printutf8(u"複数台のrec10を運用する場合に効果的です。空白でもかまいません。")
223     printutf8(u"例 kobe01")
224     iff=getConf(useB,"/etc/rec10.conf","env","iff","[] : ")
225     configwriter.setenv("iff", iff)
226     confp = ConfigParser.SafeConfigParser()
227     confp.read("/etc/rec10.conf")
228     if useB:
229         configwriter.setpath("recorded",confp.get("path","recorded"))
230         configwriter.setpath("move_destpath",confp.get("path","move_destpath"))
231         configwriter.setpath("tmp",confp.get("path","tmp"))
232         try:
233             configwriter.setenv("x264_addline",confp.get("env","x264_addline"))
234         except:
235             configwriter.setenv("x264_addline","")
236         if confp.get("path","b25_remote")=="":
237             configwriter.setpath("b25_remote", "0")
238         elif confp.get("path","b25_remote")=="1":
239             configwriter.setpath("b25_remote", confp.get("path","b25_remote"))
240             configwriter.setpath("b25", confp.get("path","b25"))
241             configwriter.setpath("b25_env", confp.get("path","b25_env"))
242         else:
243             configwriter.setpath("b25_remote", confp.get("path","b25_remote"))
244
245     configwriter.writeconf()
246     printutf8(u"初期設定が終了しました。")
247     printutf8(u"makeを実行した後にrootにてmake installを実行するとインストールされます。")
248 def getConf(useBeforeConf,Confpath,dbsection,key,defaultstr):
249     tstr=defaultstr
250     if os.path.exists(Confpath) and useBeforeConf==1:
251         try:
252             confp = ConfigParser.SafeConfigParser()
253             confp.read("/etc/rec10.conf")
254             tstr=u"["+confp.get(dbsection,key)+u"]:"
255         except:
256             tstr=defaultstr
257     tmpconf=raw_input(tstr)
258     if tmpconf.replace(" ","")=="" and os.path.exists(Confpath) and useBeforeConf==1 :
259         confp = ConfigParser.SafeConfigParser()
260         confp.read("/etc/rec10.conf")
261         try:
262             tmpconf=confp.get(dbsection,key)
263         except:
264             tmpconf=""
265     return tmpconf
266 def update_db_all():
267     import rec10d
268     tversion=0
269     if rec10d.rec10db.select_installed_in_status()==1:
270         if rec10d.rec10db.select_version_in_status()==0:
271             tversion=0
272             recdblist.printutf8(u"既存のDBが見つかりました。0.9.1と仮定してアップデート処理を行います。")
273             update_db(0)
274             time.sleep(1)
275         else:
276             recdblist.printutf8(u"既存のDBが見つかりました。アップデート処理を行います。")
277         tversion=int(rec10d.rec10db.select_version_in_status())
278         while (recdblist.version>tversion):
279             update_db(tversion)
280             tversion=int(rec10d.rec10db.select_version_in_status())
281     else:
282         rec10d.rec10db.drop_in_status()
283         rec10d.rec10db.drop_in_settings()
284         time.sleep(2)
285         rec10d.rec10db.new_in_status()
286         rec10d.rec10db.new_in_settings()
287         rec10d.rec10db.change_version_in_status(recdblist.version)
288 def update_db(version):
289     import rec10d
290     if version==0:
291         rec10d.rec10db.update_db_to93()
292     elif version==93:
293         rec10d.rec10db.update_db_93to94()
294     elif version==94:
295         rec10d.rec10db.update_db_94to95()
296     elif version==95:
297         rec10d.rec10db.update_db_95to96()
298 if __name__ == "__main__":
299     move()