add "is_available" function to all version control modules
[translate_toolkit.git] / storage / versioncontrol / darcs.py
blobfcdb50fe5585f2fcf36c9fd774fdf805c3edb826
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright 2004-2007 Zuza Software Foundation
5 #
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 from translate.storage.versioncontrol import run_command
24 from translate.storage.versioncontrol import GenericRevisionControlSystem
27 def is_available():
28 """check if darcs is installed"""
29 exitcode, output_revert, error = run_command(["darcs", "--version"])
30 return exitcode == 0
33 class darcs(GenericRevisionControlSystem):
34 """Class to manage items under revision control of darcs."""
36 RCS_METADIR = "_darcs"
37 SCAN_PARENTS = True
39 def update(self, revision=None):
40 """Does a clean update of the given path
42 @param revision: ignored for darcs
43 """
44 # revert local changes (avoids conflicts)
45 command = ["darcs", "revert", "--repodir", self.root_dir,
46 "-a", self.location_rel]
47 exitcode, output_revert, error = run_command(command)
48 if exitcode != 0:
49 raise IOError("[Darcs] error running '%s': %s" % (command, error))
50 # pull new patches
51 command = ["darcs", "pull", "--repodir", self.root_dir, "-a"]
52 exitcode, output_pull, error = run_command(command)
53 if exitcode != 0:
54 raise IOError("[Darcs] error running '%s': %s" % (command, error))
55 return output_revert + output_pull
57 def commit(self, message=None):
58 """Commits the file and supplies the given commit message if present"""
59 if message is None:
60 message = ""
61 # set change message
62 command = ["darcs", "record", "-a", "--repodir", self.root_dir,
63 "--skip-long-comment", "-m", message, self.location_rel]
64 exitcode, output_record, error = run_command(command)
65 if exitcode != 0:
66 raise IOError("[Darcs] Error running darcs command '%s': %s" \
67 % (command, error))
68 # push changes
69 command = ["darcs", "push", "-a", "--repodir", self.root_dir]
70 exitcode, output_push, error = run_command(command)
71 if exitcode != 0:
72 raise IOError("[Darcs] Error running darcs command '%s': %s" \
73 % (command, error))
74 return output_record + output_push
76 def getcleanfile(self, revision=None):
77 """Get a clean version of a file from the darcs repository
79 @param revision: ignored for darcs
80 """
81 import os
82 filename = os.path.join(self.root_dir, self.RCS_METADIR, 'pristine',
83 self.location_rel)
84 try:
85 darcs_file = open(filename)
86 output = darcs_file.read()
87 darcs_file.close()
88 except IOError, error:
89 raise IOError("[Darcs] error reading original file '%s': %s" % \
90 (filename, error))
91 return output