comparison piecrust/admin/scm/git.py @ 778:5e91bc0e3b4d

internal: Move admin panel code into the piecrust package.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Jul 2016 15:02:24 +0200
parents foodtruck/scm/git.py@8d633ca59bc5
children 82509bce94ca
comparison
equal deleted inserted replaced
777:8d633ca59bc5 778:5e91bc0e3b4d
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 for line in st_out.split('\n'):
20 if not line:
21 continue
22 if line.startswith('?? '):
23 path = line[3:].strip()
24 if path[-1] == '/':
25 import glob
26 res.new_files += [
27 f for f in glob.glob(path + '**', recursive=True)
28 if f[-1] != '/']
29 else:
30 res.new_files.append(path)
31 elif line.startswith(' M '):
32 res.edited_files.append(line[3:])
33 return res
34
35 def _doCommit(self, paths, message, author):
36 self._run('add', *paths)
37
38 # Create a temp file with the commit message.
39 f, temp = tempfile.mkstemp()
40 with os.fdopen(f, 'w') as fd:
41 fd.write(message)
42
43 # Commit and clean up the temp file.
44 try:
45 commit_args = list(paths) + ['-F', temp]
46 if author:
47 commit_args += ['--author="%s"' % author]
48 self._run('commit', *commit_args)
49 finally:
50 os.remove(temp)
51
52 def _run(self, cmd, *args, **kwargs):
53 exe = [self.git]
54 exe.append(cmd)
55 exe += args
56
57 logger.debug("Running Git: " + str(exe))
58 proc = subprocess.Popen(
59 exe, stdout=subprocess.PIPE, cwd=self.root_dir)
60 out, _ = proc.communicate()
61
62 encoded_out = _s(out)
63 return encoded_out
64