gnulib-tool-py
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gnulib-tool-py] func_relativize


From: Bruno Haible
Subject: Re: [gnulib-tool-py] func_relativize
Date: Fri, 27 Jul 2012 11:52:06 +0200
User-agent: KMail/4.7.4 (Linux/3.1.10-1.9-desktop; KDE/4.7.4; x86_64; ; )

Hi Dmitriy,

> The plans for the next two days will be to finish
> GLFileAssistant.add_or_update, GLFileAssistant.add and to continue to work
> on GLImport. As I see this part of code is the most hard

Yes, this is not so easy, and I'm glad that through the GLFileAssistant
class you found a structure for it.

> how func_relativize works. I undestand what it does, but
> can't understand how. Could you help a bit, please? I could have used
> default Python's relpath, but it returns the wrong result, because it reads
> directories from system (while we need to compute reldir without scanning
> system).

Correct. func_relativize is a purely syntactic operation.

The coded implementation is the simplest one you can think of. Here's how
it works:

Suppose DIR1 = a/b/c/d/e/ and DIR2 = a/b/c/f/g/h/
Then the result should be ../../f/g/h/.
But note that DIR1 or DIR2 could also contain components '.' or '..' -
this complicates the cases.

How do we get there? The idea is to proceed in steps like this:

DIR0 = /home/bruno           DIR1 = a/b/c/d/e/   DIR2 = a/b/c/f/g/h/
DIR0 = /home/bruno/a         DIR1 = b/c/d/e/     DIR2 = b/c/f/g/h/
DIR0 = /home/bruno/a/b       DIR1 = c/d/e/       DIR2 = c/f/g/h/
DIR0 = /home/bruno/a/b/c     DIR1 = d/e/         DIR2 = f/g/h/
DIR0 = /home/bruno/a/b/c/d   DIR1 = e/           DIR2 = ../f/g/h/
DIR0 = /home/bruno/a/b/c/d/e DIR1 =              DIR2 = ../../f/g/h/

To this effect you have to understand what
  sed_first='s,^\([^/]*\)/.*$,\1,'
  sed_rest='s,^[^/]*/*,,'
  sed_last='s,^.*/\([^/]*\)$,\1,'
  sed_butlast='s,/*[^/]*$,,'
do:
  sed_first maps   a/b/c/d  to   a
  sed_rest maps    a/b/c/d  to   b/c/d
  sed_last maps    a/b/c/d  to   d
  sed_butlast maps a/b/c/d  to   a/b/c

At each step we try to eat the first element from DIR1 and massage DIR0
and DIR2 in consequences.

> Could we do the work of the func_relativize a bit simplier? I
> think that we can split SRC and DEST paths using str.split('/') to work
> with them

Yes, you can do this. Be careful that slashes at the end are ignored
(since the file names are supposed to be directories) and multiple
consecutive slashes are treated like a single slash (POSIX semantics).

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]