File: vcs.py

package info (click to toggle)
espresso 6.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 311,068 kB
  • sloc: f90: 447,429; ansic: 52,566; sh: 40,631; xml: 37,561; tcl: 20,077; lisp: 5,923; makefile: 4,503; python: 4,379; perl: 1,219; cpp: 761; fortran: 618; java: 568; awk: 128
file content (49 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'''
testcode2.vcs
-------------

Lightweight access to required version control system functions.

:copyright: (c) 2012 James Spencer.
:license: modified BSD; see LICENSE for more details.
'''

import os
import subprocess

class VCSRepository(object):
    '''Handle information about a version control repository.

vcs: version control system used.  Currently git, mercurial and subversion are supported.
repository: (local) directory containing a checked-out version of the repository.
remote_repository: remote location of the repository.
'''
    def __init__(self, vcs, repository, remote_repository=None):
        if vcs in ['svn', 'git', 'hg']:
            self.vcs = vcs
        else:
            self.vcs = None
        self.repository = repository
        if remote_repository:
            self.remote_repository = remote_repository

    def get_code_id(self):
        '''Return the id (i.e. version number or hash) of the VCS repository.'''
        old_dir = os.getcwd()
        os.chdir(self.repository)
        code_id = 'UNKNOWN'
        id_popen = None
        if self.vcs == 'svn':
            id_popen = subprocess.Popen(['svnversion', '.'],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        elif self.vcs == 'git':
            id_popen = subprocess.Popen(['git', 'rev-parse', '--short', 'HEAD'],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        elif self.vcs == 'hg':
            id_popen = subprocess.Popen(['hg', 'id', '-i'],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if id_popen:
            id_popen.wait()
            code_id = id_popen.communicate()[0].decode('utf-8').strip()
        os.chdir(old_dir)
        return (code_id)