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