summaryrefslogtreecommitdiffstats
path: root/scripts/makeupdates
blob: 1ffb166e11cb04cfb7f7692515f487df646172ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/python
#
# makeupdates - Generate an updates.img containing changes since the last
#               tag, but only changes to the main anaconda runtime.
#               initrd/stage1 updates have to be created separately.
#
# Copyright (C) 2009  Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: David Cantrell <dcantrell@redhat.com>

import getopt
import os
import shutil
import subprocess
import sys

def getArchiveTag(configure, spec):
    tag = ""

    f = open(configure)
    lines = f.readlines()
    f.close()

    for line in lines:
        if line.startswith('AC_INIT('):
            fields = line.split('[')
            tag += fields[1].split(']')[0] + '-' + fields[2].split(']')[0]
            break
        else:
            continue

    f = open(spec)
    lines = f.readlines()
    f.close()

    for line in lines:
        if line.startswith('Release:'):
            tag += '-' + line.split()[1].split('%')[0]
        else:
            continue

    return tag

def getArchiveTagOffset(configure, spec, offset):
    tag = getArchiveTag(configure, spec)

    if not tag.count("-") >= 2:
        return tag
    ldash = tag.rfind("-")
    bldash = tag[:ldash].rfind("-")
    ver = tag[bldash+1:ldash]

    if not ver.count(".") >= 1:
        return tag
    ver = ver[:ver.rfind(".")]

    if not len(ver) > 0:
        return tag
    globstr = "refs/tags/" + tag[:bldash+1] + ver + ".*"
    proc = subprocess.Popen(['git', 'for-each-ref', '--sort=taggerdate',
                             '--format=%(tag)', globstr],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE).communicate()
    lines = proc[0].strip("\n").split('\n')
    lines.reverse()

    try:
        return lines[offset]
    except IndexError:
        return tag

def doGitDiff(tag, args=[]):
    proc = subprocess.Popen(['git', 'diff', '--name-status', tag] + args,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE).communicate()

    lines = proc[0].split('\n')
    return lines

def copyUpdatedFiles(tag, updates, cwd):
    def pruneFile(src, names):
        lst = []

        for name in names:
            if name.startswith('Makefile') or name.endswith('.pyc'):
                lst.append(name)

        return lst

    def install_to_dir(fname, relpath):
        sys.stdout.write("Including %s\n" % fname)
        outdir = os.path.join(updates, relpath)
        if not os.path.isdir(outdir):
            os.makedirs(outdir)
        shutil.copy2(file, outdir)

    subdirs = []

    # Updates get overlaid onto the runtime filesystem. Anaconda expects them
    # to be in /run/install/updates, so put them in
    # $updatedir/run/install/updates.
    tmpupdates = updates.rstrip('/')
    if not tmpupdates.endswith("/run/install/updates"):
        tmpupdates = os.path.join(tmpupdates, "run/install/updates")

    lines = doGitDiff(tag)
    for line in lines:
        fields = line.split()

        if len(fields) < 2:
            continue

        status = fields[0]
        file = fields[1]

        if status == "D":
            continue

        if file.endswith('.spec.in') or (file.find('Makefile') != -1) or \
           file.endswith('.c') or file.endswith('.h') or \
           file.endswith('.sh') or file == 'configure.ac':
            continue

        if file.endswith('.glade'):
            # Some UI files should go under ui/<dir> where dir is the
            # directory above the file.glade
            dir_parts = os.path.dirname(file).split(os.path.sep)
            g_idx = dir_parts.index("gui")
            uidir = os.path.sep.join(dir_parts[g_idx+1:])
            path_comps = [tmpupdates, "ui"]
            if uidir:
                path_comps.append(uidir)
            install_to_dir(file, os.path.join(*path_comps))
        elif file.startswith('pyanaconda/'):
            # pyanaconda stuff goes into /tmp/updates/[path]
            dirname = os.path.join(tmpupdates, os.path.dirname(file))
            install_to_dir(file, dirname)
        elif file == 'anaconda':
            # anaconda itself we just overwrite
            install_to_dir(file, "usr/sbin")
        elif file.endswith('.service') or file.endswith(".target"):
            # same for systemd services
            install_to_dir(file, "lib/systemd/system")
        elif file.endswith('/anaconda-generator'):
            # yeah, this should probably be more clever..
            install_to_dir(file, "lib/systemd/system-generators")
        elif file == "data/tmux.conf":
            install_to_dir(file, "usr/share/anaconda")
        elif file == "data/interactive-defaults.ks":
            install_to_dir(file, "usr/share/anaconda")
        elif file == "data/liveinst/liveinst":
            install_to_dir(file, "usr/sbin")
        elif file == "data/70-anaconda.rules":
            install_to_dir(file, "lib/udev/rules.d")
        elif file.startswith("data/pixmaps"):
            install_to_dir(file, "usr/share/anaconda/pixmaps")
        elif file.startswith("data/ui/"):
            install_to_dir(file, "usr/share/anaconda/ui")
        elif file.startswith("data/post-scripts/"):
            install_to_dir(file, "usr/share/anaconda/post-scripts")
        elif file.find('/') != -1:
            fields = file.split('/')
            subdir = fields[0]
            if subdir in ['po', 'scripts','command-stubs', 'tests',
                          'docs', 'fonts', 'utils',
                          'liveinst', 'dracut', 'data']:
                continue
            else:
                sys.stdout.write("Including %s\n" % (file,))
                install_to_dir(file, tmpupdates)
        else:
            sys.stdout.write("Including %s\n" % (file,))
            install_to_dir(file, tmpupdates)

