Discussion:
[A-a-p-user] recursive creating of directories
calmar
2005-12-16 14:19:58 UTC
Permalink
Hi all,

when I have a file like:

aa/bb/cc/dd/file.html

and when I want to publish that via ftp, when no directory there yet exist.

it seems aap is not able to create those directories. Aap can
create 'one' and then put the file into it, but aap seems not to
be able to handle subdirectories?

At least, that's what it seems here, hm.

Cheers and thanks,

marco
--
calmar

(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Bram Moolenaar
2005-12-16 15:27:46 UTC
Permalink
Post by calmar
aa/bb/cc/dd/file.html
and when I want to publish that via ftp, when no directory there yet exist.
it seems aap is not able to create those directories. Aap can
create 'one' and then put the file into it, but aap seems not to
be able to handle subdirectories?
At least, that's what it seems here, hm.
That's right, when using ftp Aap only creates one level of directories.
The "mkdir" command of ftp doesn't have a flag to create directories
recursively. I haven't found a great need to handle it inside Aap.
Would need to detect that a "mkdir" fails because a directory is
missing. Can you see what error message the ftp server gives? Hmm,
this might differ between various ftp servers...
--
Lose weight, NEVER Diet again with
The "Invisible Weight Loss Patch"
(spam e-mail)

/// Bram Moolenaar -- ***@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://www.ICCF.nl ///
calmar
2005-12-16 21:42:10 UTC
Permalink
Post by Bram Moolenaar
That's right, when using ftp Aap only creates one level of directories.
The "mkdir" command of ftp doesn't have a flag to create directories
recursively. I haven't found a great need to handle it inside Aap.
Would need to detect that a "mkdir" fails because a directory is
missing. Can you see what error message the ftp server gives? Hmm,
this might differ between various ftp servers...
mkdir: Access failed: 550 aaa/ddd: No such file or directory

ftp can't access the not existing aaa folder, I think.

Cheers and thanks
marco
--
calmar

(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Bram Moolenaar
2005-12-17 12:49:15 UTC
Permalink
Post by calmar
Post by Bram Moolenaar
That's right, when using ftp Aap only creates one level of directories.
The "mkdir" command of ftp doesn't have a flag to create directories
recursively. I haven't found a great need to handle it inside Aap.
Would need to detect that a "mkdir" fails because a directory is
missing. Can you see what error message the ftp server gives? Hmm,
this might differ between various ftp servers...
mkdir: Access failed: 550 aaa/ddd: No such file or directory
ftp can't access the not existing aaa folder, I think.
OK, it's not too difficult to try creating the directory recursively. I
wrote the code for it, but I can't test it, because the ftp server I
have access to does creates the directory recursivel already.
Apparently the "mkdir" command is implemented differently depending on
the ftp server used.

Please try the patch below with your ftp server.


Index: CopyMove.py
===================================================================
RCS file: /cvsroot/a-a-p/Exec/CopyMove.py,v
retrieving revision 1.49
diff -u -r1.49 CopyMove.py
--- CopyMove.py 30 May 2005 11:39:03 -0000 1.49
+++ CopyMove.py 17 Dec 2005 11:36:39 -0000
@@ -271,6 +271,32 @@
return ftp, msg


+def ftp_may_mkdir(recdict, error, ftp, destpath):
+ """If "error" indicates we failed to upload through ftp because the
+ directory doesn't exist try creating that directory. This works
+ recursively.
+ Return non-zero if the directory was created."""
+ if string.find(str(error), "No such file or dir") >= 0:
+ adir = os.path.dirname(destpath)
+ if len(adir) < len(destpath):
+ import ftplib
+ msg_info(recdict, _('Directory "%s" does not appear to exist, creating it...') % adir)
+ try:
+ ftp.mkd(adir)
+ except ftplib.all_errors, e:
+ # Maybe the upper directory doesn't exist, try creating it.
+ if ftp_may_mkdir(recdict, e, ftp, adir):
+ try:
+ ftp.mkd(adir)
+ except ftplib.all_errors, e:
+ msg_info(recdict, _('Could not create directory "%s"') % adir)
+ return 0
+ else:
+ return 0
+ return 1
+ return 0
+
+
def remote_copy_move(rpstack, recdict, copy, from_items, to_item,
optiondict, argexpand, errmsg = 0):
"""Copy or move command. Copy when "copy" is non-zero.
@@ -572,20 +598,9 @@

# If it looks like the directory doesn't exist, try
# creating it.
- if (error and string.find(str(error),
- "No such file or dir") >= 0):
- adir = os.path.dirname(destpath)
- if adir:
- import ftplib
- msg_info(recdict, _('Directory does not appear to exist, creating it...'))
- try:
- ftp.mkd(adir)
- except ftplib.all_errors, e:
- msg_info(recdict, _('Could not create directory'))
- pass
- else:
- msg_info(recdict, _('Directory created, attempt uploading again...'))
- error = store_file(ftp, fromfile, destpath)
+ if error and ftp_may_mkdir(recdict, error, ftp, destpath):
+ msg_info(recdict, _('Directory created, attempt uploading again...'))
+ error = store_file(ftp, fromfile, destpath)

if error:
msg = (_('Cannot upload "%s" to "%s": %s')
--
BEDEVERE: Oooooh!
LAUNCELOT: No "Aaaaarrrrrrggghhh ... " at the back of the throat.
BEDEVERE: No! "Oooooh!" in surprise and alarm!
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- ***@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://www.ICCF.nl ///
calmar
2005-12-17 21:45:10 UTC
Permalink
On Sat, Dec 17, 2005 at 01:49:15PM +0100, Bram Moolenaar wrote:

Hi Bram,
Post by Bram Moolenaar
Post by calmar
ftp can't access the not existing aaa folder, I think.
OK, it's not too difficult to try creating the directory recursively. I
Please try the patch below with your ftp server.
yeah, that seems to work very nicely!

that's the output I get, just in case:

Aap: Uploading ['ddd/aaa/uuu/hello.txt'] to ftp://dd:***@server.org/html/calmar/./ddd/aaa/uuu/hello.txt
Aap: Uploading "/var/www/calmar/ddd/aaa/uuu/hello.txt" to "ftp://dd:***@server.org/html/calmar/./ddd/aaa/uuu/hello.txt"
Aap: Directory "html/calmar/./ddd/aaa/uuu" does not appear to exist, creating it...
Aap: Directory "html/calmar/./ddd/aaa" does not appear to exist, creating it...
Aap: Directory "html/calmar/./ddd" does not appear to exist, creating it...
Aap: Directory created, attempt uploading again...
Aap: Uploaded "ddd/aaa/uuu/hello.txt" to "ftp://dd:***@server.org/html/calmar/./ddd/aaa/uuu/hello.txt"

Cheers,
marco
--
calmar

(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Bram Moolenaar
2005-12-18 13:35:19 UTC
Permalink
Post by calmar
Post by Bram Moolenaar
Post by calmar
ftp can't access the not existing aaa folder, I think.
OK, it's not too difficult to try creating the directory recursively. I
Please try the patch below with your ftp server.
yeah, that seems to work very nicely!
Aap: Directory "html/calmar/./ddd/aaa/uuu" does not appear to exist, creating it...
Aap: Directory "html/calmar/./ddd/aaa" does not appear to exist, creating it...
Aap: Directory "html/calmar/./ddd" does not appear to exist, creating it...
Aap: Directory created, attempt uploading again...
Looks good. Thanks for testing this.
--
Every exit is an entrance into something else.

/// Bram Moolenaar -- ***@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ download, build and distribute -- http://www.A-A-P.org ///
\\\ help me help AIDS victims -- http://www.ICCF.nl ///
calmar
2005-12-19 03:39:39 UTC
Permalink
Post by Bram Moolenaar
Post by calmar
Post by Bram Moolenaar
Post by calmar
ftp can't access the not existing aaa folder, I think.
OK, it's not too difficult to try creating the directory recursively. I
Please try the patch below with your ftp server.
yeah, that seems to work very nicely!
Aap: Directory "html/calmar/./ddd/aaa/uuu" does not appear to exist, creating it...
Aap: Directory "html/calmar/./ddd/aaa" does not appear to exist, creating it...
Aap: Directory "html/calmar/./ddd" does not appear to exist, creating it...
Aap: Directory created, attempt uploading again...
Looks good.
Whenever you want to test something yourself with a ftp server
not capable of recursive mkdir or however:

ftp://n4a403f5:***@eden4.netclusive.de

Feel free to use it, even so there does not seem to be a need,
since app handles is nicely with that patch.

Cheers
marco

PS: for half a year that link should work, then I probably switch to a
'local' hoster.
--
calmar

(o_ It rocks: LINUX + Command-Line-Interface
//\
V_/_ http://www.calmar.ws
Loading...