# -*- coding: utf-8 -*-
import os
import urllib
import subprocess



def downLoadFile(url, dir, fname):
    print url + u"をダウンロードしています"
    urllib.urlretrieve(url, os.path.join(dir,fname))
    print u"ダウンロードを終了しました"
    return fname

def installMSI(url, dir):
    dir = os.path.abspath(dir).replace("/","\\")

    fname = os.path.basename(url)

    tempdir = os.environ.get("TEMP")
    #downLoadFile(url,tempdir,fname)

    downloadPath = os.path.join(tempdir,fname).replace("/","\\")
    
    print fname+u"を抽出しています"

    cmd = 'msiexec /a ' + downloadPath + ' targetdir="' + dir + '" /qn'
    #subprocess.call(cmd)

    print fname+u"を" + dir + u"に抽出しました"

    print u"一時ファイルを削除しています"
    #os.remove(downloadPath)

    print u"終了しました"
    
def main():

    url = "http://openrtm.org/pub/Windows/OpenRTM-aist/cxx/1.1/OpenRTM-aist-1.1.1-RELEASE_x86_vc10.msi"
    
    dir = "."
    installMSI(url, dir)

if __name__ == "__main__":
    main()