~duncaneastoe/cpr/devel

« back to all changes in this revision

Viewing changes to cpr/cpr.py

  • Committer: Duncan Eastoe
  • Date: 2009-05-29 20:11:04 UTC
  • Revision ID: dj@dj-laptop-20090529201104-tyt3l0rsunhbratx
Verbosity off without use of flag, added README

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import sys
26
26
import distutils.dir_util
27
27
 
28
 
version = "lp:cpr_rev-11"
 
28
version = "lp:cpr_rev-12"
29
29
 
30
30
class cpr:
31
31
        def __init__(self):
32
32
                if len(sys.argv) < 2: #ensure at least some argument was given
33
33
                        print "No arguments specified, use cpr --help for a list of valid arguments and usage"
34
34
                        sys.exit(1)
35
 
                if sys.argv[1].startswith("--"): #test for argument type
 
35
                if sys.argv[1].startswith("--") or sys.argv[1].startswith("-"): #test for argument type
36
36
                        arg = sys.argv[1]
37
 
                        if arg == "--help": #print help message
38
 
                                print "cpr repeatedly copies the same files/directories to the same destination\nUsage is: cpr SOURCE(S) DESTINATION\nOptional arguments are:\n    --help Display this help message\n    --version Display the cpr version"
 
37
                        if arg == "-v" or arg == "--verbose": #set verbosity to true
 
38
                                verbose = True
 
39
                        elif arg == "--help": #print help message
 
40
                                print "cpr repeatedly copies the same files/directories to the same destination\nUsage is: cpr SOURCE(S) DESTINATION\nOptional arguments are:\n    -v, --verbose  Display files copied after each copy\n\n    --help  Display this help message\n    --version  Display the cpr version"
39
41
                                sys.exit(0)
40
42
                        elif arg == "--version": #print version number
41
43
                                print version
43
45
                        else: #print unknown argument message
44
46
                                print "Unknown argument, use cpr --help for a list of valid arguments"
45
47
                                sys.exit(1)
46
 
                src = sys.argv[1:-1] #get all arguments between the first and last, these are the source(s)
 
48
                src = sys.argv[2:-1] #get all arguments between the first and last, these are the source(s)
47
49
                for path in src:
48
50
                        src_check = os.path.exists(path); #check each passed path exists
49
51
                        if src_check == False: #if false give an error message
57
59
                        for path in src:
58
60
                                if os.path.exists(path): #only attempt copy if the source directory exists
59
61
                                        if os.path.isfile(path): #run if source is a file
 
62
                                                full_dest = dest
60
63
                                                if not os.path.isdir(dest): #if the destination directory does not exist, create it
61
64
                                                        os.mkdir(dest)
62
65
                                                shutil.copy(path, dest) #copy the file to the destination directory
66
69
                                                if not os.path.isdir(full_dest): #if directory does not exist, create it
67
70
                                                        os.mkdir(full_dest)
68
71
                                                distutils.dir_util.copy_tree(path, full_dest) #copy source to (full) destination
69
 
                                        print "Copied "+path #print summary message
 
72
                                        if verbose == True:
 
73
                                                print "Copied "+path+" to "+full_dest+"\n"  #if verbosity is true, be verbose!
70
74
                                else:
71
 
                                        print "!! ERROR !! "+path+" does not exist, not copied" #displayed if any source path can no longer be found
72
 
                        print
 
75
                                        print "!! ERROR !! "+path+" does not exist, not copied\n" #displayed if any source path can no longer be found
73
76
                        raw_input("Press Enter to copy again") #print repeat confirmation message
74
77
 
75
78
if __name__ == '__main__':