def _compilableChanged(tag, compilable):
    lines = doGitDiff(tag, [compilable])

    for line in lines:
        fields = line.split()

        if len(fields) < 2:
            continue

        status = fields[0]
        file = fields[1]

        if status == "D":
            continue

        if file.startswith('Makefile') or file.endswith('.h') or \
           file.endswith('.c') or file.endswith('.py'):
            return True

    return False

def isysChanged(tag):
    return _compilableChanged(tag, 'pyanaconda/isys')

def widgetsChanged(tag):
    return _compilableChanged(tag, 'widgets')

def copyUpdatedIsys(updates, cwd):
    os.chdir(cwd)

    if not os.path.isfile('Makefile'):
        if not os.path.isfile('configure'):
            os.system('./autogen.sh')
        os.system('./configure --prefix=`rpm --eval %_prefix`')

    os.system('make')

    # Updates get overlaid onto the runtime filesystem. Anaconda expects them
    # to be in /run/install/updates, so put them in
    # $updatedir/run/install/updates.
    tmpupdates = updates.rstrip('/')
    if not tmpupdates.endswith("/run/install/updates/pyanaconda"):
        tmpupdates = os.path.join(tmpupdates, "run/install/updates/pyanaconda/isys")

    if not os.path.isdir(tmpupdates):
        os.makedirs(tmpupdates)

    isysmodule = os.path.realpath(cwd + '/pyanaconda/isys/.libs/_isys.so')
    isysinit = os.path.realpath(cwd + '/pyanaconda/isys/__init__.py')

    if os.path.isfile(isysmodule):
        shutil.copy2(isysmodule, tmpupdates)
    if os.path.isfile(isysinit):
        shutil.copy2(isysinit, tmpupdates)

def copyUpdatedWidgets(updates, cwd):
    os.chdir(cwd)

    if os.path.isdir("/lib64"):
        libdir = "/lib64/"
    else:
        libdir = "/lib/"

    if not os.path.isdir(updates + libdir):
        os.makedirs(updates + libdir)

    if not os.path.isdir(updates + libdir + "girepository-1.0"):
        os.makedirs(updates + libdir + "girepository-1.0")

    if not os.path.isfile('Makefile'):
        if not os.path.isfile('configure'):
            os.system('./autogen.sh')
        os.system('./configure --prefix=`rpm --eval %_prefix` --enable-gtk-doc --enable-introspection')

    os.system('make')

    files = ["libAnacondaWidgets.so", "libAnacondaWidgets.so.0", "libAnacondaWidgets.so.0.0.0"]
    for f in files:
        path = os.path.normpath(cwd + "/widgets/src/.libs/" + f)
        if os.path.islink(path) and not os.path.exists(updates + libdir + os.path.basename(path)):
            os.symlink(os.readlink(path), updates + libdir + os.path.basename(path))
        elif os.path.isfile(path):
            shutil.copy2(path, updates + libdir)

    typelib = os.path.realpath(cwd + "/widgets/src/AnacondaWidgets-1.0.typelib")
    if os.path.isfile(typelib):
        shutil.copy2(typelib, updates + libdir + "girepository-1.0")

