source: rtems-source-builder/source-builder/sb/path.py @ 5b5d6bf

4.104.114.95
Last change on this file since 5b5d6bf was 5b5d6bf, checked in by Chris Johns <chrisj@…>, on 06/16/15 at 10:57:06

sb: Fix the downloader file:// URL to copy the file to the local path.

  • Property mode set to 100644
File size: 8.1 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        os.makedirs(hdst)
205
206    for name in names:
207        srcname = host(os.path.join(hsrc, name))
208        dstname = host(os.path.join(hdst, name))
209        try:
210            if os.path.islink(srcname):
211                linkto = os.readlink(srcname)
212                if os.path.exists(dstname):
213                    if os.path.islink(dstname):
214                        dstlinkto = os.readlink(dstname)
215                        if linkto != dstlinkto:
216                            log.warning('copying tree: link does not match: %s -> %s' % \
217                                            (dstname, dstlinkto))
218                            os.remove(dstname)
219                    else:
220                        log.warning('copying tree: destination is not a link: %s' % \
221                                        (dstname))
222                        os.remove(dstname)
223                else:
224                    os.symlink(linkto, dstname)
225            elif os.path.isdir(srcname):
226                copy_tree(srcname, dstname)
227            else:
228                    shutil.copy2(host(srcname), host(dstname))
229        except shutil.Error, err:
230            raise error.general('copying tree: %s -> %s: %s' % \
231                                (hsrc, hdst, str(err)))
232        except EnvironmentError, why:
233            raise error.general('copying tree: %s -> %s: %s' % \
234                                (srcname, dstname, str(why)))
235    try:
236        shutil.copystat(hsrc, hdst)
237    except OSError, why:
238        if windows:
239            if WindowsError is not None and isinstance(why, WindowsError):
240                pass
241        else:
242            raise error.general('copying tree: %s -> %s: %s' % (hsrc, hdst, str(why)))
243
244if __name__ == '__main__':
245    print host('/a/b/c/d-e-f')
246    print host('//a/b//c/d-e-f')
247    print shell('/w/x/y/z')
248    print basename('/as/sd/df/fg/me.txt')
249    print dirname('/as/sd/df/fg/me.txt')
250    print join('/d', 'g', '/tyty/fgfg')
251    windows = True
252    print host('/a/b/c/d-e-f')
253    print host('//a/b//c/d-e-f')
254    print shell('/w/x/y/z')
255    print shell('w:/x/y/z')
256    print basename('x:/sd/df/fg/me.txt')
257    print dirname('x:/sd/df/fg/me.txt')
258    print join('s:/d/e\\f/g', '/h', '/tyty/zxzx', '\\mm\\nn/p')
Note: See TracBrowser for help on using the repository browser.