1 | # |
---|
2 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
3 | # Copyright 2010-2012 Chris Johns (chrisj@rtems.org) |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
7 | # |
---|
8 | # Permission to use, copy, modify, and/or distribute this software for any |
---|
9 | # purpose with or without fee is hereby granted, provided that the above |
---|
10 | # copyright notice and this permission notice appear in all copies. |
---|
11 | # |
---|
12 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
13 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
14 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
15 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
16 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
17 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
18 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
19 | |
---|
20 | # |
---|
21 | # Manage paths locally. The internally the path is in Unix or shell format and |
---|
22 | # we convert to the native format when performing operations at the Python |
---|
23 | # level. This allows macro expansion to work. |
---|
24 | # |
---|
25 | |
---|
26 | import os |
---|
27 | import shutil |
---|
28 | import string |
---|
29 | |
---|
30 | import error |
---|
31 | |
---|
32 | windows = os.name == 'nt' |
---|
33 | |
---|
34 | def host(path): |
---|
35 | if path is not None: |
---|
36 | while '//' in path: |
---|
37 | path = path.replace('//', '/') |
---|
38 | if windows and len(path) > 2: |
---|
39 | if path[0] == '/' and path[2] == '/' and \ |
---|
40 | (path[1] in string.ascii_lowercase or \ |
---|
41 | path[1] in string.ascii_uppercase): |
---|
42 | path = ('%s:%s' % (path[1], path[2:])).replace('/', '\\') |
---|
43 | return path |
---|
44 | |
---|
45 | def shell(path): |
---|
46 | if path is not None: |
---|
47 | if windows and len(path) > 1 and path[1] == ':': |
---|
48 | path = ('/%s%s' % (path[0], path[2:])).replace('\\', '/') |
---|
49 | while '//' in path: |
---|
50 | path = path.replace('//', '/') |
---|
51 | return path |
---|
52 | |
---|
53 | def basename(path): |
---|
54 | return shell(os.path.basename(path)) |
---|
55 | |
---|
56 | def dirname(path): |
---|
57 | return shell(os.path.dirname(path)) |
---|
58 | |
---|
59 | def join(path, *args): |
---|
60 | path = shell(path) |
---|
61 | for arg in args: |
---|
62 | if len(path): |
---|
63 | path += '/' + shell(arg) |
---|
64 | else: |
---|
65 | path = shell(arg) |
---|
66 | return shell(path) |
---|
67 | |
---|
68 | def abspath(path): |
---|
69 | return shell(os.path.abspath(host(path))) |
---|
70 | |
---|
71 | def splitext(path): |
---|
72 | root, ext = os.path.splitext(host(path)) |
---|
73 | return shell(root), ext |
---|
74 | |
---|
75 | def exists(path): |
---|
76 | return os.path.exists(host(path)) |
---|
77 | |
---|
78 | def isdir(path): |
---|
79 | return os.path.isdir(host(path)) |
---|
80 | |
---|
81 | def isfile(path): |
---|
82 | return os.path.isfile(host(path)) |
---|
83 | |
---|
84 | def isabspath(path): |
---|
85 | return path[0] == '/' |
---|
86 | |
---|
87 | def mkdir(path): |
---|
88 | path = host(path) |
---|
89 | if exists(path): |
---|
90 | if not isdir(path): |
---|
91 | raise error.general('path exists and is not a directory: %s' % (path)) |
---|
92 | else: |
---|
93 | if windows: |
---|
94 | try: |
---|
95 | os.makedirs(host(path)) |
---|
96 | except IOError, err: |
---|
97 | raise error.general('cannot make directory: %s' % (path)) |
---|
98 | except OSError, err: |
---|
99 | raise error.general('cannot make directory: %s' % (path)) |
---|
100 | except WindowsError, err: |
---|
101 | raise error.general('cannot make directory: %s' % (path)) |
---|
102 | else: |
---|
103 | try: |
---|
104 | os.makedirs(host(path)) |
---|
105 | except IOError, err: |
---|
106 | raise error.general('cannot make directory: %s' % (path)) |
---|
107 | except OSError, err: |
---|
108 | raise error.general('cannot make directory: %s' % (path)) |
---|
109 | |
---|
110 | def removeall(path): |
---|
111 | |
---|
112 | def _onerror(function, path, excinfo): |
---|
113 | print 'removeall error: (%r) %s' % (function, path) |
---|
114 | |
---|
115 | path = host(path) |
---|
116 | shutil.rmtree(path, onerror = _onerror) |
---|
117 | return |
---|
118 | |
---|
119 | if __name__ == '__main__': |
---|
120 | print host('/a/b/c/d-e-f') |
---|
121 | print host('//a/b//c/d-e-f') |
---|
122 | print shell('/w/x/y/z') |
---|
123 | print basename('/as/sd/df/fg/me.txt') |
---|
124 | print dirname('/as/sd/df/fg/me.txt') |
---|
125 | print join('/d', 'g', '/tyty/fgfg') |
---|
126 | windows = True |
---|
127 | print host('/a/b/c/d-e-f') |
---|
128 | print host('//a/b//c/d-e-f') |
---|
129 | print shell('/w/x/y/z') |
---|
130 | print shell('w:/x/y/z') |
---|
131 | print basename('x:/sd/df/fg/me.txt') |
---|
132 | print dirname('x:/sd/df/fg/me.txt') |
---|
133 | print join('s:/d/', '/g', '/tyty/fgfg') |
---|