comparison piecrust/admin/scm/mercurial.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/mercurial.py@a77b4656c602
children
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 MercurialSourceControl(SourceControl):
12 def __init__(self, root_dir, cfg):
13 super(MercurialSourceControl, self).__init__(root_dir, cfg)
14 self.hg = cfg.get('exe', 'hg')
15
16 def getStatus(self):
17 res = RepoStatus()
18 st_out = self._run('status')
19 for line in st_out.split('\n'):
20 if len(line) == 0:
21 continue
22 if line[0] == '?' or line[0] == 'A':
23 res.new_files.append(line[2:])
24 elif line[0] == 'M':
25 res.edited_files.append(line[2:])
26 return res
27
28 def _doCommit(self, paths, message, author):
29 # Check if any of those paths needs to be added.
30 st_out = self._run('status', *paths)
31 add_paths = []
32 for line in st_out.splitlines():
33 if line[0] == '?':
34 add_paths.append(line[2:])
35 if len(add_paths) > 0:
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) + ['-l', temp]
46 if author:
47 commit_args += ['-u', author]
48 self._run('commit', *commit_args)
49 finally:
50 os.remove(temp)
51
52 def _run(self, cmd, *args, **kwargs):
53 exe = [self.hg, '-R', self.root_dir]
54 exe.append(cmd)
55 exe += args
56
57 env = dict(os.environ)
58 env['HGPLAIN'] = 'True'
59
60 logger.debug("Running Mercurial: " + str(exe))
61 proc = subprocess.Popen(exe, stdout=subprocess.PIPE, env=env)
62 out, _ = proc.communicate()
63
64 encoded_out = _s(out)
65 return encoded_out
66