OSDN Git Service

6b457a1301907e5dd91139a44d0c2328fad416ec
[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 0.9.4 installer.")
79     printutf8(u"rec10のインストールを開始します。")
80     maxn=14
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"保存する画質を教えてください(7/"+str(maxn)+")")
140     printutf8(u"x264のcrfの値です。標準だと30分で800MBほどになります")
141     crf=getConf(useB,"/etc/rec10.conf","env","crf","[24] : ")
142     if crf.replace(" ","")=="":
143         crf="24"
144     configwriter.setenv("crf", crf)
145     printutf8(u"アニメーションを保存する画質を教えてください(8/"+str(maxn)+")")
146     printutf8(u"x264のcrfの値です。標準だと30分で800MBほどになります")
147     a_crf=getConf(useB,"/etc/rec10.conf","env","animation_crf","[20] : ")
148     if a_crf.replace(" ","")=="":
149         a_crf="20"
150     configwriter.setenv("animation_crf", a_crf)
151     printutf8(u"x264の圧縮率を設定してください(8/"+str(maxn)+")")
152     printutf8(u"x264のpresetの値です。標準だとmedium、小さければ小さいほど高圧縮になります")
153     preset=getConf(useB,"/etc/rec10.conf","env","x264_preset","[4] : ")
154     if preset.replace(" ","")=="":
155         preset="4"
156     configwriter.setenv("x264_preset", preset)
157     printutf8(u"保存するコンテナの設定(9/"+str(maxn)+")")
158     printutf8(u"MP4を標準にしますか(もしくはMKVが使えます)")
159     useMP4=raw_input("[y/N]:")
160     if useMP4=="Y" or useMP4 == "y" :
161         useMP4="1"
162     else:
163         useMP4="0"
164     configwriter.setenv("make_mp4", useMP4)
165     printutf8(u"TSファイルの自動削除(10/"+str(maxn)+")")
166     printutf8(u"中間体のtsファイルを削除しますか")
167     printutf8(u"(削除するとやり直しができないため残しておくことを推奨します。)")
168     removeTS=raw_input("[y/N]:")
169     if removeTS=="Y" or removeTS == "y" :
170         removeTS="1"
171     else:
172         removeTS="0"
173     configwriter.setenv("remove_ts", removeTS)
174     printutf8(u"チャンネルの設定に入ります(11/"+str(maxn)+")")
175     printutf8(u"rec10を設置する地域を選んでください。")
176     printutf8(u"1:東京 2:神戸 3:名古屋 4:その他")
177     ch=raw_input()
178     ch=int(ch)
179     if ch==1:
180         try:
181             shutil.copy(os.path.join(path,"chlist_sample_tokyo.xml"),os.path.join(path,"chlist.xml"))
182         except:
183             printutf8(u"既存のchlis.xmlを上書きできないため、前のものを使います。")
184     elif ch==2:
185         try:
186             shutil.copy(os.path.join(path,"chlist_sample_kobe.xml"),os.path.join(path,"chlist.xml"))
187         except:
188             printutf8(u"既存のchlis.xmlを上書きできないため、前のものを使います。")
189     elif ch==3:
190         try:
191             shutil.copy(os.path.join(path,"chlist_sample_nagoya.xml"),os.path.join(path,"chlist.xml"))
192         except:
193             printutf8(u"既存のchlis.xmlを上書きできないため、前のものを使います。")
194     elif ch==4:
195         if not os.path.exists(os.path.join(path,"chlist.xml")):
196             printutf8(u"チャンネルの設定が必要です。")
197             printutf8(u"chlist_sample_*を参考にchlist.xmlを作成した後に次のステップに進んでください。")
198             time.sleep(5)
199     printutf8(u"BS放送は受信可能ですか?(12/"+str(maxn)+")")
200     bsok=raw_input("y/N:")
201     if bsok=="Y" or bsok == "y" :
202         bsok="1"
203     else:
204         bsok="0"
205     configwriter.setenv("bs", bsok)
206     printutf8(u"CS放送(スカパーe2)は受信可能ですか?(13/"+str(maxn)+")")
207     csok=raw_input("y/N:")
208     if csok=="Y" or csok == "y" :
209         csok="1"
210         printutf8(u"標準パックの番組は入力してありますがそれ以外は追加でchlist_cs.xmlに追加する必要があります。")
211     else:
212         csok="0"
213     configwriter.setenv("cs", csok)
214
215     printutf8(u"自機識別オプション(14/"+str(maxn)+")")
216     printutf8(u"本rec10の識別名を入力してください")
217     printutf8(u"複数台のrec10を運用する場合に効果的です。空白でもかまいません。")
218     printutf8(u"例 kobe01")
219     iff=getConf(useB,"/etc/rec10.conf","env","iff","[] : ")
220     configwriter.setenv("iff", iff)
221     confp = ConfigParser.SafeConfigParser()
222     confp.read("/etc/rec10.conf")
223     if useB:
224         configwriter.setpath("recorded",confp.get("path","recorded"))
225         configwriter.setpath("move_destpath",confp.get("path","move_destpath"))
226         configwriter.setpath("tmp",confp.get("path","tmp"))
227         try:
228             configwriter.setenv("x264_addline",confp.get("env","x264_addline"))
229         except:
230             configwriter.setenv("x264_addline","")
231         if confp.get("path","b25_remote")=="":
232             configwriter.setpath("b25_remote", "0")
233         elif confp.get("path","b25_remote")=="1":
234             configwriter.setpath("b25_remote", confp.get("path","b25_remote"))
235             configwriter.setpath("b25", confp.get("path","b25"))
236             configwriter.setpath("b25_env", confp.get("path","b25_env"))
237         else:
238             configwriter.setpath("b25_remote", confp.get("path","b25_remote"))
239
240     configwriter.writeconf()
241     printutf8(u"初期設定が終了しました。")
242     printutf8(u"makeを実行した後にrootにてmake installを実行するとインストールされます。")
243 def getConf(useBeforeConf,Confpath,dbsection,key,defaultstr):
244     tstr=defaultstr
245     if os.path.exists(Confpath) and useBeforeConf==1:
246         try:
247             confp = ConfigParser.SafeConfigParser()
248             confp.read("/etc/rec10.conf")
249             tstr=u"["+confp.get(dbsection,key)+u"]:"
250         except:
251             tstr=defaultstr
252     tmpconf=raw_input(tstr)
253     if tmpconf.replace(" ","")=="" and os.path.exists(Confpath) and useBeforeConf==1 :
254         confp = ConfigParser.SafeConfigParser()
255         confp.read("/etc/rec10.conf")
256         try:
257             tmpconf=confp.get(dbsection,key)
258         except:
259             tmpconf=""
260     return tmpconf
261 def update_db_all():
262     import rec10d
263     tversion=0
264     if rec10d.rec10db.select_installed_in_status()==1:
265         if rec10d.rec10db.select_version_in_status()==0:
266             tversion=0
267             recdblist.printutf8(u"既存のDBが見つかりました。0.9.1と仮定してアップデート処理を行います。")
268             update_db(0)
269             time.sleep(1)
270         else:
271             recdblist.printutf8(u"既存のDBが見つかりました。アップデート処理を行います。")
272         tversion=int(rec10d.rec10db.select_version_in_status())
273         while (recdblist.version>tversion):
274             update_db(tversion)
275             tversion=int(rec10d.rec10db.select_version_in_status())
276     else:
277         rec10d.rec10db.drop_in_status()
278         rec10d.rec10db.drop_in_settings()
279         time.sleep(2)
280         rec10d.rec10db.new_in_status()
281         rec10d.rec10db.new_in_settings()
282         rec10d.rec10db.change_version_in_status(recdblist.version)
283 def update_db(version):
284     import rec10d
285     if version==0:
286         rec10d.rec10db.update_db_to93()
287     elif version==93:
288         rec10d.rec10db.update_db_93to94()
289     elif version==94:
290         rec10d.rec10db.update_db_94to95()
291 if __name__ == "__main__":
292     move()