Line 0
Link Here
|
|
|
1 |
=== modified file 'duplicity/backend.py' |
2 |
--- src/backend.py 2010-05-23 15:52:45 +0000 |
3 |
+++ src/backend.py 2010-08-09 18:56:03 +0000 |
4 |
@@ -372,78 +372,76 @@ |
5 |
else: |
6 |
return commandline |
7 |
|
8 |
- """ |
9 |
- DEPRECATED: |
10 |
- run_command(_persist) - legacy wrappers for subprocess_popen(_persist) |
11 |
- """ |
12 |
def run_command(self, commandline): |
13 |
- return self.subprocess_popen(commandline) |
14 |
+ """ |
15 |
+ Execute the given command line, interpreted as a shell |
16 |
+ command, with logging and error detection. If execution fails, |
17 |
+ raise a BackendException. |
18 |
+ """ |
19 |
+ private = self.munge_password(commandline) |
20 |
+ log.Info(_("Running '%s'") % private) |
21 |
+ if os.system(commandline): |
22 |
+ raise BackendException("Error running '%s'" % private) |
23 |
+ |
24 |
def run_command_persist(self, commandline): |
25 |
- return self.subprocess_popen_persist(commandline) |
26 |
+ """ |
27 |
+ Like run_command(), but repeat the attempt several times (with |
28 |
+ a delay in between) if it fails. |
29 |
+ """ |
30 |
+ private = self.munge_password(commandline) |
31 |
+ for n in range(1, globals.num_retries+1): |
32 |
+ if n > 1: |
33 |
+ # sleep before retry |
34 |
+ time.sleep(30) |
35 |
+ log.Info(gettext.ngettext("Running '%s' (attempt #%d)", |
36 |
+ "Running '%s' (attempt #%d)", n) % |
37 |
+ (private, n)) |
38 |
+ if not os.system(commandline): |
39 |
+ return |
40 |
+ log.Warn(gettext.ngettext("Running '%s' failed (attempt #%d)", |
41 |
+ "Running '%s' failed (attempt #%d)", n) % |
42 |
+ (private, n), 1) |
43 |
+ log.Warn(gettext.ngettext("Giving up trying to execute '%s' after %d attempt", |
44 |
+ "Giving up trying to execute '%s' after %d attempts", |
45 |
+ globals.num_retries) % (private, globals.num_retries)) |
46 |
+ raise BackendException("Error running '%s'" % private) |
47 |
|
48 |
- """ |
49 |
- DEPRECATED: |
50 |
- popen(_persist) - legacy wrappers for subprocess_popen(_persist) |
51 |
- """ |
52 |
def popen(self, commandline): |
53 |
- result, stdout, stderr = self.subprocess_popen(commandline) |
54 |
- return stdout |
55 |
+ """ |
56 |
+ Like run_command(), but capture stdout and return it (the |
57 |
+ contents read from stdout) as a string. |
58 |
+ """ |
59 |
+ private = self.munge_password(commandline) |
60 |
+ log.Info(_("Reading results of '%s'") % private) |
61 |
+ fout = os.popen(commandline) |
62 |
+ results = fout.read() |
63 |
+ if fout.close(): |
64 |
+ raise BackendException("Error running '%s'" % private) |
65 |
+ return results |
66 |
+ |
67 |
def popen_persist(self, commandline): |
68 |
- result, stdout, stderr = self.subprocess_popen_persist(commandline) |
69 |
- return stdout |
70 |
- |
71 |
- def _subprocess_popen(self, commandline): |
72 |
- """ |
73 |
- For internal use. |
74 |
- Execute the given command line, interpreted as a shell command. |
75 |
- Returns int Exitcode, string StdOut, string StdErr |
76 |
- """ |
77 |
- from subprocess import Popen, PIPE |
78 |
- p = Popen(commandline, shell=True, stdout=PIPE, stderr=PIPE) |
79 |
- stdout, stderr = p.communicate() |
80 |
- |
81 |
- return p.returncode, stdout, stderr |
82 |
- |
83 |
- def subprocess_popen(self, commandline): |
84 |
- """ |
85 |
- Execute the given command line with error check. |
86 |
- Returns int Exitcode, string StdOut, string StdErr |
87 |
- |
88 |
- Raise a BackendException on failure. |
89 |
- """ |
90 |
- private = self.munge_password(commandline) |
91 |
- log.Info(_("Reading results of '%s'") % private) |
92 |
- result, stdout, stderr = self._subprocess_popen(commandline) |
93 |
- if result != 0: |
94 |
- raise BackendException("Error running '%s'" % private) |
95 |
- return result, stdout, stderr |
96 |
- |
97 |
- def subprocess_popen_persist(self, commandline): |
98 |
- """ |
99 |
- Execute the given command line with error check. |
100 |
- Retries globals.num_retries times with 30s delay. |
101 |
- Returns int Exitcode, string StdOut, string StdErr |
102 |
- |
103 |
- Raise a BackendException on failure. |
104 |
+ """ |
105 |
+ Like run_command_persist(), but capture stdout and return it |
106 |
+ (the contents read from stdout) as a string. |
107 |
""" |
108 |
private = self.munge_password(commandline) |
109 |
for n in range(1, globals.num_retries+1): |
110 |
- # sleep before retry |
111 |
if n > 1: |
112 |
+ # sleep before retry |
113 |
time.sleep(30) |
114 |
log.Info(_("Reading results of '%s'") % private) |
115 |
- result, stdout, stderr = self._subprocess_popen(commandline) |
116 |
- if result == 0: |
117 |
- return result, stdout, stderr |
118 |
- elif result == 1280 and self.parsed_url.scheme == 'ftp': |
119 |
+ fout = os.popen(commandline) |
120 |
+ results = fout.read() |
121 |
+ result_status = fout.close() |
122 |
+ if not result_status: |
123 |
+ return results |
124 |
+ elif result_status == 1280 and self.parsed_url.scheme == 'ftp': |
125 |
# This squelches the "file not found" result fromm ncftpls when |
126 |
# the ftp backend looks for a collection that does not exist. |
127 |
return '' |
128 |
log.Warn(gettext.ngettext("Running '%s' failed (attempt #%d)", |
129 |
"Running '%s' failed (attempt #%d)", n) % |
130 |
(private, n)) |
131 |
- if stdout or stderr: |
132 |
- log.Warn(_("Error is:\n%s") % stderr + (stderr and stdout and "\n") + stdout) |
133 |
log.Warn(gettext.ngettext("Giving up trying to execute '%s' after %d attempt", |
134 |
"Giving up trying to execute '%s' after %d attempts", |
135 |
globals.num_retries) % (private, globals.num_retries)) |
136 |
|