aboutsummaryrefslogtreecommitdiffstats
path: root/Tools/scripts/patchtool.py
blob: 21545e007dd4feedac9d04009fb35855026c44eb (plain) (blame)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/usr/bin/env python
# ex:ts=4
#-*- mode: Fundamental; tab-width: 4; -*-
#
# patchtool.py - a tool to automate common operation with patchfiles in the
# FreeBSD Ports Collection.
#
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp):
# Maxim Sobolev <sobomax@FreeBSD.org> wrote this file.  As long as you retain
# this notice you can do whatever you want with this stuff. If we meet some
# day, and you think this stuff is worth it, you can buy me a beer in return.
#
# Maxim Sobolev
# ----------------------------------------------------------------------------
#
# $FreeBSD$
#
# MAINTAINER= sobomax@FreeBSD.org <- any unapproved commits to this file are
#                    highly discouraged!!!
#

import os, os.path, subprocess, sys, getopt, glob, errno, types

# Some global variables used as constants
True = 1
False = 0


# Tweakable global variables. User is able to override any of these by setting
# appropriate environment variable prefixed by `PT_', eg:
# $ export PT_CVS_ID="FooOS"
# $ export PT_DIFF_CMD="/usr/local/bin/mydiff"
# will force script to use "FooOS" as a CVS_ID instead of "FreeBSD" and
# "/usr/local/bin/mydiff" as a command to generate diffs.
class Vars:
    CVS_ID = 'FreeBSD'

    DIFF_ARGS = '-du'
    DIFF_SUFX = '.orig'
    PATCH_PREFIX = 'patch-'
    PATCH_IGN_SUFX = ('.orig', '.rej')
    RCSDIFF_SUFX = ',v'

    CD_CMD = 'cd'
    DIFF_CMD = '/usr/bin/diff'
    MAKE_CMD = '/usr/bin/make'
    PRINTF_CMD = '/usr/bin/printf'
    RCSDIFF_CMD = '/usr/bin/rcsdiff'

    DEFAULT_MAKEFILE = 'Makefile'
    DEV_NULL = '/dev/null'
    ETC_MAKE_CONF = '/etc/make.conf'
    
    SLASH_REPL_SYMBOL = '_' # The symbol to replace '/' when auto-generating
                            # patchnames


#
# Check if the supplied patch refers to a port's directory.
#
def isportdir(path, soft = False):
    REQ_FILES = ('Makefile', 'pkg-descr', 'distinfo')
    if not os.path.isdir(path) and soft != True:
        raise IOError(errno.ENOENT, path)
        # Not reached #

    try:
        content = os.listdir(path)
    except OSError:
        return False

    for file in REQ_FILES:
        if file not in content:
            return False
    return True


#
# Traverse directory tree up from the path pointed by argument and return if
# root directory of a port is found.
#
def locateportdir(path, wrkdirprefix= '', strict = False):
    # Flag to relax error checking in isportdir() function. It required when
    # WRKDIRPREFIX is defined.
    softisport = False

    path = os.path.abspath(path)

    if wrkdirprefix != '':
        wrkdirprefix= os.path.abspath(wrkdirprefix)
        commonprefix = os.path.commonprefix((path, wrkdirprefix))
        if commonprefix != wrkdirprefix:
            return ''
        path = path[len(wrkdirprefix):]
        softisport = True

    while path != '/':
        if isportdir(path, softisport) == True:
            return path
        path = os.path.abspath(os.path.join(path, '..'))

    if strict == True:
        raise LocatePDirError(path)
        # Not reached #
    else:
        return ''


#
# Get value of a make(1) variable called varname. Optionally maintain a cache
# for resolved varname:makepath pairs to speed-up operation if the same variable
# from the exactly same file is requested repeatedly (invocation of make(1) is
# very expensive operation...)
#
def querymakevar(varname, path = 'Makefile', strict = False, cache = {}):
    path = os.path.abspath(path)

    if cache.has_key((varname, path)) == 1:
        return cache[(varname, path)]

    origpath = path
    if os.path.isdir(path):
        path = os.path.join(path, Vars.DEFAULT_MAKEFILE)
    if not os.path.isfile(path):
        raise IOError(errno.ENOENT, path)
        # Not reached #

    dir = os.path.dirname(path)
    CMDLINE = '%s %s && %s -f %s -V %s' % (Vars.CD_CMD, dir, Vars.MAKE_CMD, \
      path, varname)
    devnull = open('/dev/null', 'a')
    pipe = subprocess.Popen(CMDLINE, shell = True, stdin = subprocess.PIPE, \
            stdout = subprocess.PIPE, stderr = devnull, close_fds = True)
    retval = ''
    for line in pipe.stdout.readlines():
        retval = retval + line.strip() + ' '
    retval = retval[:-1]
    if strict == True and retval.strip() == '':
        raise MakeVarError(path, varname)
        # Not reached #

    cache[(varname, origpath)] = retval
    return retval


