Package osh :: Package command :: Module copyto
[frames] | no frames]

Source Code for Module osh.command.copyto

  1  # osh 
  2  # Copyright (C) 2005 Jack Orenstein <jao@geophile.com> 
  3  # 
  4  # This program is free software; you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published by 
  6  # the Free Software Foundation; either version 2 of the License, or 
  7  # (at your option) any later version. 
  8  # 
  9  # This program is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with this program; if not, write to the Free Software 
 16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 17   
 18  """C{copyto [-rpC] -c CLUSTER FILE ... REMOTE_DIR} 
 19   
 20  Copies C{FILE}s to C{REMOTE_DIR} on each node of the specified 
 21  C{CLUSTER}. If C{CLUSTER} is omitted, then the default cluster is 
 22  used. 
 23   
 24  C{REMOTE_DIR} must be an absolute path, (i.e. must begin with C{'/'}). 
 25   
 26  C{copyto} is implemented using C{scp}, and the following C{scp} flags 
 27  are supported: 
 28      - C{-C}: enable compression. 
 29      - C{-r}: recursive copy 
 30      - C{-p}: preserve modification times, access times, and modes. 
 31  """ 
 32   
 33  import pickle 
 34  import threading 
 35   
 36  import osh.core 
 37  import osh.spawn 
 38   
 39  Spawn = osh.spawn.Spawn 
 40   
 41  # CLI 
42 -def _copyto():
43 return _CopyTo()
44 45 # API
46 -def copyto(cluster, files, 47 compress = False, recursive = False, preserve = False):
48 """Copies files to each node of the specified C{cluster}. The last elements 49 of C{files} is the target directory on each node. The preceding elements are 50 the local files to be copied. Compression is used for copying if C{compress} 51 is True. Directories are copied recursively if C{recursive} is 52 True. File attributes are preserved if C{preserve} is True. 53 """ 54 if len(args) > 0: 55 cluster = args[0] 56 args = (osh.args.Option('-c', cluster),) + args[1:] 57 return _CopyTo().process_args(*args)
58
59 -class _CopyTo(osh.core.RemoteOp):
60 61 # state 62 63 _remote_dir = None 64 _files = None 65 _scp_options = None 66 67 68 # object interface 69
70 - def __init__(self):
71 osh.core.RemoteOp.__init__(self, 'rpCc:', (2, None))
72 73 74 # BaseOp interface 75
76 - def doc(self):
77 return __doc__
78
79 - def setup(self):
80 osh.core.RemoteOp.setup(self) 81 args = self._args.remaining() 82 if len(args) < 2: 83 self.usage() 84 self._remote_dir = args[-1] 85 if not self._remote_dir.startswith('/'): 86 self.usage() 87 self._files = args[:-1] 88 for option in osh.core.SCP_OPTIONS: 89 self._check_option(option)
90 91 92 # RemoteOp interface 93
94 - def action(self, host):
95 _copyup(self._files, 96 self.user(), 97 host.address, 98 self._remote_dir, 99 self.scp_options())
100
101 -def _copyup(files, user, host, remote_dir, options = ''):
102 if isinstance(files, list) or isinstance(files, tuple): 103 files = ' '.join(files) 104 scp_command = 'scp %s %s %s@%s:%s' % (options, files, user, host, remote_dir) 105 Spawn(scp_command, None, None, None).run()
106