<s> import os , sys , subprocess , hashlib , urllib , tempfile , shutil <EOL> from locale import normalize <EOL> sys . path . append ( os . path . dirname ( os . path . dirname ( __file__ ) ) ) <EOL> import timesavers , xxdg . DesktopEntry , xxdg . BaseDirectory <EOL> def get_appimages_in_dir ( directory ) : <EOL> "<STR_LIT>" <EOL> list = [ ] <EOL> files = timesavers . run_shell_command ( "<STR_LIT>" + directory + "<STR_LIT:'>" ) <EOL> for file in files : <EOL> AI = AppImage ( file ) <EOL> if AI . check_whether_is_appimage ( ) == True : <EOL> list . append ( AI ) <EOL> return list <EOL> class AppImage : <EOL> def __init__ ( self , path ) : <EOL> self . path = os . path . normpath ( os . path . abspath ( path ) ) <EOL> self . path_md5 = hashlib . md5 ( '<STR_LIT>' + urllib . quote ( self . path ) ) . hexdigest ( ) <EOL> self . is_appimage = False <EOL> self . iconfile_path = os . path . expanduser ( '<STR_LIT>' ) + self . path_md5 + '<STR_LIT>' <EOL> desktopfiles_location = xxdg . BaseDirectory . xdg_data_home + "<STR_LIT>" <EOL> self . desktopfile_path = desktopfiles_location + self . path_md5 + "<STR_LIT>" <EOL> def check_whether_is_appimage ( self ) : <EOL> "<STR_LIT>" <EOL> type = timesavers . run_shell_command ( "<STR_LIT>" + os . path . realpath ( self . path ) + "<STR_LIT>" ) <EOL> if type != None : <EOL> self . is_appimage = True <EOL> return True <EOL> else : <EOL> return False <EOL> def get_file ( self , filepath , destination = None ) : <EOL> "<STR_LIT>" <EOL> command = "<STR_LIT>" + self . path + "<STR_LIT>" + filepath + "<STR_LIT:'>" <EOL> result = timesavers . run_shell_command ( command , False ) <EOL> if result != "<STR_LIT>" : <EOL> return result <EOL> else : <EOL> return None <EOL> def get_desktop_filename ( self , destination = None ) : <EOL> command = "<STR_LIT>" + self . path + "<STR_LIT:'>" <EOL> toplevel_files = timesavers . run_shell_command ( command ) <EOL> for toplevel_file in toplevel_files : <EOL> if "<STR_LIT>" in toplevel_file : <EOL> return toplevel_file <EOL> def get_icon_filename ( self , destination = None ) : <EOL> return "<STR_LIT>" <EOL> def get_file_list ( self ) : <EOL> command = "<STR_LIT>" + self . path + "<STR_LIT:'>" <EOL> return timesavers . run_shell_command ( command ) <EOL> def install_desktop_integration ( self , parentifthreaded = None ) : <EOL> if not os . path . exists ( self . iconfile_path ) : <EOL> try : os . makedirs ( os . path . dirname ( self . iconfile_path ) ) <EOL> except : pass <EOL> print "<STR_LIT>" % ( self . iconfile_path ) <EOL> f = open ( self . iconfile_path , "<STR_LIT:w>" ) <EOL> f . write ( self . get_file ( "<STR_LIT>" ) ) <EOL> f . close ( ) <EOL> if not os . path . exists ( self . desktopfile_path ) : <EOL> try : os . makedirs ( os . path . dirname ( self . desktopfile_path ) ) <EOL> except : pass <EOL> print "<STR_LIT>" % ( self . desktopfile_path ) <EOL> f = tempfile . NamedTemporaryFile ( delete = False ) <EOL> f . write ( self . get_file ( self . get_desktop_filename ( ) ) ) <EOL> f . close ( ) <EOL> desktop = xxdg . DesktopEntry . DesktopEntry ( ) <EOL> desktop . parse ( f . name ) <EOL> desktop . set ( "<STR_LIT>" , desktop . get ( "<STR_LIT>" ) ) <EOL> desktop . set ( "<STR_LIT>" , desktop . get ( "<STR_LIT>" ) ) <EOL> try : <EOL> if desktop . get ( "<STR_LIT>" ) : <EOL> desktop . set ( "<STR_LIT>" , desktop . get ( "<STR_LIT>" ) ) <EOL> desktop . set ( "<STR_LIT>" , self . path ) <EOL> except : <EOL> pass <EOL> desktop . set ( "<STR_LIT>" , self . iconfile_path ) <EOL> desktop . set ( "<STR_LIT>" , self . path ) <EOL> desktop . set ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> desktop . set ( "<STR_LIT>" , '<STR_LIT:">' + self . path + '<STR_LIT:">' ) <EOL> desktop . write ( f . name ) <EOL> os . chmod ( f . name , <NUM_LIT:0> <NUM_LIT> ) <EOL> print self . desktopfile_path <EOL> shutil . move ( f . name , self . desktopfile_path ) <EOL> if os . env ( "<STR_LIT>" ) == "<STR_LIT:4>" : <EOL> timesavers . run_shell_command ( "<STR_LIT>" ) <EOL> def uninstall_desktop_integration ( self ) : <EOL> if os . path . isfile ( self . desktopfile_path ) : <EOL> try : <EOL> os . unlink ( self . desktopfile_path ) <EOL> print "<STR_LIT>" % ( self . desktopfile_path ) <EOL> except : <EOL> print "<STR_LIT>" % ( self . desktopfile_path ) <EOL> if os . path . isfile ( self . iconfile_path ) : <EOL> try : <EOL> os . unlink ( self . iconfile_path ) <EOL> print "<STR_LIT>" % ( self . iconfile_path ) <EOL> except : <EOL> print "<STR_LIT>" % ( self . iconfile_path ) <EOL> def extract ( self , destination_dir = "<STR_LIT>" ) : <EOL> dest_dir = os . path . join ( destination_dir , os . path . basename ( self . path ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) + "<STR_LIT>" ) <EOL> print "<STR_LIT>" <EOL> def set_executable_bit ( self , boolean ) : <EOL> if boolean == True : <EOL> timesavers . run_shell_command ( "<STR_LIT>" + self . path + "<STR_LIT:'>" ) <EOL> else : <EOL> timesavers . run_shell_command ( "<STR_LIT>" + self . path + "<STR_LIT:'>" ) <EOL> def __repr__ ( self ) : <EOL> if self . is_appimage : <EOL> existing = "<STR_LIT>" <EOL> else : <EOL> existing = "<STR_LIT>" <EOL> return "<STR_LIT>" % ( existing , self . path ) <EOL> def main ( ) : <EOL> print "<STR_LIT>" <EOL> appimages = get_appimages_in_dir ( "<STR_LIT>" ) <EOL> for ai in appimages : <EOL> print ai <EOL> ai . install_desktop_integration ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
