source: rtems-source-builder/source-builder/sb/path.py @ 69e5938

4.104.114.95
Last change on this file since 69e5938 was bc71066, checked in by Chris Johns <chrisj@…>, on 02/27/13 at 05:04:29

Fix host paths on Windows.

  • Property mode set to 100644
File size: 4.0 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(path):
76    return os.path.exists(host(path))
77
78def isdir(path):
79    return os.path.isdir(host(path))
80
81def isfile(path):
82    return os.path.isfile(host(path))
83
84def isabspath(path):
85    return path[0] == '/'
86
87def 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
110def 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
119if __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')
Note: See TracBrowser for help on using the repository browser.