Mercurial > piecrust2
comparison foodtruck/scm/mercurial.py @ 587:d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 16 Jan 2016 14:24:35 -0800 |
parents | |
children | 3cec8634209a |
comparison
equal
deleted
inserted
replaced
586:59268b4d8c71 | 587:d4a01a023998 |
---|---|
1 import os | |
2 import logging | |
3 import tempfile | |
4 import subprocess | |
5 from .base import SourceControl, RepoStatus | |
6 | |
7 | |
8 logger = logging.getLogger(__name__) | |
9 | |
10 | |
11 def _s(strs): | |
12 """ Convert a byte array to string using UTF8 encoding. """ | |
13 if strs is None: | |
14 return None | |
15 assert isinstance(strs, bytes) | |
16 return strs.decode('utf8') | |
17 | |
18 | |
19 class MercurialSourceControl(SourceControl): | |
20 def __init__(self, root_dir): | |
21 super(MercurialSourceControl, self).__init__(root_dir) | |
22 self.hg = 'hg' | |
23 | |
24 def getStatus(self): | |
25 res = RepoStatus() | |
26 st_out = self._run('status') | |
27 for line in st_out.split('\n'): | |
28 if len(line) == 0: | |
29 continue | |
30 if line[0] == '?' or line[0] == 'A': | |
31 res.new_files.append(line[2:]) | |
32 elif line[0] == 'M': | |
33 res.edited_files.append(line[2:]) | |
34 return res | |
35 | |
36 def commit(self, paths, author, message): | |
37 if not message: | |
38 raise ValueError("No commit message specified.") | |
39 | |
40 # Check if any of those paths needs to be added. | |
41 st_out = self._run('status', *paths) | |
42 add_paths = [] | |
43 for line in st_out.splitlines(): | |
44 if line[0] == '?': | |
45 add_paths.append(line[2:]) | |
46 if len(add_paths) > 0: | |
47 self._run('add', *paths) | |
48 | |
49 # Create a temp file with the commit message. | |
50 f, temp = tempfile.mkstemp() | |
51 with os.fdopen(f, 'w') as fd: | |
52 fd.write(message) | |
53 | |
54 # Commit and clean up the temp file. | |
55 try: | |
56 commit_args = list(paths) + ['-l', temp] | |
57 if author: | |
58 commit_args += ['-u', author] | |
59 self._run('commit', *commit_args) | |
60 finally: | |
61 os.remove(temp) | |
62 | |
63 def _run(self, cmd, *args, **kwargs): | |
64 exe = [self.hg, '-R', self.root_dir] | |
65 exe.append(cmd) | |
66 exe += args | |
67 logger.debug("Running Mercurial: " + str(exe)) | |
68 out = subprocess.check_output(exe) | |
69 encoded_out = _s(out) | |
70 return encoded_out | |
71 |