Skip to content

Instantly share code, notes, and snippets.

@bradwright
Created October 24, 2015 21:11
Show Gist options
  • Select an option

  • Save bradwright/53fd3ce5a58843c1285f to your computer and use it in GitHub Desktop.

Select an option

Save bradwright/53fd3ce5a58843c1285f to your computer and use it in GitHub Desktop.

Revisions

  1. bradwright created this gist Oct 24, 2015.
    3 changes: 3 additions & 0 deletions Instructions.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    1. Download the above file and save it somewhere (eg `~/Downloads`)
    2. Open Terminal.app, and go to where it's saved (eg `cd ~/Downloads`)
    3. Run the script, with the HDD volume as the first argument, eg: `python sleep-and-copy.py /Volumes/Thing/Path/to/itunes /Library/Local-path-to-itunes`
    48 changes: 48 additions & 0 deletions sleep-and-copy.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/usr/bin/env python

    import os
    import sys
    import time
    import shutil

    SLEEP_TIME = 3

    def recursive_copy_and_sleep(source_folder, destination_folder):
    "Copies files from `SOURCE` one at a time to `TARGET`, sleeping in between operations"

    for root, dirs, files in os.walk(source_folder):
    for item in files:
    src_path = os.path.join(root, item)
    dst_path = os.path.join(destination_folder, src_path.replace(source_folder, "")[1:])
    time.sleep(SLEEP_TIME)
    if os.path.exists(dst_path):
    if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
    print "Copying %s to %s" % (src_path, dst_path)
    shutil.copy2(src_path, dst_path)
    else:
    print "Copying %s to %s" % (src_path, dst_path)
    shutil.copy2(src_path, dst_path)

    for item in dirs:
    src_path = os.path.join(root, item)
    dst_path = os.path.join(destination_folder, src_path.replace(source_folder, "")[1:]) # Lop off leading slash, otherwise the directory maker gets confused
    if not os.path.exists(dst_path):
    print "Making %s" % dst_path
    os.mkdir(dst_path) #
    return True

    def main():
    if len(sys.argv) != 3:
    print "ERROR:\n\tPlease pass source and target directories, eg:\n\t%s /start/here /finish/here" % sys.argv[0]
    sys.exit(1)
    (source, target) = sys.argv[1:]

    if os.path.exists(source) and os.path.exists(target):
    recursive_copy_and_sleep(source, target)
    sys.exit(0)

    print "ERROR: invalid directories passed"
    sys.exit(1)

    if __name__ == '__main__':
    main()