"Fossies" - the Fresh Open Source Software Archive 
Member "svnchecker-0.3/modules/Transaction.py" (11 Jul 2008, 6332 Bytes) of package /linux/privat/old/svnchecker-0.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "Transaction.py" see the
Fossies "Dox" file reference documentation.
1 # pylint: disable-msg=W0102, W0704
2
3 # Copyright 2008 German Aerospace Center (DLR)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """ Class to work with transactions. """
18
19
20 import os
21 import re
22 import shutil
23 import tempfile
24
25 from modules import Process
26
27
28 class FileNotFoundException(Exception):
29 def __init__(self, filename):
30 Exception.__init__(self, "No such file in repository or transaction %r." % filename)
31
32 class PropertyNotFoundException(Exception):
33 def __init__(self, keyword, filename):
34 Exception.__init__(self, "Property %r for file %r not set." % (keyword, filename))
35
36
37 class Transaction:
38
39 def __init__(self, reposPath, txnName):
40 """ Initialize the transaction object. """
41
42 txnName = str(txnName)
43 try:
44 int(txnName)
45 self.type = "revision"
46 except ValueError:
47 self.type = "transaction"
48
49 self.reposPath = reposPath
50 self.txnName = txnName
51
52 self.profile = re.compile(".*")
53
54 self.tmpdir = tempfile.mkdtemp()
55
56 self.cache = {}
57
58 def __executeSVN(self, command, arg = "", split=False):
59 command = "svnlook --%s %s %s %s %s" % (self.type, self.txnName, command, self.reposPath, arg)
60 if command in self.cache:
61 return self.cache[command]
62
63 output = Process.execute(command)
64 if split:
65 output = [x.strip() for x in output.split("\n") if x.strip()]
66
67 self.cache[command] = output
68 return self.cache[command]
69
70 def cleanup(self):
71 """
72 Delete the temporary directory.
73 Do NOT use in your checks or handlers.
74 """
75 shutil.rmtree(self.tmpdir)
76
77 def setProfile(self, profile):
78 """
79 Sets the current profile.
80 Do NOT use in your checks or handlers.
81 """
82 self.profile = re.compile(profile)
83
84 def getUserID(self):
85 """ Returns a string with the username of the current transaction. """
86 user = self.__executeSVN("author")
87 return user.strip()
88
89 def getFiles(self, checkList=[".*"], ignoreList=[]):
90 """
91 Returns a map of all modified files. The keys of the map
92 are the filenames. The values of the map is the
93 associated attribute, which can be one of the default
94 svnlook changed attributes:
95 http://svnbook.red-bean.com/en/1.4/svn-book.html#svn.ref.svnlook.c.changed
96
97 @param checkList List of regular expressions for files which should be included.
98 @param ignoreList List of regular expressions for files which should be ignored.
99 """
100 output = self.__executeSVN("changed", split=True)
101 files = {}
102 for entry in output:
103 attributes = entry[0:3].strip()
104 filename = entry[4:].strip()
105
106 if self.profile.search(filename) and self.__check(filename, checkList) and not self.__check(filename, ignoreList):
107 files[filename] = attributes
108 return files
109
110 def __check(self, datei, items):
111 for item in items:
112 regex = re.compile(item)
113 if regex.search(datei):
114 return True
115 return False
116
117 def getFile(self, filename):
118 """ Returns the path to a temporary copy of a file in the repository. """
119 if not self.fileExists(filename):
120 raise FileNotFoundException(filename)
121
122 tmpfilename = os.path.join(self.tmpdir, filename)
123 if os.path.exists(tmpfilename):
124 return tmpfilename
125
126 content = self.__executeSVN("cat", "\"" + filename + "\"")
127
128 dirname = os.path.dirname(filename)
129 tmpdirname = os.path.join(self.tmpdir, dirname)
130 if dirname and not os.path.exists(tmpdirname):
131 os.makedirs(tmpdirname)
132
133 fd = open(tmpfilename, "w")
134 fd.write(content)
135 fd.close()
136
137 return tmpfilename
138
139 def fileExists(self, filename, ignoreCase=False):
140 """ Returns whether a file exists in the current transaction or revision of the repository, optionally case-insensitive. """
141 exists = False
142
143 if ignoreCase:
144 filename = filename.lower()
145 files = self.__executeSVN("tree", "--full-paths", split=True)
146 count = 0
147 for fname in files:
148 if fname.lower() == filename:
149 count += 1
150 if count >= 2:
151 exists = True
152 break
153
154 else:
155 try:
156 self.__executeSVN("proplist", "\"" + filename + "\"", split=True)
157 exists = True
158 except Process.ProcessException:
159 pass
160
161 return exists
162
163 def getCommitMsg(self):
164 """ Returns the commit message. """
165 output = self.__executeSVN("info", split=True)
166 temp = output[3:]
167 msg = "".join(temp)
168 return msg.strip()
169
170 def getRevision(self):
171 """ Returns the id of the revision or transaction. """
172 return self.txnName
173
174 def getProperty(self, keyword, filename):
175 """ Returns a specified property of a file. """
176 if not self.hasProperty(keyword, filename):
177 raise PropertyNotFoundException(keyword, filename)
178
179 return self.__executeSVN("propget", " ".join([keyword, "\"" + filename + "\""]))
180
181 def hasProperty(self, keyword, filename):
182 """ Checks if a given file has the given property. """
183 return keyword in self.listProperties(filename)
184
185 def listProperties(self, filename):
186 """ Returns a list of names of the properties for a file."""
187 if not self.fileExists(filename):
188 raise FileNotFoundException(filename)
189
190 return self.__executeSVN("proplist", "\"" + filename + "\"", split=True)