source: rtems-source-builder/source-builder/sb/path.py @ 06dad0a

4.104.114.9
Last change on this file since 06dad0a was 06dad0a, checked in by Chris Johns <chrisj@…>, on Apr 13, 2013 at 12:30:07 AM

Make exists support lists. Add a path expander call.

  • Property mode set to 100644
File size: 4.3 KB
Line 
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
26import os
27import shutil
28import string
29
30import error
31
32windows = os.name == 'nt'
33
34def 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
45def 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
53def basename(path):
54    return shell(os.path.basename(path))
55
56def dirname(path):
57    return shell(os.path.dirname(path))
58
59def 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
68def abspath(path):
69    return shell(os.path.abspath(host(path)))
70
71def splitext(path):
72    root, ext = os.path.splitext(host(path))
73    return shell(root), ext
74
75def exists(paths):
76    if type(paths) == list:
77        results = []
78        for p in paths:
79            results += [os.path.exists(host(p))]
80        return results
81    return os.path.exists(host(paths))
82
83def isdir(path):
84    return os.path.isdir(host(path))
85
86def isfile(path):
87    return os.path.isfile(host(path))
88
89def isabspath(path):
90    return path[0] == '/'
91
92def mkdir(path):
93    path = host(path)
94    if exists(path):
95        if not isdir(path):
96            raise error.general('path exists and is not a directory: %s' % (path))
97    else:
98        if windows:
99            try:
100                os.makedirs(host(path))
101            except IOError, err:
102                raise error.general('cannot make directory: %s' % (path))
103            except OSError, err:
104                raise error.general('cannot make directory: %s' % (path))
105            except WindowsError, err:
106                raise error.general('cannot make directory: %s' % (path))
107        else:
108            try:
109                os.makedirs(host(path))
110            except IOError, err:
111                raise error.general('cannot make directory: %s' % (path))
112            except OSError, err:
113                raise error.general('cannot make directory: %s' % (path))
114
115def removeall(path):
116
117    def _onerror(function, path, excinfo):
118        print 'removeall error: (%r) %s' % (function, path)
119
120    path = host(path)
121    shutil.rmtree(path, onerror = _onerror)
122    return
123
124def expand(name, paths):
125    l = []
126    for p in paths:
127        l += [join(p, name)]
128    return l
129
130if __name__ == '__main__':
131    print host('/a/b/c/d-e-f')
132    print host('//a/b//c/d-e-f')
133    print shell('/w/x/y/z')
134    print basename('/as/sd/df/fg/me.txt')
135    print dirname('/as/sd/df/fg/me.txt')
136    print join('/d', 'g', '/tyty/fgfg')
137    windows = True
138    print host('/a/b/c/d-e-f')
139    print host('//a/b//c/d-e-f')
140    print shell('/w/x/y/z')
141    print shell('w:/x/y/z')
142    print basename('x:/sd/df/fg/me.txt')
143    print dirname('x:/sd/df/fg/me.txt')
144    print join('s:/d/', '/g', '/tyty/fgfg')
Note: See TracBrowser for help on using the repository browser.