source: soft/build_system/build_system/repsys/tags/V1_6_14/RepSys/util.py @ 1

Last change on this file since 1 was 1, checked in by fasma, 12 years ago

Initial Import from Mandriva's soft revision 224062 and package revision 45733

File size: 2.8 KB
Line 
1#!/usr/bin/python
2
3from RepSys import Error, config
4
5import getpass
6import sys
7import os
8import re
9import logging
10#import commands
11
12log = logging.getLogger("repsys")
13
14# Our own version of commands' getstatusoutput(). We have a commands
15# module directory, so we can't import Python's standard module
16def commands_getstatusoutput(cmd):
17    """Return (status, output) of executing cmd in a shell."""
18    import os
19    pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
20    text = pipe.read()
21    sts = pipe.close()
22    if sts is None: sts = 0
23    if text[-1:] == '\n': text = text[:-1]
24    return sts, text
25
26def execcmd(*cmd, **kwargs):
27    cmdstr = " ".join(cmd)
28    if kwargs.get("show"):
29        status = os.system(cmdstr)
30        output = ""
31    else:
32        status, output = commands_getstatusoutput(
33                "LANG=C LANGUAGE=C LC_ALL=C "+cmdstr)
34    if status != 0 and not kwargs.get("noerror"):
35        raise Error, "command failed: %s\n%s\n" % (cmdstr, output)
36    if config.getbool("global", "verbose", 0):
37        print cmdstr
38        sys.stdout.write(output)
39    return status, output
40
41def get_auth(username=None, password=None):
42    set_username = 1
43    set_password = 1
44    if not username:
45        username = config.get("auth", "username")
46        if not username:
47            username = raw_input("username: ")
48        else:
49            set_username = 0
50    if not password:
51        password = config.get("auth", "password")
52        if not password:
53            password = getpass.getpass("password: ")
54        else:
55            set_password = 0
56    if set_username:
57        config.set("auth", "username", username)
58    if set_password:
59        config.set("auth", "password", password)
60    return username, password
61
62
63def mapurl(url):
64    """Maps a url following the regexp provided by the option url-map in
65    repsys.conf
66    """
67    urlmap = config.get("global", "url-map")
68    newurl = url
69    if urlmap:
70        try:
71            expr_, replace = urlmap.split()[:2]
72        except ValueError:
73            log.error("invalid url-map: %s", urlmap)
74        else:
75            try:
76                newurl = re.sub(expr_, replace, url)
77            except re.error, errmsg:
78                log.error("error in URL mapping regexp: %s", errmsg)
79    return newurl
80
81
82def get_helper(name):
83    """Tries to find the path of a helper script
84
85    It first looks if the helper has been explicitly defined in
86    configuration, if not, falls back to the default helper path, which can
87    also be defined in configuration file(s).
88    """
89    helperdir = config.get("helper", "prefix", "/usr/share/repsys")
90    hpath = config.get("helper", name, None) or \
91            os.path.join(helperdir, name)
92    if not os.path.isfile(hpath):
93        log.warn("providing unexistent helper: %s", hpath)
94    return hpath
95   
96
97# vim:et:ts=4:sw=4
Note: See TracBrowser for help on using the repository browser.