# HG changeset patch
# User Mads Kiilerich <[email protected]>
# Date 1239750286 -7200
# Node ID 200bc69bd39cd74927d3ec1cb61ba45a7b06ebe7
# Parent  c6d3fe0afabcf6e9c7fc9a3666e74a4efbb37b19
RFC: Cleanup/destruction of resource paths

Steve said on http://bitbucket.org/tortoisehg/stable/issue/144 :

  I would be ok with a patch for nautilus-thg.py that removes the environment
  variable and hard-coded path hacks since the symlink method works better than
  both of them, and we want the installed path to be the default.

  For the path problems (icons, COPYING.txt), my thought was for packagers to 
add
  paths.py into the hggtk package with path functions for all of these things
  which get scattered about. shlib.py and about.py would look for this file
  first, then fall back to their original search methods.

I gave it a try and ended up with this, mostly removing and rewriting the old
search methods.

Tested on linux running from source and from installed rpm.

It has _not_ been tested on windows. I have probably removed some essential
hacks and workarounds which must be added again somehow. But which hacks were
essential and which were cruft?

How to continue from here? Can some parts of this patch be used? Which? Could
the code needed for windows be added on top of this? Could the code
specific for frozen windows packages be written to the paths.pys?

diff --git a/contrib/nautilus-thg.py b/contrib/nautilus-thg.py
--- a/contrib/nautilus-thg.py
+++ b/contrib/nautilus-thg.py
@@ -9,25 +9,21 @@
 #
 # Published under the GNU GPL
 
+import os
+import subprocess
+import sys
+import urllib
+
+# sys.path.insert(0, 
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) # if symlink to 
source tree
+# sys.path.insert(0, '/some/path') # if other custom PYTHON_PATH is needed
+
 import gtk
 import gobject
 import nautilus
 
-try:
-    from mercurial import demandimport
-except ImportError:
-    # workaround to use user's local python libs
-    userlib = os.path.expanduser('~/lib/python')
-    if os.path.exists(userlib) and userlib not in sys.path:
-        sys.path.append(userlib)
-    from mercurial import demandimport
+from mercurial import demandimport
 demandimport.enable()
 
-import os
-import subprocess
-import sys
-import urllib
-
 from mercurial import hg, ui, match, util
 try:
     from mercurial.error import RepoError
@@ -35,7 +31,9 @@
     from mercurial.repo import RepoError
 from mercurial.node import short
 
-TORTOISEHG_PATH = '~/tools/tortoisehg-dev'
+import tortoise.thgutil
+import tortoise.menuthg
+
 nofilecmds = 'about serve synch repoconfig userconfig merge unmerge'.split()
 nocachecmds = 'about serve repoconfig userconfig'.split()
 
@@ -50,47 +48,10 @@
         self.cacheroot = None
         self.scanStack = []
 
-        # check if nautilus-thg.py is a symlink first
-        pfile = __file__
-        if pfile.endswith('.pyc'):
-            pfile = pfile[:-1]
-        path = os.path.dirname(os.path.realpath(pfile))
-        thgpath = os.path.normpath(os.path.join(path, '..'))
-        testpath = os.path.join(thgpath, 'tortoise')
-        if os.path.isdir(testpath):
-            if thgpath not in sys.path:
-                sys.path.insert(0, thgpath)
-        else:
-            # try environment or hard-coded path
-            thgpath = os.environ.get('TORTOISEHG_PATH', TORTOISEHG_PATH)
-            thgpath = os.path.normpath(os.path.expanduser(thgpath))
-            if os.path.exists(thgpath) and thgpath not in sys.path:
-                sys.path.insert(0, thgpath)
-        # else assume tortoise is already in PYTHONPATH
-        try:
-            import tortoise.thgutil
-            import tortoise.menuthg
-        except ImportError, e:
-            # if thgutil is not found, then repository cannot be found
-            # if menuthg is not found, you have an older version in sys.path
-            print e
-            self.menu = None
-            return
-
-        self.env = os.environ
-        self.env['PYTHONPATH'] = ':'.join(sys.path)
-        self.env['TORTOISEHG_PATH'] = thgpath
-        self.env['THG_ICON_PATH'] = os.path.join(thgpath, 'icons')
-
-        self.hgproc = tortoise.thgutil.find_path('hgtk',
-              tortoise.thgutil.get_prog_root())
-        if not self.hgproc:
-            self.hgproc = tortoise.thgutil.find_path('hgtk')
-        self.ipath = os.path.join(thgpath, 'icons', 'tortoise')
         self.menu = tortoise.menuthg.menuThg()
 
     def icon(self, iname):
-        return os.path.join(self.ipath, iname)
+        return tortoise.thgutil.get_icon_path('tortoise', iname)
 
     def get_path_for_vfs_file(self, vfs_file):
         if vfs_file.is_gone() or vfs_file.get_uri_scheme() != 'file':
@@ -139,10 +100,11 @@
             if diffcmd:
                 cmdline = ['hg', diffcmd]
                 cmdline.extend(self.files)