#
# Get a path of `path'  relatively to wrksrc. For example:
# path:     /foo/bar
# wrksrc:   /foo/bar/baz/somefile.c
# getrelpath:   baz/somefile.c
# Most of the code here is to handle cases when ../ operation is required to
# reach wrksrc from path, for example:
# path:     /foo/bar
# wrksrc:   /foo/baz/somefile.c
# getrelpath:   ../baz/somefile.c
#
def getrelpath(path, wrksrc):
    path = os.path.abspath(path)
    wrksrc = os.path.abspath(wrksrc) + '/'
    commonpart = os.path.commonprefix((path, wrksrc))
    while commonpart[-1:] != '/':
        commonpart = commonpart[:-1]
    path = path[len(commonpart):]
    wrksrc = wrksrc[len(commonpart):]
    adjust = ''
    while os.path.normpath(os.path.join(wrksrc, adjust)) != '.':
        adjust = os.path.join(adjust, '..')
    relpath = os.path.join(adjust, path)
    return relpath


#
# Generare a diff between saved and current versions of the file pointed by the
# wrksrc+path. Apply heuristics to locate saved version of the file in question
# and if it fails assume that file is new, so /dev/null is to be used as
# original file. Optionally save generated patch into `outfile' instead of
# dumping it to stdout. Generated patches automatically being tagged with
# "FreeBSD" cvs id.
#
def gendiff(path, wrksrc, outfile = ''):
    fullpath = os.path.join(wrksrc, path)
    if not os.path.isfile(fullpath):
        raise IOError(errno.ENOENT, fullpath)
        # Not reached #

    cmdline = ''
    if os.path.isfile(fullpath + Vars.DIFF_SUFX):       # Normal diff
        path_orig = path + Vars.DIFF_SUFX
        cmdline = '%s %s %s %s' % (Vars.DIFF_CMD, Vars.DIFF_ARGS, path_orig, path)
    elif os.path.isfile(fullpath + Vars.RCSDIFF_SUFX):  # RCS diff
        path_orig = path
        cmdline = '%s %s %s' % (Vars.RCSDIFF_CMD, Vars.DIFF_ARGS, path)
    else:                           # New file
        path_orig = Vars.DEV_NULL
        cmdline = '%s %s %s %s' % (Vars.DIFF_CMD, Vars.DIFF_ARGS, path_orig, path)

    savedir = os.getcwd()
    os.chdir(wrksrc)
    devnull = open('/dev/null', 'a')
    pipe = subprocess.Popen(cmdline, shell = True, stdin = subprocess.PIPE, \
            stdout = subprocess.PIPE, stderr = devnull, close_fds = True)
    outbuf = pipe.stdout.readlines()
    exitval = pipe.wait()
    if exitval == 0:    # No differences were found
        retval = False
        retmsg = 'no differencies found between original and current ' \
              'version of "%s"' % fullpath
    elif exitval == 1:  # Some differences  were  found
        if (outfile != ''):
            outbuf[0] = '--- %s\n' % path_orig
            outbuf[1] = '+++ %s\n' % path
            outbuf.insert(0, '\n')
            outbuf.insert(0, '$%s$\n' % Vars.CVS_ID)
            outbuf.insert(0, '\n')
            open(outfile, 'w').writelines(outbuf)
        else:
            sys.stdout.writelines(outbuf)
        retval = True
        retmsg = ''
    else:               # Error occured
        raise ECmdError('"%s"' % cmdline, \
          'external command returned non-zero error code')
        # Not reached #

    os.chdir(savedir)
    return (retval, retmsg)


