~bzr-pqm/bzr/bzr.dev

1861.2.18 by Alexander Belchenko
Makefile: cross-platform actions for building installer
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
2691.1.16 by Ian Clatworthy
Ensure doc hierarchy is setup correctly for the Windows installer
12
    ostools.py copytree FILES... DIR
13
                    copy files to specified directory keeping relative paths
14
1861.2.18 by Alexander Belchenko
Makefile: cross-platform actions for building installer
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
2691.1.16 by Ian Clatworthy
Ensure doc hierarchy is setup correctly for the Windows installer
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
1861.2.18 by Alexander Belchenko
Makefile: cross-platform actions for building installer
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())