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