Last active
December 17, 2015 00:10
-
-
Save spmurrayzzz/5518778 to your computer and use it in GitHub Desktop.
Semi-intelligently archive the contents of Mac OS X Desktop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
archive.py | |
Quick and dirty script that moves desktop clutter to a date-sorted archive | |
location. Nothing elegant here, just keeps desktop workspace much less | |
cluttered. Intended to be run as a cron job at a sane frequency. | |
Usage is ideal if your desktop is merely a swap space, not so ideal if your | |
desktop is a functional workspace. | |
Copyright (c) 2013 Stephen Murray | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
''' | |
import os | |
import pwd | |
from subprocess import call | |
from filecmp import (cmp, dircmp) | |
from datetime import date | |
def move_file(src, dest): | |
'''Move src file/dir to dest, with a naive sanity check for collision. | |
Directory collision does not recursively compare file content deltas, but | |
file collision does''' | |
if os.path.basename(src)[0] == '.': | |
return False | |
if os.path.isfile(src) and os.path.exists(dest): | |
if cmp(src, dest): | |
return False | |
if os.path.isdir(src) and os.path.exists(dest): | |
comparison = dircmp(src, dest) | |
if not comparison.left_only and not comparison.right_only: | |
return False | |
index = 0 | |
indexed_dest = dest | |
while os.path.exists(indexed_dest): | |
index += 1 | |
path, base = os.path.split(dest) | |
baseName, baseExt = os.path.splitext(base) | |
indexed_dest = os.path.join( | |
path, | |
'%s_%s%s' % (baseName, index, baseExt) | |
) | |
if index: | |
dest = indexed_dest | |
call(['mv', src, dest]) | |
if __name__ == '__main__': | |
username = pwd.getpwuid(os.getuid())[0] | |
desktop_path = '/Users/%s/Desktop' % username | |
archive_basepath = '/Users/%s/archive' % username | |
archive_path = os.path.join(archive_basepath, date.today().isoformat()) | |
if not os.path.exists(archive_path): | |
os.makedirs(archive_path) | |
for (dirpath, dirnames, filenames) in os.walk(desktop_path): | |
if dirpath == desktop_path: | |
for file in filenames: | |
src = os.path.join(desktop_path, file) | |
dest = os.path.join(archive_path, file) | |
move_file(src, dest) | |
for path in dirnames: | |
src = os.path.join(desktop_path, path) | |
dest = os.path.join(archive_path, path) | |
move_file(src, dest) | |
if not os.listdir(archive_path): | |
os.rmdir(archive_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Run every four hours | |
0 */4 * * * yourusername /path/to/archive.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment