Package translate :: Package storage :: Package versioncontrol :: Module hg

[hide private]
[frames] | no frames]

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

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004-2008 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  from translate.storage.versioncontrol import GenericRevisionControlSystem
     24  from translate.storage.versioncontrol import run_command
     25
     26
     27 -def is_available():
     28      """check if hg is installed"""
     29      exitcode, output, error = run_command(["hg", "--version"])
     30      return exitcode == 0
     31
     32
     33 -def get_version():
     34      """return a tuple of (major, minor) for the installed bazaar client"""
     35      import re
     36      command = ["hg", "--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 hg(GenericRevisionControlSystem):
     50      """Class to manage items under revision control of mercurial."""
     51
     52      RCS_METADIR = ".hg"
     53      SCAN_PARENTS = True
     54
     55 -    def update(self, revision=None):
     56          """Does a clean update of the given path
     57
     58          @param revision: ignored for hg
     59          """
     60          # revert local changes (avoids conflicts)
     61          command = ["hg", "-R", self.root_dir, "revert",
     62                  "--all", self.location_abs]
     63          exitcode, output_revert, error = run_command(command)
     64          if exitcode != 0:
     65              raise IOError("[Mercurial] error running '%s': %s" % (command, error))
     66          # pull new patches
     67          command = ["hg", "-R", self.root_dir, "pull"]
     68          exitcode, output_pull, error = run_command(command)
     69          if exitcode != 0:
     70              raise IOError("[Mercurial] error running '%s': %s" % (command, error))
     71          # update working directory
     72          command = ["hg", "-R", self.root_dir, "update"]
     73          exitcode, output_update, error = run_command(command)
     74          if exitcode != 0:
     75              raise IOError("[Mercurial] error running '%s': %s" % (command, error))
     76          return output_revert + output_pull + output_update
     77
     78 -    def commit(self, message=None, author=None):
     79          """Commits the file and supplies the given commit message if present"""
     80          if message is None:
     81              message = ""
     82          # commit changes
     83          command = ["hg", "-R", self.root_dir, "commit", "-m", message]
     84          # add the 'author' argument, if it was given (only supported since v1.0)
     85          if author and (get_version() >= (1, 0)):
     86              command.extend(["--user", author])
     87          # the location is the last argument
     88          command.append(self.location_abs)
     89          exitcode, output_commit, error = run_command(command)
     90          if exitcode != 0:
     91              raise IOError("[Mercurial] Error running '%s': %s" \
     92                      % (command, error))
     93          # push changes
     94          command = ["hg", "-R", self.root_dir, "push"]
     95          exitcode, output_push, error = run_command(command)
     96          if exitcode != 0:
     97              raise IOError("[Mercurial] Error running '%s': %s" \
     98                      % (command, error))
     99          return output_commit + output_push
    100
    101 -    def getcleanfile(self, revision=None):
    102          """Get a clean version of a file from the hg repository"""
    103          # run hg cat
    104          command = ["hg", "-R", self.root_dir, "cat",
    105                  self.location_abs]
    106          exitcode, output, error = run_command(command)
    107          if exitcode != 0:
    108              raise IOError("[Mercurial] Error running '%s': %s" \
    109                      % (command, error))
    110          return output
    111