comparison foodtruck/scm/git.py @ 660:9f391ab1b4e0

admin: Add support for Git source-control.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 27 Feb 2016 22:00:52 -0800
parents
children 8d633ca59bc5
comparison
equal deleted inserted replaced
659:a77b4656c602 660:9f391ab1b4e0
1 import os
2 import logging
3 import tempfile
4 import subprocess
5 from .base import SourceControl, RepoStatus, _s
6
7
8 logger = logging.getLogger(__name__)
9
10
11 class GitSourceControl(SourceControl):
12 def __init__(self, root_dir, cfg):
13 super(GitSourceControl, self).__init__(root_dir, cfg)
14 self.git = cfg.get('exe', 'git')
15
16 def getStatus(self):
17 res = RepoStatus()
18 st_out = self._run('status', '-s')
19 print(st_out)
20 for line in st_out.split('\n'):
21 print(line)
22 if not line:
23 continue
24 if line.startswith('?? '):
25 path = line[3:].strip()
26 if path[-1] == '/':
27 import glob
28 res.new_files += [
29 f for f in glob.glob(path + '**', recursive=True)
30 if f[-1] != '/']
31 else:
32 res.new_files.append(path)
33 elif line.startswith(' M '):
34 res.edited_files.append(path[3:])
35 print(res.__dict__)
36 return res
37
38 def _doCommit(self, paths, message, author):
39 self._run('add', *paths)
40
41 # Create a temp file with the commit message.
42 f, temp = tempfile.mkstemp()
43 with os.fdopen(f, 'w') as fd:
44 fd.write(message)
45
46 # Commit and clean up the temp file.
47 try:
48 commit_args = list(paths) + ['-F', temp]
49 if author:
50 commit_args += ['--author="%s"' % author]
51 self._run('commit', *commit_args)
52 finally:
53 os.remove(temp)
54
55 def _run(self, cmd, *args, **kwargs):
56 exe = [self.git]
57 exe.append(cmd)
58 exe += args
59
60 logger.debug("Running Git: " + str(exe))
61 proc = subprocess.Popen(
62 exe, stdout=subprocess.PIPE, cwd=self.root_dir)
63 out, _ = proc.communicate()
64
65 encoded_out = _s(out)
66 return encoded_out
67