Mercurial > public > mercurial-scm > hg
comparison mercurial/state.py @ 39451:5bfab9400daf
state: use our CBOR module
This was the last consumer of the vendored CBOR package in
core.
Differential Revision: https://phab.mercurial-scm.org/D4471
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 04 Sep 2018 10:22:42 -0700 |
parents | 6f67bfe4b82f |
children | 050ea8eb42a5 |
comparison
equal
deleted
inserted
replaced
39450:9f51fd22ed50 | 39451:5bfab9400daf |
---|---|
17 to serialize and deserialize data. | 17 to serialize and deserialize data. |
18 """ | 18 """ |
19 | 19 |
20 from __future__ import absolute_import | 20 from __future__ import absolute_import |
21 | 21 |
22 from .thirdparty import cbor | |
23 | |
24 from . import ( | 22 from . import ( |
25 error, | 23 error, |
26 util, | 24 util, |
25 ) | |
26 from .utils import ( | |
27 cborutil, | |
27 ) | 28 ) |
28 | 29 |
29 class cmdstate(object): | 30 class cmdstate(object): |
30 """a wrapper class to store the state of commands like `rebase`, `graft`, | 31 """a wrapper class to store the state of commands like `rebase`, `graft`, |
31 `histedit`, `shelve` etc. Extensions can also use this to write state files. | 32 `histedit`, `shelve` etc. Extensions can also use this to write state files. |
60 raise error.ProgrammingError("version of state file should be" | 61 raise error.ProgrammingError("version of state file should be" |
61 " an integer") | 62 " an integer") |
62 | 63 |
63 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: | 64 with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: |
64 fp.write('%d\n' % version) | 65 fp.write('%d\n' % version) |
65 cbor.dump(data, fp, canonical=True) | 66 for chunk in cborutil.streamencode(data): |
67 fp.write(chunk) | |
66 | 68 |
67 def _read(self): | 69 def _read(self): |
68 """reads the state file and returns a dictionary which contain | 70 """reads the state file and returns a dictionary which contain |
69 data in the same format as it was before storing""" | 71 data in the same format as it was before storing""" |
70 with self._repo.vfs(self.fname, 'rb') as fp: | 72 with self._repo.vfs(self.fname, 'rb') as fp: |
71 try: | 73 try: |
72 int(fp.readline()) | 74 int(fp.readline()) |
73 except ValueError: | 75 except ValueError: |
74 raise error.CorruptedState("unknown version of state file" | 76 raise error.CorruptedState("unknown version of state file" |
75 " found") | 77 " found") |
76 return cbor.load(fp) | 78 |
79 return cborutil.decodeall(fp.read())[0] | |
77 | 80 |
78 def delete(self): | 81 def delete(self): |
79 """drop the state file if exists""" | 82 """drop the state file if exists""" |
80 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) | 83 util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) |
81 | 84 |