source: rtems-source-builder/source-builder/sb/path.py @ b537e55

4.104.95
Last change on this file since b537e55 was 6444d58, checked in by Chris Johns <chrisj@…>, on 07/20/15 at 03:49:42

Canandian Cross Compiling and RTEMS 3rd party package building Fixes.

The change fixes installing for RTEMS 3rd Party packages where the
RSB considered them Canadian Cross Compiling (Cxc). Fixing the
Cxc issue broke real Cxc builds. The change corrects the issue of
macros being changed in the Cxc and the prep data not being udpated.
The configuration is loaded again after the updated macros. The
macros are also copied and restored to ensure a clean stable base.

The change also introduces --rtems-tools and --rtems-bsp to align
the command line with the waf configure process or RTEMS application.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2015 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 log
27import os
28import shutil
29import stat
30import string
31
32import error
33
34windows = os.name == 'nt'
35
36def host(path):
37    if path is not None:
38        while '//' in path:
39            path = path.replace('//', '/')
40        if windows:
41            if len(path) > 2 and \
42               path[0] == '/' and path[2] == '/' and \
43               (path[1] in string.ascii_lowercase or \
44                path[1] in string.ascii_uppercase):
45                path = '%s:%s' % (path[1], path[2:])
46            path = path.replace('/', '\\')
47            if not path.startswith('\\\\?\\') and len(path) > 254:
48                path = '\\\\?\\' + path
49    return path
50
51def shell(path):
52    if path is not None:
53        if windows:
54            if path.startswith('\\\\?\\'):
55                path = path[4:]
56            if len(path) > 1 and path[1] == ':':
57                path = '/%s%s' % (path[0], path[2:])
58            path = path.replace('\\', '/')
59        while '//' in path:
60            path = path.replace('//', '/')
61    return path
62
63def basename(path):
64    return shell(os.path.basename(path))
65
66def dirname(path):
67    return shell(os.path.dirname(path))
68
69def join(path, *args):
70    path = shell(path)
71    for arg in args:
72        if len(path):
73            path += '/' + shell(arg)
74        else:
75            path = shell(arg)
76    return shell(path)
77
78def abspath(path):
79    return shell(os.path.abspath(host(path)))
80
81def splitext(path):
82    root, ext = os.path.splitext(host(path))
83    return shell(root), ext
84
85def exists(paths):
86    if type(paths) == list:
87        results = []
88        for p in paths:
89            results += [os.path.exists(host(p))]
90        return results
91    return os.path.exists(host(paths))
92
93def isdir(path):
94    return os.path.isdir(host(path))
95
96def isfile(path):
97    return os.path.isfile(host(path))
98
99def isabspath(path):
100    return path[0] == '/'
101
102def iswritable(path):
103    return os.access(host(path), os.W_OK)
104
105def ispathwritable(path):
106    path = host(path)
107    while len(path) != 0:
108        if os.path.exists(path):
109            return iswritable(path)
110        path = os.path.dirname(path)
111    return False
112
113def mkdir(path):
114    path = host(path)
115    if exists(path):
116        if not isdir(path):
117            raise error.general('path exists and is not a directory: %s' % (path))
118    else:
119        if windows:
120            try:
121                os.makedirs(host(path))
122            except IOError, err:
123                raise error.general('cannot make directory: %s' % (path))
124            except OSError, err:
125                raise error.general('cannot make directory: %s' % (path))
126            except WindowsError, err:
127                raise error.general('cannot make directory: %s' % (path))
128        else:
129            try:
130                os.makedirs(host(path))
131            except IOError, err:
132                raise error.general('cannot make directory: %s' % (path))
133            except OSError, err:
134                raise error.general('cannot make directory: %s' % (path))
135
136def removeall(path):
137    #
138    # Perform the removal of the directory tree manually so we can
139    # make sure on Windows the files and correctly encoded to avoid
140    # the size limit.
141    #
142    path = host(path)
143    for root, dirs, files in os.walk(path, topdown = False):
144        for name in files:
145            file = host(os.path.join(root, name))
146            if not os.path.islink(file) and not os.access(file, os.W_OK):
147                os.chmod(file, stat.S_IWUSR)
148            os.unlink(file)
149        for name in dirs:
150            dir = host(os.path.join(root, name))
151            if os.path.islink(dir):
152                os.unlink(dir)
153            else:
154                if not os.access(dir, os.W_OK):
155                    os.chmod(dir, stat.S_IWUSR)
156                os.rmdir(dir)
157    if not os.path.islink(path) and not os.access(path, os.W_OK):
158        os.chmod(path, stat.S_IWUSR)
159    if os.path.islink(path):
160        os.unlink(path)
161    else:
162        os.rmdir(path)
163
164def expand(name, paths):
165    l = []
166    for p in paths:
167        l += [join(p, name)]
168    return l
169
170def copy(src, dst):
171    hsrc = host(src)
172    hdst = host(dst)
173    try:
174        shutil.copy(hsrc, hdst)
175    except OSError, why:
176        if windows:
177            if WindowsError is not None and isinstance(why, WindowsError):
178                pass
179        else:
180            raise error.general('copying tree: %s -> %s: %s' % (hsrc, hdst, str(why)))
181
182def copy_tree(src, dst):
183    trace = False
184
185    hsrc = host(src)
186    hdst = host(dst)
187
188    if os.path.exists(hsrc):
189        names = os.listdir(hsrc)
190    else:
191        names = []
192
193    if trace:
194        print 'path.copy_tree:'
195        print '   src: %s' % (src)
196        print '  hsrc: %s' % (hsrc)
197        print '   dst: %s' % (dst)
198        print '  hdst: %s' % (hdst)
199        print ' names: %r' % (names)
200
201    if not os.path.isdir(hdst):
202        if trace:
203            print ' mkdir: %s' % (hdst)
204        try:
205            os.makedirs(hdst)
206        except OSError, why:
207            raise error.general('copying tree: cannot create target directory %s: %s' % \
208                                (hdst, str(why)))
209
210    for name in names:
211        srcname = host(os.path.join(hsrc, name))
212        dstname = host(os.path.join(hdst, name))
213        try:
214            if os.path.islink(srcname):
215                linkto = os.readlink(srcname)
216                if os.path.exists(dstname):
217                    if os.path.islink(dstname):
218                        dstlinkto = os.readlink(dstname)
219                        if linkto != dstlinkto:
220                            log.warning('copying tree: link does not match: %s -> %s' % \
221                                            (dstname, dstlinkto))
222                            os.remove(dstname)
223                    else:
224                        log.warning('copying tree: destination is not a link: %s' % \
225                                        (dstname))
226                        os.remove(dstname)
227                else:
228                    os.symlink(linkto, dstname)
229            elif os.path.isdir(srcname):
230                copy_tree(srcname, dstname)
231            else:
232                    shutil.copy2(host(srcname), host(dstname))
233        except shutil.Error, err:
234            raise error.general('copying tree: %s -> %s: %s' % \
235                                (hsrc, hdst, str(err)))
236        except EnvironmentError, why:
237            raise error.general('copying tree: %s -> %s: %s' % \
238                                (srcname, dstname, str(why)))
239    try:
240        shutil.copystat(hsrc, hdst)
241    except OSError, why:
242        if windows:
243            if WindowsError is not None and isinstance(why, WindowsError):
244                pass
245        else:
246            raise error.general('copying tree: %s -> %s: %s' % (hsrc, hdst, str(why)))
247
248if __name__ == '__main__':
249    print host('/a/b/c/d-e-f')
250    print host('//a/b//c/d-e-f')
251    print shell('/w/x/y/z')
252    print basename('/as/sd/df/fg/me.txt')
253    print dirname('/as/sd/df/fg/me.txt')
254    print join('/d', 'g', '/tyty/fgfg')
255    windows = True
256    print host('/a/b/c/d-e-f')
257    print host('//a/b//c/d-e-f')
258    print shell('/w/x/y/z')
259    print shell('w:/x/y/z')
260    print basename('x:/sd/df/fg/me.txt')
261    print dirname('x:/sd/df/fg/me.txt')
262    print join('s:/d/e\\f/g', '/h', '/tyty/zxzx', '\\mm\\nn/p')
Note: See TracBrowser for help on using the repository browser.