forked from charleso/git-cc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
118 lines (98 loc) · 3.04 KB
/
common.py
File metadata and controls
118 lines (98 loc) · 3.04 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from distutils import __version__
v30 = __version__.find("3.") == 0
from subprocess import Popen, PIPE
import os, sys
from os.path import join, exists, abspath, dirname
if v30:
from configparser import SafeConfigParser
else:
from ConfigParser import SafeConfigParser
CC_TAG = 'clearcase'
CI_TAG = 'clearcase_ci'
CFG_CC = 'clearcase'
CC_DIR = None
ENCODING = sys.stdin.encoding
def fail(string):
print(string)
sys.exit(2)
def doStash(f, stash):
if(stash):
git_exec(['stash'])
f()
if(stash):
git_exec(['stash', 'pop'])
def debug(string):
if DEBUG:
print(string)
def git_exec(cmd, **args):
return popen('git', cmd, GIT_DIR, **args)
def cc_exec(cmd):
return popen('cleartool', cmd, CC_DIR)
def popen(exe, cmd, cwd, env=None, decode=True):
cmd.insert(0, exe)
if DEBUG:
debug('> ' + ' '.join(cmd))
input = Popen(cmd, cwd=cwd, stdout=PIPE, env=env).stdout.read()
return input if not decode else input.decode(ENCODING)
def tag(tag, id="HEAD"):
git_exec(['tag', '-f', tag, id])
def reset(tag=CC_TAG):
git_exec(['reset', '--hard', tag])
def getBlob(sha, file):
return git_exec(['ls-tree', '-z', sha, file]).split(' ')[2].split('\t')[0]
def gitDir():
def findGitDir(dir):
if not exists(dir) or dirname(dir) == dir:
return '.'
if exists(join(dir, '.git')):
return dir
return findGitDir(dirname(dir))
return findGitDir(abspath('.'))
class GitConfigParser():
section = 'gitcc'
def __init__(self):
self.file = join(GIT_DIR, '.git', 'gitcc')
self.parser = SafeConfigParser();
self.parser.add_section(self.section)
def set(self, name, value):
self.parser.set(self.section, name, value)
def read(self):
self.parser.read(self.file)
def write(self):
self.parser.write(open(self.file, 'w'))
def get(self, name, default=None):
if not self.parser.has_option(self.section, name):
return default
return self.parser.get(self.section, name)
def getList(self, name, default=None):
return self.get(name, default).split('|')
def checkPristine():
if not CC_DIR:
fail('No .git directory found')
if(len(git_exec(['ls-files', '--modified']).splitlines()) > 0):
fail('There are uncommitted files in your git directory')
def write(file, blob):
_write(file, blob)
def _write(file, blob):
f = open(file, 'wb')
f.write(blob)
f.close()
def mkdirs(file):
dir = dirname(file)
if not exists(dir):
os.makedirs(dir)
def removeFile(file):
if exists(file):
os.remove(file)
def buildPath(seq):
# If running in a unix-like environment (including cygwin) use '/' as a path separator, otherwise use '\'
if "TERM" in os.environ.keys():
return '/'.join(seq).replace("\\","/")
else:
return '\\'.join(seq).replace("/","\\")
GIT_DIR = gitDir()
cfg = GitConfigParser()
if exists(join(GIT_DIR, '.git')):
cfg.read()
CC_DIR = cfg.get(CFG_CC)
DEBUG = cfg.get('debug', True)