* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
[svn.git] / subversion / bindings / swig / python / svn / fs.py
blob79def8d1af226834635bd5beaf43f4cf20da56ef
2 # fs.py: public Python interface for fs components
4 # Subversion is a tool for revision control.
5 # See http://subversion.tigris.org for more information.
7 ######################################################################
9 # Copyright (c) 2000-2004 CollabNet. All rights reserved.
11 # This software is licensed as described in the file COPYING, which
12 # you should have received as part of this distribution. The terms
13 # are also available at http://subversion.tigris.org/license-1.html.
14 # If newer versions of this license are posted there, you may use a
15 # newer version instead, at your option.
17 ######################################################################
19 from libsvn.fs import *
20 from svn.core import _unprefix_names, Pool
21 _unprefix_names(locals(), 'svn_fs_')
22 _unprefix_names(locals(), 'SVN_FS_')
23 del _unprefix_names
26 # Names that are not to be exported
27 import sys as _sys, os as _os, popen2 as _popen2, tempfile as _tempfile
28 import __builtin__
29 import svn.core as _svncore
32 def entries(root, path, pool=None):
33 "Call dir_entries returning a dictionary mappings names to IDs."
34 e = dir_entries(root, path, pool)
35 for name, entry in e.items():
36 e[name] = dirent_t_id_get(entry)
37 return e
40 class FileDiff:
41 def __init__(self, root1, path1, root2, path2, pool=None, diffoptions=[]):
42 assert path1 or path2
44 self.tempfile1 = None
45 self.tempfile2 = None
47 self.root1 = root1
48 self.path1 = path1
49 self.root2 = root2
50 self.path2 = path2
51 self.diffoptions = diffoptions
53 def either_binary(self):
54 "Return true if either of the files are binary."
55 if self.path1 is not None:
56 prop = node_prop(self.root1, self.path1, _svncore.SVN_PROP_MIME_TYPE)
57 if prop and _svncore.svn_mime_type_is_binary(prop):
58 return 1
59 if self.path2 is not None:
60 prop = node_prop(self.root2, self.path2, _svncore.SVN_PROP_MIME_TYPE)
61 if prop and _svncore.svn_mime_type_is_binary(prop):
62 return 1
63 return 0
65 def _dump_contents(self, file, root, path, pool=None):
66 fp = __builtin__.open(file, 'w+') # avoid namespace clash with
67 # trimmed-down svn_fs_open()
68 if path is not None:
69 stream = file_contents(root, path, pool)
70 try:
71 while 1:
72 chunk = _svncore.svn_stream_read(stream, _svncore.SVN_STREAM_CHUNK_SIZE)
73 if not chunk:
74 break
75 fp.write(chunk)
76 finally:
77 _svncore.svn_stream_close(stream)
78 fp.close()
81 def get_files(self):
82 if self.tempfile1:
83 # no need to do more. we ran this already.
84 return self.tempfile1, self.tempfile2
86 # Make tempfiles, and dump the file contents into those tempfiles.
87 self.tempfile1 = _tempfile.mktemp()
88 self.tempfile2 = _tempfile.mktemp()
90 self._dump_contents(self.tempfile1, self.root1, self.path1)
91 self._dump_contents(self.tempfile2, self.root2, self.path2)
93 return self.tempfile1, self.tempfile2
95 def get_pipe(self):
96 self.get_files()
98 # use an array for the command to avoid the shell and potential
99 # security exposures
100 cmd = ["diff"] \
101 + self.diffoptions \
102 + [self.tempfile1, self.tempfile2]
104 # the windows implementation of popen2 requires a string
105 if _sys.platform == "win32":
106 cmd = _svncore.argv_to_command_string(cmd)
108 # open the pipe, forget the end for writing to the child (we won't),
109 # and then return the file object for reading from the child.
110 fromchild, tochild = _popen2.popen2(cmd)
111 tochild.close()
112 return fromchild
114 def __del__(self):
115 # it seems that sometimes the files are deleted, so just ignore any
116 # failures trying to remove them
117 if self.tempfile1 is not None:
118 try:
119 _os.remove(self.tempfile1)
120 except OSError:
121 pass
122 if self.tempfile2 is not None:
123 try:
124 _os.remove(self.tempfile2)
125 except OSError:
126 pass