#
# Automatically generate a name for a patch based on its path relative to
# wrksrc. Use simple scheme to ensute 1-to-1 mapping between path and
# patchname - replace all '_' with '__' and all '/' with '_'.
#
def makepatchname(path, patchdir = ''):
    SRS = Vars.SLASH_REPL_SYMBOL
    retval = Vars.PATCH_PREFIX + \
      path.replace(SRS, SRS + SRS).replace('/', SRS)
    retval = os.path.join(patchdir, retval)
    return retval


#
# Write a specified message to stderr.
#
def write_msg(message):
    if type(message) == types.StringType:
        message = message,
    sys.stderr.writelines(message)


#
# Print specified message to stdout and ask user [y/N]?. Optionally allow
# specify default answer, i.e. return value if user typed only <cr>
#
def query_yn(message, default = False):
    while True:
        if default == True:
            yn = 'Y/n'
        elif default == False:
            yn = 'y/N'
        else:
            yn = 'Y/N'

        reply = raw_input('%s [%s]: ' % (message, yn))

        if reply == 'y' or reply == 'Y':
            return True
        elif reply == 'n' or reply == 'N':
            return False
        elif reply == '' and default in (True, False):
            return default
        print 'Wrong answer "%s", please try again' % reply
    return default


#
# Print optional message and usage information and exit with specified exit
# code.
#
def usage(code, msg = ''):
    myname = os.path.basename(sys.argv[0])
    write_msg((str(msg), """
Usage: %s [-afi] file ...
       %s -u [-i] [patchfile|patchdir ...]
""" % (myname, myname)))
    sys.exit(code)


#
# Simple custom exception
#
class MyError(Exception):
    msg = 'error'

    def __init__(self, file, msg=''):
        self.file = file
        if msg != '':
            self.msg = msg

    def __str__(self):
            return '%s: %s' % (self.file, self.msg)


#
# Error parsing patchfile
#
class PatchError(MyError):
    msg = 'corrupt patchfile, or not patchfile at all'


#
# Error executing external command
#
class ECmdError(MyError):
    pass


#
# Error getting value of makefile variable
#
class MakeVarError(MyError):
    def __init__(self, file, makevar, msg=''):
        self.file = file
        if msg != '':
            self.msg = msg
        else:
            self.msg = 'can\'t get %s value' % makevar


#
# Error locating portdir
#
class LocatePDirError(MyError):
    msg = 'can\'t locate portdir'


class Patch:
    fullpath = ''
    minus3file = ''
    plus3file = ''
    wrksrc = ''
    patchmtime = 0
    targetmtime = 0

    def __init__(self, path, wrksrc):
        MINUS3_DELIM = '--- '
        PLUS3_DELIM = '+++ '

        path = os.path.abspath(path)
        if not os.path.isfile(path):
            raise IOError(errno.ENOENT, path)
            # Not reached #

        self.fullpath = path
        filedes = open(path)

        for line in filedes.readlines():
            if self.minus3file == '':
                if line[:len(MINUS3_DELIM)] == MINUS3_DELIM:
                    lineparts = line.split()
                    try:
                        self.minus3file = lineparts[1]
                    except IndexError:
                        raise PatchError(path)
                        # Not reached #
                    continue
            elif line[:len(PLUS3_DELIM)] == PLUS3_DELIM:
                lineparts = line.split()
                try:
                    self.plus3file = lineparts[1]
                except IndexError:
                    raise PatchError(path)
                    # Not reached #
                break

        filedes.close()

        if self.minus3file == '' or self.plus3file == '':
            raise PatchError(path)
            # Not reached #

        self.wrksrc = os.path.abspath(wrksrc)
        self.patchmtime = os.path.getmtime(self.fullpath)
        plus3file = os.path.join(self.wrksrc, self.plus3file)
        if os.path.isfile(plus3file):
            self.targetmtime = os.path.getmtime(plus3file)
        else:
            self.targetmtime = 0

    def update(self, patch_cookiemtime = 0, ignoremtime = False):
        targetfile = os.path.join(self.wrksrc, self.plus3file)
        if not os.path.isfile(targetfile):
            raise IOError(errno.ENOENT, targetfile)
            # Not reached #

        patchdir = os.path.dirname(self.fullpath)
        if not os.path.isdir(patchdir):
            os.mkdir(patchdir)

        if ignoremtime == True or self.patchmtime == 0 or \
          self.targetmtime == 0 or \
          (self.patchmtime < self.targetmtime and \
          patch_cookiemtime < self.targetmtime):
            retval = gendiff(self.plus3file, self.wrksrc, self.fullpath)
            if retval[0] == True:
                self.patchmtime = os.path.getmtime(self.fullpath)
        else:
            retval = (False, 'patch is already up to date')
        return retval


