#!/usr/bin/env python # A simple cross-platform hack to copy Anki directories for backup. # User-friendly: If everything works, just press Enter to back up to your desktop. # Confirmed working on Ubuntu Linux 11.04. # Version 0.3.1 def complex(with_help): #Determine if the user wants advanced options. with_help is used to not display the help message again if the user has already seen help. if with_help == 0: print "For help and information, press . For advanced options, type ." else: print "For advanced options, type ." iput = raw_input("Otherwise, press to back up: ") print "" if iput == "?": return 2 elif iput == "i": return 1 elif iput == "": return 0 else: print "That was not a valid option!" complex(0) def clearscreen(): #Clear the screen of the help message to start again. import os if os.name == "posix": os.system('clear') elif os.name in ("nt", "dos", "ce"): os.system('CLS') else: print '\n' * 100 return 0 def help(): #Print a quick help and about message. print "--About--" print "AnkiBackup, Version 0.3.1" print "By Soren Bjornstad, 2011" print "You can do pretty much whatever you want with this code." print "I'd appreciate it if you link to my site so that users can" print "easily download a newer version if one is available." print "Also, if you make any useful changes, please contact me" print "so I can consider them as well." print "" print "If you find a bug, or if AnkiBackup doesn't run properly, please" print "contact me so that I can fix it." print "" print "Contact me at webmaster@thetechnicalgeekery.com." print "The latest version of this program can be found at:" print "http://thetechnicalgeekery.com/anki" print "" print "--Quick and dirty help:--" print "If everything goes well, you can just press Enter to back up upon starting" print "AnkiBackup. If that doesn't work, please contact me so that I can add" print "support for your computer setup to AnkiBackup." print "You can also press i for advanced options, to specify where you want" print "the Anki data to come from and be backed up to." print "" raw_input("--Press --") clearscreen() def win_get_homedir(): #Windows doesn't support ~, so we have to jump through some hoops (code from the Internet) to find homedir. import ctypes dll = ctypes.windll.shell32 buf = ctypes.create_unicode_buffer(300) dll.SHGetSpecialFolderPathW(None, buf, 0x0005, False) return buf.value def searchanki(): #Based on the operating system, find the default path of Anki decks. import sys import os if sys.platform.startswith('linux2'): PathToTry = os.path.expanduser('~/.anki/decks') elif sys.platform.startswith('darwin'): PathToTry = os.path.expanduser('~/Documents/Anki') elif sys.platform.startswith('win32'): PathToTry = win_get_homedir() + '\Anki' if os.path.exists(PathToTry): return PathToTry else: return "NotFound" def oops(type): #Not sure why I didn't put this inline, but here it is. if type == 1: print "Oops! That source folder doesn't exist!" print "Please check your pathname and rerun the script." print "" raw_input("--Press --") import sys sys.exit(1) def oopsrm(destination): #Same here. Something was already in that spot, and user cancelled or the program couldn't delete it automatically (lack permissions, maybe?) print "AnkiBackup cannot begin copying because there is an earlier backup already." print "(Or you might have created a folder named AnkiBackup on your desktop....)" print "Please delete the " + destination print "folder and then rerun this program." print "" raw_input("--Press --") import sys sys.exit(2) ################### #------MAIN-------# ################### #Source and destination start at "". Complex asks for options, simple doesn't. source = "" destination = "" isComplex = complex(0) if isComplex == 2: help() isComplex = complex(1) if isComplex == 1: # Return leaves the options at "", therefore automatic detection is attempted. print "Press Enter to attempt to automatically detect the location of your Anki decks, or enter the path to the folder." source = raw_input("> ") if source == "": #User chose not to input anything # Detect the location. source = searchanki() import os if not os.path.exists(source): oops(1) print "" print "Press Enter to copy your backup to your desktop, or enter the full path to the" print "location you want to place your backup:" destination = raw_input("> ") print "" else: if source == "": # Detect the location. source = searchanki() if source == "NotFound": # Autodetect failed, and we had to ask for a path anyway. print "AnkiBackup could not find the location of your deck automatically. Please specify the path:" source = raw_input("> ") if destination == "": # Destination not specified. Revert to default on the desktop. import os import sys if sys.platform.startswith('linux2'): destination = os.path.expanduser('~/Desktop/AnkiBackup') elif sys.platform.startswith('darwin'): destination = os.path.expanduser('~/Desktop/AnkiBackup') elif sys.platform.startswith('win32'): #win_get_homedir returns the Documents folder, so we find Desktop with ../Desktop. destination = win_get_homedir() + '\..\Desktop\AnkiBackup' import shutil import os if os.path.exists(destination): print "The location " + destination goahead = raw_input("already exists. Press to overwrite, or to cancel: ") print "" if goahead == "": try: shutil.rmtree(destination) except: oopsrm(destination) else: oopsrm(destination) try: print "Copying from " + source print "to " + destination + "." shutil.copytree(source, destination) except: print "Copying files failed!" print "If you provided paths, make sure you provided folder (not file) names" print "and you have permission to access them." import sys sys.exit(1) print "The backup completed successfully."