<s> from __future__ import division <EOL> from biicode . common . settings . version import Version <EOL> import os <EOL> from biicode . client . shell . biistream import Color <EOL> import sys <EOL> from biicode . client . command . process_executor import execute <EOL> from biicode . common . settings . osinfo import OSInfo <EOL> import urllib <EOL> import tempfile <EOL> def decompress ( filename , destination ) : <EOL> if filename . endswith ( "<STR_LIT>" ) : <EOL> unzip ( filename , destination ) <EOL> elif filename . endswith ( "<STR_LIT>" ) or filename . endswith ( "<STR_LIT>" ) : <EOL> untargz ( filename , destination ) <EOL> def unzip ( filename , destination ) : <EOL> import zipfile <EOL> with zipfile . ZipFile ( filename , "<STR_LIT:r>" ) as z : <EOL> if OSInfo . is_linux ( ) or OSInfo . is_mac ( ) : <EOL> for zinfo in z . filelist : <EOL> zinfo . create_system = <NUM_LIT:3> <EOL> if OSInfo . is_mac ( ) : <EOL> for thefile in z . filelist : <EOL> name = thefile . filename <EOL> perm = ( ( thefile . external_attr >> <NUM_LIT:16> L ) & <NUM_LIT:0> <NUM_LIT> ) <EOL> if name . endswith ( '<STR_LIT:/>' ) : <EOL> os . mkdir ( os . path . join ( destination , name ) , perm ) <EOL> else : <EOL> outfile = os . path . join ( destination , name ) <EOL> fh = os . open ( outfile , os . O_CREAT | os . O_WRONLY , perm ) <EOL> os . write ( fh , z . read ( name ) ) <EOL> os . close ( fh ) <EOL> z . close ( ) <EOL> else : <EOL> z . extractall ( destination ) <EOL> def untargz ( filename , destination ) : <EOL> import tarfile <EOL> with tarfile . TarFile . open ( filename , '<STR_LIT>' ) as tarredgzippedFile : <EOL> tarredgzippedFile . extractall ( destination ) <EOL> def execute_as ( command , root = False ) : <EOL> try : <EOL> if root and sys . platform == "<STR_LIT:win32>" : <EOL> os . startfile ( command , "<STR_LIT>" ) <EOL> else : <EOL> ret_code , output = execute ( command ) <EOL> return ret_code , output <EOL> except Exception as e : <EOL> print e <EOL> return - <NUM_LIT:1> , '<STR_LIT>' <EOL> def install_or_upgrade ( installed_version , version , user_io ) : <EOL> if not installed_version : <EOL> user_io . out . writeln ( '<STR_LIT>' , front = Color . CYAN ) <EOL> return user_io . request_boolean ( default_option = True ) <EOL> elif Version ( installed_version ) < Version ( version ) : <EOL> user_io . out . writeln ( '<STR_LIT>' <EOL> % ( Version ( installed_version ) , version ) , front = Color . CYAN ) <EOL> return user_io . request_boolean ( default_option = True ) <EOL> elif Version ( installed_version ) >= Version ( version ) : <EOL> user_io . out . writeln ( '<STR_LIT>' <EOL> % ( Version ( installed_version ) , version ) , front = Color . GREEN ) <EOL> def notify_win_reg_changes ( ) : <EOL> '''<STR_LIT>''' <EOL> import ctypes <EOL> from ctypes . wintypes import HWND , UINT , WPARAM , LPARAM , LPVOID <EOL> LRESULT = LPARAM <EOL> SendMessage = ctypes . windll . user32 . SendMessageW <EOL> SendMessage . argtypes = HWND , UINT , WPARAM , LPVOID <EOL> SendMessage . restype = LRESULT <EOL> HWND_BROADCAST = <NUM_LIT> <EOL> WM_SETTINGCHANGE = <NUM_LIT> <EOL> SendMessage ( HWND_BROADCAST , WM_SETTINGCHANGE , <NUM_LIT:0> , u'<STR_LIT>' ) <EOL> def add2path ( folder ) : <EOL> if not folder : <EOL> return <EOL> import _winreg <EOL> HKCU = _winreg . HKEY_CURRENT_USER <EOL> ENV = "<STR_LIT>" <EOL> PATH = "<STR_LIT>" <EOL> DEFAULT = u"<STR_LIT>" <EOL> with _winreg . CreateKey ( HKCU , ENV ) as key : <EOL> try : <EOL> envpath = _winreg . QueryValueEx ( key , PATH ) [ <NUM_LIT:0> ] <EOL> except WindowsError : <EOL> envpath = DEFAULT <EOL> paths = [ envpath ] <EOL> if folder not in envpath : <EOL> if os . path . isdir ( folder ) : <EOL> paths . insert ( <NUM_LIT:0> , folder ) <EOL> envpath = os . pathsep . join ( paths ) <EOL> _winreg . SetValueEx ( key , PATH , <NUM_LIT:0> , _winreg . REG_EXPAND_SZ , envpath ) <EOL> _winreg . CloseKey ( key ) <EOL> notify_win_reg_changes ( ) <EOL> return str ( envpath ) . split ( '<STR_LIT:;>' ) <EOL> def update_progress ( progress , total_size ) : <EOL> '''<STR_LIT>''' <EOL> bar_length = <NUM_LIT> <EOL> status = "<STR_LIT>" <EOL> if progress < <NUM_LIT:0> : <EOL> progress = <NUM_LIT:0> <EOL> status = "<STR_LIT>" <EOL> if progress >= <NUM_LIT:1> : <EOL> progress = <NUM_LIT:1> <EOL> status = "<STR_LIT>" <EOL> block = int ( round ( bar_length * progress ) ) <EOL> text = "<STR_LIT>" . format ( "<STR_LIT:#>" * block + <EOL> "<STR_LIT:->" * ( bar_length - block ) , <EOL> progress * <NUM_LIT:100> , total_size , status ) <EOL> sys . stdout . write ( text ) <EOL> sys . stdout . flush ( ) <EOL> def download ( url , filename , dl_progress = None , download_dir = None ) : <EOL> '''<STR_LIT>''' <EOL> if not download_dir : <EOL> download_dir = tempfile . mkdtemp ( ) <EOL> if not dl_progress : <EOL> def dl_progress_callback_cmd ( count , block_size , total_size ) : <EOL> update_progress ( min ( count * block_size , total_size ) / total_size , <EOL> total_size / ( <NUM_LIT> ** <NUM_LIT:2> ) ) <EOL> dl_progress_callback = dl_progress_callback_cmd <EOL> else : <EOL> dl_progress_callback = dl_progress <EOL> print '<STR_LIT>' , filename <EOL> print '<STR_LIT>' , url <EOL> filename = os . path . join ( download_dir , filename ) <EOL> print '<STR_LIT>' , filename <EOL> urllib . urlretrieve ( url , filename , reporthook = dl_progress_callback ) <EOL> return filename <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> file_ = download ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> print file_ </s>
