Best Python code snippet using playwright-python
shlwapi.py
Source:shlwapi.py  
1#!/usr/bin/env python2# -*- coding: utf-8 -*-3# Copyright (c) 2009-2014, Mario Vilas4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions are met:8#9#     * Redistributions of source code must retain the above copyright notice,10#       this list of conditions and the following disclaimer.11#     * Redistributions in binary form must reproduce the above copyright12#       notice,this list of conditions and the following disclaimer in the13#       documentation and/or other materials provided with the distribution.14#     * Neither the name of the copyright holder nor the names of its15#       contributors may be used to endorse or promote products derived from16#       this software without specific prior written permission.17#18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE28# POSSIBILITY OF SUCH DAMAGE.29"""30Wrapper for shlwapi.dll in ctypes.31"""32__revision__ = "$Id$"33from winappdbg.win32.defines import *34from winappdbg.win32.kernel32 import *35#==============================================================================36# This is used later on to calculate the list of exported symbols.37_all = None38_all = set(vars().keys())39#==============================================================================40OS_WINDOWS                  = 041OS_NT                       = 142OS_WIN95ORGREATER           = 243OS_NT4ORGREATER             = 344OS_WIN98ORGREATER           = 545OS_WIN98_GOLD               = 646OS_WIN2000ORGREATER         = 747OS_WIN2000PRO               = 848OS_WIN2000SERVER            = 949OS_WIN2000ADVSERVER         = 1050OS_WIN2000DATACENTER        = 1151OS_WIN2000TERMINAL          = 1252OS_EMBEDDED                 = 1353OS_TERMINALCLIENT           = 1454OS_TERMINALREMOTEADMIN      = 1555OS_WIN95_GOLD               = 1656OS_MEORGREATER              = 1757OS_XPORGREATER              = 1858OS_HOME                     = 1959OS_PROFESSIONAL             = 2060OS_DATACENTER               = 2161OS_ADVSERVER                = 2262OS_SERVER                   = 2363OS_TERMINALSERVER           = 2464OS_PERSONALTERMINALSERVER   = 2565OS_FASTUSERSWITCHING        = 2666OS_WELCOMELOGONUI           = 2767OS_DOMAINMEMBER             = 2868OS_ANYSERVER                = 2969OS_WOW6432                  = 3070OS_WEBSERVER                = 3171OS_SMALLBUSINESSSERVER      = 3272OS_TABLETPC                 = 3373OS_SERVERADMINUI            = 3474OS_MEDIACENTER              = 3575OS_APPLIANCE                = 3676#--- shlwapi.dll --------------------------------------------------------------77# BOOL IsOS(78#     DWORD dwOS79# );80def IsOS(dwOS):81    try:82        _IsOS = windll.shlwapi.IsOS83        _IsOS.argtypes = [DWORD]84        _IsOS.restype  = bool85    except AttributeError:86        # According to MSDN, on Windows versions prior to Vista87        # this function is exported only by ordinal number 437.88        # http://msdn.microsoft.com/en-us/library/bb773795%28VS.85%29.aspx89        _GetProcAddress = windll.kernel32.GetProcAddress90        _GetProcAddress.argtypes = [HINSTANCE, DWORD]91        _GetProcAddress.restype  = LPVOID92        _IsOS = windll.kernel32.GetProcAddress(windll.shlwapi._handle, 437)93        _IsOS = WINFUNCTYPE(bool, DWORD)(_IsOS)94    return _IsOS(dwOS)95# LPTSTR PathAddBackslash(96#     LPTSTR lpszPath97# );98def PathAddBackslashA(lpszPath):99    _PathAddBackslashA = windll.shlwapi.PathAddBackslashA100    _PathAddBackslashA.argtypes = [LPSTR]101    _PathAddBackslashA.restype  = LPSTR102    lpszPath = ctypes.create_string_buffer(lpszPath, MAX_PATH)103    retval = _PathAddBackslashA(lpszPath)104    if retval == NULL:105        raise ctypes.WinError()106    return lpszPath.value107def PathAddBackslashW(lpszPath):108    _PathAddBackslashW = windll.shlwapi.PathAddBackslashW109    _PathAddBackslashW.argtypes = [LPWSTR]110    _PathAddBackslashW.restype  = LPWSTR111    lpszPath = ctypes.create_unicode_buffer(lpszPath, MAX_PATH)112    retval = _PathAddBackslashW(lpszPath)113    if retval == NULL:114        raise ctypes.WinError()115    return lpszPath.value116PathAddBackslash = GuessStringType(PathAddBackslashA, PathAddBackslashW)117# BOOL PathAddExtension(118#     LPTSTR pszPath,119#     LPCTSTR pszExtension120# );121def PathAddExtensionA(lpszPath, pszExtension = None):122    _PathAddExtensionA = windll.shlwapi.PathAddExtensionA123    _PathAddExtensionA.argtypes = [LPSTR, LPSTR]124    _PathAddExtensionA.restype  = bool125    _PathAddExtensionA.errcheck = RaiseIfZero126    if not pszExtension:127        pszExtension = None128    lpszPath = ctypes.create_string_buffer(lpszPath, MAX_PATH)129    _PathAddExtensionA(lpszPath, pszExtension)130    return lpszPath.value131def PathAddExtensionW(lpszPath, pszExtension = None):132    _PathAddExtensionW = windll.shlwapi.PathAddExtensionW133    _PathAddExtensionW.argtypes = [LPWSTR, LPWSTR]134    _PathAddExtensionW.restype  = bool135    _PathAddExtensionW.errcheck = RaiseIfZero136    if not pszExtension:137        pszExtension = None138    lpszPath = ctypes.create_unicode_buffer(lpszPath, MAX_PATH)139    _PathAddExtensionW(lpszPath, pszExtension)140    return lpszPath.value141PathAddExtension = GuessStringType(PathAddExtensionA, PathAddExtensionW)142# BOOL PathAppend(143#     LPTSTR pszPath,144#     LPCTSTR pszMore145# );146def PathAppendA(lpszPath, pszMore = None):147    _PathAppendA = windll.shlwapi.PathAppendA148    _PathAppendA.argtypes = [LPSTR, LPSTR]149    _PathAppendA.restype  = bool150    _PathAppendA.errcheck = RaiseIfZero151    if not pszMore:152        pszMore = None153    lpszPath = ctypes.create_string_buffer(lpszPath, MAX_PATH)154    _PathAppendA(lpszPath, pszMore)155    return lpszPath.value156def PathAppendW(lpszPath, pszMore = None):157    _PathAppendW = windll.shlwapi.PathAppendW158    _PathAppendW.argtypes = [LPWSTR, LPWSTR]159    _PathAppendW.restype  = bool160    _PathAppendW.errcheck = RaiseIfZero161    if not pszMore:162        pszMore = None163    lpszPath = ctypes.create_unicode_buffer(lpszPath, MAX_PATH)164    _PathAppendW(lpszPath, pszMore)165    return lpszPath.value166PathAppend = GuessStringType(PathAppendA, PathAppendW)167# LPTSTR PathCombine(168#     LPTSTR lpszDest,169#     LPCTSTR lpszDir,170#     LPCTSTR lpszFile171# );172def PathCombineA(lpszDir, lpszFile):173    _PathCombineA = windll.shlwapi.PathCombineA174    _PathCombineA.argtypes = [LPSTR, LPSTR, LPSTR]175    _PathCombineA.restype  = LPSTR176    lpszDest = ctypes.create_string_buffer("", max(MAX_PATH, len(lpszDir) + len(lpszFile) + 1))177    retval = _PathCombineA(lpszDest, lpszDir, lpszFile)178    if retval == NULL:179        return None180    return lpszDest.value181def PathCombineW(lpszDir, lpszFile):182    _PathCombineW = windll.shlwapi.PathCombineW183    _PathCombineW.argtypes = [LPWSTR, LPWSTR, LPWSTR]184    _PathCombineW.restype  = LPWSTR185    lpszDest = ctypes.create_unicode_buffer(u"", max(MAX_PATH, len(lpszDir) + len(lpszFile) + 1))186    retval = _PathCombineW(lpszDest, lpszDir, lpszFile)187    if retval == NULL:188        return None189    return lpszDest.value190PathCombine = GuessStringType(PathCombineA, PathCombineW)191# BOOL PathCanonicalize(192#     LPTSTR lpszDst,193#     LPCTSTR lpszSrc194# );195def PathCanonicalizeA(lpszSrc):196    _PathCanonicalizeA = windll.shlwapi.PathCanonicalizeA197    _PathCanonicalizeA.argtypes = [LPSTR, LPSTR]198    _PathCanonicalizeA.restype  = bool199    _PathCanonicalizeA.errcheck = RaiseIfZero200    lpszDst = ctypes.create_string_buffer("", MAX_PATH)201    _PathCanonicalizeA(lpszDst, lpszSrc)202    return lpszDst.value203def PathCanonicalizeW(lpszSrc):204    _PathCanonicalizeW = windll.shlwapi.PathCanonicalizeW205    _PathCanonicalizeW.argtypes = [LPWSTR, LPWSTR]206    _PathCanonicalizeW.restype  = bool207    _PathCanonicalizeW.errcheck = RaiseIfZero208    lpszDst = ctypes.create_unicode_buffer(u"", MAX_PATH)209    _PathCanonicalizeW(lpszDst, lpszSrc)210    return lpszDst.value211PathCanonicalize = GuessStringType(PathCanonicalizeA, PathCanonicalizeW)212# BOOL PathRelativePathTo(213#   _Out_  LPTSTR pszPath,214#   _In_   LPCTSTR pszFrom,215#   _In_   DWORD dwAttrFrom,216#   _In_   LPCTSTR pszTo,217#   _In_   DWORD dwAttrTo218# );219def PathRelativePathToA(pszFrom = None, dwAttrFrom = FILE_ATTRIBUTE_DIRECTORY, pszTo = None, dwAttrTo = FILE_ATTRIBUTE_DIRECTORY):220    _PathRelativePathToA = windll.shlwapi.PathRelativePathToA221    _PathRelativePathToA.argtypes = [LPSTR, LPSTR, DWORD, LPSTR, DWORD]222    _PathRelativePathToA.restype  = bool223    _PathRelativePathToA.errcheck = RaiseIfZero224    # Make the paths absolute or the function fails.225    if pszFrom:226        pszFrom = GetFullPathNameA(pszFrom)[0]227    else:228        pszFrom = GetCurrentDirectoryA()229    if pszTo:230        pszTo = GetFullPathNameA(pszTo)[0]231    else:232        pszTo = GetCurrentDirectoryA()233    # Argh, this function doesn't receive an output buffer size!234    # We'll try to guess the maximum possible buffer size.235    dwPath = max((len(pszFrom) + len(pszTo)) * 2 + 1, MAX_PATH + 1)236    pszPath = ctypes.create_string_buffer('', dwPath)237    # Also, it doesn't set the last error value.238    # Whoever coded it must have been drunk or tripping on acid. Or both.239    # The only failure conditions I've seen were invalid paths, paths not240    # on the same drive, or the path is not absolute.241    SetLastError(ERROR_INVALID_PARAMETER)242    _PathRelativePathToA(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo)243    return pszPath.value244def PathRelativePathToW(pszFrom = None, dwAttrFrom = FILE_ATTRIBUTE_DIRECTORY, pszTo = None, dwAttrTo = FILE_ATTRIBUTE_DIRECTORY):245    _PathRelativePathToW = windll.shlwapi.PathRelativePathToW246    _PathRelativePathToW.argtypes = [LPWSTR, LPWSTR, DWORD, LPWSTR, DWORD]247    _PathRelativePathToW.restype  = bool248    _PathRelativePathToW.errcheck = RaiseIfZero249    # Refer to PathRelativePathToA to know why this code is so ugly.250    if pszFrom:251        pszFrom = GetFullPathNameW(pszFrom)[0]252    else:253        pszFrom = GetCurrentDirectoryW()254    if pszTo:255        pszTo = GetFullPathNameW(pszTo)[0]256    else:257        pszTo = GetCurrentDirectoryW()258    dwPath = max((len(pszFrom) + len(pszTo)) * 2 + 1, MAX_PATH + 1)259    pszPath = ctypes.create_unicode_buffer(u'', dwPath)260    SetLastError(ERROR_INVALID_PARAMETER)261    _PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo)262    return pszPath.value263PathRelativePathTo = GuessStringType(PathRelativePathToA, PathRelativePathToW)264# BOOL PathFileExists(265#     LPCTSTR pszPath266# );267def PathFileExistsA(pszPath):268    _PathFileExistsA = windll.shlwapi.PathFileExistsA269    _PathFileExistsA.argtypes = [LPSTR]270    _PathFileExistsA.restype  = bool271    return _PathFileExistsA(pszPath)272def PathFileExistsW(pszPath):273    _PathFileExistsW = windll.shlwapi.PathFileExistsW274    _PathFileExistsW.argtypes = [LPWSTR]275    _PathFileExistsW.restype  = bool276    return _PathFileExistsW(pszPath)277PathFileExists = GuessStringType(PathFileExistsA, PathFileExistsW)278# LPTSTR PathFindExtension(279#     LPCTSTR pszPath280# );281def PathFindExtensionA(pszPath):282    _PathFindExtensionA = windll.shlwapi.PathFindExtensionA283    _PathFindExtensionA.argtypes = [LPSTR]284    _PathFindExtensionA.restype  = LPSTR285    pszPath = ctypes.create_string_buffer(pszPath)286    return _PathFindExtensionA(pszPath)287def PathFindExtensionW(pszPath):288    _PathFindExtensionW = windll.shlwapi.PathFindExtensionW289    _PathFindExtensionW.argtypes = [LPWSTR]290    _PathFindExtensionW.restype  = LPWSTR291    pszPath = ctypes.create_unicode_buffer(pszPath)292    return _PathFindExtensionW(pszPath)293PathFindExtension = GuessStringType(PathFindExtensionA, PathFindExtensionW)294# LPTSTR PathFindFileName(295#     LPCTSTR pszPath296# );297def PathFindFileNameA(pszPath):298    _PathFindFileNameA = windll.shlwapi.PathFindFileNameA299    _PathFindFileNameA.argtypes = [LPSTR]300    _PathFindFileNameA.restype  = LPSTR301    pszPath = ctypes.create_string_buffer(pszPath)302    return _PathFindFileNameA(pszPath)303def PathFindFileNameW(pszPath):304    _PathFindFileNameW = windll.shlwapi.PathFindFileNameW305    _PathFindFileNameW.argtypes = [LPWSTR]306    _PathFindFileNameW.restype  = LPWSTR307    pszPath = ctypes.create_unicode_buffer(pszPath)308    return _PathFindFileNameW(pszPath)309PathFindFileName = GuessStringType(PathFindFileNameA, PathFindFileNameW)310# LPTSTR PathFindNextComponent(311#     LPCTSTR pszPath312# );313def PathFindNextComponentA(pszPath):314    _PathFindNextComponentA = windll.shlwapi.PathFindNextComponentA315    _PathFindNextComponentA.argtypes = [LPSTR]316    _PathFindNextComponentA.restype  = LPSTR317    pszPath = ctypes.create_string_buffer(pszPath)318    return _PathFindNextComponentA(pszPath)319def PathFindNextComponentW(pszPath):320    _PathFindNextComponentW = windll.shlwapi.PathFindNextComponentW321    _PathFindNextComponentW.argtypes = [LPWSTR]322    _PathFindNextComponentW.restype  = LPWSTR323    pszPath = ctypes.create_unicode_buffer(pszPath)324    return _PathFindNextComponentW(pszPath)325PathFindNextComponent = GuessStringType(PathFindNextComponentA, PathFindNextComponentW)326# BOOL PathFindOnPath(327#     LPTSTR pszFile,328#     LPCTSTR *ppszOtherDirs329# );330def PathFindOnPathA(pszFile, ppszOtherDirs = None):331    _PathFindOnPathA = windll.shlwapi.PathFindOnPathA332    _PathFindOnPathA.argtypes = [LPSTR, LPSTR]333    _PathFindOnPathA.restype  = bool334    pszFile = ctypes.create_string_buffer(pszFile, MAX_PATH)335    if not ppszOtherDirs:336        ppszOtherDirs = None337    else:338        szArray = ""339        for pszOtherDirs in ppszOtherDirs:340            if pszOtherDirs:341                szArray = "%s%s\0" % (szArray, pszOtherDirs)342        szArray = szArray + "\0"343        pszOtherDirs = ctypes.create_string_buffer(szArray)344        ppszOtherDirs = ctypes.pointer(pszOtherDirs)345    if _PathFindOnPathA(pszFile, ppszOtherDirs):346        return pszFile.value347    return None348def PathFindOnPathW(pszFile, ppszOtherDirs = None):349    _PathFindOnPathW = windll.shlwapi.PathFindOnPathA350    _PathFindOnPathW.argtypes = [LPWSTR, LPWSTR]351    _PathFindOnPathW.restype  = bool352    pszFile = ctypes.create_unicode_buffer(pszFile, MAX_PATH)353    if not ppszOtherDirs:354        ppszOtherDirs = None355    else:356        szArray = u""357        for pszOtherDirs in ppszOtherDirs:358            if pszOtherDirs:359                szArray = u"%s%s\0" % (szArray, pszOtherDirs)360        szArray = szArray + u"\0"361        pszOtherDirs = ctypes.create_unicode_buffer(szArray)362        ppszOtherDirs = ctypes.pointer(pszOtherDirs)363    if _PathFindOnPathW(pszFile, ppszOtherDirs):364        return pszFile.value365    return None366PathFindOnPath = GuessStringType(PathFindOnPathA, PathFindOnPathW)367# LPTSTR PathGetArgs(368#     LPCTSTR pszPath369# );370def PathGetArgsA(pszPath):371    _PathGetArgsA = windll.shlwapi.PathGetArgsA372    _PathGetArgsA.argtypes = [LPSTR]373    _PathGetArgsA.restype  = LPSTR374    pszPath = ctypes.create_string_buffer(pszPath)375    return _PathGetArgsA(pszPath)376def PathGetArgsW(pszPath):377    _PathGetArgsW = windll.shlwapi.PathGetArgsW378    _PathGetArgsW.argtypes = [LPWSTR]379    _PathGetArgsW.restype  = LPWSTR380    pszPath = ctypes.create_unicode_buffer(pszPath)381    return _PathGetArgsW(pszPath)382PathGetArgs = GuessStringType(PathGetArgsA, PathGetArgsW)383# BOOL PathIsContentType(384#     LPCTSTR pszPath,385#     LPCTSTR pszContentType386# );387def PathIsContentTypeA(pszPath, pszContentType):388    _PathIsContentTypeA = windll.shlwapi.PathIsContentTypeA389    _PathIsContentTypeA.argtypes = [LPSTR, LPSTR]390    _PathIsContentTypeA.restype  = bool391    return _PathIsContentTypeA(pszPath, pszContentType)392def PathIsContentTypeW(pszPath, pszContentType):393    _PathIsContentTypeW = windll.shlwapi.PathIsContentTypeW394    _PathIsContentTypeW.argtypes = [LPWSTR, LPWSTR]395    _PathIsContentTypeW.restype  = bool396    return _PathIsContentTypeW(pszPath, pszContentType)397PathIsContentType = GuessStringType(PathIsContentTypeA, PathIsContentTypeW)398# BOOL PathIsDirectory(399#     LPCTSTR pszPath400# );401def PathIsDirectoryA(pszPath):402    _PathIsDirectoryA = windll.shlwapi.PathIsDirectoryA403    _PathIsDirectoryA.argtypes = [LPSTR]404    _PathIsDirectoryA.restype  = bool405    return _PathIsDirectoryA(pszPath)406def PathIsDirectoryW(pszPath):407    _PathIsDirectoryW = windll.shlwapi.PathIsDirectoryW408    _PathIsDirectoryW.argtypes = [LPWSTR]409    _PathIsDirectoryW.restype  = bool410    return _PathIsDirectoryW(pszPath)411PathIsDirectory = GuessStringType(PathIsDirectoryA, PathIsDirectoryW)412# BOOL PathIsDirectoryEmpty(413#     LPCTSTR pszPath414# );415def PathIsDirectoryEmptyA(pszPath):416    _PathIsDirectoryEmptyA = windll.shlwapi.PathIsDirectoryEmptyA417    _PathIsDirectoryEmptyA.argtypes = [LPSTR]418    _PathIsDirectoryEmptyA.restype  = bool419    return _PathIsDirectoryEmptyA(pszPath)420def PathIsDirectoryEmptyW(pszPath):421    _PathIsDirectoryEmptyW = windll.shlwapi.PathIsDirectoryEmptyW422    _PathIsDirectoryEmptyW.argtypes = [LPWSTR]423    _PathIsDirectoryEmptyW.restype  = bool424    return _PathIsDirectoryEmptyW(pszPath)425PathIsDirectoryEmpty = GuessStringType(PathIsDirectoryEmptyA, PathIsDirectoryEmptyW)426# BOOL PathIsNetworkPath(427#     LPCTSTR pszPath428# );429def PathIsNetworkPathA(pszPath):430    _PathIsNetworkPathA = windll.shlwapi.PathIsNetworkPathA431    _PathIsNetworkPathA.argtypes = [LPSTR]432    _PathIsNetworkPathA.restype  = bool433    return _PathIsNetworkPathA(pszPath)434def PathIsNetworkPathW(pszPath):435    _PathIsNetworkPathW = windll.shlwapi.PathIsNetworkPathW436    _PathIsNetworkPathW.argtypes = [LPWSTR]437    _PathIsNetworkPathW.restype  = bool438    return _PathIsNetworkPathW(pszPath)439PathIsNetworkPath = GuessStringType(PathIsNetworkPathA, PathIsNetworkPathW)440# BOOL PathIsRelative(441#     LPCTSTR lpszPath442# );443def PathIsRelativeA(pszPath):444    _PathIsRelativeA = windll.shlwapi.PathIsRelativeA445    _PathIsRelativeA.argtypes = [LPSTR]446    _PathIsRelativeA.restype  = bool447    return _PathIsRelativeA(pszPath)448def PathIsRelativeW(pszPath):449    _PathIsRelativeW = windll.shlwapi.PathIsRelativeW450    _PathIsRelativeW.argtypes = [LPWSTR]451    _PathIsRelativeW.restype  = bool452    return _PathIsRelativeW(pszPath)453PathIsRelative = GuessStringType(PathIsRelativeA, PathIsRelativeW)454# BOOL PathIsRoot(455#     LPCTSTR pPath456# );457def PathIsRootA(pszPath):458    _PathIsRootA = windll.shlwapi.PathIsRootA459    _PathIsRootA.argtypes = [LPSTR]460    _PathIsRootA.restype  = bool461    return _PathIsRootA(pszPath)462def PathIsRootW(pszPath):463    _PathIsRootW = windll.shlwapi.PathIsRootW464    _PathIsRootW.argtypes = [LPWSTR]465    _PathIsRootW.restype  = bool466    return _PathIsRootW(pszPath)467PathIsRoot = GuessStringType(PathIsRootA, PathIsRootW)468# BOOL PathIsSameRoot(469#     LPCTSTR pszPath1,470#     LPCTSTR pszPath2471# );472def PathIsSameRootA(pszPath1, pszPath2):473    _PathIsSameRootA = windll.shlwapi.PathIsSameRootA474    _PathIsSameRootA.argtypes = [LPSTR, LPSTR]475    _PathIsSameRootA.restype  = bool476    return _PathIsSameRootA(pszPath1, pszPath2)477def PathIsSameRootW(pszPath1, pszPath2):478    _PathIsSameRootW = windll.shlwapi.PathIsSameRootW479    _PathIsSameRootW.argtypes = [LPWSTR, LPWSTR]480    _PathIsSameRootW.restype  = bool481    return _PathIsSameRootW(pszPath1, pszPath2)482PathIsSameRoot = GuessStringType(PathIsSameRootA, PathIsSameRootW)483# BOOL PathIsUNC(484#     LPCTSTR pszPath485# );486def PathIsUNCA(pszPath):487    _PathIsUNCA = windll.shlwapi.PathIsUNCA488    _PathIsUNCA.argtypes = [LPSTR]489    _PathIsUNCA.restype  = bool490    return _PathIsUNCA(pszPath)491def PathIsUNCW(pszPath):492    _PathIsUNCW = windll.shlwapi.PathIsUNCW493    _PathIsUNCW.argtypes = [LPWSTR]494    _PathIsUNCW.restype  = bool495    return _PathIsUNCW(pszPath)496PathIsUNC = GuessStringType(PathIsUNCA, PathIsUNCW)497# XXX WARNING498# PathMakePretty turns filenames into all lowercase.499# I'm not sure how well that might work on Wine.500# BOOL PathMakePretty(501#     LPCTSTR pszPath502# );503def PathMakePrettyA(pszPath):504    _PathMakePrettyA = windll.shlwapi.PathMakePrettyA505    _PathMakePrettyA.argtypes = [LPSTR]506    _PathMakePrettyA.restype  = bool507    _PathMakePrettyA.errcheck = RaiseIfZero508    pszPath = ctypes.create_string_buffer(pszPath, MAX_PATH)509    _PathMakePrettyA(pszPath)510    return pszPath.value511def PathMakePrettyW(pszPath):512    _PathMakePrettyW = windll.shlwapi.PathMakePrettyW513    _PathMakePrettyW.argtypes = [LPWSTR]514    _PathMakePrettyW.restype  = bool515    _PathMakePrettyW.errcheck = RaiseIfZero516    pszPath = ctypes.create_unicode_buffer(pszPath, MAX_PATH)517    _PathMakePrettyW(pszPath)518    return pszPath.value519PathMakePretty = GuessStringType(PathMakePrettyA, PathMakePrettyW)520# void PathRemoveArgs(521#     LPTSTR pszPath522# );523def PathRemoveArgsA(pszPath):524    _PathRemoveArgsA = windll.shlwapi.PathRemoveArgsA525    _PathRemoveArgsA.argtypes = [LPSTR]526    pszPath = ctypes.create_string_buffer(pszPath, MAX_PATH)527    _PathRemoveArgsA(pszPath)528    return pszPath.value529def PathRemoveArgsW(pszPath):530    _PathRemoveArgsW = windll.shlwapi.PathRemoveArgsW531    _PathRemoveArgsW.argtypes = [LPWSTR]532    pszPath = ctypes.create_unicode_buffer(pszPath, MAX_PATH)533    _PathRemoveArgsW(pszPath)534    return pszPath.value535PathRemoveArgs = GuessStringType(PathRemoveArgsA, PathRemoveArgsW)536# void PathRemoveBackslash(537#     LPTSTR pszPath538# );539def PathRemoveBackslashA(pszPath):540    _PathRemoveBackslashA = windll.shlwapi.PathRemoveBackslashA541    _PathRemoveBackslashA.argtypes = [LPSTR]542    pszPath = ctypes.create_string_buffer(pszPath, MAX_PATH)543    _PathRemoveBackslashA(pszPath)544    return pszPath.value545def PathRemoveBackslashW(pszPath):546    _PathRemoveBackslashW = windll.shlwapi.PathRemoveBackslashW547    _PathRemoveBackslashW.argtypes = [LPWSTR]548    pszPath = ctypes.create_unicode_buffer(pszPath, MAX_PATH)549    _PathRemoveBackslashW(pszPath)550    return pszPath.value551PathRemoveBackslash = GuessStringType(PathRemoveBackslashA, PathRemoveBackslashW)552# void PathRemoveExtension(553#     LPTSTR pszPath554# );555def PathRemoveExtensionA(pszPath):556    _PathRemoveExtensionA = windll.shlwapi.PathRemoveExtensionA557    _PathRemoveExtensionA.argtypes = [LPSTR]558    pszPath = ctypes.create_string_buffer(pszPath, MAX_PATH)559    _PathRemoveExtensionA(pszPath)560    return pszPath.value561def PathRemoveExtensionW(pszPath):562    _PathRemoveExtensionW = windll.shlwapi.PathRemoveExtensionW563    _PathRemoveExtensionW.argtypes = [LPWSTR]564    pszPath = ctypes.create_unicode_buffer(pszPath, MAX_PATH)565    _PathRemoveExtensionW(pszPath)566    return pszPath.value567PathRemoveExtension = GuessStringType(PathRemoveExtensionA, PathRemoveExtensionW)568# void PathRemoveFileSpec(569#     LPTSTR pszPath570# );571def PathRemoveFileSpecA(pszPath):572    _PathRemoveFileSpecA = windll.shlwapi.PathRemoveFileSpecA573    _PathRemoveFileSpecA.argtypes = [LPSTR]574    pszPath = ctypes.create_string_buffer(pszPath, MAX_PATH)575    _PathRemoveFileSpecA(pszPath)576    return pszPath.value577def PathRemoveFileSpecW(pszPath):578    _PathRemoveFileSpecW = windll.shlwapi.PathRemoveFileSpecW579    _PathRemoveFileSpecW.argtypes = [LPWSTR]580    pszPath = ctypes.create_unicode_buffer(pszPath, MAX_PATH)581    _PathRemoveFileSpecW(pszPath)582    return pszPath.value583PathRemoveFileSpec = GuessStringType(PathRemoveFileSpecA, PathRemoveFileSpecW)584# BOOL PathRenameExtension(585#     LPTSTR pszPath,586#     LPCTSTR pszExt587# );588def PathRenameExtensionA(pszPath, pszExt):589    _PathRenameExtensionA = windll.shlwapi.PathRenameExtensionA590    _PathRenameExtensionA.argtypes = [LPSTR, LPSTR]591    _PathRenameExtensionA.restype  = bool592    pszPath = ctypes.create_string_buffer(pszPath, MAX_PATH)593    if _PathRenameExtensionA(pszPath, pszExt):594        return pszPath.value595    return None596def PathRenameExtensionW(pszPath, pszExt):597    _PathRenameExtensionW = windll.shlwapi.PathRenameExtensionW598    _PathRenameExtensionW.argtypes = [LPWSTR, LPWSTR]599    _PathRenameExtensionW.restype  = bool600    pszPath = ctypes.create_unicode_buffer(pszPath, MAX_PATH)601    if _PathRenameExtensionW(pszPath, pszExt):602        return pszPath.value603    return None604PathRenameExtension = GuessStringType(PathRenameExtensionA, PathRenameExtensionW)605# BOOL PathUnExpandEnvStrings(606#     LPCTSTR pszPath,607#     LPTSTR pszBuf,608#     UINT cchBuf609# );610def PathUnExpandEnvStringsA(pszPath):611    _PathUnExpandEnvStringsA = windll.shlwapi.PathUnExpandEnvStringsA612    _PathUnExpandEnvStringsA.argtypes = [LPSTR, LPSTR]613    _PathUnExpandEnvStringsA.restype  = bool614    _PathUnExpandEnvStringsA.errcheck = RaiseIfZero615    cchBuf = MAX_PATH616    pszBuf = ctypes.create_string_buffer("", cchBuf)617    _PathUnExpandEnvStringsA(pszPath, pszBuf, cchBuf)618    return pszBuf.value619def PathUnExpandEnvStringsW(pszPath):620    _PathUnExpandEnvStringsW = windll.shlwapi.PathUnExpandEnvStringsW621    _PathUnExpandEnvStringsW.argtypes = [LPWSTR, LPWSTR]622    _PathUnExpandEnvStringsW.restype  = bool623    _PathUnExpandEnvStringsW.errcheck = RaiseIfZero624    cchBuf = MAX_PATH625    pszBuf = ctypes.create_unicode_buffer(u"", cchBuf)626    _PathUnExpandEnvStringsW(pszPath, pszBuf, cchBuf)627    return pszBuf.value628PathUnExpandEnvStrings = GuessStringType(PathUnExpandEnvStringsA, PathUnExpandEnvStringsW)629#==============================================================================630# This calculates the list of exported symbols.631_all = set(vars().keys()).difference(_all)632__all__ = [_x for _x in _all if not _x.startswith('_')]633__all__.sort()...wrapper_util.py
Source:wrapper_util.py  
...32    sys.stderr.write('Error: Python %d.%d is not supported. Please use '33                     'version %s.\n' % (version_tuple[0], version_tuple[1],34                                        minimum_version_string))35    sys.exit(1)36def get_dir_path(script_file, sibling):37  """Get a path to the directory of the script script_file.38  By default, the canonical path (symlinks resolved) will be returned. In some39  environments the canonical directory is not sufficient because different40  parts of the SDK are referenced by symlinks, including script_file.41  In this case, the non-canonical path to script_file's directory will be42  returned (i.e., the directory where the symlink lives, not the directory43  where it points).44  Args:45    script_file: The script file whose directory is wanted.46    sibling: Relative path to a sibling of script_file. Choose a sibling47    that is potentially symlinked into the parent directory.48  Returns:49    A directory name.50  Raises:51    ValueError: If no proper path could be determined.52  """53  if 'GAE_SDK_ROOT' in os.environ:54    gae_sdk_root = os.path.abspath(os.environ['GAE_SDK_ROOT'])55    os.environ['GAE_SDK_ROOT'] = gae_sdk_root56    for dir_path in [gae_sdk_root,57                     os.path.join(gae_sdk_root, 'google_appengine')]:58      if os.path.exists(os.path.join(dir_path, sibling)):59        return dir_path60    raise ValueError('GAE_SDK_ROOT %r does not refer to a valid SDK '61                     'directory' % gae_sdk_root)62  else:63    py_file = script_file.replace('.pyc', '.py')64    dir_paths = [os.path.abspath(os.path.dirname(os.path.realpath(py_file))),65                 os.path.abspath(os.path.dirname(py_file))]66    for dir_path in dir_paths:67      sibling_path = os.path.join(dir_path, sibling)68      if os.path.exists(sibling_path):69        return dir_path70    raise ValueError('Could not determine SDK root; please set GAE_SDK_ROOT '71                     'environment variable.')72class Paths(object):73  """Encapsulates the path and unwrapped script details for a wrapper script.74  Most of the attributes of this object are there so that wrapper_script_v175  can continue to export the same global variables it historically has, in case76  any end-users are referencing those.77  Attributes:78    default_script_dir: the path where the corresponding unwrapped script will79      be found, apart from a few exceptional scripts.80  """81  def __init__(self, dir_path):82    """Make a new Paths object.83    Args:84      dir_path: the directory path where the calling script is to be found.85        This directory should have a lib subdirectory.86    """87    self.v1_extra_paths = [88        dir_path,89        os.path.join(dir_path, 'lib', 'antlr3'),90        os.path.join(dir_path, 'lib', 'django-0.96'),91        os.path.join(dir_path, 'lib', 'fancy_urllib'),92        os.path.join(dir_path, 'lib', 'ipaddr'),93        os.path.join(dir_path, 'lib', 'jinja2-2.6'),94        os.path.join(dir_path, 'lib', 'protorpc-1.0'),95        os.path.join(dir_path, 'lib', 'PyAMF'),96        os.path.join(dir_path, 'lib', 'markupsafe'),97        os.path.join(dir_path, 'lib', 'webob_0_9'),98        os.path.join(dir_path, 'lib', 'webapp2-2.5.2'),99        os.path.join(dir_path, 'lib', 'yaml', 'lib'),100        os.path.join(dir_path, 'lib', 'simplejson'),101        os.path.join(dir_path, 'lib', 'rsa'),102        os.path.join(dir_path, 'lib', 'pyasn1'),103        os.path.join(dir_path, 'lib', 'pyasn1_modules'),104    ]105    self.api_server_extra_paths = [106        os.path.join(dir_path, 'lib', 'argparse'),107    ]108    self.endpointscfg_extra_paths = [109        os.path.join(dir_path, 'lib', 'cherrypy'),110        os.path.join(dir_path, 'lib', 'concurrent'),111        os.path.join(dir_path, 'lib', 'endpoints-1.0'),112    ]113    self.oauth_client_extra_paths = [114        os.path.join(dir_path, 'lib', 'google-api-python-client'),115        os.path.join(dir_path, 'lib', 'httplib2'),116        os.path.join(dir_path, 'lib', 'python-gflags'),117    ]118    self.google_sql_extra_paths = self.oauth_client_extra_paths + [119        os.path.join(dir_path, 'lib', 'enum'),120        os.path.join(dir_path, 'lib', 'grizzled'),121        os.path.join(dir_path, 'lib', 'oauth2'),122        os.path.join(dir_path, 'lib', 'prettytable'),123        os.path.join(dir_path, 'lib', 'sqlcmd'),124    ]125    devappserver2_dir = os.path.join(126        dir_path, 'google', 'appengine', 'tools', 'devappserver2')127    php_runtime_dir = os.path.join(devappserver2_dir, 'php')128    python_runtime_dir = os.path.join(devappserver2_dir, 'python')129    stub_paths = [130        os.path.join(dir_path, 'lib', 'antlr3'),131        os.path.join(dir_path, 'lib', 'fancy_urllib'),132        os.path.join(dir_path, 'lib', 'ipaddr'),133        os.path.join(dir_path, 'lib', 'yaml-3.10'),134        os.path.join(dir_path, 'lib', 'rsa'),135        os.path.join(dir_path, 'lib', 'pyasn1'),136        os.path.join(dir_path, 'lib', 'pyasn1_modules'),137    ]138    self.v2_extra_paths = stub_paths + [139        dir_path,140        os.path.join(dir_path, 'lib', 'simplejson'),141        os.path.join(dir_path, 'lib', 'django-1.4'),142        os.path.join(dir_path, 'lib', 'endpoints-1.0'),143        os.path.join(dir_path, 'lib', 'jinja2-2.6'),144        os.path.join(dir_path, 'lib', 'protorpc-1.0'),145        os.path.join(dir_path, 'lib', 'PyAMF-0.6.1'),146        os.path.join(dir_path, 'lib', 'markupsafe-0.15'),147        os.path.join(dir_path, 'lib', 'webob-1.2.3'),148        os.path.join(dir_path, 'lib', 'webapp2-2.5.2'),149    ]150    devappserver2_paths = stub_paths + [151        dir_path,152        os.path.join(dir_path, 'lib', 'concurrent'),153        os.path.join(dir_path, 'lib', 'cherrypy'),154        os.path.join(dir_path, 'lib', 'requests'),155        os.path.join(dir_path, 'lib', 'six'),156        os.path.join(dir_path, 'lib', 'websocket'),157        os.path.join(dir_path, 'lib', 'docker'),158        os.path.join(dir_path, 'lib', 'jinja2-2.6'),159        os.path.join(dir_path, 'lib', 'webob-1.2.3'),160        os.path.join(dir_path, 'lib', 'webapp2-2.5.1'),161    ]162    php_runtime_paths = [163        dir_path,164        os.path.join(dir_path, 'lib', 'concurrent'),165        os.path.join(dir_path, 'lib', 'cherrypy'),166        os.path.join(dir_path, 'lib', 'yaml-3.10'),167    ]168    python_runtime_paths = [169        dir_path,170        os.path.join(dir_path, 'lib', 'concurrent'),171        os.path.join(dir_path, 'lib', 'cherrypy'),172        os.path.join(dir_path, 'lib', 'fancy_urllib'),173        os.path.join(dir_path, 'lib', 'protorpc-1.0'),174        os.path.join(dir_path, 'lib', 'yaml-3.10'),175    ]176    self._script_to_paths = {177        'api_server.py': self.v1_extra_paths + self.api_server_extra_paths,178        'appcfg.py': self.v1_extra_paths + self.oauth_client_extra_paths,179        'backends_conversion.py': self.v1_extra_paths,180        'bulkload_client.py': self.v1_extra_paths,181        'bulkloader.py': self.v1_extra_paths + self.oauth_client_extra_paths,182        'dev_appserver.py': devappserver2_paths,183        'download_appstats.py': self.v1_extra_paths,184        'endpointscfg.py': self.v1_extra_paths + self.endpointscfg_extra_paths,185        'gen_protorpc.py': self.v1_extra_paths,186        'google_sql.py': self.v1_extra_paths + self.google_sql_extra_paths,187        'old_dev_appserver.py': self.v1_extra_paths,188        'php_cli.py': devappserver2_paths,189        'remote_api_shell.py': self.v1_extra_paths,190        'vmboot.py': self.v1_extra_paths,191        '_php_runtime.py': php_runtime_paths,192        '_python_runtime.py': python_runtime_paths,193    }194    self._wrapper_name_to_real_name = {195        'old_dev_appserver.py': 'dev_appserver_main.py',196        'dev_appserver.py': 'devappserver2.py',197        '_php_runtime.py': 'runtime.py',198        '_python_runtime.py': 'runtime.py',199    }200    self.default_script_dir = os.path.join(201        dir_path, 'google', 'appengine', 'tools')202    self.google_sql_dir = os.path.join(203        dir_path, 'google', 'storage', 'speckle', 'python', 'tool')204    self._script_to_dir = {205        'google_sql.py': self.google_sql_dir,206        'dev_appserver.py': devappserver2_dir,207        '_php_runtime.py': php_runtime_dir,208        '_python_runtime.py': python_runtime_dir,209    }210    self._sys_paths_to_scrub = {211        'dev_appserver.py':212            [os.path.normcase(os.path.join(dir_path, 'launcher'))],213    }214  def script_paths(self, script_name):215    """Returns the sys.path prefix appropriate for this script.216    Args:217      script_name: the basename of the script, for example 'appcfg.py'.218    """219    try:220      return self._script_to_paths[script_name]221    except KeyError:222      raise KeyError('Script name %s not recognized' % script_name)223  def script_file(self, script_name):224    """Returns the absolute name of the wrapped script.225    Args:226      script_name: the basename of the script, for example 'appcfg.py'.227    """228    script_dir = self._script_to_dir.get(script_name, self.default_script_dir)229    script_name = self._wrapper_name_to_real_name.get(script_name, script_name)230    return os.path.join(script_dir, script_name)231  def scrub_path(self, script_name, paths):232    """Removes bad paths from a list of paths.233    Args:234      script_name: the basename of the script, for example 'appcfg.py'.235      paths: a list of paths236    Returns:237      The list of paths with any bad paths removed.238    """239    sys_paths_to_scrub = self._sys_paths_to_scrub.get(script_name, [])240    return [path for path in paths...test_util_filepath.py
Source:test_util_filepath.py  
1import unittest2import vdebug.opts3from vdebug.util import FilePath4from vdebug.error import FilePathError5class LocalFilePathTest(unittest.TestCase):6    def setUp(self):7        vdebug.opts.Options.set({'path_maps':{}})8    def test_as_local(self):9        filename = "/home/user/some/path"10        file = FilePath(filename)11        self.assertEqual(filename,file.as_local())12    def test_remote_prefix(self):13        prefix = "file://"14        filename = "/home/user/some/path"15        file = FilePath(prefix+filename)16        self.assertEqual(filename,file.as_local())17    def test_quoted(self):18        quoted = "file:///home/user/file%2etcl"19        file = FilePath(quoted)20        self.assertEqual("/home/user/file.tcl",file.as_local())21    def test_win(self):22        quoted = "file:///C:/home/user/file%2etcl"23        file = FilePath(quoted)24        self.assertEqual("C:\\home\\user\\file.tcl",file.as_local())25    def test_as_remote(self):26        filename = "/home/user/some/path"27        file = FilePath(filename)28        self.assertEqual("file://"+filename,file.as_remote())29    def test_eq(self):30        filename = "/home/user/some/path"31        file1 = FilePath(filename)32        file2 = FilePath(filename)33        assert file1 == file234    def test_eq_false(self):35        filename1 = "/home/user/some/path"36        file1 = FilePath(filename1)37        filename2 = "/home/user/some/other/path"38        file2 = FilePath(filename2)39        self.assertFalse(file1 == file2)40    def test_neq(self):41        filename1 = "/home/user/some/path"42        file1 = FilePath(filename1)43        filename2 = "/home/user/some/other/path"44        file2 = FilePath(filename2)45        assert file1 != file246    def test_neq_false(self):47        filename = "/home/user/some/path"48        file1 = FilePath(filename)49        file2 = FilePath(filename)50        self.assertFalse(file1 != file2)51    def test_add(self):52        filename = "/home/user/some/path"53        file = FilePath(filename)54        append = "/myfile.txt"55        assert (file + append) == (filename + append)56    def test_add_reverse(self):57        filename = "/user/some/path"58        file = FilePath(filename)59        prepend = "/home/"60        assert (prepend + file) == (prepend + filename)61    def test_empty_file_raises_error(self):62        self.assertRaises(FilePathError,FilePath,"")63class RemotePathTest(unittest.TestCase):64    def setUp(self):65        vdebug.opts.Options.set({'path_maps':{'remote1':'local1', 'remote2':'local2'}})66    def test_as_local(self):67        filename = "/remote1/path/to/file"68        file = FilePath(filename)69        self.assertEqual("/local1/path/to/file",file.as_local())70        filename = "/remote2/path/to/file"71        file = FilePath(filename)72        self.assertEqual("/local2/path/to/file",file.as_local())73    def test_as_local_with_uri(self):74        filename = "file:///remote1/path/to/file"75        file = FilePath(filename)76        self.assertEqual("/local1/path/to/file",file.as_local())77        filename = "file:///remote2/path/to/file"78        file = FilePath(filename)79        self.assertEqual("/local2/path/to/file",file.as_local())80    def test_as_local_does_nothing(self):81        filename = "/the/remote/path/to/file"82        file = FilePath(filename)83        self.assertEqual("/the/remote/path/to/file",file.as_local())84    def test_as_remote_with_unix_paths(self):85        filename = "/local1/path/to/file"86        file = FilePath(filename)87        self.assertEqual("file:///remote1/path/to/file",file.as_remote())88        filename = "file:///local2/path/to/file"89        file = FilePath(filename)90        self.assertEqual("file:///remote2/path/to/file",file.as_remote())91    def test_as_remote_with_win_paths(self):92        filename = "C:/local1/path/to/file"93        file = FilePath(filename)94        self.assertEqual("file:///C:/remote1/path/to/file",file.as_remote())95        filename = "file:///C:/local2/path/to/file"96        file = FilePath(filename)97        self.assertEqual("file:///C:/remote2/path/to/file",file.as_remote())98    def test_as_remote_with_backslashed_win_paths(self):99        filename = "C:\\local1\\path\\to\\file"100        file = FilePath(filename)101        self.assertEqual("file:///C:/remote1/path/to/file",file.as_remote())102        filename = "C:\\local2\\path\\to\\file"103        file = FilePath(filename)104        self.assertEqual("file:///C:/remote2/path/to/file",file.as_remote())105        filename = "C:/local2/path/to/file"106        file = FilePath(filename)107        self.assertEqual("C:\\local2\\path\\to\\file",file.as_local())108class RemoteWinLocalUnixPathTest(unittest.TestCase):109    def setUp(self):110        vdebug.opts.Options.set({'path_maps':{'G:\\remote\\path':'/local/path', 'G:\\remote2\\path':'/local2/path'}})111    def test_as_local(self):112        filename = "G:\\remote\\path\\to\\file"113        file = FilePath(filename)114        self.assertEqual("/local/path/to/file",file.as_local())115        filename = "file:///G:/remote2/path/to/file"116        file = FilePath(filename)117        self.assertEqual("/local2/path/to/file",file.as_local())118    def test_as_local_does_nothing(self):119        filename = "/the/path/to/file"120        file = FilePath(filename)121        self.assertEqual("/the/path/to/file",file.as_local())122    def test_as_remote(self):123        filename = "/local/path/to/file"124        file = FilePath(filename)125        self.assertEqual("file:///G:/remote/path/to/file",file.as_remote())126        filename = "file:///local2/path/to/file"127        file = FilePath(filename)128        self.assertEqual("file:///G:/remote2/path/to/file",file.as_remote())129class RemoteUnixLocalWinPathTest(unittest.TestCase):130    def setUp(self):131        vdebug.opts.Options.set({'path_maps':{'/remote/path':'G:\\local\\path', '/remote2/path':'G:\\local2\\path'}})132    def test_as_local(self):133        filename = "/remote/path/to/file"134        file = FilePath(filename)135        self.assertEqual("G:\\local\\path\\to\\file",file.as_local())136        filename = "file:///remote2/path/to/file"137        file = FilePath(filename)138        self.assertEqual("G:\\local2\\path\\to\\file",file.as_local())139    def test_as_local_does_nothing(self):140        filename = "G:\\the\\path\\to\\file"141        file = FilePath(filename)142        self.assertEqual("G:\\the\\path\\to\\file",file.as_local())143    def test_as_remote(self):144        filename = "G:\\local\\path\\to\\file"145        file = FilePath(filename)146        self.assertEqual("file:///remote/path/to/file",file.as_remote())147        filename = "file:///G:/local2/path/to/file"148        file = FilePath(filename)149        self.assertEqual("file:///remote2/path/to/file",file.as_remote())150class MismatchingSeparatorsTest(unittest.TestCase):151    def setUp(self):152        vdebug.opts.Options.set({'path_maps':{'remote1/':'local1', 'remote2':'local2/'}})153    def test_as_local(self):154        filename = "/remote1/path/to/file"155        file = FilePath(filename)156        self.assertEqual("/local1/path/to/file",file.as_local())157        filename = "/remote2/path/to/file"158        file = FilePath(filename)159        self.assertEqual("/local2/path/to/file",file.as_local())160    def test_as_remote(self):161        filename = "/local1/path/to/file"162        file = FilePath(filename)163        self.assertEqual("file:///remote1/path/to/file",file.as_remote())164        filename = "/local2/path/to/file"165        file = FilePath(filename)...tracing_project.py
Source:tracing_project.py  
...23    for dirpath, _, filenames in os.walk(source_path):24      for f in filenames:25        if f.startswith('.'):26          continue27        x = os.path.abspath(os.path.join(dirpath, f))28        all_filenames.add(x)29  return all_filenames30def _IsFilenameATest(x):31  if x.endswith('_test.js'):32    return True33  if x.endswith('_test.html'):34    return True35  if x.endswith('_unittest.js'):36    return True37  if x.endswith('_unittest.html'):38    return True39  # TODO(nduca): Add content test?40  return False41class TracingProject(object):42  catapult_path = os.path.abspath(43      os.path.join(os.path.dirname(__file__), os.path.pardir))44  tracing_root_path = os.path.join(catapult_path, 'tracing')45  trace_processor_root_path = os.path.join(catapult_path, 'trace_processor')46  tracing_src_path = os.path.join(tracing_root_path, 'tracing')47  extras_path = os.path.join(tracing_src_path, 'extras')48  ui_extras_path = os.path.join(tracing_src_path, 'ui', 'extras')49  catapult_third_party_path = os.path.join(catapult_path, 'third_party')50  polymer_path = os.path.join(catapult_third_party_path, 'polymer')51  tracing_third_party_path = os.path.join(tracing_root_path, 'third_party')52  py_vulcanize_path = os.path.join(catapult_third_party_path, 'py_vulcanize')53  vinn_path = os.path.join(catapult_third_party_path, 'vinn')54  jszip_path = os.path.join(tracing_third_party_path, 'jszip')55  glmatrix_path = os.path.join(56      tracing_third_party_path, 'gl-matrix', 'dist')57  mannwhitneyu_path = os.path.join(58      tracing_third_party_path, 'mannwhitneyu')59  ui_path = os.path.join(tracing_src_path, 'ui')60  d3_path = os.path.join(tracing_third_party_path, 'd3')61  chai_path = os.path.join(tracing_third_party_path, 'chai')62  mocha_path = os.path.join(tracing_third_party_path, 'mocha')63  oboe_path = os.path.join(tracing_third_party_path, 'oboe')64  mre_path = os.path.join(tracing_src_path, 'mre')65  metrics_path = os.path.join(tracing_src_path, 'metrics')66  diagnostics_path = os.path.join(tracing_src_path, 'value', 'diagnostics')67  value_ui_path = os.path.join(tracing_src_path, 'value', 'ui')68  metrics_ui_path = os.path.join(tracing_src_path, 'metrics', 'ui')69  test_data_path = os.path.join(tracing_root_path, 'test_data')70  skp_data_path = os.path.join(tracing_root_path, 'skp_data')71  rjsmin_path = os.path.join(72      tracing_third_party_path, 'tvcm', 'third_party', 'rjsmin')73  rcssmin_path = os.path.join(74      tracing_third_party_path, 'tvcm', 'third_party', 'rcssmin')75  def __init__(self):76    self.source_paths = []77    self.source_paths.append(self.tracing_root_path)78    self.source_paths.append(self.polymer_path)79    self.source_paths.append(self.tracing_third_party_path)80    self.source_paths.append(self.mre_path)81    self.source_paths.append(self.jszip_path)82    self.source_paths.append(self.glmatrix_path)83    self.source_paths.append(self.mannwhitneyu_path)84    self.source_paths.append(self.d3_path)85    self.source_paths.append(self.chai_path)86    self.source_paths.append(self.mocha_path)87    self.source_paths.append(self.oboe_path)88  def CreateVulcanizer(self):89    from py_vulcanize import project as project_module90    return project_module.Project(self.source_paths)91  def IsD8CompatibleFile(self, filename):92    if filename.startswith(self.ui_path):93      return False94    if filename.startswith(self.value_ui_path):95      return False96    if filename.startswith(self.metrics_ui_path):97      return False98    return True99  def FindAllTestModuleRelPaths(self, pred=None):100    if pred is None:101      pred = lambda x: True102    all_filenames = _FindAllFilesRecursive([self.tracing_src_path])103    test_module_filenames = [x for x in all_filenames if104                             _IsFilenameATest(x) and pred(x)]105    test_module_filenames.sort()106    return [os.path.relpath(x, self.tracing_root_path)107            for x in test_module_filenames]108  def FindAllMetricsModuleRelPaths(self):109    all_filenames = _FindAllFilesRecursive([self.tracing_src_path])110    all_metrics_module_filenames = []111    for x in all_filenames:112      if x.startswith(self.metrics_path) and not _IsFilenameATest(x):113        all_metrics_module_filenames.append(x)114    all_metrics_module_filenames.sort()115    return [os.path.relpath(x, self.tracing_root_path)116            for x in all_metrics_module_filenames]117  def FindAllDiagnosticsModuleRelPaths(self):118    all_filenames = _FindAllFilesRecursive([self.tracing_src_path])119    all_diagnostics_module_filenames = []120    for x in all_filenames:121      if x.startswith(self.diagnostics_path) and not _IsFilenameATest(x):122        all_diagnostics_module_filenames.append(x)123    all_diagnostics_module_filenames.sort()124    return [os.path.relpath(x, self.tracing_root_path)125            for x in all_diagnostics_module_filenames]126  def FindAllD8TestModuleRelPaths(self):127    return self.FindAllTestModuleRelPaths(pred=self.IsD8CompatibleFile)128  def GetConfigNames(self):129    config_files = [130        os.path.join(self.ui_extras_path, x)131        for x in os.listdir(self.ui_extras_path)132        if x.endswith('_config.html')133    ]134    config_files = [x for x in config_files if os.path.isfile(x)]135    config_basenames = [os.path.basename(x) for x in config_files]136    config_names = [re.match('(.+)_config.html$', x).group(1)137                    for x in config_basenames]138    return config_names...LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
