Permalink
Browse files

python script working

  • Loading branch information...
1 parent 5e37a3c commit 5f860bbff6ccfdedcd47ae5cb9e1f841473d17bd @dorey dorey committed Jan 30, 2012
Showing with 42 additions and 78 deletions.
  1. +42 −78 data_unloader/unload_from_phone.py
@@ -1,91 +1,55 @@
import sys, os, shutil, glob, time, re, datetime
from optparse import OptionParser
-CURRENT_FILE = os.path.abspath(__file__)
-CURRENT_DIR = os.path.dirname(CURRENT_FILE)
-UNLOAD_DIR = os.path.join(CURRENT_DIR, 'files')
-
-SETTINGS = {
- 'UNLOAD_DIR': os.path.join(CURRENT_DIR, 'files'),
- 'MOUNT_ROOT': '/media',
-# dont_delete will preserve a hidden directory with
-# the files. this is the safe option
- 'dont_delete': True,
- 'hidden_files_directory': '.odk_backups',
- 'clean_odk_dir': os.path.join(CURRENT_DIR, 'odk_fresh'),
- 'replace_contents': False,
- 'preserve_forms': True,
-}
-
-MOUNTED_DRIVE_PATH = os.path.join(SETTINGS['MOUNT_ROOT'], '*', 'odk')
-
-def get_mounted_drives():
- # path to ODK:
- # eg: /Volumes/*/odk in os x
- return glob.glob(MOUNTED_DRIVE_PATH)
-
-def count_instances(drive):
- icount = glob.glob(os.path.join(drive, 'instances', '*'))
- return len(icount)
-
-def ensure_dir(dirname):
- if not os.path.exists(dirname):
- os.mkdir(dirname)
-
-def load_contents_to_dir(pn, dest, odk_src_dir):
+def load_contents_to_dir(phone_id, dest, odk_src_dir):
if os.path.exists(dest):
- print "Destination already exists. Saving to a modified path."
- load_contents_to_dir(pn, "%s~" % dest, odk_src_dir)
+ print "Destination already exists. Saving to a modified path.\n"
+ load_contents_to_dir(phone_id, "%s~" % dest, odk_src_dir)
else:
- # Copying
shutil.copytree(odk_src_dir, dest)
- print "[ctrl-C to quit ] - %s: Backed up %d instances to %s" % (str(pn),
- len(glob.glob(os.path.join(dest, 'instances', '*'))),
- dest)
- if SETTINGS['dont_delete']:
- hf_path = os.path.abspath(os.path.join(odk_src_dir, '..', SETTINGS['hidden_files_directory']))
- print "Hidden files dir %s" % hf_path
- if not os.path.exists(hf_path):
- os.mkdir(hf_path)
- timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
- backup_dest = os.path.join(hf_path, 'odk_%s' % timestamp)
- shutil.copytree(odk_src_dir, backup_dest)
- dirs_to_empty = [os.path.join(odk_src_dir, x)
- for x in ['instances', 'metadata']]
- [shutil.rmtree(x) for x in dirs_to_empty]
- [os.mkdir(x) for x in dirs_to_empty]
-def replace_contents_with_skeleton(skeleton_dir, odk_src_dir):
- # "skeleton" dir contains the odk directory to be put on the phones after old data is wiped.
- shutil.copytree(skeleton_dir, odk_src_dir)
+def backup_on_sdcard(odk_src_dir):
+ hf_path = os.path.abspath(os.path.join(odk_src_dir, '..', '.odk_backups'))
+ if not os.path.exists(hf_path):
+ os.mkdir(hf_path)
+ timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
+ backup_dest = os.path.join(hf_path, 'odk_%s' % timestamp)
+ shutil.copytree(odk_src_dir, backup_dest)
-def unmount_drive(drive_path):
- os.system("sudo umount \"%s\"" % os.path.abspath(drive_path))
- print "[ctrl-C to quit ] - Drive is ejected."
+def remove_instances_and_metadata(odk_src_dir):
+ dirs_to_empty = [os.path.join(odk_src_dir, x)
+ for x in ['instances', 'metadata']]
+ for x in dirs_to_empty: shutil.rmtree(x)
+ for x in dirs_to_empty: os.mkdir(x)
-def poll(replace_contents=False):
- mounted_drives = get_mounted_drives()
- if len(mounted_drives) > 0:
- drive = mounted_drives[0]
- print "[ctrl-C to quit ] - A phone is found with %d instances." % count_instances(drive)
- phone_id = raw_input("[ctrl-C to quit ] - What is the phone's ID number?: ")
- if phone_id.lower() in ["quit", "q"]:
- print "Quitting"
- sys.exit(0)
- ensure_dir(UNLOAD_DIR)
- dest = os.path.join(UNLOAD_DIR, phone_id)
- src_dir = mounted_drives[0]
- load_contents_to_dir(phone_id, dest, src_dir)
-# if replace_contents:
-# replace_contents_with_skeleton(SETTINGS['clean_odk_dir'], src_dir)
- unmount_drive(os.path.join(src_dir, '..'))
- time.sleep(1)
+def run(options):
+ """
+ preserve_on_sdcard: False
+ clean_odk_dir: None
+ debug: True
+ phone_id: 1256
+ output_directory: /home/formhub/Desktop/Unloaded Data
+ remove_instances: True
+ drive_path: /media/Kingston
+ """
+ odk_src_dir = os.path.join(options.drive_path, 'odk')
+ try:
+ load_contents_to_dir(options.phone_id, os.path.join(options.output_directory, options.phone_id), odk_src_dir)
+ except:
+ print "There was a problem copying ODK contents"
+ sys.exit(2)
+ if options.preserve_on_sdcard:
+ try:
+ backup_on_sdcard(odk_src_dir)
+ except:
+ print "There was a problem backing up data on SD card"
+ sys.exit(2)
+ if options.remove_instances:
+ remove_instances_and_metadata(odk_src_dir)
def main():
usage = "Usage: %prog [--flagged-arguments *...]"
parser = OptionParser(usage)
-# parser.add_option("-s", "--silent", dest="silent",
-# action="store_true", default=False, help="suppress output")
parser.add_option("-B", "--preserve-on-sdcard", dest="preserve_on_sdcard",
action="store_true", default=False, help="data can be moved to "
"a hidden directory on the sdcard. This is a measure to prevent data "
@@ -109,19 +73,19 @@ def main():
parser.add_option("-D", "--debug", dest="debug",
action="store_true", default=False, help="Prints debug information and then "
"exits.")
-
(options, args) = parser.parse_args()
if len(args) > 0:
print "This script takes no unflagged arguments"
- sys.exit(0)
+ sys.exit(2)
if options.debug:
print "Debug:"
odict = eval(str(options))
for key in ["preserve_on_sdcard", "clean_odk_dir", \
"unmount_drive", "debug", "phone_id", "output_directory", \
"remove_instances", "drive_path"]:
print "%s: %s" % (key, odict[key])
- sys.exit(0)
+ sys.exit(2)
+ run(options)
if __name__=="__main__":
main()

0 comments on commit 5f860bb

Please sign in to comment.