]> TLD Linux GIT Repositories - TLD.git/blob - pld-builder.new/PLD_Builder/util.py
316a8f19f1461a377da5a5054dc23abdf7f8e312
[TLD.git] / pld-builder.new / PLD_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 = string.strip(f.read())
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):
53     f = open(log, 'r')
54     rx = re.compile(r"^Wrote: (/home.*\.rpm)$")
55     files = []
56     for l in f.xreadlines():
57         m = rx.search(l)
58         if m:
59             files.append(m.group(1))
60     f.close()
61     return files
62
63 def find_last_section(log):
64     f = open(log, 'r')
65     rx1 = re.compile(r"^Executing\(%(\w+)\).*$")
66     rx2 = re.compile(r"^Processing (files):.*$")
67     last_section = None
68     for l in f:
69         m = rx1.search(l)
70         if not m:
71             m = rx2.search(l)
72         if m:
73             last_section = m.group(1)
74     f.close()
75     return last_section