summaryrefslogtreecommitdiff
path: root/patchtracker/DiffGzHandler.py
blob: 84be8279dd9320be647592e105d4da159abd4646 (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
import tempfile
import sys
import os
import stat

from Patch import Patch, PatchSeries

class DiffGzException(Exception):
  pass

class DiffGzHandler:
  diff = None
  def __init__(self,fname):
    self.diff = fname
    self.size = os.stat(fname)[stat.ST_SIZE]

  def filterdiff(self, include=None, exclude=None):
    cmd = ["filterdiff","-z","-p","1"]
    if include:
      cmd += [ "-i", include]
    elif exclude:
      cmd += [ "-x", exclude]
    else:
      raise Exception("DiffGzHandler.filterdiff called w/o include/exclude")
    i,o,e=os.popen3(cmd+[self.diff])
    i.close()
    p = Patch(o)
    err = e.read()
    if len(err):
      raise DiffGzException("filterdiff gave errors: "+err)
    return p

  def debiandir(self):
    return self.filterdiff(include='debian/*')

  def nondebiandir(self):
    return self.filterdiff(exclude='debian/*')

  def series(self):
    patches = None
    embedded = self.filterdiff(include='debian/patches*')

    # XXX *cough* cache *cough*
    if embedded.lines():
      td = tempfile.mkdtemp()
      i,o,e=os.popen3("patch -d %s -p3"%(td))
      i.write(str(embedded))
      i.close()
      err = e.read()
      if len(err):
        raise Exception("unable to extract series patches:\n"+err)
      patches = PatchSeries(td)
      os.system("rm -rf %s"%(td))

    return patches
  
if __name__ == "__main__":
  print "DiffGzHandler testing"
  try:
    dh = DiffGzHandler(sys.argv[1])
  except IndexError:
    print "usage: %s <diffgz>"%(sys.argv[0])
    sys.exit(1)

  print "debian dir:"
  print dh.debiandir().diffstat()
  print "nondebian dir:"
  print dh.nondebiandir().diffstat()
  print "series:"
  s = dh.series()
  print s
  for name,patch in s.iterpatches():
    print "patch:",name
    print patch.diffstat()