translate.storage.versioncontrol.bzr

Package translate :: Package storage :: Package versioncontrol :: Module bzr |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.versioncontrol.bzr

      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.
      7  #
      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.
     12  #
     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.
     17  #
     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
     21
     22
     23  import translate.storage.versioncontrol
     24  from translate.storage.versioncontrol import run_command
     25  from translate.storage.versioncontrol import GenericRevisionControlSystem
     26
     27
     28 -def is_available():
     29      """check if bzr is installed"""
     30      exitcode, output, error = run_command(["bzr", "version"])
     31      return exitcode == 0
     32
     33 -def get_version():
     34      """return a tuple of (major, minor) for the installed bazaar client"""
     35      import re
     36      command = ["bzr", "--version"]
     37      exitcode, output, error = run_command(command)
     38      if exitcode == 0:
     39          version_line = output.splitlines()[0]
     40          version_match = re.search(r"\d+\.\d+", version_line)
     41          if version_match:
     42              major, minor = version_match.group().split(".")
     43              if (major.isdigit() and minor.isdigit()):
     44                  return (int(major), int(minor))
     45      # if anything broke before, then we return the invalid version number
     46      return (0, 0)
     47
     48
     49 -class bzr(GenericRevisionControlSystem):
     50      """Class to manage items under revision control of bzr."""
     51
     52      RCS_METADIR = ".bzr"
     53      SCAN_PARENTS = True
     54
     55 -    def update(self, revision=None):
     56          """Does a clean update of the given path"""
     57          # bzr revert
     58          command = ["bzr", "revert", self.location_abs]
     59          exitcode, output_revert, error = run_command(command)
     60          if exitcode != 0:
     61              raise IOError("[BZR] revert of '%s' failed: %s" \
     62                      % (self.location_abs, error))
     63          # bzr pull
     64          command = ["bzr", "pull"]
     65          exitcode, output_pull, error = run_command(command)
     66          if exitcode != 0:
     67              raise IOError("[BZR] pull of '%s' failed: %s" \
     68                      % (self.location_abs, error))
     69          return output_revert + output_pull
     70
     71 -    def commit(self, message=None, author=None):
     72          """Commits the file and supplies the given commit message if present"""
     73          # bzr commit
     74          command = ["bzr", "commit"]
     75          if message:
     76              command.extend(["-m", message])
     77          # the "--author" argument is supported since bzr v0.91rc1
     78          if author and (get_version() >= (0, 91)):
     79              command.extend(["--author", author])
     80          # the filename is the last argument
     81          command.append(self.location_abs)
     82          exitcode, output_commit, error = run_command(command)
     83          if exitcode != 0:
     84              raise IOError("[BZR] commit of '%s' failed: %s" \
     85                      % (self.location_abs, error))
     86          # bzr push
     87          command = ["bzr", "push"]
     88          exitcode, output_push, error = run_command(command)
     89          if exitcode != 0:
     90              raise IOError("[BZR] push of '%s' failed: %s" \
     91                      % (self.location_abs, error))
     92          return output_commit + output_push
     93
     94 -    def getcleanfile(self, revision=None):
     95          """Get a clean version of a file from the bzr repository"""
     96          # bzr cat
     97          command = ["bzr", "cat", self.location_abs]
     98          exitcode, output, error = run_command(command)
     99          if exitcode != 0:
    100              raise IOError("[BZR] cat failed for '%s': %s" \
    101                      % (self.location_abs, error))
    102          return output
    103