mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-16 15:41:16 +01:00
don't chdir (which isn't cool when a cgi); use subprocess's cwd param instead
This commit is contained in:
parent
d91c307792
commit
6d194e5362
1 changed files with 42 additions and 21 deletions
|
@ -1,34 +1,47 @@
|
|||
#!/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:
|
||||
|
||||
def __init__(self, path):
|
||||
startdir = os.getcwd()
|
||||
# print 'startdir:', startdir
|
||||
gitdir = path + '/' + '.git'
|
||||
while not os.path.exists( gitdir ) or not os.path.isdir( gitdir ):
|
||||
if os.getcwd() == '/': self.__error( '.git never found' )
|
||||
os.chdir('..')
|
||||
gitdir = os.getcwd() + '/.git'
|
||||
self.gitRoot = os.path.dirname(gitdir)
|
||||
# print 'self.gitRoot:', self.gitRoot
|
||||
startPath = path
|
||||
logging.debug( '__init__(%s)' % path )
|
||||
while True:
|
||||
gitdir = path + '/' + '.git'
|
||||
print 'looking for', gitdir
|
||||
if os.path.exists( gitdir ) and os.path.isdir( gitdir ): break
|
||||
elif path == '/':
|
||||
self.__error( '.git never found (starting from %s)' % startPath )
|
||||
path = os.path.dirname(path)
|
||||
|
||||
print 'startdir:', path
|
||||
self.gitRoot = path
|
||||
print 'self.gitRoot:', self.gitRoot
|
||||
|
||||
def status(self):
|
||||
self.__chdir()
|
||||
process = subprocess.Popen(['git', 'status'], shell=False, stdout=subprocess.PIPE)
|
||||
process = subprocess.Popen(['git', 'status'], shell=False, \
|
||||
stdout=subprocess.PIPE, cwd=self.gitRoot)
|
||||
out, err = process.communicate()
|
||||
if out: print out
|
||||
if out:
|
||||
return out
|
||||
|
||||
def filesSame(self, path, rev1, rev2):
|
||||
result = None
|
||||
self.__chdir()
|
||||
path1 = self.__gitPath(path, rev1)
|
||||
path2 = self.__gitPath(path, rev2)
|
||||
if path1 and path2:
|
||||
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()
|
||||
if err: self.__error( 'git diff failed' )
|
||||
result = 0 == len(out)
|
||||
|
@ -43,26 +56,30 @@ class GitRepo:
|
|||
path = self.__gitPath(path, rev)
|
||||
if 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()
|
||||
if out: result = out
|
||||
else:
|
||||
print 'cat: no path'
|
||||
return result
|
||||
|
||||
######################################################################
|
||||
# private methods
|
||||
######################################################################
|
||||
def __chdir(self):
|
||||
os.chdir(self.gitRoot)
|
||||
|
||||
def __error(self, msg):
|
||||
print msg
|
||||
sys.exit(1)
|
||||
|
||||
# return full_path if exists for rev and is unique, None otherwise
|
||||
def __gitPath(self, path, rev):
|
||||
logging.debug('__gitPath(path=%s, rev=%s)' % (path, rev))
|
||||
result = []
|
||||
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]
|
||||
for line in out.splitlines():
|
||||
line = line.split()
|
||||
|
@ -72,6 +89,7 @@ class GitRepo:
|
|||
result.append( line )
|
||||
if 1 == len(result): result = result[0]
|
||||
else: result = None
|
||||
logging.debug('__gitPath(%s)=>%s' % (path, result))
|
||||
return result
|
||||
|
||||
# test function
|
||||
|
@ -85,10 +103,13 @@ def main():
|
|||
'd463ea3f30909e85acdd7d0e24f00dea15fce81d' )
|
||||
print repo.filesSame( file, 'a7c4730eb55f311e20cd406d4b2b819f0cd1edfe', \
|
||||
'd463ea3f30909e85acdd7d0e24f00dea15fce81d' )
|
||||
cat = repo.cat( file, 'a7c4730eb55f311e20cd406d4b2b819f0cd1edfe' )
|
||||
file = 'XWords4/archive/R.java'
|
||||
cat = repo.cat( file, '33a83b0e2fcf062f4f640ccab0785b2d2b439542' )
|
||||
if cat:
|
||||
print 'first line of file %s:' % (file)
|
||||
print cat.splitlines()[0]
|
||||
else:
|
||||
print 'cat(%s) failed' % (file)
|
||||
|
||||
##############################################################################
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in a new issue