NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

subst.py (3799B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\osutils\subst.py
      2 from future.utils import raise_
      3 import os
      4 from . import win32api
      5 from osutils import systemcall
      6 
      7 class SubstError(Exception):
      8     pass
      9 
     10 
     11 class SubstInvalidParameterError(SubstError):
     12     pass
     13 
     14 
     15 class SubstPathNotFoundError(SubstError):
     16     pass
     17 
     18 
     19 class SubstIncorrectNumberOfParamsError(SubstError):
     20     pass
     21 
     22 
     23 class SubstDriveAlreadySubstedError(SubstError):
     24     pass
     25 
     26 
     27 def _parse_drive_mapping_to_tuples(substoutput):
     28     lines = substoutput.splitlines()
     29     for li in lines:
     30         words = li.split('=>')
     31         drive = words[0][:2]
     32         path = words[-1].strip()
     33         yield (drive, path.lower())
     34 
     35 
     36 def _parse_result_for_err(out = '', err = ''):
     37     lines = (out or '').splitlines() + (err or '').splitlines()
     38     for li in lines:
     39         if li.startswith('Invalid parameter - '):
     40             raise_(SubstInvalidParameterError, li)
     41         if li.startswith('Path not found - '):
     42             raise_(SubstPathNotFoundError, li)
     43         if li.startswith('Incorrect number of parameters - '):
     44             raise_(SubstIncorrectNumberOfParamsError, li)
     45         if li == 'Drive already SUBSTed':
     46             raise_(SubstDriveAlreadySubstedError, li)
     47 
     48 
     49 def _get_mapped_drives_using_systemcall():
     50     out, err = systemcall('subst')
     51     return tuple(_parse_drive_mapping_to_tuples(out))
     52 
     53 
     54 def _get_mapped_drives_using_win32api():
     55 
     56     def _getDrivePath():
     57         pattern = '\\??\\'
     58         zeroendChar = '\x00'
     59         isexisting = reversed(bin(win32api.GetLogicalDrives())[2:])
     60         for i, exists in enumerate(isexisting):
     61             if exists == '0':
     62                 continue
     63             drive = chr(65 + i) + ':'
     64             dospath = win32api.QueryDosDevice(drive)
     65             if not dospath.startswith(pattern):
     66                 continue
     67             path = dospath.split(pattern)[-1].rstrip(zeroendChar)
     68             yield (drive, path)
     69 
     70     return tuple(_getDrivePath())
     71 
     72 
     73 def get_mapped_drives():
     74     if win32api:
     75         return _get_mapped_drives_using_win32api()
     76     return _get_mapped_drives_using_systemcall()
     77 
     78 
     79 GetMappedDrives = get_mapped_drives
     80 
     81 def _set_drive_mapping_using_win32api(drive, path):
     82     win32api.DefineDosDevice(0, drive, path)
     83 
     84 
     85 def _set_drive_mapping_using_systemcall(drive, path):
     86     out, err = systemcall('subst', drive, path)
     87     _parse_result_for_err(out, err)
     88 
     89 
     90 def set_drive_mapping(drive, path):
     91     if win32api:
     92         _set_drive_mapping_using_win32api(drive, path)
     93     else:
     94         _set_drive_mapping_using_systemcall(drive, path)
     95 
     96 
     97 SetDriveMapping = set_drive_mapping
     98 
     99 def delete_drive_mapping(drive):
    100     out, err = systemcall('subst', '/D', drive)
    101     _parse_result_for_err(out, err)
    102 
    103 
    104 DeleteDriveMapping = delete_drive_mapping
    105 
    106 def get_subst_path(path):
    107     for drive, substpath in get_mapped_drives():
    108         if path.lower().startswith(drive.lower()):
    109             return path
    110         if path.lower().startswith(substpath.lower()):
    111             return path.lower().replace(substpath.lower(), drive)
    112 
    113     raise SubstPathNotFoundError(path)
    114 
    115 
    116 GetSubstedPath = get_subst_path
    117 
    118 def get_unsubst_path(path):
    119 
    120     def testArg(p):
    121         if p is None:
    122             raise ValueError('path cannot be None')
    123 
    124     testArg(path)
    125     driveToPath = dict(((k.lower(), v) for k, v in get_mapped_drives()))
    126 
    127     def getUnsubst(p):
    128         testArg(p)
    129         p = os.path.abspath(p)
    130         head, tail = os.path.splitdrive(p)
    131         if tail.startswith(os.path.sep):
    132             tail = tail[1:]
    133         real = driveToPath.get(head.lower())
    134         if real is None:
    135             return p
    136         return os.path.join(real, tail)
    137 
    138     if isinstance(path, basestring):
    139         return getUnsubst(path)
    140     return map(getUnsubst, path)
    141 
    142 
    143 GetUnsubstedPath = get_unsubst_path