class NewPatch(Patch):
    def __init__(self, patchdir, wrksrc, relpath):
        self.fullpath = makepatchname(relpath, os.path.abspath(patchdir))
        self.wrksrc = os.path.abspath(wrksrc)
        self.plus3file = relpath
        self.minus3file = relpath
        self.patchmtime = 0
        plus3file = os.path.join(self.wrksrc, self.plus3file)
        if os.path.isfile(plus3file):
            self.targetmtime = os.path.getmtime(plus3file)
        else:
            self.targetmtime = 0


class PatchesCollection:
    patches = {}

    def __init__(self):
        self.patches = {}
        pass

    def adddir(self, patchdir, wrksrc):
        if not os.path.isdir(patchdir):
            raise IOError(errno.ENOENT, patchdir)
            # Not reached #

        for filename in glob.glob(os.path.join(patchdir, Vars.PATCH_PREFIX + '*')):
            for sufx in Vars.PATCH_IGN_SUFX:
                if filename[-len(sufx):] == sufx:
                    write_msg('WARNING: patchfile "%s" ignored\n' % filename)
                    break
            else:
                self.addpatchfile(filename, wrksrc)

    def addpatchfile(self, path, wrksrc):
        path = os.path.abspath(path)
        if not self.patches.has_key(path):
            self.addpatchobj(Patch(path, wrksrc))

    def addpatchobj(self, patchobj):
        self.patches[patchobj.fullpath] = patchobj

    def lookupbyname(self, path):
        path = os.path.abspath(path)
        if self.patches.has_key(path):
            return self.patches[path]
        return None

    def lookupbytarget(self, wrksrc, relpath):
        wrksrc = os.path.abspath(wrksrc)
        for patch in self.patches.values():
            if wrksrc == patch.wrksrc and relpath == patch.plus3file:
                return patch
        return None

    def getpatchobjs(self):
        return self.patches.values()


#
# Resolve all symbolic links in the given path to a file
#
def truepath(path):
    if not os.path.isfile(path):
        raise IOError(errno.ENOENT, path)

    result = ''
    while len(path) > 0:
        path, lastcomp = os.path.split(path)
        if len(lastcomp) == 0:
            lastcomp = path
            path = ''
        result = os.path.join(lastcomp, result)
        if len(path) == 0:
            break
        if os.path.islink(path):
            linkto = os.path.normpath(os.readlink(path))
            if linkto[0] != '/':
                path = os.path.join(path, linkto)
            else:
                path = linkto
    return result[:-1]


def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'afui')
    except getopt.GetoptError, msg:
        usage(2, msg)

    automatic = False
    force = False
    mode = generate
    ignoremtime = False

    for o, a in opts:
        if o == '-a':
            automatic = True
        elif o == '-f':
            force = True
        elif o == '-u':
            mode = update
        elif o == '-i':
            ignoremtime = True
        else:
            usage(2)

    # Allow user to override internal constants
    for varname in dir(Vars):
        if varname[:2] == '__' and varname[-2:] == '__':
            continue
        try:
            value = os.environ['PT_' + varname]
            setattr(Vars, varname, value)
        except KeyError:
            pass

    mode(args, automatic, force, ignoremtime)

    sys.exit(0)


