summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2010-08-25 15:53:58 +0200
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2010-08-25 15:53:58 +0200
commiteb4a7a21de9091cedec29ef141ed70ce78add0d7 (patch)
treec6ef4bb234797b29179c6bafb07da36a3110baf8
parent0b2bafabf371c62e202caaa9d6040987c8987438 (diff)
parentee56220ef01a184d9aba727d44826f2cdedf95b1 (diff)
downloadlogilab-common-eb4a7a21de9091cedec29ef141ed70ce78add0d7.tar.gz
backport stable
-rw-r--r--clcommands.py2
-rw-r--r--hg.py98
-rw-r--r--optparser.py4
-rw-r--r--textutils.py10
4 files changed, 113 insertions, 1 deletions
diff --git a/clcommands.py b/clcommands.py
index 1c312a2..02e7deb 100644
--- a/clcommands.py
+++ b/clcommands.py
@@ -196,8 +196,8 @@ class Command(Configuration):
if rcfile:
self.load_file_configuration(rcfile)
args = self.load_command_line_configuration(args)
- self.check_args(args)
try:
+ self.check_args(args)
self.run(args)
except KeyboardInterrupt:
print 'interrupted'
diff --git a/hg.py b/hg.py
new file mode 100644
index 0000000..406c856
--- /dev/null
+++ b/hg.py
@@ -0,0 +1,98 @@
+# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
+#
+# This file is part of logilab-common.
+#
+# logilab-common is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Lesser General Public License as published by the Free
+# Software Foundation, either version 2.1 of the License, or (at your option) any
+# later version.
+#
+# logilab-common is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Lesser General Public License along
+# with logilab-common. If not, see <http://www.gnu.org/licenses/>.
+"""mercurial utilities (mercurial should be installed)"""
+
+__docformat__ = "restructuredtext en"
+
+import os.path as osp
+
+try:
+ from mercurial.error import RepoError
+ from mercurial.__version__ import version as hg_version
+except ImportError:
+ from mercurial.repo import RepoError
+ from mercurial.version import get_version
+ hg_version = get_version()
+
+from mercurial.hg import repository as Repository
+from mercurial.ui import ui as Ui
+from mercurial.node import short
+try:
+ # mercurial >= 1.2 (?)
+ from mercurial.cmdutil import walkchangerevs
+except ImportError, ex:
+ from mercurial.commands import walkchangerevs
+try:
+ # mercurial >= 1.1 (.1?)
+ from mercurial.util import cachefunc
+except ImportError, ex:
+ def cachefunc(func):
+ return func
+try:
+ # mercurial >= 1.3.1
+ from mercurial import encoding
+ _encoding = encoding.encoding
+except ImportError:
+ try:
+ from mercurial.util import _encoding
+ except ImportError:
+ import locale
+ # stay compatible with mercurial 0.9.1 (etch debian release)
+ # (borrowed from mercurial.util 1.1.2)
+ try:
+ _encoding = os.environ.get("HGENCODING")
+ if sys.platform == 'darwin' and not _encoding:
+ # On darwin, getpreferredencoding ignores the locale environment and
+ # always returns mac-roman. We override this if the environment is
+ # not C (has been customized by the user).
+ locale.setlocale(locale.LC_CTYPE, '')
+ _encoding = locale.getlocale()[1]
+ if not _encoding:
+ _encoding = locale.getpreferredencoding() or 'ascii'
+ except locale.Error:
+ _encoding = 'ascii'
+try:
+ # demandimport causes problems when activated, ensure it isn't
+ # XXX put this in apycot where the pb has been noticed?
+ from mercurial import demandimport
+ demandimport.disable()
+except:
+ pass
+
+Ui.warn = lambda *args, **kwargs: 0 # make it quiet
+
+def find_repository(path):
+ """returns <path>'s mercurial repository
+
+ None if <path> is not under hg control
+ """
+ path = osp.realpath(osp.abspath(path))
+ while not osp.isdir(osp.join(path, ".hg")):
+ oldpath = path
+ path = osp.dirname(path)
+ if path == oldpath:
+ return None
+ return path
+
+
+def get_repository(path):
+ """Simple function that open a hg repository"""
+ repopath = find_repository(path)
+ if repopath is None:
+ raise RuntimeError('no repository found in %s' % osp.abspath(path))
+ return Repository(Ui(), path=repopath)
diff --git a/optparser.py b/optparser.py
index 123e6a6..e149e40 100644
--- a/optparser.py
+++ b/optparser.py
@@ -36,6 +36,10 @@ __docformat__ = "restructuredtext en"
import sys
import optparse
+from warnings import warn
+warn('lgc.optparser module is deprecated, use lgc.clcommands instead', DeprecationWarning,
+ stacklevel=2)
+
class OptionParser(optparse.OptionParser):
def __init__(self, *args, **kwargs):
diff --git a/textutils.py b/textutils.py
index 16926f0..db69d3b 100644
--- a/textutils.py
+++ b/textutils.py
@@ -45,6 +45,7 @@ __docformat__ = "restructuredtext en"
import sys
import re
+import os.path as osp
from unicodedata import normalize as _uninormalize
try:
from os import linesep
@@ -251,6 +252,15 @@ def splitstrip(string, sep=','):
get_csv = deprecated()(splitstrip)
+def split_url_or_path(url_or_path):
+ """return the latest component of a string containing either an url of the
+ form <scheme>://<path> or a local file system path
+ """
+ if '://' in url_or_path:
+ return url_or_path.rstrip('/').rsplit('/', 1)
+ return osp.split(url_or_path.rstrip(osp.sep))
+
+
def text_to_dict(text):
"""parse multilines text containing simple 'key=value' lines and return a
dict of {'key': 'value'}. When the same key is encountered multiple time,