VirtualBox

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

Last change on this file since 94896 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1# -*- coding: utf-8 -*-
2# $Id: pathutils.py 93115 2022-01-01 11:31:46Z vboxsync $
3# pylint: disable=too-many-lines
4
5"""
6Path Utility Functions.
7"""
8
9__copyright__ = \
10"""
11Copyright (C) 2012-2022 Oracle Corporation
12
13This file is part of VirtualBox Open Source Edition (OSE), as
14available from http://www.virtualbox.org. This file is free software;
15you can redistribute it and/or modify it under the terms of the GNU
16General Public License (GPL) as published by the Free Software
17Foundation, in version 2 as it comes in the "COPYING" file of the
18VirtualBox OSE distribution. VirtualBox OSE is distributed in the
19hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
20
21The contents of this file may alternatively be used under the terms
22of the Common Development and Distribution License Version 1.0
23(CDDL) only, as it comes in the "COPYING.CDDL" file of the
24VirtualBox OSE distribution, in which case the provisions of the
25CDDL are applicable instead of those of the GPL.
26
27You may elect to license modified versions of this file under the
28terms and conditions of either the GPL or the CDDL or both.
29"""
30__version__ = "$Revision: 93115 $"
31
32
33# Standard Python imports.
34import unittest;
35
36
37## @name Path data keyed by fDosStyle bool value.
38## @{
39g_dsPathSlash = { False: '/', True: '\\', };
40g_dasPathSlashes = { False: ('/',), True: ('\\', '/', ), };
41g_dasPathSeparators = { False: ('/',), True: ('\\', '/', ':', ), };
42## @}
43
44
45def 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
83class 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
110if __name__ == '__main__':
111 unittest.main();
112 # not reached.
113
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