Fix minor typos
[snf-image-creator] / image_creator / rsync.py
1 # Copyright 2012 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
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.
15 #
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.
28 #
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.
33
34 import subprocess
35 import time
36 import signal
37
38 from image_creator.util import FatalError
39
40
41 class Rsync:
42     """Wrapper class for the rsync command"""
43
44     def __init__(self, output):
45         """Create an instance """
46         self._out = output
47         self._exclude = []
48         self._options = ['-v']
49
50     def archive(self):
51         """Enable the archive option"""
52         self._options.append('-a')
53         return self
54
55     def xattrs(self):
56         """Preserve extended attributes"""
57         self._options.append('-X')
58         return self
59
60     def hard_links(self):
61         """Preserve hard links"""
62         self._options.append('-H')
63         return self
64
65     def acls(self):
66         """Preserve ACLs"""
67         self._options.append('-A')
68         return self
69
70     def sparse(self):
71         """Handle sparse files efficiently"""
72         self._options.append('-S')
73         return self
74
75     def exclude(self, pattern):
76         """Add an exclude pattern"""
77         self._exclude.append(pattern)
78         return self
79
80     def reset(self):
81         """Reset all rsync options"""
82         self._exclude = []
83         self._options = ['-v']
84
85     def run(self, src, dest, slabel='source', dlabel='destination'):
86         """Run the actual command"""
87         cmd = []
88         cmd.append('rsync')
89         cmd.extend(self._options)
90         for i in self._exclude:
91             cmd.extend(['--exclude', i])
92
93         self._out.output("Calculating total number of %s files ..." % slabel,
94                          False)
95
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)
99         try:
100             total = 0
101             for line in iter(dry_run.stdout.readline, b''):
102                 total += 1
103         finally:
104             dry_run.communicate()
105             if dry_run.returncode != 0:
106                 raise FatalError("rsync failed")
107
108         self._out.success("%d" % total)
109
110         progress = self._out.Progress(total, "Copying %s files to %s" %
111                                       (slabel, dlabel))
112         run = subprocess.Popen(cmd + [src, dest], shell=False,
113                                stdout=subprocess.PIPE, bufsize=0)
114         try:
115             t = time.time()
116             i = 0
117             for line in iter(run.stdout.readline, b''):
118                 i += 1
119                 current = time.time()
120                 if current - t > 0.1:
121                     t = current
122                     progress.goto(i)
123
124             progress.success('done')
125
126         finally:
127             run.poll()
128             if run.returncode is None:
129                 run.send_signal(signal.SIGHUP)
130             run.communicate()
131             if run.returncode != 0:
132                 raise FatalError("rsync failed")
133
134
135 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :