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