source: trunk/essentials/dev-lang/python/Demo/pdist/RCSProxy.py@ 3397

Last change on this file since 3397 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 4.6 KB
Line 
1#! /usr/bin/env python
2
3"""RCS Proxy.
4
5Provide a simplified interface on RCS files, locally or remotely.
6The functionality is geared towards implementing some sort of
7remote CVS like utility. It is modeled after the similar module
8FSProxy.
9
10The module defines two classes:
11
12RCSProxyLocal -- used for local access
13RCSProxyServer -- used on the server side of remote access
14
15The corresponding client class, RCSProxyClient, is defined in module
16rcsclient.
17
18The remote classes are instantiated with an IP address and an optional
19verbosity flag.
20"""
21
22import server
23import md5
24import os
25import fnmatch
26import string
27import tempfile
28import rcslib
29
30
31class DirSupport:
32
33 def __init__(self):
34 self._dirstack = []
35
36 def __del__(self):
37 self._close()
38
39 def _close(self):
40 while self._dirstack:
41 self.back()
42
43 def pwd(self):
44 return os.getcwd()
45
46 def cd(self, name):
47 save = os.getcwd()
48 os.chdir(name)
49 self._dirstack.append(save)
50
51 def back(self):
52 if not self._dirstack:
53 raise os.error, "empty directory stack"
54 dir = self._dirstack[-1]
55 os.chdir(dir)
56 del self._dirstack[-1]
57
58 def listsubdirs(self, pat = None):
59 files = os.listdir(os.curdir)
60 files = filter(os.path.isdir, files)
61 return self._filter(files, pat)
62
63 def isdir(self, name):
64 return os.path.isdir(name)
65
66 def mkdir(self, name):
67 os.mkdir(name, 0777)
68
69 def rmdir(self, name):
70 os.rmdir(name)
71
72
73class RCSProxyLocal(rcslib.RCS, DirSupport):
74
75 def __init__(self):
76 rcslib.RCS.__init__(self)
77 DirSupport.__init__(self)
78
79 def __del__(self):
80 DirSupport.__del__(self)
81 rcslib.RCS.__del__(self)
82
83 def sumlist(self, list = None):
84 return self._list(self.sum, list)
85
86 def sumdict(self, list = None):
87 return self._dict(self.sum, list)
88
89 def sum(self, name_rev):
90 f = self._open(name_rev)
91 BUFFERSIZE = 1024*8
92 sum = md5.new()
93 while 1:
94 buffer = f.read(BUFFERSIZE)
95 if not buffer:
96 break
97 sum.update(buffer)
98 self._closepipe(f)
99 return sum.digest()
100
101 def get(self, name_rev):
102 f = self._open(name_rev)
103 data = f.read()
104 self._closepipe(f)
105 return data
106
107 def put(self, name_rev, data, message=None):
108 name, rev = self._unmangle(name_rev)
109 f = open(name, 'w')
110 f.write(data)
111 f.close()
112 self.checkin(name_rev, message)
113 self._remove(name)
114
115 def _list(self, function, list = None):
116 """INTERNAL: apply FUNCTION to all files in LIST.
117
118 Return a list of the results.
119
120 The list defaults to all files in the directory if None.
121
122 """
123 if list is None:
124 list = self.listfiles()
125 res = []
126 for name in list:
127 try:
128 res.append((name, function(name)))
129 except (os.error, IOError):
130 res.append((name, None))
131 return res
132
133 def _dict(self, function, list = None):
134 """INTERNAL: apply FUNCTION to all files in LIST.
135
136 Return a dictionary mapping files to results.
137
138 The list defaults to all files in the directory if None.
139
140 """