~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to tools/win32/ostools.py

  • Committer: Martin Pool
  • Date: 2009-06-19 10:00:56 UTC
  • mto: This revision was merged to the branch mainline in revision 4464.
  • Revision ID: mbp@sourcefrog.net-20090619100056-fco5ooae2ybl88ne
Fix copyrights and remove assert statement from doc_generate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
"""Cross-platform os tools: files/directories manipulations
 
4
Usage:
 
5
 
 
6
    ostools.py help
 
7
                    prints this help
 
8
 
 
9
    ostools.py copytodir FILES... DIR
 
10
                    copy files to specified directory
 
11
 
 
12
    ostools.py copytree FILES... DIR
 
13
                    copy files to specified directory keeping relative paths
 
14
 
 
15
    ostools.py remove [FILES...] [DIRS...]
 
16
                    remove files or directories (recursive)
 
17
"""
 
18
 
 
19
import glob
 
20
import os
 
21
import shutil
 
22
import sys
 
23
 
 
24
 
 
25
def main(argv=None):
 
26
    if argv is None:
 
27
        argv = sys.argv[1:]
 
28
 
 
29
    if not argv:
 
30
        argv = ['help']
 
31
 
 
32
    cmd = argv.pop(0)
 
33
 
 
34
    if cmd == 'help':
 
35
        print __doc__
 
36
        return 0
 
37
 
 
38
    if cmd == 'copytodir':
 
39
        if len(argv) < 2:
 
40
            print "Usage:  ostools.py copytodir FILES... DIR"
 
41
            return 1
 
42
 
 
43
        todir = argv.pop()
 
44
        if not os.path.exists(todir):
 
45
            os.makedirs(todir)
 
46
        if not os.path.isdir(todir):
 
47
            print "Error: Destination is not a directory"
 
48
            return 2
 
49
 
 
50
        files = []
 
51
        for possible_glob in argv:
 
52
            files += glob.glob(possible_glob)
 
53
 
 
54
        for src in files:
 
55
            dest = os.path.join(todir, os.path.basename(src))
 
56
            shutil.copy(src, dest)
 
57
            print "Copied:", src, "=>", dest
 
58
 
 
59
        return 0
 
60
 
 
61
    if cmd == 'copytree':
 
62
        if len(argv) < 2:
 
63
            print "Usage:  ostools.py copytree FILES... DIR"
 
64
            return 1
 
65
 
 
66
        todir = argv.pop()
 
67
        if not os.path.exists(todir):
 
68
            os.makedirs(todir)
 
69
        if not os.path.isdir(todir):
 
70
            print "Error: Destination is not a directory"
 
71
            return 2
 
72
 
 
73
        files = []
 
74
        for possible_glob in argv:
 
75
            files += glob.glob(possible_glob)
 
76
 
 
77
        for src in files:
 
78
            relative_path = src
 
79
            dest = os.path.join(todir, relative_path)
 
80
            dest_dir = os.path.dirname(dest)
 
81
            if not os.path.isdir(dest_dir):
 
82
                os.makedirs(dest_dir)
 
83
            shutil.copy(src, dest)
 
84
            print "Copied:", src, "=>", dest
 
85
 
 
86
        return 0
 
87
 
 
88
    if cmd == 'remove':
 
89
        if len(argv) == 0:
 
90
            print "Usage:  ostools.py remove [FILES...] [DIRS...]"
 
91
            return 1
 
92
 
 
93
        filesdirs = []
 
94
        for possible_glob in argv:
 
95
            filesdirs += glob.glob(possible_glob)
 
96
 
 
97
        for i in filesdirs:
 
98
            if os.path.isdir(i):
 
99
                shutil.rmtree(i)
 
100
                print "Removed:", i
 
101
            elif os.path.isfile(i):
 
102
                os.remove(i)
 
103
                print "Removed:", i
 
104
            else:
 
105
                print "Not found:", i
 
106
 
 
107
        return 0
 
108
 
 
109
    print "Usage error"
 
110
    print __doc__
 
111
    return 1
 
112
 
 
113
 
 
114
if __name__ == "__main__":
 
115
    sys.exit(main())