~ubuntu-branches/debian/experimental/spyder/experimental

« back to all changes in this revision

Viewing changes to spyderlib/utils/vcs.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2013-01-20 12:19:54 UTC
  • mfrom: (1.1.16)
  • Revision ID: package-import@ubuntu.com-20130120121954-1jt1xa924bshhvh0
Tags: 2.2.0~beta1+dfsg-2
fix typo ipython-qtconsol -> ipython-qtconsole

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright © 2011 Pierre Raybaut
 
4
# Licensed under the terms of the MIT License
 
5
# (see spyderlib/__init__.py for details)
 
6
 
 
7
"""Utilities for version control systems"""
 
8
 
 
9
import os.path as osp
 
10
import subprocess
 
11
 
 
12
# Local imports
 
13
from spyderlib.baseconfig import _
 
14
from spyderlib.utils import programs
 
15
from spyderlib.utils.misc import abspardir
 
16
 
 
17
 
 
18
VCS_INFOS = {
 
19
             '.hg':  dict(name="Mercurial",
 
20
                          commit=( ('thg', ['commit']),
 
21
                                   ('hgtk', ['commit']) ),
 
22
                          browse=( ('thg', ['log']),
 
23
                                   ('hgtk', ['log']) )
 
24
                          ),
 
25
             '.git': dict(name="git",
 
26
                          commit=( ('git', ['gui']), ),
 
27
                          browse=( ('gitk', []), )
 
28
                          ),
 
29
             }
 
30
 
 
31
 
 
32
def get_vcs_infos(path):
 
33
    """Return VCS infos if path is a supported VCS repository"""
 
34
    for dirname, infos in VCS_INFOS.iteritems():
 
35
        vcs_path = osp.join(path, dirname)
 
36
        if osp.isdir(vcs_path):
 
37
            return infos
 
38
 
 
39
 
 
40
def get_vcs_root(path):
 
41
    """Return VCS root directory path
 
42
    Return None if path is not within a supported VCS repository"""
 
43
    previous_path = path
 
44
    while get_vcs_infos(path) is None:
 
45
        path = abspardir(path)
 
46
        if path == previous_path:
 
47
            return
 
48
        else:
 
49
            previous_path = path
 
50
    return osp.abspath(path)
 
51
 
 
52
 
 
53
def is_vcs_repository(path):
 
54
    """Return True if path is a supported VCS repository"""
 
55
    return get_vcs_root(path) is not None
 
56
 
 
57
 
 
58
def run_vcs_tool(path, tool):
 
59
    """If path is a valid VCS repository, run the corresponding VCS tool
 
60
    Supported VCS tools: 'commit', 'browse'
 
61
    Return False if the VCS tool is not installed"""
 
62
    infos = get_vcs_infos(get_vcs_root(path))
 
63
    for name, args in infos[tool]:
 
64
        if programs.find_program(name):
 
65
            programs.run_program(name, args, cwd=path)
 
66
            return
 
67
    else:
 
68
        raise RuntimeError(_("For %s support, please install one of the<br/> "
 
69
                             "following tools:<br/><br/>  %s")
 
70
                           % (infos['name'],
 
71
                              ', '.join([name for name,cmd in infos['commit']])
 
72
                              ))
 
73
 
 
74
 
 
75
def is_hg_installed():
 
76
    """Return True if Mercurial is installed"""
 
77
    return programs.find_program('hg') is not None
 
78
 
 
79
 
 
80
def get_hg_revision(repopath):
 
81
    """Return Mercurial revision for the repository located at repopath
 
82
       Result is a tuple (global, local, branch), with None values on error
 
83
       For example:
 
84
           >>> get_hg_revision(".")
 
85
           ('eba7273c69df+', '2015+', 'default')
 
86
    """
 
87
    try:
 
88
        hg = programs.find_program('hg')
 
89
        assert hg
 
90
        output = subprocess.check_output([hg, 'id', '-nib', repopath])
 
91
        # output is now: ('eba7273c69df+ 2015+ default\n', None)
 
92
        return tuple(output.strip().split())
 
93
    except (subprocess.CalledProcessError, AssertionError, AttributeError):
 
94
        # print("Error: Failed to get revision number from Mercurial - %s" % exc)
 
95
        return (None, None, None)
 
96
 
 
97
 
 
98
if __name__ == '__main__':
 
99
    print get_vcs_root(osp.dirname(__file__))
 
100
    print get_vcs_root(r'D:\Python\ipython\IPython\frontend')
 
101
    #run_vcs_tool(r'D:\Python\userconfig\userconfig', 'commit')
 
102
    print get_hg_revision(osp.dirname(__file__)+"/../..")
 
103
    print get_hg_revision('/')