screenlets.install

Package screenlets :: Module install |
[hide private]
[frames] | no frames]

# Source Code for Module screenlets.install

      1  # This program is free software: you can redistribute it and/or modify
      2  # it under the terms of the GNU General Public License as published by
      3  # the Free Software Foundation, either version 3 of the License, or
      4  # (at your option) any later version.
      5  #
      6  # This program is distributed in the hope that it will be useful,
      7  # but WITHOUT ANY WARRANTY; without even the implied warranty of
      8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      9  # GNU General Public License for more details.
     10  #
     11  # You should have received a copy of the GNU General Public License
     12  # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     13  #
     14  #  screenlets.install (c) RYX (Rico Pfaus 2007) <ryx@ryxperience.com>,
     15  #  Whise (Helder Fraga) 2008 <helder.fraga@hotmail.com>
     16  #
     17
     18  import screenlets
     19  from screenlets import utils
     20  import os
     21  import gettext
     22
     23  gettext.textdomain('screenlets')
     24  gettext.bindtextdomain('screenlets', screenlets.INSTALL_PREFIX +  '/share/locale')
     25
     26
     27  # stub for gettext
     28 -def _(s):
     29          #return s
     30          return gettext.gettext(s)
     31
     32
     33  if os.geteuid()==0:
     34          # we run as root, install system-wide
     35          USER = 0
     36          DIR_USER                = screenlets.DIR_USER_ROOT
     37          DIR_AUTOSTART   = '/etc/xdg/autostart'                  # TODO: use pyxdg here
     38  else:
     39          # we run as normal user, install into $HOME
     40          USER = 1
     41          DIR_USER                = screenlets.DIR_USER
     42          DIR_AUTOSTART = utils.get_autostart_dir()
     43
     44 -class ScreenletInstaller(object):
     45          """A simple utility to install screenlets into the current user's directory
     46          (so either into $HOME/.screenlets/ for normal users or, if run as root,
     47          into screenlets.INSTALL_PREFIX/share/screenlets/)."""
     48
     49 -        def __init__ (self):
     50                  self._message = ''
     51
     52
     53 -        def get_info_from_package_name (self, filename):
     54                  """Return all info we can get from the package-name or return None
     55                  if something went wrong. If nothing failed, the returned value is
     56                  a 4-tuple of the form: (name, version, basename, extension)."""
     57                  base    = os.path.basename(filename)
     58                  ext             = str(filename)[len(str(filename)) -3:]
     59                  # prepend "tar." if we have a bz2 or gz archive
     60                  tar_opts = 'xfz'
     61                  if ext == 'bz2':
     62                          tar_opts = 'xfj'
     63                  if ext == 'skz':
     64                          screenlets.show_error(None,_('This type of karamba theme is not supported yet\n Only older versions can be used'))
     65                          return False
     66                  # extract archive to temporary dir
     67
     68                  if not os.path.isdir('/tmp/screenlets/'):
     69                          os.system('mkdir ' + '/tmp/screenlets/')
     70                  try: os.system('rm -rf /tmp/screenlets/install-temp')
     71                  except: pass
     72                  tmpdir = '/tmp/screenlets' + '/install-temp/'
     73                  os.system('mkdir %s' % tmpdir)
     74
     75
     76
     77                  os.system('tar %s %s -C %s' % (tar_opts, chr(34)+filename+chr(34), tmpdir))
     78                  for dd in os.listdir(tmpdir):
     79                          if str(dd).endswith('.theme'):
     80                                  os.system('mv ' + tmpdir + ' ' + '/tmp/screenlets/' + dd[:-6])
     81                                  os.system('mkdir %s' % tmpdir)
     82                                  os.system('mv ' + '/tmp/screenlets/' + dd[:-6] + ' '+ tmpdir)
     83                                  name = dd[:-6]
     84                                  return (name, ext)
     85
     86                  for d in tmpdir : #for each item in folders
     87                          if os.path.exists(d) and os.path.isdir(d): #is it a valid folder?
     88                                  for f in os.listdir(tmpdir):
     89
     90                                          name = f
     91                  try:
     92                          print name
     93                          return (name, ext)
     94                  except:
     95
     96                          return False
     97
     98 -        def get_result_message (self):
     99                  """Return a human-readable result message about the last operation."""
    100                  return self._message
    101
    102 -        def is_installed (self, name):
    103                  """Checks if the given screenlet with the name defined by 'name'
    104                  (without trailing 'Screenlet') is already installed in the current
    105                  install target location."""
    106                  return os.path.isdir(DIR_USER + '/' + name)
    107
    108 -        def install (self, filename):
    109                  """Install a screenlet from a given archive-file. Extracts the
    110                  contents of the archive to the user's screenlet dir."""
    111                  print 'Installing %s' % filename
    112                  result = False
    113                  # get name of screenlet
    114                  #basename       = os.path.basename(filename)
    115                  #extension      = os.path.splitext(filename)
    116                  #name           = basename[:basename.find('.')]
    117                  #print name
    118                  info = self.get_info_from_package_name(filename)
    119                  if info == False:
    120                          self._message= _("Archive is damaged or unsupported, use only tar, bz2 or gz.")
    121                          return False
    122                  name    = info[0]
    123                  ext             = info[1]
    124
    125                  tar_opts = 'xfz'
    126                  if ext == 'bz2':
    127                          tar_opts = 'xfj'
    128
    129
    130
    131                  # check if screenlet is already installed
    132                  #found_path = screenlets.utils.find_first_screenlet_path(name)
    133                  if self.is_installed(name):#found_path != None:
    134                          if screenlets.show_question(None,(_("The %(slet)sScreenlet is already installed in '%(dir)s'.\nDo you wish to continue?") % {"slet":name, "dir":DIR_USER}),(_('Install %s') % (name))):
    135                                  pass
    136                          else:
    137                                  self._message= _('%sScreenlet is already installed') % (name)
    138                                  return False
    139                  # check extension and create appropriate args for tar
    140                  tmpdir = screenlets.DIR_TMP + '/install-temp'
    141                  # verify contents
    142                  if not os.path.isdir('%s/%s' % (tmpdir, name)):
    143                          # dir missing
    144                          self._message = _("Invalid archive. Archive must contain a directory with the screenlet's name.")
    145                  elif not os.path.isfile('%s/%s/%sScreenlet.py' % (tmpdir, name, name)):
    146                          # Not a screenlet , lets check for karamba theme
    147                          themename = ''
    148                          for findtheme in os.listdir('%s/%s/' % (tmpdir, name)):
    149                                  if str(findtheme).lower().endswith('.theme'):
    150                                          print findtheme
    151                                          if themename == '':
    152                                                  print tmpdir + '/'+ name + '/' + themename[:-6] + '.py'
    153                                                  if not os.path.isfile(tmpdir + '/'+ name + '/' + findtheme[:-6] + '.py'):
    154                                                          themename = findtheme[:-6]
    155                                                  else:
    156                                                          self._message = _("Compatibility for this karamba theme is not yet implemented")
    157                                                          return False
    158                          if themename != '':
    159                                  name1 = name.replace(' ','')
    160                                  name1 = name1.replace('-','')
    161                                  name1 = name1.replace('.','')
    162                                  name1 = name1.replace('_','')
    163                                  print name1
    164                                  #os.system('tar %s %s -C %s' % (tar_opts, chr(34)+filename+chr(34), DIR_USER))
    165                                  #os.system('mkdir %s/%s' % (DIR_USER,name1))
    166                                  if self.is_installed(name1):os.system('rm -rf %s/%s' % (DIR_USER, name1))
    167
    168                                  os.system('mv %s/%s %s/%s ' % (chr(34) + tmpdir, name + chr(34),DIR_USER,name1))#tar %s %s -C %s' % (tar_opts, chr(34)+filename+chr(34), DIR_USER))
    169                                  os.system('mkdir %s/%s/themes' % (DIR_USER,name1))
    170                                  os.system('mkdir %s/%s/themes/default' % (DIR_USER,name1))
    171                                  os.system('cp ' + screenlets.INSTALL_PREFIX + '/share/screenlets-manager/karamba.png ' + DIR_USER + '/' + name1 + '/themes/default/')
    172                                  if os.path.isfile(DIR_USER + '/' + name1 + '/icon.png') == False or os.path.isfile(DIR_USER + '/' + name1 + '/icon.svg') == False:
    173                                          os.system('cp ' + screenlets.INSTALL_PREFIX + '/share/screenlets-manager/karamba.png ' + DIR_USER + '/' + name1 + '/icon.png')
    174                                  widgetengine = open(screenlets.INSTALL_PREFIX + '/share/screenlets-manager/KarambaScreenlet.py', 'r')
    175                                  enginecopy = widgetengine.read()
    176                                  widgetengine.close()
    177                                  widgetengine = None
    178                                  enginecopy = enginecopy.replace('KarambaScreenlet',name1 + 'Screenlet')
    179
    180                                  enginesave = open(DIR_USER + '/' + name1 + '/' + name1 + 'Screenlet.py','w')
    181                                  enginesave.write(enginecopy)
    182                                  enginesave.close()
    183                                  self._message = _("Karamba theme was successfully installed")
    184                                  result = True
    185                          else:self._message = _("Invalid archive. Archive does not contain a screenlet or a valid karamba theme.")
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196                  else:
    197                          # check for package-info
    198
    199                          if not os.path.isfile('%s/%s/Screenlet.package' % (tmpdir, name)):
    200                                  if screenlets.show_question(None,(_("%s was not packaged with the screenlet packager. Do you wish to continue and try to install it?") % (name)),(_('Install %s') % (name))):
    201                                          pass
    202                                  else:
    203                                          self._message = _("This package was not packaged with the screenlet packager.")
    204                                          return False
    205
    206                          # copy archive to user dir (and create if not exists)
    207                          utils.create_user_dir()
    208                          os.system('tar %s %s -C %s' % (tar_opts, chr(34)+filename+chr(34), DIR_USER))
    209                          # delete package info from target dir
    210                          os.system('rm %s/%s/Screenlet.package' % (DIR_USER, name))
    211                          # set msg/result
    212                          self._message = _("The %sScreenlet has been succesfully installed.") % name
    213                          result = True
    214                  # remove temp contents
    215                  os.system('rm -rf %s/install-temp' % screenlets.DIR_TMP)
    216                  # ready
    217                  return result
    218