text: # -*- coding:utf-8 -*-
  
  import os
  import sys
  import codecs
  import requests
  import json
  import datetime
  import time
  
  class Builder:
      def __init__(self, upload_dir):
          
          self.projectPath = os.path.join(os.getcwd())
          self.CompilePath = os.path.join(self.projectPath, 'dii_win')
          
          #  rtmp sdk output dir
          self.lib_output_dir =  os.path.join(self.projectPath, 'sdk_output')
          self.remote_dir = "client/dii-media-kit/windows/" + upload_dir
  
      def run(self):
          self.build()  
          self.upload_output()
  
      def build(self):
          if os.path.exists(self.CompilePath):
              os.chdir(self.CompilePath)
              print 'Change dir to ' + self.CompilePath
  
              # build 
              print 'Start build dii_media_kit.sln  x32 release versioin'
              ret = os.system('devenv.com dii_media_kit.sln /Rebuild "Release|Win32" /Project dii_media_kit')
              
              if ret != 0 :
                  exit(1)
          else:
              print 'error: %s are not exist!' %self.CompilePath
              exit(1)
  
      def upload_output(self):
          os.chdir(self.CompilePath)
          # get the latest commit id
          out = os.popen('git rev-parse --short HEAD')
          commit_id = out.read().replace('\n', "")
          version = self.get_sdk_version()
          
          # archive lib
          if os.path.exists(self.lib_output_dir):
              os.chdir(self.lib_output_dir)
              ts = time.strftime("%Y%m%d_%H%M")
              self.targetFile =  os.path.join(self.lib_output_dir, 'dii_win_' + version + '_' + ts  + '.zip')
              print self.targetFile
              os.system('zip -r ' + self.targetFile + ' ' + './')
             
              print 'start archive file to ' + self.remote_dir
              self.http_login()
              self.http_upload()
          else:
              print '%s path are not exist.' %demo_dir
      
      def get_sdk_version(self):
          version_file = os.path.join(self.projectPath, 'dii_player', 'dii_common.h')
          version = None
          fd = open(version_file, 'r')
          for line in fd.readlines():
              if 'DII_MEDIA_KIT_VERSION' in line  :
                  version = line.split(' ')[-1]
                  break
          if version != None:
              return version.strip('\r\n').strip(' ').strip('"')
          else:
              print 'error: can not get version.'
              exit(1)
  
      def http_login(self):
          url = "http://sdk.fifo.site/chfs/session"
          params = {"user": (None, "admin"), "pwd": (None, "admin123")}
          res = requests.post(url, files=params)
          if res.status_code != 201:
              print 'error: file server login failed!'
              exit(1)
          self.Cookie = 'JWT=' + res.cookies['JWT'] + '; ' + 'user=' + res.cookies['user']
          #print self.Cookie
          self.jar = res.cookies
          print 'file server login success!'
  
      def http_upload(self):
          url = "http://sdk.fifo.site/chfs/upload"
          data = {'file':(self.targetFile, open(self.targetFile, 'rb'), "application/zip"),'folder':(None, self.remote_dir)}
          res = requests.post(url, files=data, cookies=self.jar)
          print res.status_code
          if res.status_code != 201:
              print "error: file upload failed!"
              exit(1)
          print "upload file success! file: " + self.targetFile
          
  if __name__=='__main__' :
      upload_dir = ''
      if len(sys.argv) == 2:
          upload_dir = sys.argv[1]
      buildWindows = Builder(upload_dir)
      buildWindows.run()
  