-                subprocess.Popen(cmdline, shell=False, env=self.env, cwd=cwd)
+                subprocess.Popen(cmdline, shell=False, cwd=cwd)
                 return
 
-        cmdopts  = [sys.executable, self.hgproc, hgtkcmd]
+        hgtk_binary = tortoise.thgutil.get_hgtk_binary()
+        cmdopts = [sys.executable, hgtk_binary, hgtkcmd]
 
         if hgtkcmd not in nofilecmds and self.files:
             # Use stdin to pass file list (avoid shell command
@@ -152,7 +114,7 @@
         else:
             pipe = None
 
-        stdin = subprocess.Popen(cmdopts, cwd=cwd, stdin=pipe, env=self.env, 
shell=False).stdin
+        stdin = subprocess.Popen(cmdopts, cwd=cwd, stdin=pipe, 
shell=False).stdin
 
         if pipe:
             stdin.write('\n'.join(self.files))
diff --git a/hggtk/about.py b/hggtk/about.py
--- a/hggtk/about.py
+++ b/hggtk/about.py
@@ -66,17 +66,10 @@
             self.set_wrap_license(True)
         self.set_copyright("Copyright 2009 TK Soh and others")
 
-        thg_logo = 
os.path.normpath(shlib.get_tortoise_icon('thg_logo_92x50.png'))
-        thg_icon = os.path.normpath(shlib.get_tortoise_icon('thg_logo.ico'))
-        prog_root = os.path.dirname(os.path.dirname(os.path.dirname(thg_icon)))
-        try:
-            license_file = os.path.join(prog_root, "COPYING.txt")
-            self.set_license(file(license_file).read())
-        except IOError:
-            import hgtk
-            license = hgtk.shortlicense.splitlines()[1:]
-            self.set_license('\n'.join(license))
-
+        thg_logo = shlib.get_tortoise_icon('thg_logo_92x50.png')
+        thg_icon = shlib.get_tortoise_icon('thg_logo.ico')
+        license_text = shlib.get_license_text()
+        self.set_license(license_text)
         self.set_comments("with " + lib_versions + "\n\n" + comment)
         self.set_logo(gtk.gdk.pixbuf_new_from_file(thg_logo))
         self.set_icon_from_file(thg_icon)
diff --git a/hggtk/shlib.py b/hggtk/shlib.py
--- a/hggtk/shlib.py
+++ b/hggtk/shlib.py
@@ -16,6 +16,20 @@
 import time
 from mercurial.i18n import _
 
+import hgtk
+
+source_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+icons_path = os.path.join(source_dir, 'icons')
+license_file = os.path.join(source_dir, "COPYING.txt")
+try:
+    import hggtk.paths
+    # icons_path = '/usr/share/pixmaps/tortoisehg/icons'
+    # license_file = '/usr/share/doc/tortoisehg-0.7.3/COPYING.txt'
+    icons_path = hggtk.paths.icons_path
+    license_file = hggtk.paths.license_file
+except ImportError:
+    pass
+
 class SimpleMRUList(object):
     def __init__(self, size=10, reflist=[], compact=True):
         self._size = size
@@ -140,32 +154,22 @@
 
 def get_tortoise_icon(thgicon):
     '''Find a tortoise icon, apply to PyGtk window'''
-    # The context menu should set this variable
-    var = os.environ.get('THG_ICON_PATH', None)
-    paths = var and [ var ] or []
+    icon_path = os.path.join(icons_path, 'tortoise', thgicon)
+    if not os.path.isfile(icon_path):
+        print _('icon not found'), thgicon
+    return icon_path
+
+def get_license_text():
     try:
-        # Else try relative paths from hggtk, the repository layout
-        fdir = os.path.dirname(__file__)
-        paths.append(os.path.join(fdir, '..', 'icons'))
-        # ... or the unix installer layout
-        paths.append(os.path.join(fdir, '..', '..', '..',
-            'share', 'pixmaps', 'tortoisehg', 'icons'))
-        paths.append(os.path.join(fdir, '..', '..', '..', '..',
-            'share', 'pixmaps', 'tortoisehg', 'icons'))
-    except NameError: # __file__ is not always available
-        pass
-    for p in paths:
-        path = os.path.join(p, 'tortoise', thgicon)
-        if os.path.isfile(path):
-            return path
-    else:
-        print _('icon not found'), thgicon
-        return None
+        return file(license_file).read()
+    except IOError:
+        import hgtk # FIXME: Circular dependency?
+        return '\n'.join(hgtk.shortlicense.strip().splitlines())
 
 def version():
     try:
-        import __version__
-        return __version__.version
+        import hggtk.__version__
+        return hggtk.__version__.version
     except ImportError:
         return _('unknown')
 
diff --git a/hgtk b/hgtk
old mode 100755
new mode 100644
--- a/hgtk
+++ b/hgtk
@@ -6,36 +6,18 @@
 # Copyright (C) 2008 TK Soh <[email protected]>
 #
 
+import sys
+
+# sys.path.insert(0, 
os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) # if symlink to 
source tree
+# sys.path.insert(0, '/some/path') # if other custom PYTHON_PATH is needed
+
 import pygtk
 pygtk.require('2.0')
 import gtk
 
