]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/util.py
67bd9a1be30d230f7be2c0bdf4d9a32b4a1c3ba3
[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
9 def uuid_python():
10     return str(uuid_random())
11
12 def uuid_external():
13     f = os.popen("uuidgen 2>&1")
14     u = f.read().strip()
15     f.close()
16     if len(u) != 36:
17         raise Exception("uuid: fatal, cannot generate uuid: %s" % u)
18     return u
19
20 # uuid module available in python >= 2.5
21 try:
22     from uuid import uuid4 as uuid_random
23 except ImportError:
24     uuid = uuid_external
25 else:
26     uuid = uuid_python
27
28 def pkg_name(nvr):
29     return re.match(r"(.+)-[^-]+-[^-]+", nvr).group(1)
30
31 def msg(m):
32     sys.stderr.write(m)
33
34 def sendfile(src, dst):
35     cnt = 0
36     while 1:
37         s = src.read(10000)
38         if s == "": break
39         cnt += len(s)
40         dst.write(s)
41     return cnt
42
43 def append_to(log, msg):
44     f = open(log, "a")
45     f.write("%s\n" % msg)
46     f.close()
47
48 def clean_tmp(dir):
49     # FIXME: use python
50     os.system("rm -f %s/* 2>/dev/null; rmdir %s 2>/dev/null" % (dir, dir))
51
52 def collect_files(log, basedir = "/home"):
53     f = open(log, 'r')
54     rx = re.compile(r"^Wrote: (%s.*\.rpm)$" % basedir)
55     proc = re.compile(r"^Processing files:.*$")
56     files = []
57     for l in reversed(list(f)):
58         if proc.match(l):
59             break
60         m = rx.search(l)
61         if m:
62             files.append(m.group(1))
63     f.close()
64     return files
65
66 def find_last_section(log):
67     f = open(log, 'r')
68     rx1 = re.compile(r"^Executing\(%(\w+)\).*$")
69     rx2 = re.compile(r"^Processing (files):.*$")
70     last_section = None
71     for l in f:
72         m = rx1.search(l)
73         if not m:
74             m = rx2.search(l)
75         if m:
76             last_section = m.group(1)
77     f.close()
78     return last_section