]>
Commit | Line | Data |
---|---|---|
1be2ed90 HC |
1 | ## @file\r |
2 | # Override built in function file.open to provide support for long file path\r | |
3 | #\r | |
1b2467c5 | 4 | # Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r |
1be2ed90 HC |
5 | # This program and the accompanying materials\r |
6 | # are licensed and made available under the terms and conditions of the BSD License\r | |
7 | # which accompanies this distribution. The full text of the license may be found at\r | |
8 | # http://opensource.org/licenses/bsd-license.php\r | |
9 | #\r | |
10 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | #\r | |
13 | \r | |
14 | import os\r | |
15 | import platform\r | |
16 | import shutil\r | |
1b2467c5 | 17 | import codecs\r |
1be2ed90 HC |
18 | \r |
19 | ##\r | |
20 | # OpenLongPath\r | |
21 | # Convert a file path to a long file path\r | |
22 | #\r | |
23 | def LongFilePath(FileName):\r | |
24 | FileName = os.path.normpath(FileName)\r | |
25 | if platform.system() == 'Windows':\r | |
26 | if FileName.startswith('\\\\?\\'):\r | |
27 | return FileName\r | |
28 | if FileName.startswith('\\\\'):\r | |
29 | return '\\\\?\\UNC\\' + FileName[2:]\r | |
30 | if os.path.isabs(FileName):\r | |
31 | return '\\\\?\\' + FileName\r | |
32 | return FileName\r | |
33 | \r | |
34 | ##\r | |
35 | # OpenLongFilePath\r | |
36 | # wrap open to support opening a long file path\r | |
37 | #\r | |
38 | def OpenLongFilePath(FileName, Mode='r', Buffer= -1):\r | |
39 | return open(LongFilePath(FileName), Mode, Buffer)\r | |
40 | \r | |
1b2467c5 HC |
41 | def CodecOpenLongFilePath(Filename, Mode='rb', Encoding=None, Errors='strict', Buffering=1):\r |
42 | return codecs.open(LongFilePath(Filename), Mode, Encoding, Errors, Buffering)\r | |
43 | \r | |
1be2ed90 HC |
44 | ##\r |
45 | # CopyLongFilePath\r | |
46 | # wrap copyfile to support copy a long file path\r | |
47 | #\r | |
48 | def CopyLongFilePath(src, dst):\r | |
49 | with open(LongFilePath(src), 'rb') as fsrc:\r | |
50 | with open(LongFilePath(dst), 'wb') as fdst:\r | |
51 | shutil.copyfileobj(fsrc, fdst)\r | |
52 | \r | |
53 | ## Convert a python unicode string to a normal string\r | |
54 | #\r | |
55 | # Convert a python unicode string to a normal string\r | |
56 | # UniToStr(u'I am a string') is 'I am a string'\r | |
57 | #\r | |
58 | # @param Uni: The python unicode string\r | |
59 | #\r | |
60 | # @retval: The formatted normal string\r | |
61 | #\r | |
62 | def UniToStr(Uni):\r | |
63 | return repr(Uni)[2:-1]\r |