Browse Source

Use of standard module optparse for command line parsing in ffx-backup.py

Thomas Jost 13 years ago
parent
commit
a9848678c7
1 changed files with 20 additions and 14 deletions
  1. 20
    14
      ffx-backup.py

+ 20
- 14
ffx-backup.py View File

@@ -12,22 +12,28 @@ EXCLUDES = [
12 12
     "/.parentlock$",
13 13
 ]
14 14
 
15
-import time, os, os.path, re, sys, tarfile
16
-
17
-if len(sys.argv) != 2 and len(sys.argv) != 4:
18
-    print "Usage: %s path_to_profile [filedropper_username filedropper_password]" % sys.argv[0]
15
+import os, os.path, re, sys, tarfile, time
16
+from optparse import OptionParser
17
+
18
+# Parse command line
19
+parser = OptionParser()
20
+parser.add_option("-f", "--folder", dest="profile_path", help="profile folder to be saved")
21
+parser.add_option("-u", "--user", dest="fd_user", help="username for connection to FileDropper (optional)")
22
+parser.add_option("-p", "--password", dest="fd_pass", help="password for connection to FileDropper (optional)")
23
+(options, args) = parser.parse_args()
24
+
25
+# Check if constraints are respected
26
+if options.profile_path is None:
27
+    print "No profile specified"
28
+    sys.exit(1)
29
+if (options.fd_user is not None and options.fd_pass is None) or (options.fd_user is None and options.fd_pass is not None):
30
+    print "Username AND password needed"
19 31
     sys.exit(1)
20 32
 
21
-profile_path = sys.argv[1]
22
-profile_name = os.path.basename(profile_path)
33
+profile_name = os.path.basename(options.profile_path)
23 34
 date = time.strftime("%Y%m%d_%H%M%S")
24 35
 archive_name = "%s-%s.tar.bz2" % (profile_name, date)
25 36
 
26
-fd_user = None
27
-fd_pass = None
28
-if len(sys.argv) == 4:
29
-    fd_user, fd_pass = sys.argv[2], sys.argv[3]
30
-
31 37
 # Compile exclusion regexps
32 38
 EXCLUDES_RE = []
33 39
 for regexp in EXCLUDES:
@@ -50,7 +56,7 @@ def is_excluded(filename):
50 56
 tar = tarfile.open(archive_name, 'w:bz2')
51 57
 
52 58
 # Add the data to the archive
53
-tar.add(profile_path, profile_name, exclude=is_excluded)
59
+tar.add(options.profile_path, profile_name, exclude=is_excluded)
54 60
 
55 61
 tar.close()
56 62
 
@@ -59,11 +65,11 @@ print "Profile saved to %s" % archive_name
59 65
 print "%.1f MB --> %.1f MB" % (total_size/(1024**2), os.path.getsize(archive_name)/(1024**2))
60 66
 
61 67
 # Upload to FileDropper if needed
62
-if fd_user is not None and fd_pass is not None:
68
+if options.fd_user is not None and options.fd_pass is not None:
63 69
     import FileDropper
64 70
 
65 71
     fd = FileDropper.FileDropper()
66
-    fd.login(fd_user, fd_pass)
72
+    fd.login(options.fd_user, options.fd_pass)
67 73
 
68 74
     # Upload the file
69 75
     url = fd.upload(archive_name)

Loading…
Cancel
Save