#
# Display a diff or generate patchfile for the files pointed out by args.
#
def generate(args, automatic, force, ignoremtime):
    if len(args) == 0:
        usage(2, "ERROR: no input files specified")

    patches = PatchesCollection()

    for filepath in args:
        for suf in Vars.RCSDIFF_SUFX, Vars.DIFF_SUFX:
            if filepath.endswith(suf):
                filepath = filepath[:-len(suf)]
                break
        if not os.path.isfile(filepath):
            raise IOError(errno.ENOENT, filepath)
            # Not reached #

        filepath = truepath(filepath)

        wrkdirprefix = querymakevar('WRKDIRPREFIX', Vars.ETC_MAKE_CONF, False)
        portdir = locateportdir(os.path.dirname(filepath), wrkdirprefix, True)
        wrksrc = querymakevar('WRKSRC', portdir, True)

        relpath = getrelpath(filepath, wrksrc)

        if automatic:
            patchdir = querymakevar('PATCHDIR', portdir, True)

            if os.path.isdir(patchdir):
                patches.adddir(patchdir, wrksrc)

            extra_patches = querymakevar('EXTRA_PATCHES', portdir, False)
            for extra_patch in extra_patches.split():
                if os.path.isfile(extra_patch):
                    patches.addpatchfile(extra_patch, wrksrc)

            patchobj = patches.lookupbytarget(wrksrc, relpath)
            if patchobj == None:
                patchobj = NewPatch(patchdir, wrksrc, relpath)
                patches.addpatchobj(patchobj)

            if not force and os.path.exists(patchobj.fullpath) and \
              os.path.getsize(patchobj.fullpath) > 0:
                try:
                    retval = query_yn('Target patchfile "%s" already ' \
                      'exists, do you want to  replace it?' % \
                      os.path.basename(patchobj.fullpath))
                except KeyboardInterrupt:
                    sys.exit('\nAction aborted')
                    # Not reached #
                if retval == False:
                    continue

            write_msg('Generating patchfile: %s...' % \
              os.path.basename(patchobj.fullpath))

            try:
                retval = None
                retval = patchobj.update(ignoremtime = ignoremtime)
            finally:
                # Following tricky magic intended to let us append \n even if
                # we are going to die due to unhandled exception
                if retval == None:
                    write_msg('OUCH!\n')

            if retval[0] == False:
                write_msg('skipped (%s)\n' % retval[1])
            else:
                write_msg('ok\n')

        else:   # automatic != True
            retval = gendiff(relpath, wrksrc)
            if retval[0] == False:
                write_msg('WARNING: %s\n' % retval[1])


#
# Atomatically update all patches pointed by args (may be individual
# patchfiles, patchdirs or any directories in a portdirs). If directory argument
# is encountered, all patches that belong to the port are updated. If no
# arguments are supplied - current directory is assumed.
#
# The procedure homours last modification times of the patchfile, file from
# which diff to be generated and `EXTRACT_COOKIE' file (usually
# ${WRKDIR}/.extract_cookie) to update only those patches that are really need
# to be updated.
#
def update(args, automatic, force, ignoremtime):
    if len(args) == 0:
        args = './',

    for path in args:
        if not os.path.exists(path):
            raise IOError(errno.ENOENT, path)
            # Not reached #

        patches = PatchesCollection()

        if os.path.isdir(path):
            for wrkdirprefix in (querymakevar('WRKDIRPREFIX', \
              Vars.ETC_MAKE_CONF, False), ''):
                portdir = locateportdir(path, wrkdirprefix, False)
                if portdir != '':
                    break
            if portdir == '':
                raise LocatePDirError(os.path.abspath(path))
                # Not reached #

            wrksrc = querymakevar('WRKSRC', portdir, True)
            patchdir = querymakevar('PATCHDIR', portdir, True)

            if os.path.isdir(patchdir):
                patches.adddir(patchdir, wrksrc)
            else:
                continue

        elif os.path.isfile(path):
            portdir = locateportdir(os.path.dirname(path), '' , True)
            wrksrc = querymakevar('WRKSRC', portdir, True)
            patches.addpatchfile(path, wrksrc)

        patch_cookie = querymakevar('PATCH_COOKIE', portdir, True)
        if os.path.isfile(patch_cookie):
            patch_cookiemtime = os.path.getmtime(patch_cookie)
        else:
            patch_cookiemtime = 0

        for patchobj in patches.getpatchobjs():
            write_msg('Updating patchfile: %s...' % \
              os.path.basename(patchobj.fullpath))

            try:
                retval = None
                retval = patchobj.update(patch_cookiemtime, \
                  ignoremtime)
            finally:
                if retval == None:
                    write_msg('OUCH!\n')

            if retval[0] == False:
                write_msg('skipped (%s)\n' % retval[1])
            else:
                write_msg('ok\n')


if __name__ == '__main__':
    try:
        main()
    except (PatchError, ECmdError, MakeVarError, LocatePDirError), msg:
        sys.exit('ERROR: ' + str(msg))
    except IOError, (code, msg):
        sys.exit('ERROR: %s: %s' % (str(msg), os.strerror(code)))