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# 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
14import os
15import platform
16import shutil
17import codecs
18
19##
20# OpenLongPath
21# Convert a file path to a long file path
22#
23def LongFilePath(FileName):
24    FileName = os.path.normpath(FileName)
25    if platform.system() == 'Windows':
26        if FileName.startswith('\\\\?\\'):
27            return FileName
28        if FileName.startswith('\\\\'):
29            return '\\\\?\\UNC\\' + FileName[2:]
30        if os.path.isabs(FileName):
31            return '\\\\?\\' + FileName
32    return FileName
33
34##
35# OpenLongFilePath
36# wrap open to support opening a long file path
37#
38def OpenLongFilePath(FileName, Mode='r', Buffer= -1):
39    return open(LongFilePath(FileName), Mode, Buffer)
40
41def CodecOpenLongFilePath(Filename, Mode='rb', Encoding=None, Errors='strict', Buffering=1):
42    return codecs.open(LongFilePath(Filename), Mode, Encoding, Errors, Buffering)
43
44##
45# CopyLongFilePath
46# wrap copyfile to support copy a long file path
47#
48def CopyLongFilePath(src, dst):
49    with open(LongFilePath(src), 'rb') as fsrc:
50        with open(LongFilePath(dst), 'wb') as fdst:
51            shutil.copyfileobj(fsrc, fdst)
52
53## Convert a python unicode string to a normal string
54#
55# Convert a python unicode string to a normal string
56# UniToStr(u'I am a string') is 'I am a string'
57#
58# @param Uni:  The python unicode string
59#
60# @retval:     The formatted normal string
61#
62def UniToStr(Uni):
63    return repr(Uni)[2:-1]
64