source: soft/build_system/build_system/repsys/tags/V1_6_10/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("LANG=C LANGUAGE=C LC_ALL=C "+cmdstr)
33    if status != 0 and not kwargs.get("noerror"):
34        raise Error, "command failed: %s\n%s\n" % (cmdstr, output)
35    if config.getbool("global", "verbose", 0):
36        print cmdstr
37        sys.stdout.write(output)
38    return status, output
39
40def get_auth(username=None, password=None):
41    set_username = 1
42    set_password = 1
43    if not username:
44        username = config.get("auth", "username")
45        if not username:
46            username = raw_input("username: ")
47        else:
48            set_username = 0
49    if not password:
50        password = config.get("auth", "password")
51        if not password:
52            password = getpass.getpass("password: ")
53        else:
54            set_password = 0
55    if set_username:
56        config.set("auth", "username", username)
57    if set_password:
58        config.set("auth", "password", password)
59    return username, password
60
61
62def mapurl(url):
63    """Maps a url following the regexp provided by the option url-map in
64    repsys.conf
65    """
66    urlmap = config.get("global", "url-map")
67    newurl = url
68    if urlmap:
69        try:
70            expr_, replace = urlmap.split()[:2]
71        except ValueError:
72            log.error("invalid url-map: %s", urlmap)
73        else:
74            try:
75                newurl = re.sub(expr_, replace, url)
76            except re.error, errmsg:
77                log.error("error in URL mapping regexp: %s", errmsg)
78    return newurl
79
80
81def get_helper(name):
82    """Tries to find the path of a helper script
83
84    It first looks if the helper has been explicitly defined in
85    configuration, if not, falls back to the default helper path, which can
86    also be defined in configuration file(s).
87    """
88    helperdir = config.get("helper", "prefix", "/usr/share/repsys")
89    hpath = config.get("helper", name, None) or \
90            os.path.join(helperdir, name)
91    if not os.path.isfile(hpath):
92        log.warn("providing unexistent helper: %s", hpath)
93    return hpath
94   
95
96# vim:et:ts=4:sw=4
Note: See TracBrowser for help on using the repository browser.