]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/LongFilePathSupport.py
BaseTools: Replace BSD License with BSD+Patent License
[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 - 2015, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7
8 import os
9 import platform
10 import shutil
11 import codecs
12
13 ##
14 # OpenLongPath
15 # Convert a file path to a long file path
16 #
17 def LongFilePath(FileName):
18 FileName = os.path.normpath(FileName)
19 if platform.system() == 'Windows':
20 if FileName.startswith('\\\\?\\'):
21 return FileName
22 if FileName.startswith('\\\\'):
23 return '\\\\?\\UNC\\' + FileName[2:]
24 if os.path.isabs(FileName):
25 return '\\\\?\\' + FileName
26 return FileName
27
28 ##
29 # OpenLongFilePath
30 # wrap open to support opening a long file path
31 #
32 def OpenLongFilePath(FileName, Mode='r', Buffer= -1):
33 return open(LongFilePath(FileName), Mode, Buffer)
34
35 def CodecOpenLongFilePath(Filename, Mode='rb', Encoding=None, Errors='strict', Buffering=1):
36 return codecs.open(LongFilePath(Filename), Mode, Encoding, Errors, Buffering)
37
38 ##
39 # CopyLongFilePath
40 # wrap copyfile to support copy a long file path
41 #
42 def CopyLongFilePath(src, dst):
43 with open(LongFilePath(src), 'rb') as fsrc:
44 with open(LongFilePath(dst), 'wb') as fdst:
45 shutil.copyfileobj(fsrc, fdst)