summaryrefslogtreecommitdiff
path: root/patchtracker/Patch.py
blob: 8a1dbd0564c002f1befb0947cab21d712582c4e8 (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
import sys
import os
import errno
from glob import glob

class Patch:
  p = []

  def __init__(self, fh):
    self.p = fh.readlines()

  def __str__(self):
    return "".join(self.p)

  def lines(self):
    return len(self.p)

class PatchSeries (list):
  style = "simple"
  names = []
  patches = {}

  def __init__(self, dir):
    fd = None
    try:
      fd = file(os.sep.join([dir, "00list"]))
      self.style = "dpatch"
    except IOError, e:
      if e.errno != errno.ENOENT:
        raise e
    try:
      fd = file(os.sep.join([dir, "series"]))
      self.style = "quilt"
    except IOError, e:
      if e.errno != errno.ENOENT:
        raise e

    if fd:
      self.names = [n.rstrip() for n in fd.readlines()]
    else:
      self.names = os.listdir(dir)
      self.names.sort()

    for p in self.names:
      self.patches[p] = Patch(file(os.sep.join([dir, p])))

  def __getitem__(self, y):
    return self.patches[self.names[y]]

  def __len__(self):
    return len(self.names)

  def fetch(self, name):
    return self.patches[name]

  def __str__(self):
    return "\n".join(self.names)

if __name__ == "__main__":
  print "Patch.py testing"
  try:
    p = Patch(file(sys.argv[1]))
    print "patch contents:"
    print p
  except IndexError:
    print "usage: %s <patch>"