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

Source Code for Module osh.command.copyto

  1  # osh 
  2  # Copyright (C) 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] FILE ... REMOTE_DIR} 
 19   
 20  Copies C{FILE}s to C{REMOTE_DIR} on each node of a cluster. 
 21  The cluster is identified using remote execution syntax, for example:: 
 22   
 23      osh @fred [ copyto log.properties /opt/foobar/config ] 
 24   
 25  C{REMOTE_DIR} must be an absolute path, (i.e. must begin with C{'/'}). 
 26   
 27  C{copyto} is implemented using C{scp}, and the following C{scp} flags 
 28  are supported: 
 29      - C{-C}: enable compression. 
 30      - C{-r}: recursive copy 
 31      - C{-p}: preserve modification times, access times, and modes. 
 32  """ 
 33   
 34  import osh.core 
 35  import osh.spawn 
 36   
 37  Spawn = osh.spawn.Spawn 
 38   
 39  # CLI 
40 -def _copyto():
41 return _CopyTo()
42 43 # API
44 -def copyto(files, compress = False, recursive = False, preserve = False):
45 """Copies files to each node of the specified C{cluster}. The last elements 46 of C{files} is the target directory on each node. The preceding elements are 47 the local files to be copied. Compression is used for copying if C{compress} 48 is True. Directories are copied recursively if C{recursive} is 49 True. File attributes are preserved if C{preserve} is True. 50 """ 51 args = [] 52 if compress: 53 args.append(Option('-C')) 54 if recursive: 55 args.append(Option('-r')) 56 if preserve: 57 args.append(Option('-p')) 58 return _CopyTo().process_args(*args)
59
60 -class _CopyTo(osh.core.RunLocal):
61 62 # state 63 64 _remote_dir = None 65 _files = None 66 _scp_options = None 67 68 69 # object interface 70
71 - def __init__(self):
72 osh.core.RunLocal.__init__(self, 'rpC', (2, None))
73 74 75 # BaseOp interface 76
77 - def doc(self):
78 return __doc__
79
80 - def setup(self):
81 args = self.args() 82 scp_options = '' 83 for option in 'Crp': 84 if args.flag('-' + option): 85 scp_options += option 86 if scp_options: 87 self._scp_options = '-' + scp_options 88 else: 89 self._scp_options = '' 90 files = args.remaining() 91 if len(files) < 2: 92 self.usage() 93 self._remote_dir = files[-1] 94 if not self._remote_dir.startswith('/'): 95 self.usage() 96 self._files = files[:-1]
97 98 # Generator interface 99
100 - def execute(self):
101 host = self.thread_state 102 _copyup(self._files, 103 host.user, 104 host.identity, 105 host.address, 106 self._remote_dir, 107 self._scp_options)
108
109 -def _copyup(files, user, identity, host, remote_dir, options):
110 if isinstance(files, list) or isinstance(files, tuple): 111 files = ' '.join(files) 112 if identity: 113 scp_command = 'scp %s -i %s %s %s@%s:%s' % (options, 114 identity, 115 files, 116 user, 117 host, 118 remote_dir) 119 else: 120 scp_command = 'scp %s %s %s@%s:%s' % (options, files, user, host, remote_dir) 121 Spawn(scp_command, None, None, None).run()
122