Teach RevisionReader.get_content() to handle AppleSingle content.
[cvs2svn.git] / cvs2svn_lib / cvs_revision_manager.py
blobbdc36e41110f621951cfb3f0ff142d4d56df6625
1 # (Be in -*- python -*- mode.)
3 # ====================================================================
4 # Copyright (c) 2000-2008 CollabNet. All rights reserved.
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms
8 # are also available at http://subversion.tigris.org/license-1.html.
9 # If newer versions of this license are posted there, you may use a
10 # newer version instead, at your option.
12 # This software consists of voluntary contributions made by many
13 # individuals. For exact contribution history, see the revision
14 # history and logs, available at http://cvs2svn.tigris.org/.
15 # ====================================================================
17 """Access the CVS repository via CVS's 'cvs' command."""
20 from cvs2svn_lib.common import FatalError
21 from cvs2svn_lib.process import check_command_runs
22 from cvs2svn_lib.process import get_command_output
23 from cvs2svn_lib.process import CommandFailedException
24 from cvs2svn_lib.context import Ctx
25 from cvs2svn_lib.revision_manager import RevisionReader
26 from cvs2svn_lib.apple_single_filter import get_maybe_apple_single
29 class CVSRevisionReader(RevisionReader):
30 """A RevisionReader that reads the contents via CVS."""
32 # Different versions of CVS support different global options. Here
33 # are the global options that we try to use, in order of decreasing
34 # preference:
35 _possible_global_options = [
36 ['-Q', '-R', '-f'],
37 ['-Q', '-R'],
38 ['-Q', '-f'],
39 ['-Q'],
40 ['-q', '-R', '-f'],
41 ['-q', '-R'],
42 ['-q', '-f'],
43 ['-q'],
46 def __init__(self, cvs_executable, global_options=None):
47 """Initialize a CVSRevisionReader.
49 CVS_EXECUTABLE is the CVS command (possibly including the full
50 path to the executable; otherwise it is sought in the $PATH).
51 GLOBAL_ARGUMENTS, if specified, should be a list of global options
52 that are passed to the CVS command before the subcommand. If
53 GLOBAL_ARGUMENTS is not specified, then each of the possibilities
54 listed in _possible_global_options is checked in order until one
55 is found that runs successfully and without any output to stderr."""
57 self.cvs_executable = cvs_executable
59 if global_options is None:
60 for global_options in self._possible_global_options:
61 try:
62 self._check_cvs_runs(global_options)
63 except CommandFailedException, e:
64 pass
65 else:
66 break
67 else:
68 raise FatalError(
69 '%s\n'
70 'Please check that cvs is installed and in your PATH.' % (e,)
72 else:
73 try:
74 self._check_cvs_runs(global_options)
75 except CommandFailedException, e:
76 raise FatalError(
77 '%s\n'
78 'Please check that cvs is installed and in your PATH and that\n'
79 'the global options that you specified (%r) are correct.'
80 % (e, global_options,)
83 # The global options were OK; use them for all CVS invocations.
84 self.global_options = global_options
86 def _check_cvs_runs(self, global_options):
87 """Check that CVS can be started.
89 Try running 'cvs --version' with the current setting for
90 self.cvs_executable and the specified global_options. If not
91 successful, raise a CommandFailedException."""
93 check_command_runs(
94 [self.cvs_executable] + global_options + ['--version'],
95 self.cvs_executable,
98 def get_content(self, cvs_rev):
99 project = cvs_rev.cvs_file.project
100 pipe_cmd = [
101 self.cvs_executable
102 ] + self.global_options + [
103 '-d', ':local:' + project.cvs_repository_root,
104 'co',
105 '-r' + cvs_rev.rev,
106 '-p'
108 if cvs_rev.get_property('_keyword_handling') == 'collapsed':
109 pipe_cmd.append('-kk')
110 pipe_cmd.append(project.cvs_module + cvs_rev.cvs_path)
111 data = get_command_output(pipe_cmd)
113 if Ctx().decode_apple_single:
114 # Insert a filter to decode any files that are in AppleSingle
115 # format:
116 data = get_maybe_apple_single(data)
118 return data