def addRpms(updates, add_rpms):
    for rpm in add_rpms:
        cmd = "cd %s && rpm2cpio %s | cpio -dium" % (updates, rpm)
        sys.stdout.write(cmd+"\n")
        os.system(cmd)

def createUpdatesImage(cwd, updates):
    os.chdir(updates)
    os.system("find . | cpio -c -o | gzip -9cv > %s/updates.img" % (cwd,))
    sys.stdout.write("updates.img ready\n")

def usage(cmd):
    sys.stdout.write("Usage: %s [OPTION]...\n" % (cmd,))
    sys.stdout.write("Options:\n")
    sys.stdout.write("    -k, --keep       Do not delete updates subdirectory.\n")
    sys.stdout.write("    -c, --compile    Compile code if there are isys changes.\n")
    sys.stdout.write("    -h, --help       Display this help and exit.\n")
    sys.stdout.write("    -t, --tag        Make image from TAG to HEAD.\n")
    sys.stdout.write("    -o, --offset     Make image from (latest_tag - OFFSET) to HEAD.\n")
    sys.stdout.write("    -a, --add        Add contents of rpm to the update\n")

def main(argv):
    prog = os.path.basename(sys.argv[0])
    cwd = os.getcwd()
    configure = os.path.realpath(cwd + '/configure.ac')
    spec = os.path.realpath(cwd + '/anaconda.spec.in')
    updates = cwd + '/updates'
    keep, compile, help, unknown = False, False, False, False
    tag = None
    opts = []
    offset = 0
    add_rpms = []

    try:
        opts, args = getopt.getopt(sys.argv[1:], 'a:t:o:kc?',
                                   ['add=', 'tag=', 'offset=',
                                    'keep', 'compile', 'help'])
    except getopt.GetoptError:
        help = True

    for o, a in opts:
        if o in ('-k', '--keep'):
            keep = True
        elif o in ('-c', '--compile'):
            compile = True
        elif o in ('-?', '--help'):
            help = True
        elif o in ('-t', '--tag'):
            tag = a
        elif o in ('-o', '--offset'):
            offset = int(a)
        elif o in ('-a', '--add'):
            add_rpms.append(os.path.abspath(a))
        else:
            unknown = True

    if help:
        usage(prog)
        sys.exit(0)
    elif unknown:
        sys.stderr.write("%s: extra operand `%s'" % (prog, sys.argv[1],))
        sys.stderr.write("Try `%s --help' for more information." % (prog,))
        sys.exit(1)

    if not os.path.isfile(configure) and not os.path.isfile(spec):
        sys.stderr.write("You must be at the top level of the anaconda source tree.\n")
        sys.exit(1)

    if not tag:
        if offset < 1:
            tag = getArchiveTag(configure, spec)
        else:
            tag = getArchiveTagOffset(configure, spec, offset)
        sys.stdout.write("Using tag: %s\n" % tag)

    if not os.path.isdir(updates):
        os.makedirs(updates)

    copyUpdatedFiles(tag, updates, cwd)

    if compile:
        if isysChanged(tag):
            copyUpdatedIsys(updates, cwd)

        if widgetsChanged(tag):
            copyUpdatedWidgets(updates, cwd)

    if add_rpms:
        addRpms(updates, add_rpms)

    createUpdatesImage(cwd, updates)

    if not keep:
        shutil.rmtree(updates)

if __name__ == "__main__":
    main(sys.argv)