Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

import-files-by-mtime: cleanups

- Can now copy instead of move (-c, --copy). Both copy and move are
  now cross-filesystem safe, thanks to shutil.

- Checks for existing files before copying or moving. This check can
  be overridden by -R, --replace. This should make it easier to just
  import files without caring too much about which ones are already
  there.

- Changed --format from -f to -F, to leave -f for "--force" if that
  ever turns out to be useful.
  • Loading branch information...
commit 92280dba592d1bcac5c2848388c38149cca32a53 1 parent 1689ff4
@glasserc authored
Showing with 20 additions and 3 deletions.
  1. +20 −3 bin/import-files-by-mtime
View
23 bin/import-files-by-mtime
@@ -24,6 +24,7 @@ find repo
import os
import os.path
import sys
+import shutil
import datetime
import argparse
import EXIF
@@ -41,7 +42,7 @@ class ImportFiles(object):
parser.add_argument('filename', type=str, nargs='+',
help='files to import into the repository')
- parser.add_argument('-f', '--format', type=str, nargs='?', default="%Y/%m/%d/",
+ parser.add_argument('-F', '--format', type=str, nargs='?', default="%Y/%m/%d/",
help="""date format to use (default %%Y/%%m/%%d/); %%f means file basename. Be sure to put a / at the end if you mean this to be a directory!""")
parser.add_argument('-n', '--dry-run', action='store_true', default=False,
help='simulate; print what would be done')
@@ -49,6 +50,12 @@ class ImportFiles(object):
parser.add_argument('-l', '--lowercase', action='store_true', default=False,
help="make new filenames lowercase")
+ parser.add_argument('-c', '--copy', action='store_true', default=False,
+ help="copy instead of moving")
+
+ parser.add_argument('-R', '--replace', action='store_true', default=False,
+ help="replace existing files")
+
parser.add_argument('-e', '--confirm-exif', action='store_true', default=False,
help='confirm mtime against EXIF tags')
@@ -113,13 +120,23 @@ class ImportFiles(object):
new_file = os.path.join(new_dir, new_basename)
- print "Moving", file, 'to', new_file
+ if os.path.exists(new_file) and not self.replace:
+ print "Skipping", file, "because", new_file, "already exists"
+ continue
+
+ if self.copy:
+ print "Copying", file, 'to', new_file
+ else:
+ print "Moving", file, 'to', new_file
if not self.dry_run:
if not os.path.exists(new_dir):
os.makedirs(new_dir)
- os.rename(file, new_file)
+ if self.copy:
+ shutil.copy2(file, new_file)
+ else:
+ shutil.move(file, new_file)
if __name__ == '__main__':
ImportFiles().run(sys.argv[1:])
Please sign in to comment.
Something went wrong with that request. Please try again.