annotate foodtruck/scm/git.py @ 777:8d633ca59bc5

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