OSDN Git Service

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