Statistics
| Branch: | Tag: | Revision:

root / image_creator / util.py @ d144e954

History | View | Annotate | Download (2.6 kB)

1 ae48a082 Nikos Skalkotos
# Copyright 2012 GRNET S.A. All rights reserved.
2 ae48a082 Nikos Skalkotos
#
3 ae48a082 Nikos Skalkotos
# Redistribution and use in source and binary forms, with or
4 ae48a082 Nikos Skalkotos
# without modification, are permitted provided that the following
5 ae48a082 Nikos Skalkotos
# conditions are met:
6 ae48a082 Nikos Skalkotos
#
7 ae48a082 Nikos Skalkotos
#   1. Redistributions of source code must retain the above
8 ae48a082 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
9 ae48a082 Nikos Skalkotos
#      disclaimer.
10 ae48a082 Nikos Skalkotos
#
11 ae48a082 Nikos Skalkotos
#   2. Redistributions in binary form must reproduce the above
12 ae48a082 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
13 ae48a082 Nikos Skalkotos
#      disclaimer in the documentation and/or other materials
14 ae48a082 Nikos Skalkotos
#      provided with the distribution.
15 ae48a082 Nikos Skalkotos
#
16 ae48a082 Nikos Skalkotos
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 ae48a082 Nikos Skalkotos
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ae48a082 Nikos Skalkotos
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 ae48a082 Nikos Skalkotos
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 ae48a082 Nikos Skalkotos
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ae48a082 Nikos Skalkotos
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 ae48a082 Nikos Skalkotos
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 ae48a082 Nikos Skalkotos
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 ae48a082 Nikos Skalkotos
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 ae48a082 Nikos Skalkotos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 ae48a082 Nikos Skalkotos
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 ae48a082 Nikos Skalkotos
# POSSIBILITY OF SUCH DAMAGE.
28 ae48a082 Nikos Skalkotos
#
29 ae48a082 Nikos Skalkotos
# The views and conclusions contained in the software and
30 ae48a082 Nikos Skalkotos
# documentation are those of the authors and should not be
31 ae48a082 Nikos Skalkotos
# interpreted as representing official policies, either expressed
32 ae48a082 Nikos Skalkotos
# or implied, of GRNET S.A.
33 3ccb2618 Nikos Skalkotos
34 3ccb2618 Nikos Skalkotos
import pbs
35 22a6d232 Nikos Skalkotos
from clint.textui import puts, puts_err, colored, progress
36 3ccb2618 Nikos Skalkotos
37 ae48a082 Nikos Skalkotos
38 3ccb2618 Nikos Skalkotos
def get_command(command):
39 3ccb2618 Nikos Skalkotos
    def find_sbin_command(command, exception):
40 3ccb2618 Nikos Skalkotos
        search_paths = ['/usr/local/sbin', '/usr/sbin', '/sbin']
41 3ccb2618 Nikos Skalkotos
        for fullpath in map(lambda x: "%s/%s" % (x, command), search_paths):
42 3ccb2618 Nikos Skalkotos
            if os.path.exists(fullpath) and os.access(fullpath, os.X_OK):
43 3ccb2618 Nikos Skalkotos
                return pbs.Command(fullpath)
44 3ccb2618 Nikos Skalkotos
        raise exception
45 3ccb2618 Nikos Skalkotos
46 3ccb2618 Nikos Skalkotos
    try:
47 3ccb2618 Nikos Skalkotos
        return pbs.__getattr__(command)
48 3ccb2618 Nikos Skalkotos
    except pbs.CommadNotFount as e:
49 3ccb2618 Nikos Skalkotos
        return find_sbin_command(command, e)
50 22a6d232 Nikos Skalkotos
51 22a6d232 Nikos Skalkotos
52 e2cf65d3 Nikos Skalkotos
def error(msg, new_line=True):
53 e2cf65d3 Nikos Skalkotos
    puts_err(colored.red("Error: %s\n" % msg), new_line)
54 22a6d232 Nikos Skalkotos
55 22a6d232 Nikos Skalkotos
56 e2cf65d3 Nikos Skalkotos
def warn(msg, new_line=True):
57 e2cf65d3 Nikos Skalkotos
    puts_err(colored.yellow("Warning: %s" % msg), new_line)
58 22a6d232 Nikos Skalkotos
59 22a6d232 Nikos Skalkotos
60 e2cf65d3 Nikos Skalkotos
def success(msg, new_line=True):
61 e2cf65d3 Nikos Skalkotos
    puts(colored.green(msg), new_line)
62 22a6d232 Nikos Skalkotos
63 22a6d232 Nikos Skalkotos
64 22a6d232 Nikos Skalkotos
def progress_generator(label='', n=100):
65 22a6d232 Nikos Skalkotos
    position = 0
66 22a6d232 Nikos Skalkotos
    for i in progress.bar(range(n), label):
67 22a6d232 Nikos Skalkotos
        if i < position:
68 22a6d232 Nikos Skalkotos
            continue
69 22a6d232 Nikos Skalkotos
        position = yield
70 22a6d232 Nikos Skalkotos
    yield  # suppress the StopIteration exception
71 22a6d232 Nikos Skalkotos
72 22a6d232 Nikos Skalkotos
73 22a6d232 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :