]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/util.py
- more python3 fixes, dropped python2 support
[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 sendfile(src, dst):
36     cnt = 0
37     while 1:
38         s = src.read(10000)
39         if s == "": break
40         cnt += len(s)
41         dst.write(s)
42     return cnt
43
44 def append_to(log, msg):
45     f = open(log, "a")
46     f.write("%s\n" % msg)
47     f.close()
48
49 def clean_tmp(dir):
50     # FIXME: use python
51     os.system("rm -f %s/* 2>/dev/null; rmdir %s 2>/dev/null" % (dir, dir))
52
53 def collect_files(log, basedir = "/home"):
54     f = open(log, 'r')
55     rx = re.compile(r"^Wrote: (%s.*\.rpm)$" % basedir)
56     proc = re.compile(r"^Processing files:.*$")
57     files = []
58     for l in reversed(list(f)):
59         if proc.match(l):
60             break
61         m = rx.search(l)
62         if m:
63             files.append(m.group(1))
64     f.close()
65     return files
66
67 def find_last_section(log):
68     f = open(log, 'r')
69     rx1 = re.compile(r"^Executing\(%(\w+)\).*$")
70     rx2 = re.compile(r"^Processing (files):.*$")
71     last_section = None
72     for l in f:
73         m = rx1.search(l)
74         if not m:
75             m = rx2.search(l)
76         if m:
77             last_section = m.group(1)
78     f.close()
79     return last_section
80
81 def cmp_to_key(mycmp):
82     'Convert a cmp= function into a key= function'
83     class K:
84         def __init__(self, obj, *args):
85             self.obj = obj
86         def __lt__(self, other):
87             return mycmp(self.obj, other.obj) < 0
88         def __gt__(self, other):
89             return mycmp(self.obj, other.obj) > 0
90         def __eq__(self, other):
91             return mycmp(self.obj, other.obj) == 0
92         def __le__(self, other):
93             return mycmp(self.obj, other.obj) <= 0
94         def __ge__(self, other):
95             return mycmp(self.obj, other.obj) >= 0
96         def __ne__(self, other):
97             return mycmp(self.obj, other.obj) != 0
98     return K
99
100 def to_bytes(s):
101     if type(s) is bytes:
102         return s
103     elif type(s) is str or (sys.version_info[0] < 3 and type(s) is unicode):
104         return codecs.encode(s, 'utf-8')
105     else:
106         raise TypeError("Expected bytes or string, but got %s." % type(s))
107
108 def to_str(s):
109     if type(s) is bytes:
110         return codecs.decode(s, 'utf-8')
111     elif type(s) is str or (sys.version_info[0] < 3 and type(s) is unicode):
112         return s
113     else:
114         raise TypeError("Expected bytes or string, but got %s." % type(s))