]>
Commit | Line | Data |
---|---|---|
47a4cb89 MT |
1 | #!/usr/bin/python |
2 | ||
47a4cb89 | 3 | import os |
4496b160 | 4 | import progressbar |
47a4cb89 | 5 | import random |
47a4cb89 MT |
6 | import shutil |
7 | import string | |
47a4cb89 MT |
8 | import sys |
9 | import time | |
10 | ||
11 | from errors import Error | |
8ba01c0a | 12 | from packages.util import calc_hash1, format_size |
c0fd807c | 13 | from i18n import _ |
47a4cb89 | 14 | |
e9c20259 MT |
15 | def cli_is_interactive(): |
16 | """ | |
17 | Say weather a shell is interactive or not. | |
18 | """ | |
19 | if sys.stdin.isatty() and sys.stdout.isatty() and sys.stderr.isatty(): | |
20 | return True | |
21 | ||
22 | return False | |
23 | ||
c0fd807c MT |
24 | def ask_user(question): |
25 | """ | |
26 | Ask the user the question, he or she can answer with yes or no. | |
27 | ||
28 | This function returns True for "yes" and False for "no". | |
29 | ||
30 | If the software is running in a non-inteactive shell, no question | |
31 | is asked at all and the answer is always "yes". | |
32 | """ | |
33 | if not cli_is_interactive(): | |
34 | return True | |
35 | ||
36 | print _("%s [y/N]") % question, | |
37 | ret = raw_input() | |
38 | print # Just an empty line. | |
39 | ||
40 | return ret in ("y", "Y", "z", "Z", "j", "J") | |
41 | ||
7c8f2953 MT |
42 | def random_string(length=20): |
43 | s = "" | |
44 | ||
45 | for i in range(length): | |
46 | s += random.choice(string.letters) | |
47 | ||
48 | return s | |
49 | ||
4496b160 MT |
50 | def make_progress(message, maxval): |
51 | # Return nothing if stdout is not a terminal. | |
52 | if not sys.stdout.isatty(): | |
53 | return | |
54 | ||
55 | widgets = [ | |
56 | " ", | |
57 | "%-40s" % message, | |
58 | " ", | |
59 | progressbar.Bar(left="[", right="]"), | |
60 | " ", | |
61 | progressbar.ETA(), | |
62 | " ", | |
63 | ] | |
64 | ||
65 | if not maxval: | |
66 | maxval = 1 | |
67 | ||
68 | pb = progressbar.ProgressBar(widgets=widgets, maxval=maxval) | |
69 | pb.start() | |
70 | ||
71 | return pb | |
72 | ||
47a4cb89 MT |
73 | def rm(path, *args, **kargs): |
74 | """ | |
75 | version of shutil.rmtree that ignores no-such-file-or-directory errors, | |
76 | and tries harder if it finds immutable files | |
77 | """ | |
78 | tryAgain = 1 | |
79 | failedFilename = None | |
80 | while tryAgain: | |
81 | tryAgain = 0 | |
82 | try: | |
83 | shutil.rmtree(path, *args, **kargs) | |
84 | except OSError, e: | |
85 | if e.errno == 2: # no such file or directory | |
86 | pass | |
87 | elif e.errno==1 or e.errno==13: | |
88 | tryAgain = 1 | |
89 | if failedFilename == e.filename: | |
90 | raise | |
91 | failedFilename = e.filename | |
92 | os.system("chattr -R -i %s" % path) | |
93 | else: | |
94 | raise |