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