Show More
@@ -1,87 +1,87 b'' | |||||
1 | # state.py - writing and reading state files in Mercurial |
|
1 | # state.py - writing and reading state files in Mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2018 Pulkit Goyal <pulkitmgoyal@gmail.com> |
|
3 | # Copyright 2018 Pulkit Goyal <pulkitmgoyal@gmail.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | """ |
|
8 | """ | |
9 | This file contains class to wrap the state for commands and other |
|
9 | This file contains class to wrap the state for commands and other | |
10 | related logic. |
|
10 | related logic. | |
11 |
|
11 | |||
12 | All the data related to the command state is stored as dictionary in the object. |
|
12 | All the data related to the command state is stored as dictionary in the object. | |
13 | The class has methods using which the data can be stored to disk in a file under |
|
13 | The class has methods using which the data can be stored to disk in a file under | |
14 | .hg/ directory. |
|
14 | .hg/ directory. | |
15 |
|
15 | |||
16 |
We store the data on disk in cbor, for which we use the |
|
16 | We store the data on disk in cbor, for which we use the CBOR format to encode | |
17 | to serialize and deserialize data. |
|
17 | the data. | |
18 | """ |
|
18 | """ | |
19 |
|
19 | |||
20 | from __future__ import absolute_import |
|
20 | from __future__ import absolute_import | |
21 |
|
21 | |||
22 | from . import ( |
|
22 | from . import ( | |
23 | error, |
|
23 | error, | |
24 | util, |
|
24 | util, | |
25 | ) |
|
25 | ) | |
26 | from .utils import ( |
|
26 | from .utils import ( | |
27 | cborutil, |
|
27 | cborutil, | |
28 | ) |
|
28 | ) | |
29 |
|
29 | |||
30 | class cmdstate(object): |
|
30 | class cmdstate(object): | |
31 | """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`, | |
32 | `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. | |
33 |
|
33 | |||
34 | All the data for the state is stored in the form of key-value pairs in a |
|
34 | All the data for the state is stored in the form of key-value pairs in a | |
35 | dictionary. |
|
35 | dictionary. | |
36 |
|
36 | |||
37 | The class object can write all the data to a file in .hg/ directory and |
|
37 | The class object can write all the data to a file in .hg/ directory and | |
38 | can populate the object data reading that file. |
|
38 | can populate the object data reading that file. | |
39 |
|
39 | |||
40 | Uses cbor to serialize and deserialize data while writing and reading from |
|
40 | Uses cbor to serialize and deserialize data while writing and reading from | |
41 | disk. |
|
41 | disk. | |
42 | """ |
|
42 | """ | |
43 |
|
43 | |||
44 | def __init__(self, repo, fname): |
|
44 | def __init__(self, repo, fname): | |
45 | """ repo is the repo object |
|
45 | """ repo is the repo object | |
46 | fname is the file name in which data should be stored in .hg directory |
|
46 | fname is the file name in which data should be stored in .hg directory | |
47 | """ |
|
47 | """ | |
48 | self._repo = repo |
|
48 | self._repo = repo | |
49 | self.fname = fname |
|
49 | self.fname = fname | |
50 |
|
50 | |||
51 | def read(self): |
|
51 | def read(self): | |
52 | """read the existing state file and return a dict of data stored""" |
|
52 | """read the existing state file and return a dict of data stored""" | |
53 | return self._read() |
|
53 | return self._read() | |
54 |
|
54 | |||
55 | def save(self, version, data): |
|
55 | def save(self, version, data): | |
56 | """write all the state data stored to .hg/<filename> file |
|
56 | """write all the state data stored to .hg/<filename> file | |
57 |
|
57 | |||
58 | we use third-party library cbor to serialize data to write in the file. |
|
58 | we use third-party library cbor to serialize data to write in the file. | |
59 | """ |
|
59 | """ | |
60 | if not isinstance(version, int): |
|
60 | if not isinstance(version, int): | |
61 | raise error.ProgrammingError("version of state file should be" |
|
61 | raise error.ProgrammingError("version of state file should be" | |
62 | " an integer") |
|
62 | " an integer") | |
63 |
|
63 | |||
64 | with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: |
|
64 | with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp: | |
65 | fp.write('%d\n' % version) |
|
65 | fp.write('%d\n' % version) | |
66 | for chunk in cborutil.streamencode(data): |
|
66 | for chunk in cborutil.streamencode(data): | |
67 | fp.write(chunk) |
|
67 | fp.write(chunk) | |
68 |
|
68 | |||
69 | def _read(self): |
|
69 | def _read(self): | |
70 | """reads the state file and returns a dictionary which contain |
|
70 | """reads the state file and returns a dictionary which contain | |
71 | data in the same format as it was before storing""" |
|
71 | data in the same format as it was before storing""" | |
72 | with self._repo.vfs(self.fname, 'rb') as fp: |
|
72 | with self._repo.vfs(self.fname, 'rb') as fp: | |
73 | try: |
|
73 | try: | |
74 | int(fp.readline()) |
|
74 | int(fp.readline()) | |
75 | except ValueError: |
|
75 | except ValueError: | |
76 | raise error.CorruptedState("unknown version of state file" |
|
76 | raise error.CorruptedState("unknown version of state file" | |
77 | " found") |
|
77 | " found") | |
78 |
|
78 | |||
79 | return cborutil.decodeall(fp.read())[0] |
|
79 | return cborutil.decodeall(fp.read())[0] | |
80 |
|
80 | |||
81 | def delete(self): |
|
81 | def delete(self): | |
82 | """drop the state file if exists""" |
|
82 | """drop the state file if exists""" | |
83 | util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) |
|
83 | util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True) | |
84 |
|
84 | |||
85 | def exists(self): |
|
85 | def exists(self): | |
86 | """check whether the state file exists or not""" |
|
86 | """check whether the state file exists or not""" | |
87 | return self._repo.vfs.exists(self.fname) |
|
87 | return self._repo.vfs.exists(self.fname) |
General Comments 0
You need to be logged in to leave comments.
Login now