>From 96f7bad09085273649f4b26a25e9d8fd51279c32 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 31 Jul 2022 20:02:40 +0200 Subject: [PATCH 11/16] gnulib-tool.py: Make --copy-file work. * gnulib-tool.py (main) [copy-file]: Fix reference to uninitialized variable. Fix error handling of os.makedirs. Pass the destdir to the GLFileAssistant. --- ChangeLog | 5 +++++ gnulib-tool.py | 34 +++++++++++++++------------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index a48f89db6d..59bfec205e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2022-07-31 Bruno Haible + gnulib-tool.py: Make --copy-file work. + * gnulib-tool.py (main) [copy-file]: Fix reference to uninitialized + variable. Fix error handling of os.makedirs. Pass the destdir to the + GLFileAssistant. + gnulib-tool.py: Allow module arguments to occur at any position. * gnulib-tool.py (main): Collect the non-option arguments in a single list, regardless of their position. Use parse_known_args instead of diff --git a/gnulib-tool.py b/gnulib-tool.py index f1cf389c36..f20d1157a3 100755 --- a/gnulib-tool.py +++ b/gnulib-tool.py @@ -892,9 +892,11 @@ def main(): elif mode == 'copy-file': srcpath = files[0] + # The second argument is the destination; either a directory ot a file. + # It defaults to the current directory. if len(files) == 2: dest = files[1] - else: # if len(files) != 2 + else: # if len(files) < 2 dest = '.' if not auxdir: auxdir = 'build-aux' @@ -914,53 +916,47 @@ def main(): filesystem = classes.GLFileSystem(config) lookedup, flag = filesystem.lookup(srcpath) if isdir(dest): - destdir = str(dest) + destdir = dest if srcpath.startswith('build-aux/'): - destpath = constants.substart( - 'build-aux/', '%s/' % auxdir, srcpath) + destpath = constants.substart('build-aux/', '%s/' % auxdir, srcpath) elif srcpath.startswith('doc/'): destpath = constants.substart('doc/', '%s/' % docbase, srcpath) elif srcpath.startswith('lib/'): - destpath = constants.substart( - 'lib/', '%s/' % sourcebase, srcpath) + destpath = constants.substart('lib/', '%s/' % sourcebase, srcpath) elif srcpath.startswith('m4/'): destpath = constants.substart('m4/', '%s/' % m4base, srcpath) elif srcpath.startswith('tests/'): - destpath = constants.substart( - 'tests/', '%s/' % testsbase, srcpath) - elif srcpath.startswith('tests=lib/'): - destpath = constants.substart( - 'tests=lib/', '%s/' % testsbase, srcpath) + destpath = constants.substart('tests/', '%s/' % testsbase, srcpath) elif srcpath.startswith('top/'): destpath = constants.substart('top/', '', srcpath) else: # either case destpath = srcpath - else: # if not isdir(destpath) - destdir = os.path.dirname(destpath) - destpath = os.path.basename(destpath) + else: # if not isdir(dest) + destdir = os.path.dirname(dest) + destpath = os.path.basename(dest) # Create the directory for destfile. dirname = os.path.dirname(joinpath(destdir, destpath)) if not config['dryrun']: if dirname and not isdir(dirname): try: # Try to create directories os.makedirs(dirname) - except Exception as error: + except FileExistsError: pass # Copy the file. assistant = classes.GLFileAssistant(config) tmpfile = assistant.tmpfilename(destpath) shutil.copy(lookedup, tmpfile) - already_present = True assistant.setOriginal(srcpath) + assistant.config.setDestDir(destdir) assistant.setRewritten(destpath) if isfile(joinpath(destdir, destpath)): # The file already exists. - assistant.update(lookedup, flag, tmpfile, already_present) + assistant.update(lookedup, flag, tmpfile, True) else: # if not isfile(joinpath(destdir, destpath)) # Install the file. # Don't protest if the file should be there but isn't: it happens - # frequently that developers don't put autogenerated files under version - # control. + # frequently that developers don't put autogenerated files under + # version control. assistant.add(lookedup, flag, tmpfile) if isfile(tmpfile): os.remove(tmpfile) -- 2.34.1