your-tools / pycp

cp and mv with a progress bar
MIT License
148 stars 8 forks source link

Fix usage of weird python basename() (returns nothing for paths with trailing slashes!). #6

Closed toofar closed 10 years ago

toofar commented 10 years ago

This was leading to different results than conventional cp and mv. Ie

$ # Directory heirarchy to to make things somewhat less contrived
$ mkdir testdst
$ touch testdst/dstfile
$ mkdir testsrc
$ touch testsrc/srcfile
$ mv testsrc/ testdst/ #the trailing slashes come from autocomplete
$ ls testdst/
dstfile  testsrc
$ mv testdst/testsrc . #reset
$ pymv testsrc/ testdst/
$ ls testdst/
destfile  srcfile

The problem is os.path.basename being befferent than the posix basename. This is actually documented somewhere, I don't have the references on hand though. It is easy to fix though, just add a os.path.normpath:

>>> import os
>>> t1="/foo/bar/qux"
>>> t2="/foor/bar/qux/"
>>> t2="/foo/bar/qux/"
>>> t3="projects"
>>> t4="projects/"
>>> for i in [t1, t2, t3, t4]: print i, "=>", os.path.basename(i)
...
/foo/bar/qux => qux
/foo/bar/qux/ =>
projects => projects
projects/ =>
>>> for i in [t1, t2, t3, t4]: print i, "=>", os.path.basename(os.path.normpath(i))
...
/foo/bar/qux => qux
/foo/bar/qux/ => qux
projects => projects
projects/ => projects
toofar commented 10 years ago

Fixes issue #4? Same symptoms here and is definitely a bug in my book.