-from mercurial import demandimport; demandimport.enable()
-import os
-import sys
+from mercurial import demandimport
+demandimport.enable()
 
-if hasattr(sys, "frozen"):
-    thgdir = os.path.dirname(sys.executable)
-    os.environ['THG_ICON_PATH'] = os.path.join(thgdir, 'icons')
-else:
-    # check if hggtk is a symlink first
-    pfile = __file__
-    if pfile.endswith('.pyc'):
-        pfile = pfile[:-1]
-    thgpath = os.path.dirname(os.path.realpath(pfile))
-    testpath = os.path.join(thgpath, 'hggtk')
-    if os.path.isdir(testpath):
-        if thgpath not in sys.path:
-            sys.path.insert(0, thgpath)
-    else:
-        # try environment
-        thgpath = os.environ.get('TORTOISEHG_PATH')
-        if thgpath:
-            thgpath = os.path.normpath(os.path.expanduser(thgpath))
-            if os.path.exists(thgpath) and thgpath not in sys.path:
-                sys.path.insert(0, thgpath)
-
-# else assume tortoise is already in PYTHONPATH
 try:
     import hggtk.hgtk
 except ImportError:
diff --git a/tortoise/thgutil.py b/tortoise/thgutil.py
--- a/tortoise/thgutil.py
+++ b/tortoise/thgutil.py
@@ -9,6 +9,18 @@
 
 import os.path
 
+source_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+hgtk_binary = os.path.join(source_dir, 'hgtk')
+icons_path = os.path.join(source_dir, 'icons')
+try:
+    import tortoise.paths
+    # hgtk_binary = '/usr/bin/hgtk'
+    # icons_path = '/usr/share/pixmaps/tortoisehg/icons'
+    hgtk_binary = tortoise.paths.hgtk_binary
+    icons_path = tortoise.paths.icons_path
+except ImportError:
+    pass
+
 def find_root(path):
     p = os.path.isdir(path) and path or os.path.dirname(path)
     while not os.path.isdir(os.path.join(p, ".hg")):
@@ -20,6 +32,15 @@
             return None
     return p
 
+def get_icon_path(*args):
+    icon_path = os.path.join(icons_path, *args)
+    if not os.path.isfile(icon_path):
+        print 'icon not found %r' % icon_path
+    return icon_path
+
+def get_hgtk_binary():
+    return hgtk_binary
+
 if os.name == 'nt':
     from win32com.shell import shell, shellcon
     import win32con
@@ -30,33 +51,10 @@
 
     USE_OK  = 0     # network drive status
 
-    def find_path(pgmname, path=None, ext=None):
-        """ return first executable found in search path """
-        ospath = path and path or os.environ['PATH']
-        ospath = ospath.split(os.pathsep)
-        pathext = ext and ext or os.environ.get('PATHEXT', 
'.COM;.EXE;.BAT;.CMD')
-        pathext = pathext.lower().split(os.pathsep)
-
-        for path in ospath:
-            ppath = os.path.join(path, pgmname)
-            for ext in pathext:
-                if os.path.exists(ppath + ext):
-                    return ppath + ext
-        return None
-
-    def get_icon_path(*args):
-        dir = get_prog_root()
-        icon = os.path.join(dir, "icons", *args)
-        if not os.path.isfile(icon):
-            return None
-        return icon
-        
     def get_prog_root():
         key = r"Software\TortoiseHg"
         cat = _winreg.HKEY_LOCAL_MACHINE
         dir = _winreg.QueryValue(cat, key)
-        if 'THG_ICON_PATH' not in os.environ:
-            os.environ['THG_ICON_PATH'] = os.path.join(dir, 'icons')
         return dir
 
     def netdrive_status(drive):
@@ -135,23 +133,8 @@
 
 else: # Not Windows
 
-    def find_path(pgmname, path=None, ext=None):
-        """ return first executable found in search path """
-        ospath = path and path or os.environ['PATH']
-        ospath = ospath.split(os.pathsep)
-        for path in ospath:
-            ppath = os.path.join(path, pgmname)
-            if os.access(ppath, os.X_OK):
-                return ppath
-        return None
-
-    def get_icon_path(*args):
-        return None
-        
     def get_prog_root():
-        defpath = os.path.dirname(os.path.dirname(__file__))
-        path = os.environ.get('TORTOISEHG_PATH', defpath)
-        return os.path.isdir(path) and path or os.path.dirname(path)
+        raise NotImplementedError
 
     def netdrive_status(drive):
         """
@@ -161,4 +144,4 @@
         return None
 
     def icon_to_bitmap(iconPathName):
-        pass
+        raise NotImplementedError

------------------------------------------------------------------------------
This SF.net email is sponsored by:
High Quality Requirements in a Collaborative Environment.
Download a free trial of Rational Requirements Composer Now!
http://p.sf.net/sfu/www-ibm-com
_______________________________________________
Tortoisehg-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tortoisehg-develop

Reply via email to