don't chdir (which isn't cool when a cgi); use subprocess's cwd param instead

This commit is contained in:
Eric House 2014-04-30 07:08:59 -07:00
parent d91c307792
commit 6d194e5362

View file

@ -1,34 +1,47 @@
#!/usr/bin/python #!/usr/bin/python
import os, subprocess, sys import logging, os, subprocess, sys
import xwconfig
logging.basicConfig(level=logging.DEBUG
,format='%(asctime)s [[%(levelname)s]] %(message)s'
,datefmt='%d %b %y %H:%M'
,filename='/tmp/mygit.log')
# ,filemode='w')
class GitRepo: class GitRepo:
def __init__(self, path): def __init__(self, path):
startdir = os.getcwd() startPath = path
# print 'startdir:', startdir logging.debug( '__init__(%s)' % path )
gitdir = path + '/' + '.git' while True:
while not os.path.exists( gitdir ) or not os.path.isdir( gitdir ): gitdir = path + '/' + '.git'
if os.getcwd() == '/': self.__error( '.git never found' ) print 'looking for', gitdir
os.chdir('..') if os.path.exists( gitdir ) and os.path.isdir( gitdir ): break
gitdir = os.getcwd() + '/.git' elif path == '/':
self.gitRoot = os.path.dirname(gitdir) self.__error( '.git never found (starting from %s)' % startPath )
# print 'self.gitRoot:', self.gitRoot path = os.path.dirname(path)
print 'startdir:', path
self.gitRoot = path
print 'self.gitRoot:', self.gitRoot
def status(self): def status(self):
self.__chdir() process = subprocess.Popen(['git', 'status'], shell=False, \
process = subprocess.Popen(['git', 'status'], shell=False, stdout=subprocess.PIPE) stdout=subprocess.PIPE, cwd=self.gitRoot)
out, err = process.communicate() out, err = process.communicate()
if out: print out if out:
return out
def filesSame(self, path, rev1, rev2): def filesSame(self, path, rev1, rev2):
result = None result = None
self.__chdir()
path1 = self.__gitPath(path, rev1) path1 = self.__gitPath(path, rev1)
path2 = self.__gitPath(path, rev2) path2 = self.__gitPath(path, rev2)
if path1 and path2: if path1 and path2:
params = [ 'git', 'diff', '--no-ext-diff', rev1 + '..' + rev2, '--', path1 ] params = [ 'git', 'diff', '--no-ext-diff', rev1 + '..' + rev2, '--', path1 ]
process = subprocess.Popen(params, shell=False, stdout=subprocess.PIPE) process = subprocess.Popen(params, shell=False, stdout=subprocess.PIPE, \
cwd = self.gitRoot)
out, err = process.communicate() out, err = process.communicate()
if err: self.__error( 'git diff failed' ) if err: self.__error( 'git diff failed' )
result = 0 == len(out) result = 0 == len(out)
@ -43,26 +56,30 @@ class GitRepo:
path = self.__gitPath(path, rev) path = self.__gitPath(path, rev)
if path: if path:
params = [ 'git', 'show', rev + ':' + path ] params = [ 'git', 'show', rev + ':' + path ]
process = subprocess.Popen(params, shell=False, stdout=subprocess.PIPE) print params
process = subprocess.Popen(params, shell = False, \
stdout = subprocess.PIPE, \
cwd = self.gitRoot)
out, err = process.communicate() out, err = process.communicate()
if out: result = out if out: result = out
else:
print 'cat: no path'
return result return result
###################################################################### ######################################################################
# private methods # private methods
###################################################################### ######################################################################
def __chdir(self):
os.chdir(self.gitRoot)
def __error(self, msg): def __error(self, msg):
print msg print msg
sys.exit(1) sys.exit(1)
# return full_path if exists for rev and is unique, None otherwise # return full_path if exists for rev and is unique, None otherwise
def __gitPath(self, path, rev): def __gitPath(self, path, rev):
logging.debug('__gitPath(path=%s, rev=%s)' % (path, rev))
result = [] result = []
params = [ 'git', 'ls-tree', '-r', '--full-name', rev ] params = [ 'git', 'ls-tree', '-r', '--full-name', rev ]
process = subprocess.Popen(params, shell=False, stdout=subprocess.PIPE) process = subprocess.Popen(params, shell=False, stdout=subprocess.PIPE, \
cwd = self.gitRoot)
out = process.communicate()[0] out = process.communicate()[0]
for line in out.splitlines(): for line in out.splitlines():
line = line.split() line = line.split()
@ -72,6 +89,7 @@ class GitRepo:
result.append( line ) result.append( line )
if 1 == len(result): result = result[0] if 1 == len(result): result = result[0]
else: result = None else: result = None
logging.debug('__gitPath(%s)=>%s' % (path, result))
return result return result
# test function # test function
@ -85,10 +103,13 @@ def main():
'd463ea3f30909e85acdd7d0e24f00dea15fce81d' ) 'd463ea3f30909e85acdd7d0e24f00dea15fce81d' )
print repo.filesSame( file, 'a7c4730eb55f311e20cd406d4b2b819f0cd1edfe', \ print repo.filesSame( file, 'a7c4730eb55f311e20cd406d4b2b819f0cd1edfe', \
'd463ea3f30909e85acdd7d0e24f00dea15fce81d' ) 'd463ea3f30909e85acdd7d0e24f00dea15fce81d' )
cat = repo.cat( file, 'a7c4730eb55f311e20cd406d4b2b819f0cd1edfe' ) file = 'XWords4/archive/R.java'
cat = repo.cat( file, '33a83b0e2fcf062f4f640ccab0785b2d2b439542' )
if cat: if cat:
print 'first line of file %s:' % (file) print 'first line of file %s:' % (file)
print cat.splitlines()[0] print cat.splitlines()[0]
else:
print 'cat(%s) failed' % (file)
############################################################################## ##############################################################################
if __name__ == '__main__': if __name__ == '__main__':