1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: pathutils.py 79132 2019-06-13 15:13:12Z vboxsync $
|
---|
3 | # pylint: disable=too-many-lines
|
---|
4 |
|
---|
5 | """
|
---|
6 | Path Utility Functions.
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2012-2019 Oracle Corporation
|
---|
12 |
|
---|
13 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
14 | available from http://www.virtualbox.org. This file is free software;
|
---|
15 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
16 | General Public License (GPL) as published by the Free Software
|
---|
17 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
18 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
19 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
20 |
|
---|
21 | The contents of this file may alternatively be used under the terms
|
---|
22 | of the Common Development and Distribution License Version 1.0
|
---|
23 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
24 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
25 | CDDL are applicable instead of those of the GPL.
|
---|
26 |
|
---|
27 | You may elect to license modified versions of this file under the
|
---|
28 | terms and conditions of either the GPL or the CDDL or both.
|
---|
29 | """
|
---|
30 | __version__ = "$Revision: 79132 $"
|
---|
31 |
|
---|
32 |
|
---|
33 | # Standard Python imports.
|
---|
34 | import unittest;
|
---|
35 |
|
---|
36 |
|
---|
37 | ## @name Path data keyed by fDosStyle bool value.
|
---|
38 | ## @{
|
---|
39 | g_dsPathSlash = { False: '/', True: '\\', };
|
---|
40 | g_dasPathSlashes = { False: ('/',), True: ('\\', '/', ), };
|
---|
41 | g_dasPathSeparators = { False: ('/',), True: ('\\', '/', ':', ), };
|
---|
42 | ## @}
|
---|
43 |
|
---|
44 |
|
---|
45 | def joinEx(fDosStyle, sBase, *asAppend):
|
---|
46 | """
|
---|
47 | Mimicking os.path.join, but where target system isn't the host.
|
---|
48 | The code is very simple at present.
|
---|
49 | """
|
---|
50 | # Get the first non-None element and use it as base.
|
---|
51 | i = 0;
|
---|
52 | sRet = sBase;
|
---|
53 | while sRet is None and i < len(asAppend):
|
---|
54 | sRet = asAppend[i];
|
---|
55 | i += 1;
|
---|
56 |
|
---|
57 | while i < len(asAppend):
|
---|
58 | sAppend = asAppend[i];
|
---|
59 |
|
---|
60 | # Skip None elements.
|
---|
61 | if sAppend is not None:
|
---|
62 | # Strip leading slashes from sAppend:
|
---|
63 | offSkip = 0;
|
---|
64 | while offSkip < len(sAppend) and sAppend[offSkip] in g_dasPathSlashes[fDosStyle]:
|
---|
65 | offSkip += 1;
|
---|
66 |
|
---|
67 | # Add separator if needed before appending the new bit:
|
---|
68 | if not sRet or sRet[-1] not in g_dasPathSeparators[fDosStyle]:
|
---|
69 | sRet += g_dsPathSlash[fDosStyle] + sAppend[offSkip:];
|
---|
70 | else:
|
---|
71 | sRet += sAppend[offSkip:];
|
---|
72 |
|
---|
73 | i += 1;
|
---|
74 |
|
---|
75 | return sRet;
|
---|
76 |
|
---|
77 |
|
---|
78 | #
|
---|
79 | # Unit testing.
|
---|
80 | #
|
---|
81 |
|
---|
82 | # pylint: disable=missing-docstring,undefined-variable
|
---|
83 | class JoinExTestCase(unittest.TestCase):
|
---|
84 | def testJoinEx(self):
|
---|
85 | self.assertEqual(joinEx(True, None), None);
|
---|
86 | self.assertEqual(joinEx(False, None), None);
|
---|
87 | self.assertEqual(joinEx(True, ''), '');
|
---|
88 | self.assertEqual(joinEx(False, ''), '');
|
---|
89 | self.assertEqual(joinEx(True, '',''), '\\');
|
---|
90 | self.assertEqual(joinEx(False, '',''), '/');
|
---|
91 | self.assertEqual(joinEx(True, 'C:','dos'), 'C:dos');
|
---|
92 | self.assertEqual(joinEx(True, 'C:/','dos'), 'C:/dos');
|
---|
93 | self.assertEqual(joinEx(True, 'C:\\','dos'), 'C:\\dos');
|
---|
94 | self.assertEqual(joinEx(True, 'C:\\dos','edlin.com'), 'C:\\dos\\edlin.com');
|
---|
95 | self.assertEqual(joinEx(True, 'C:\\dos\\','edlin.com'), 'C:\\dos\\edlin.com');
|
---|
96 | self.assertEqual(joinEx(True, 'C:\\dos/','edlin.com'), 'C:\\dos/edlin.com');
|
---|
97 | self.assertEqual(joinEx(True, 'C:\\dos//','edlin.com'), 'C:\\dos//edlin.com');
|
---|
98 | self.assertEqual(joinEx(True, 'C:\\dos','\\/edlin.com'), 'C:\\dos\\edlin.com');
|
---|
99 | self.assertEqual(joinEx(True, 'C:\\dos', None, 'edlin.com'), 'C:\\dos\\edlin.com');
|
---|
100 | self.assertEqual(joinEx(True, None, 'C:\\dos', None, 'edlin.com'), 'C:\\dos\\edlin.com');
|
---|
101 | self.assertEqual(joinEx(True, None, None, 'C:\\dos', None, 'edlin.com', None), 'C:\\dos\\edlin.com');
|
---|
102 | self.assertEqual(joinEx(False, '/', 'bin', 'ls'), '/bin/ls');
|
---|
103 | self.assertEqual(joinEx(False, '/', '/bin', 'ls'), '/bin/ls');
|
---|
104 | self.assertEqual(joinEx(False, '/', '/bin/', 'ls'), '/bin/ls');
|
---|
105 | self.assertEqual(joinEx(False, '/', '/bin//', 'ls'), '/bin//ls');
|
---|
106 | self.assertEqual(joinEx(False, '/', None, 'bin', None, 'ls', None), '/bin/ls');
|
---|
107 | self.assertEqual(joinEx(False, None, '/', None, 'bin', None, 'ls', None), '/bin/ls');
|
---|
108 |
|
---|
109 |
|
---|
110 | if __name__ == '__main__':
|
---|
111 | unittest.main();
|
---|
112 | # not reached.
|
---|
113 |
|
---|