Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/env python
  2. #win32wnetfile.py
  3.  
  4. import os
  5. import os.path
  6. import shutil
  7. import sys
  8. import win32wnet
  9.  
  10. def netcopy(host, source, dest_dir, username=None, password=None, move=False):
  11.     """ Copies files or directories to a remote computer. """
  12.  
  13.     wnet_connect(host, username, password)
  14.  
  15.     dest_dir = os.path.dirname(covert_unc(host, dest_dir))
  16.  
  17.     if os.path.isfile(source):
  18.         if not os.path.exists(dest_dir) and not dest_dir[-1]=='$' and not dest_dir[-1]==':':
  19.             os.makedirs(dest_dir)
  20.  
  21.     # Pad a backslash to the destination directory if not provided.
  22.     if not dest_dir[len(dest_dir) - 1] == '\\':
  23.         dest_dir = ''.join([dest_dir, '\\'])
  24.  
  25.     if move:
  26.         if os.path.isdir(source):
  27.             shutil.copytree(source, destdir)
  28.             shutil.rmtree(source)
  29.         elif os.path.isfile(source):
  30.             shutil.move(source, dest_dir)
  31.  
  32.         else:
  33.             raise AssertionError, '%s is neither a file nor directory' % (source)
  34.  
  35.     else:
  36.         if os.path.isdir(source):
  37.             shutil.copytree(source, dest_dir)
  38.         elif os.path.isfile(source):
  39.             shutil.copy(source, dest_dir)
  40.         else:
  41.             raise AssertionError, '%s is neither a file nor directory' % (source)
  42.  
  43. def netCheck(host, path, username=None, password=None):
  44.     wnet_connect(host, username, password)
  45.     dest_dir = covert_unc(host, path)
  46.     if os.path.exists(dest_dir):
  47.         print 'existe'
  48.         print os.listdir(dest_dir)
  49.  
  50. def netdelete(host, path, username=None, password=None):
  51.     """ Deletes files or directories on a remote computer. """
  52.  
  53.     wnet_connect(host, username, password)
  54.  
  55.     path = covert_unc(host, path)
  56.     if os.path.exists(path):
  57.         # Delete directory tree if object is a directory.        
  58.         if os.path.isfile(path):
  59.             os.remove(path)
  60.         else:
  61.             shutil.rmtree(path)
  62.     else:
  63.         # Remove anyway if non-existent so as to raise an error.        
  64.         os.remove(path)
  65.  
  66. def netmove(host, source, dest_dir, username=None, password=None):
  67.     return netcopy(host, source, dest_dir, username, password, True)
  68.  
  69. def covert_unc(host, path):
  70.     """ Convert a file path on a host to a UNC path."""
  71.     return ''.join(['\\\\', host, '\\', path.replace(':', '$')])
  72.  
  73. def wnet_connect(host, username, password):
  74.     unc = ''.join(['\\\\', host])
  75.     try:
  76.         win32wnet.WNetAddConnection2(0, None, unc, None, username, password)
  77.     except Exception, err:
  78.         if isinstance(err, win32wnet.error):
  79.             # Disconnect previous connections if detected, and reconnect.
  80.             if err[0] == 1219:
  81.                 win32wnet.WNetCancelConnection2(unc, 0, 0)
  82.                 return wnet_connect(host, username, password)
  83.             #if err[0] == 1326:
  84.                 #print 'usuario ou senha invalido'
  85.            
  86.         raise err