~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python

"""Cross-platform os tools: files/directories manipulations
Usage:

    ostools.py help
                    prints this help

    ostools.py copytodir FILES... DIR
                    copy files to specified directory

    ostools.py copytree FILES... DIR
                    copy files to specified directory keeping relative paths

    ostools.py remove [FILES...] [DIRS...]
                    remove files or directories (recursive)
"""

import glob
import os
import shutil
import sys

def makedir(dirname):
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    if not os.path.isdir(dirname):
        print "Error: Destination is not a directory", dirname
        return 2
    return 0

def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    if not argv:
        argv = ['help']

    cmd = argv.pop(0)

    if cmd == 'help':
        print __doc__
        return 0

    if cmd == 'copytodir':
        if len(argv) < 2:
            print "Usage:  ostools.py copytodir FILES... DIR"
            return 1

        todir = argv.pop()
        retcode = makedir(todir)
        if retcode:
            return retcode

        files = []
        for possible_glob in argv:
            files += glob.glob(possible_glob)

        for src in files:
            dest = os.path.join(todir, os.path.basename(src))
            shutil.copy(src, dest)
            print "Copied:", src, "=>", dest

        return 0

    if cmd == 'copytree':
        if len(argv) < 2:
            print "Usage:  ostools.py copytree FILES... DIR"
            return 1

        todir = argv.pop()
        retcode = makedir(todir)
        if retcode:
            return retcode

        files = []
        for possible_glob in argv:
            files += glob.glob(possible_glob)

        for src in files:
            relative_path = src
            dest = os.path.join(todir, relative_path)
            dest_dir = os.path.dirname(dest)
            retcode = makedir(dest_dir)
            if retcode:
                return retcode
            shutil.copy(src, dest)
            print "Copied:", src, "=>", dest

        return 0

    if cmd == 'remove':
        if len(argv) == 0:
            print "Usage:  ostools.py remove [FILES...] [DIRS...]"
            return 1

        filesdirs = []
        for possible_glob in argv:
            filesdirs += glob.glob(possible_glob)

        for i in filesdirs:
            if os.path.isdir(i):
                shutil.rmtree(i)
                print "Removed:", i
            elif os.path.isfile(i):
                os.remove(i)
                print "Removed:", i
            else:
                print "Not found:", i

        return 0

    if cmd == "basename":
        if len(argv) == 0:
            print "Usage:  ostools.py basename [PATH | URL]"
            return 1

        for path in argv:
            print os.path.basename(path)
        return 0

    if cmd == 'makedir':
        if len(argv) == 0:
            print "Usage:  ostools.py makedir DIR"
            return 1

        retcode = makedir(argv.pop())
        if retcode:
            return retcode
        return 0

    print "Usage error"
    print __doc__
    return 1


if __name__ == "__main__":
    sys.exit(main())