1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
38 from image_creator.util import FatalError
42 """Wrapper class for the rsync command"""
44 def __init__(self, output):
45 """Create an instance """
48 self._options = ['-v']
51 """Enable the archive option"""
52 self._options.append('-a')
56 """Preserve extended attributes"""
57 self._options.append('-X')
61 """Preserve hard links"""
62 self._options.append('-H')
67 self._options.append('-A')
71 """Handle sparse files efficiently"""
72 self._options.append('-S')
75 def exclude(self, pattern):
76 """Add an exclude pattern"""
77 self._exclude.append(pattern)
81 """Reset all rsync options"""
83 self._options = ['-v']
85 def run(self, src, dest, slabel='source', dlabel='destination'):
86 """Run the actual command"""
89 cmd.extend(self._options)
90 for i in self._exclude:
91 cmd.extend(['--exclude', i])
93 self._out.output("Calculating total number of %s files ..." % slabel,
96 # If you don't specify a destination, rsync will list the source files.
97 dry_run = subprocess.Popen(cmd + [src], shell=False,
98 stdout=subprocess.PIPE, bufsize=0)
101 for line in iter(dry_run.stdout.readline, b''):
104 dry_run.communicate()
105 if dry_run.returncode != 0:
106 raise FatalError("rsync failed")
108 self._out.success("%d" % total)
110 progress = self._out.Progress(total, "Copying files to %s" % dlabel)
111 run = subprocess.Popen(cmd + [src, dest], shell=False,
112 stdout=subprocess.PIPE, bufsize=0)
116 for line in iter(run.stdout.readline, b''):
118 current = time.time()
119 if current - t > 0.1:
123 progress.success('done')
126 def handler(signum, frame):
130 if run.returncode is None:
134 signal.signal(signal.SIGALRM, handler)
138 if run.returncode != 0:
139 raise FatalError("rsync failed")
141 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :