* subversion/tests/cmdline/svntest/wc.py
[svnrdump.git] / svntest / entry.py
blob6108dcea43faeeb73bfb78e408ad6625ec05fd60
2 # entry.py: module to parse '.svn/entries' file
4 # Subversion is a tool for revision control.
5 # See http://subversion.tigris.org for more information.
7 # ====================================================================
8 # Copyright (c) 2000-2004, 2008 CollabNet. All rights reserved.
10 # This software is licensed as described in the file COPYING, which
11 # you should have received as part of this distribution. The terms
12 # are also available at http://subversion.tigris.org/license-1.html.
13 # If newer versions of this license are posted there, you may use a
14 # newer version instead, at your option.
16 ######################################################################
18 # Usage:
20 # Just call svn_entry.get_entries(path), where PATH is exact path
21 # to an 'entries' file. You'll get back a hash of svn_entry
22 # objects keyed by name.
24 # Each object contains a hash 'atts' that you can examine: name,
25 # kind, revision, ancestor. Other optional keys *might* be
26 # present, such as prop-time, text-time, add, delete, conflict.
28 import xml.parsers.expat # you may need to install this package
30 ### The entries file parser in tools/client-side/change-svn-wc-format.py
31 ### handles the WC format for Subversion 1.4 and 1.5, which is no
32 ### longer in XML.
34 class svn_entry:
35 "An object that represents an entry from an 'entries' file."
37 def __init__(self, attributes): # constructor
38 self.atts = attributes
40 def prettyprint(self):
41 print(" Entryname: %s" % self.atts['name'])
42 print(" Kind: %s" % self.atts['kind'])
43 print(" Revision: %s" % self.atts['revision'])
44 print(" Ancestor: %s" % self.atts['ancestor'])
45 print(" all atts: %s" % self.atts)
46 print("")
48 class svn_entryparser:
49 "A class to parse an 'entries' file."
51 def __init__(self): # constructor
52 self.entry_dict = {}
53 self.parser = xml.parsers.expat.ParserCreate()
54 self.parser.StartElementHandler = self.handle_start_tag
56 def handle_start_tag(self, name, attrs):
57 "Expat callback that receives a new open-tag."
59 if 'name' in attrs:
60 entry = svn_entry(attrs) # create new entry object
62 # Derive missing values
63 if 'kind' not in entry.atts:
64 entry.atts['kind'] = 'file' # default kind if none mentioned
65 if 'revision' not in entry.atts:
66 if "" in self.entry_dict:
67 parent = self.entry_dict[""]
68 entry.atts['revision'] = parent.atts['revision']
69 if 'ancestor' not in entry.atts:
70 if "" in self.entry_dict:
71 parent = self.entry_dict[""]
72 entry.atts['ancestor'] = parent.atts['ancestor'] + '/' \
73 + entry.atts['name']
75 self.entry_dict[attrs['name']] = entry # store the new entry
78 # The main exported routine
79 def get_entries(path):
80 "Parse the entries file at PATH and return a list of svn_entry objects."
82 entryparser = svn_entryparser() # make a parser instance
83 fp = open(path, 'r')
84 entryparser.parser.ParseFile(fp)
85 fp.close()
86 return entryparser.entry_dict
89 ### End of file.