Package screenlets :: Module install

[hide private]
[frames] | no frames]

# Source Code for Module screenlets.install

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