Wed, 22 Sep 2021 12:30:48 +0300
Work on edit history
1 | 1 | #!/usr/bin/env python |
2 | # coding: utf-8 | |
3 | # | |
4 | # Copyright 2015 Teemu Piippo | |
5 | # All rights reserved. | |
6 | # | |
7 | # Redistribution and use in source and binary forms, with or without | |
8 | # modification, are permitted provided that the following conditions | |
9 | # are met: | |
10 | # | |
11 | # 1. Redistributions of source code must retain the above copyright | |
12 | # notice, this list of conditions and the following disclaimer. | |
13 | # 2. Redistributions in binary form must reproduce the above copyright | |
14 | # notice, this list of conditions and the following disclaimer in the | |
15 | # documentation and/or other materials provided with the distribution. | |
16 | # 3. Neither the name of the copyright holder nor the names of its | |
17 | # contributors may be used to endorse or promote products derived from | |
18 | # this software without specific prior written permission. | |
19 | # | |
20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
22 | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
23 | # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER | |
24 | # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
25 | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
26 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
27 | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
28 | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
29 | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | # | |
32 | ||
33 | class OutputFile: | |
34 | def __init__ (self, filename): | |
35 | self.filename = filename | |
36 | try: | |
37 | with open (self.filename, "r") as file: | |
38 | self.oldsum = file.readline() | |
39 | self.oldsum = self.oldsum.replace ('// ', '').strip() | |
40 | except IOError: | |
41 | self.oldsum = '' | |
42 | self.body = '' | |
43 | ||
44 | def write(self, text): | |
45 | self.body += text | |
46 | ||
47 | def save(self, verbose = False): | |
48 | from hashlib import sha256 | |
49 | checksum = sha256(self.body.encode('utf-8')).hexdigest() | |
50 | if checksum == self.oldsum: | |
51 | if verbose: | |
52 | print ('%s is up to date' % self.filename) | |
53 | pass | |
54 | else: | |
55 | with open (self.filename, "w") as file: | |
56 | file.write('// %s\n' % checksum) | |
57 | file.write('// This file has been automatically generated. Do not edit by hand\n') | |
58 | file.write('\n') | |
59 | file.write(self.body) | |
60 | if verbose: | |
61 | print('%s written' % self.filename) |