Re: recursive creating of directories
Bram Moolenaar <[email protected]> Sat, 17 Dec 2005 13:49:15 +0100
| Newsgroups | gmane.comp.tools.aap.user |
|---|---|
| Message-ID | <[email protected]> |
Calmar wrote:
> On Fri, Dec 16, 2005 at 04:27:46PM +0100, Bram Moolenaar wrote:
>=20
> > That's right, when using ftp Aap only creates one level of directorie=
s.
> > 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...
> >=20
>=20
> mkdir: Access failed: 550 aaa/ddd: No such file or directory
>=20
> 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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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
=20
=20
+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") >=3D 0:
+ adir =3D 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, errms=
g =3D 0):
"""Copy or move command. Copy when "copy" is non-zero.
@@ -572,20 +598,9 @@
=20
# If it looks like the directory doesn't exist, try
# creating it.
- if (error and string.find(str(error),
- "No such file or dir")=
>=3D 0):
- adir =3D os.path.dirname(destpath)
- if adir:
- import ftplib
- msg_info(recdict, _('Directory does not appe=
ar to exist, creating it...'))
- try:
- ftp.mkd(adir)
- except ftplib.all_errors, e:
- msg_info(recdict, _('Could not create di=
rectory'))
- pass
- else:
- msg_info(recdict, _('Directory created, =
attempt uploading again...'))
- error =3D store_file(ftp, fromfile, dest=
path)
+ if error and ftp_may_mkdir(recdict, error, ftp, dest=
path):
+ msg_info(recdict, _('Directory created, attempt =
uploading again...'))
+ error =3D store_file(ftp, fromfile, destpath)
=20
if error:
msg =3D (_('Cannot upload "%s" to "%s": %s')
--=20
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) PICTURE=
S LTD
/// Bram Moolenaar -- [email protected] -- 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 /=
//
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log fi=
les
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=3D7637&alloc_id=3D16865&op=3Dclick