Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Last active September 4, 2015 07:15
Show Gist options
  • Save atifaziz/283727 to your computer and use it in GitHub Desktop.
Save atifaziz/283727 to your computer and use it in GitHub Desktop.
rempath.py
# rempath - Resolves UNC paths for paths over network mapped drives
# Copyright (c) 2010 Atif Aziz. All rights reserved.
#
# Author(s):
#
# Atif Aziz, http://www.raboof.com
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# - The names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys, clr
clr.AddReference('System.Management')
from System import Console, ArgumentException
from System.IO import Path
from System.Management import ManagementObjectSearcher
def read_lines(reader = Console.In):
line = reader.ReadLine()
while not line is None:
yield line.Trim()
line = Console.ReadLine()
def try_map_remote_path(path, mappings):
if path is None or len(path) < 3:
return None
root = path[:3].ToUpperInvariant()
drive, letter = root[:2], root[0]
if root[1:] != ':\\' or letter < 'A' or letter > 'Z':
return None
remote_name = mappings.get(drive)
if remote_name is None:
return None
try:
return Path.Combine(remote_name, path[len(root):])
except ArgumentException:
return None
def main():
connections = list(ManagementObjectSearcher('SELECT Name, ProviderName FROM Win32_MappedLogicalDisk').Get())
mappings = dict([(conn['Name'], conn['ProviderName']) for conn in connections])
for line in read_lines():
remote_path = try_map_remote_path(line, mappings)
if remote_path:
print remote_path
else:
print >> sys.stderr, line
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment