VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/common/pathutils.py@ 98715

Last change on this file since 98715 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: pathutils.py 98103 2023-01-17 14:15:46Z vboxsync $
3# pylint: disable=too-many-lines
4
5"""
6Path Utility Functions.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2012-2023 Oracle and/or its affiliates.
12
13This file is part of VirtualBox base platform packages, as
14available from https://www.virtualbox.org.
15
16This program is free software; you can redistribute it and/or
17modify it under the terms of the GNU General Public License
18as published by the Free Software Foundation, in version 3 of the
19License.
20
21This program is distributed in the hope that it will be useful, but
22WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with this program; if not, see <https://www.gnu.org/licenses>.
28
29The contents of this file may alternatively be used under the terms
30of the Common Development and Distribution License Version 1.0
31(CDDL), a copy of it is provided in the "COPYING.CDDL" file included
32in the VirtualBox distribution, in which case the provisions of the
33CDDL are applicable instead of those of the GPL.
34
35You may elect to license modified versions of this file under the
36terms and conditions of either the GPL or the CDDL or both.
37
38SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
39"""
40__version__ = "$Revision: 98103 $"
41
42
43# Standard Python imports.
44import unittest;
45
46
47## @name Path data keyed by fDosStyle bool value.
48## @{
49g_dsPathSlash = { False: '/', True: '\\', };
50g_dasPathSlashes = { False: ('/',), True: ('\\', '/', ), };
51g_dasPathSeparators = { False: ('/',), True: ('\\', '/', ':', ), };
52## @}
53
54
55def 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
93class 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
120if __name__ == '__main__':
121 unittest.main();
122 # not reached.
123
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette