]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/util.py
- drop util.sendfile, use shutil.copyfileobj instead
[tld-builder.git] / TLD_Builder / util.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import re
4 import sys
5 import os
6 import log
7 import string
8 import codecs
9
10 def uuid_python():
11     return str(uuid_random())
12
13 def uuid_external():
14     f = os.popen("uuidgen 2>&1")
15     u = f.read().strip()
16     f.close()
17     if len(u) != 36:
18         raise Exception("uuid: fatal, cannot generate uuid: %s" % u)
19     return u
20
21 # uuid module available in python >= 2.5
22 try:
23     from uuid import uuid4 as uuid_random
24 except ImportError:
25     uuid = uuid_external
26 else:
27     uuid = uuid_python
28
29 def pkg_name(nvr):
30     return re.match(r"(.+)-[^-]+-[^-]+", nvr).group(1)
31
32 def msg(m):
33     sys.stderr.write(m)
34
35 def append_to(log, msg):
36     f = open(log, "a")
37     f.write("%s\n" % msg)
38     f.close()
39
40 def clean_tmp(dir):
41     # FIXME: use python
42     os.system("rm -f %s/* 2>/dev/null; rmdir %s 2>/dev/null" % (dir, dir))
43
44 def collect_files(log, basedir = "/home"):
45     f = open(log, 'r')
46     rx = re.compile(r"^Wrote: (%s.*\.rpm)$" % basedir)
47     proc = re.compile(r"^Processing files:.*$")
48     files = []
49     for l in reversed(list(f)):
50         if proc.match(l):
51             break
52         m = rx.search(l)
53         if m:
54             files.append(m.group(1))
55     f.close()
56     return files
57
58 def find_last_section(log):
59     f = open(log, 'r')
60     rx1 = re.compile(r"^Executing\(%(\w+)\).*$")
61     rx2 = re.compile(r"^Processing (files):.*$")
62     last_section = None
63     for l in f:
64         m = rx1.search(l)
65         if not m:
66             m = rx2.search(l)
67         if m:
68             last_section = m.group(1)
69     f.close()
70     return last_section
71
72 def cmp_to_key(mycmp):
73     'Convert a cmp= function into a key= function'
74     class K:
75         def __init__(self, obj, *args):
76             self.obj = obj
77         def __lt__(self, other):
78             return mycmp(self.obj, other.obj) < 0
79         def __gt__(self, other):
80             return mycmp(self.obj, other.obj) > 0
81         def __eq__(self, other):
82             return mycmp(self.obj, other.obj) == 0
83         def __le__(self, other):
84             return mycmp(self.obj, other.obj) <= 0
85         def __ge__(self, other):
86             return mycmp(self.obj, other.obj) >= 0
87         def __ne__(self, other):
88             return mycmp(self.obj, other.obj) != 0
89     return K
90
91 def to_bytes(s):
92     if type(s) is bytes:
93         return s
94     elif type(s) is str or (sys.version_info[0] < 3 and type(s) is unicode):
95         return codecs.encode(s, 'utf-8')
96     else:
97         raise TypeError("Expected bytes or string, but got %s." % type(s))
98
99 def to_str(s):
100     if type(s) is bytes:
101         return codecs.decode(s, 'utf-8')
102     elif type(s) is str or (sys.version_info[0] < 3 and type(s) is unicode):
103         return s
104     else:
105         raise TypeError("Expected bytes or string, but got %s." % type(s))
106
107 def cmp(a, b):
108     return (a > b) - (a < b)