1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import os |
---|
4 | import glob |
---|
5 | import platform |
---|
6 | import re |
---|
7 | import subprocess |
---|
8 | import shutil |
---|
9 | import sys |
---|
10 | |
---|
11 | # CHANGE TO REFLECT YOUR PATHS |
---|
12 | BUILD_DIR = '/home/pyther/xpra_build' |
---|
13 | SRPM_DIR = os.path.join(BUILD_DIR, 'rpmbuild/SRPMS') |
---|
14 | |
---|
15 | # DO NOT CHANGE |
---|
16 | REPO_DIR = os.path.join(BUILD_DIR, 'repos') |
---|
17 | MOCK_DIR = os.path.join(BUILD_DIR, 'mock') |
---|
18 | MOCK_CONFIG_DIR = os.path.join(BUILD_DIR, 'mock_config') |
---|
19 | |
---|
20 | d = {} |
---|
21 | |
---|
22 | d['f20'] = {'name':'fedora', 'version':'20', 'build_pkgs':[], 'pkgs':[]} |
---|
23 | d['f19'] = {'name':'fedora', 'version':'19', 'build_pkgs':[], 'pkgs':[]} |
---|
24 | #d['el5'] = {'name':'el', 'version':'5', 'build_pkgs':[], 'pkgs':[]} |
---|
25 | d['el6'] = {'name':'el', 'version':'6', 'build_pkgs':[], 'pkgs':[]} |
---|
26 | |
---|
27 | #d['el5']['build_pkgs'] = ['yasm', 'Cython'] |
---|
28 | d['el6']['build_pkgs'] = ['yasm', 'Cython'] |
---|
29 | |
---|
30 | |
---|
31 | # el6 packages |
---|
32 | d['el6']['pkgs'].append('x264-xpra') |
---|
33 | d['el6']['pkgs'].append('libvpx-xpra') |
---|
34 | d['el6']['pkgs'].append('libwebp-xpra') |
---|
35 | d['el6']['pkgs'].append('ffmpeg-xpra') |
---|
36 | d['el6']['pkgs'].append('libfakeXinerama') |
---|
37 | d['el6']['pkgs'].append(('xorg-x11-drv-dummy', '0.3.6-2*el6')) |
---|
38 | d['el6']['pkgs'].append('PyOpenGL') |
---|
39 | d['el6']['pkgs'].append('PyOpenGL-accelerate') |
---|
40 | d['el6']['pkgs'].append('python-lz4') |
---|
41 | d['el6']['pkgs'].append('python-pillow') |
---|
42 | d['el6']['pkgs'].append('python-netifaces') |
---|
43 | d['el6']['pkgs'].append('python-crypto') |
---|
44 | #d['el6']['pkgs'].append('python-pycuda') |
---|
45 | d['el6']['pkgs'].append('xpra') |
---|
46 | |
---|
47 | # f19 packages |
---|
48 | d['f19']['pkgs'].append('x264-xpra') |
---|
49 | d['f19']['pkgs'].append('ffmpeg-xpra') |
---|
50 | d['f19']['pkgs'].append('libfakeXinerama') |
---|
51 | d['f19']['pkgs'].append(('xorg-x11-drv-dummy', '0.3.6-10*f19')) |
---|
52 | d['f19']['pkgs'].append('PyOpenGL') |
---|
53 | d['f19']['pkgs'].append('PyOpenGL-accelerate') |
---|
54 | d['f19']['pkgs'].append('python-lz4') |
---|
55 | d['f19']['pkgs'].append('python-pillow') |
---|
56 | d['f19']['pkgs'].append('python-netifaces') |
---|
57 | #d['f19']['pkgs'].append('python-pycuda') |
---|
58 | d['f19']['pkgs'].append('xpra') |
---|
59 | |
---|
60 | # f20 packages |
---|
61 | d['f20']['pkgs'].append('x264-xpra') |
---|
62 | d['f20']['pkgs'].append('ffmpeg-xpra') |
---|
63 | d['f20']['pkgs'].append('libfakeXinerama') |
---|
64 | d['f20']['pkgs'].append(('xorg-x11-drv-dummy', '0.3.6-11*fc20')) |
---|
65 | d['f20']['pkgs'].append('PyOpenGL') |
---|
66 | d['f20']['pkgs'].append('PyOpenGL-accelerate') |
---|
67 | d['f20']['pkgs'].append('python-lz4') |
---|
68 | d['f20']['pkgs'].append('python-pillow') |
---|
69 | d['f20']['pkgs'].append('python-netifaces') |
---|
70 | #d['f20']['pkgs'].append('python-pycuda') |
---|
71 | d['f20']['pkgs'].append('xpra') |
---|
72 | |
---|
73 | |
---|
74 | def prepare_build_root(): |
---|
75 | # create build dir |
---|
76 | if not os.path.isdir(BUILD_DIR): |
---|
77 | try: |
---|
78 | os.makedirs(BUILD_DIR) |
---|
79 | except OSError: |
---|
80 | return 1 |
---|
81 | |
---|
82 | # create repo dir |
---|
83 | if not os.path.isdir(REPO_DIR): |
---|
84 | try: |
---|
85 | os.makedirs(REPO_DIR) |
---|
86 | except OSError: |
---|
87 | return 1 |
---|
88 | |
---|
89 | # create mock dir |
---|
90 | if not os.path.isdir(MOCK_DIR): |
---|
91 | try: |
---|
92 | os.makedirs(MOCK_DIR) |
---|
93 | except OSError: |
---|
94 | return 1 |
---|
95 | |
---|
96 | # create mock config dir |
---|
97 | if not os.path.isdir(MOCK_CONFIG_DIR): |
---|
98 | try: |
---|
99 | os.mkdir(MOCK_CONFIG_DIR) |
---|
100 | except OSError: |
---|
101 | return 1 |
---|
102 | |
---|
103 | # default config files need for mock |
---|
104 | if not os.path.isfile(os.path.join(MOCK_CONFIG_DIR, 'logging.ini')): |
---|
105 | shutil.copy('/etc/mock/logging.ini', MOCK_CONFIG_DIR) |
---|
106 | |
---|
107 | if not os.path.isfile(os.path.join(MOCK_CONFIG_DIR, 'site-defaults.cfg')): |
---|
108 | shutil.copy('/etc/mock/site-defaults.cfg', MOCK_CONFIG_DIR) |
---|
109 | |
---|
110 | return |
---|
111 | |
---|
112 | def createrepodata(dir): |
---|
113 | p = subprocess.Popen(['/usr/bin/createrepo', dir], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
114 | rc = p.wait() |
---|
115 | return |
---|
116 | |
---|
117 | def prepare_distro_build(dist, arch): |
---|
118 | build_repo = os.path.join(REPO_DIR, dist, 'build') |
---|
119 | repo = os.path.join(REPO_DIR, dist, 'main') |
---|
120 | mock = os.path.join(MOCK_DIR, dist) |
---|
121 | |
---|
122 | if not os.path.isdir(build_repo): |
---|
123 | os.makedirs(build_repo) |
---|
124 | os.makedirs(os.path.join(build_repo, 'srpm')) |
---|
125 | os.makedirs(os.path.join(build_repo, arch)) |
---|
126 | |
---|
127 | # createrepo data |
---|
128 | createrepodata(os.path.join(build_repo, 'srpm')) |
---|
129 | createrepodata(os.path.join(build_repo, arch)) |
---|
130 | |
---|
131 | if not os.path.isdir(repo): |
---|
132 | os.makedirs(repo) |
---|
133 | os.makedirs(os.path.join(repo, 'srpm')) |
---|
134 | os.makedirs(os.path.join(repo, arch)) |
---|
135 | |
---|
136 | # createrepo data |
---|
137 | createrepodata(os.path.join(repo, 'srpm')) |
---|
138 | createrepodata(os.path.join(repo, arch)) |
---|
139 | |
---|
140 | if not os.path.isdir(mock): |
---|
141 | os.makedirs(mock) |
---|
142 | |
---|
143 | return (build_repo, repo, mock) |
---|
144 | |
---|
145 | def create_mock_config(dist, arch, build_repo, repo): |
---|
146 | """ |
---|
147 | generate a mock configuration that contains our local repos |
---|
148 | |
---|
149 | takes a base configuration from /etc/mock/ and applies |
---|
150 | our local (xpra, xpra-build) repos |
---|
151 | """ |
---|
152 | |
---|
153 | name = d[dist]['name'] |
---|
154 | version = d[dist]['version'] |
---|
155 | |
---|
156 | # mock config for rhel/centos is named epel |
---|
157 | if name == 'el': |
---|
158 | name = 'epel' |
---|
159 | |
---|
160 | chroot = '{0}-{1}-{2}'.format(name, version, arch) |
---|
161 | etc_cfg = os.path.join('/etc/mock', '{0}.cfg'.format(chroot)) |
---|
162 | |
---|
163 | # mock configuration is missing |
---|
164 | if not os.path.isfile(etc_cfg): |
---|
165 | raise NameError('no such file: {0}'.format(etc_cfg)) |
---|
166 | |
---|
167 | cfg = open(etc_cfg, 'r').read() |
---|
168 | |
---|
169 | # append our stuff |
---|
170 | xpra_repos = """ |
---|
171 | [xpra-build] |
---|
172 | name=xpra-build |
---|
173 | baseurl=file://{1}/{0} |
---|
174 | gpgcheck=0 |
---|
175 | |
---|
176 | [xpra] |
---|
177 | name=xpra |
---|
178 | baseurl=file://{2}/{0} |
---|
179 | gpgcheck=0 |
---|
180 | \"\"\" |
---|
181 | """.format(arch, build_repo, repo) |
---|
182 | |
---|
183 | cfg = re.sub('"""$', xpra_repos, cfg) |
---|
184 | |
---|
185 | # write cfg to file |
---|
186 | mock_cfg = os.path.join(MOCK_CONFIG_DIR, '{0}.cfg'.format(chroot)) |
---|
187 | with open(mock_cfg, 'w') as mock_cfg_file: |
---|
188 | mock_cfg_file.write(cfg) |
---|
189 | |
---|
190 | return chroot |
---|
191 | |
---|
192 | def build_pkg(pkg, chroot, result_dir, repo): |
---|
193 | """ takes in a pkg name, find the srpm, builds it using mock, and moves |
---|
194 | built package(s) to repo """ |
---|
195 | |
---|
196 | # we only expect one srpm to be found |
---|
197 | # but if we are working with a dirty buildroot, we may find newer and older |
---|
198 | # versions of a srpm. |
---|
199 | if type(pkg) == tuple: |
---|
200 | srpms = glob.glob('{0}/{1}-{2}*.src.rpm'.format(SRPM_DIR, pkg[0], pkg[1])) |
---|
201 | else: |
---|
202 | srpms = glob.glob('{0}/{1}-[0-9]*.src.rpm'.format(SRPM_DIR, pkg)) |
---|
203 | |
---|
204 | if len(srpms) < 1: |
---|
205 | print 'no source rpms found for {0}'.format(pkg) |
---|
206 | raise NameError('no source rpms found for {0}'.format(pkg)) |
---|
207 | for srpm in srpms: |
---|
208 | cmd = ['/usr/bin/mock', '--configdir={0}'.format(MOCK_CONFIG_DIR), '--rebuild', srpm, '-r', chroot, '--resultdir={0}'.format(result_dir)] |
---|
209 | print 'building {0}'.format(os.path.basename(srpm)) |
---|
210 | p_mock = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
---|
211 | rc = p_mock.wait() |
---|
212 | |
---|
213 | if rc != 0: |
---|
214 | raise NameError('failed to build {0}'.format(os.path.basename(srpm))) |
---|
215 | |
---|
216 | arch = platform.uname()[4] |
---|
217 | # move packages to repo |
---|
218 | for rpm in glob.glob(os.path.join(result_dir, '*.rpm')): |
---|
219 | if re.match(r'.*\.src\.rpm$', rpm): |
---|
220 | shutil.copy(rpm, os.path.join(repo, 'srpm')) |
---|
221 | else: |
---|
222 | shutil.copy(rpm, os.path.join(repo, arch)) |
---|
223 | |
---|
224 | os.remove(rpm) |
---|
225 | |
---|
226 | # generate repodata |
---|
227 | createrepodata(os.path.join(repo, arch)) |
---|
228 | return |
---|
229 | |
---|
230 | def main(): |
---|
231 | prepare_build_root() |
---|
232 | |
---|
233 | for key, distro in d.iteritems(): |
---|
234 | print '==== building packages for {0} {1} ===='.format(distro['name'], distro['version']) |
---|
235 | arch = platform.uname()[4] |
---|
236 | |
---|
237 | # create necessary sub-directories and do other prep work |
---|
238 | build_repo, repo, mock_root = prepare_distro_build(key, arch) |
---|
239 | |
---|
240 | try: |
---|
241 | mock_chroot = create_mock_config(key, arch, build_repo, repo) |
---|
242 | except NameError: |
---|
243 | print 'fatal error: unable to create mock config for {1}'.format(key) |
---|
244 | return 1 |
---|
245 | |
---|
246 | # TODO check to see user is part of the mock group, mock doesn't work well |
---|
247 | |
---|
248 | # build packages |
---|
249 | failed_pkgs = [] |
---|
250 | for pkg in distro['build_pkgs']: |
---|
251 | try: |
---|
252 | build_pkg(pkg, mock_chroot, mock_root, build_repo) |
---|
253 | except NameError: |
---|
254 | failed_pkgs.append(pkg) |
---|
255 | |
---|
256 | for pkg in distro['pkgs']: |
---|
257 | try: |
---|
258 | build_pkg(pkg, mock_chroot, mock_root, repo) |
---|
259 | except NameError: |
---|
260 | failed_pkgs.append(pkg) |
---|
261 | |
---|
262 | # we failed to build packages |
---|
263 | if len(failed_pkgs) > 0: |
---|
264 | print 'WARNING: FAILED TO BUILD PACKAGES!!' |
---|
265 | for pkg in failed_pkgs: |
---|
266 | print ' {0}'.format(pkg) |
---|
267 | |
---|
268 | print '==== finished building packages for {0} {1}'.format(distro['name'], distro['version']) |
---|
269 | |
---|
270 | |
---|
271 | if "__main__" in __name__: |
---|
272 | sys.exit(main()) |
---|