Commits

coderanger  committed 0baaebb

Include an implmentation of relpath for 2.5 compat.

  • Participants
  • Parent commits 52f6f31

Comments (0)

Files changed (2)

File txclib/commands.py

 import ConfigParser
 
 from txclib import utils, project
-from txclib.utils import parse_json, compile_json
+from txclib.utils import parse_json, compile_json, relpath
 
 def cmd_init(argv, path_to_tx):
     "Initialize a new transifex project."
 
         file = args[0]
         # Calculate relative path
-        path_to_file = os.path.relpath(file, path_to_tx)
+        path_to_file = relpath(file, path_to_tx)
         _set_source_file(path_to_tx, resource, options.language, path_to_file)
 
     else:
             parser.error("Please specify a file")
 
         # Calculate relative path
-        path_to_file = os.path.relpath(args[0], path_to_tx)
+        path_to_file = relpath(args[0], path_to_tx)
         # Chdir to the root dir
         os.chdir(path_to_tx)
 
                 " the source file with the -s flag.")
         if execute:
             utils.MSG("Updating source for resource %s ( %s -> %s )." % (resource,
-                source_language, os.path.relpath(source_file, path_to_tx)))
+                source_language, relpath(source_file, path_to_tx)))
             _set_source_file(path_to_tx, resource, source_language,
-                os.path.relpath(source_file, path_to_tx))
+                relpath(source_file, path_to_tx))
         else:
             utils.MSG('\ntx set --source -r %(res)s -l %(lang)s %(file)s\n' % {
                 'res': resource,
                 'lang': source_language,
-                'file': os.path.relpath(source_file, curpath)})
+                'file': relpath(source_file, curpath)})
 
     prj = project.Project(path_to_tx)
     root_dir = os.path.abspath(path_to_tx)
             utils.MSG('tx set -r %(res)s -l %(lang)s %(file)s' % {
                 'res': resource,
                 'lang': lang,
-                'file': os.path.relpath(f_path, curpath)})
+                'file': relpath(f_path, curpath)})
 
     prj.save()
     utils.MSG("Done.")
     utils.MSG("Setting source file for resource %s.%s ( %s -> %s )." % (
         proj, res, lang, path_to_file))
 
-    path_to_file = os.path.relpath(path_to_file, root_dir)
+    path_to_file = relpath(path_to_file, root_dir)
 
     prj = project.Project(path_to_tx)
 
 
     utils.MSG("Updating translations for resource %s ( %s -> %s )." % (resource,
         lang, path_to_file))
-    path_to_file = os.path.relpath(path_to_file, root_dir)
+    path_to_file = relpath(path_to_file, root_dir)
     prj.config.set("%s.%s" % (proj, res), "trans.%s" % lang,
         path_to_file)
 

File txclib/utils.py

             int(bold), COLORS.index(color_name) + 30, text)
     else:
         return text
+
+
+##############################################
+# relpath implementation taken from Python 2.7
+
+if not hasattr(os.path, 'relpath'):
+    if os.path is sys.modules.get('ntpath'):
+        def relpath(path, start=os.path.curdir):
+            """Return a relative version of a path"""
+        
+            if not path:
+                raise ValueError("no path specified")
+            start_list = os.path.abspath(start).split(os.path.sep)
+            path_list = os.path.abspath(path).split(os.path.sep)
+            if start_list[0].lower() != path_list[0].lower():
+                unc_path, rest = os.path.splitunc(path)
+                unc_start, rest = os.path.splitunc(start)
+                if bool(unc_path) ^ bool(unc_start):
+                    raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
+                                                                        % (path, start))
+                else:
+                    raise ValueError("path is on drive %s, start on drive %s"
+                                                        % (path_list[0], start_list[0]))
+            # Work out how much of the filepath is shared by start and path.
+            for i in range(min(len(start_list), len(path_list))):
+                if start_list[i].lower() != path_list[i].lower():
+                    break
+            else:
+                i += 1
+        
+            rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
+            if not rel_list:
+                return os.path.curdir
+            return os.path.join(*rel_list)
+    
+    else:
+        # default to posixpath definition
+        def relpath(path, start=os.path.curdir):
+            """Return a relative version of a path"""
+        
+            if not path:
+                raise ValueError("no path specified")
+        
+            start_list = os.path.abspath(start).split(os.path.sep)
+            path_list = os.path.abspath(path).split(os.path.sep)
+        
+            # Work out how much of the filepath is shared by start and path.
+            i = len(os.path.commonprefix([start_list, path_list]))
+        
+            rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
+            if not rel_list:
+                return os.path.curdir
+            return os.path.join(*rel_list)
+else:
+    from os.path import relpath