Debian Codegraph @

scripts/ohm/version_file_builder.py

raw
1 from __future__ import (absolute_import, division,
2 print_function, unicode_literals)
3
4 import logging
5
6 import os
7 import os.path
8 import subprocess
9 import tempfile
10
11 from . import utilfunctions
12
13
14 class VersionFileBuilder(object):
15 VERSION_TMPL = '\n'.join([
16 'const wchar_t *GITVersion = L"{version}";',
17 'const wchar_t *GITDate = L"{date}";',
18 ''
19 ])
20 notices = logging.getLogger('notices')
21
22 def __init__(self, workfile, outfile, gitpath="git"):
23 self.workfile = workfile
24 self.outfile = outfile
25 self.gitpath = gitpath
26 self.logger = logging.getLogger(self.__class__.__name__)
27
28 def remove_file(self, filename):
29 try:
30 os.remove(filename)
31 self.notices.info(' Removed %s', filename)
32 except OSError:
33 self.logger.exception(' Unable to remove %s', filename)
34
35 def build(self):
36 self.notices.info("Building...")
37 if self.need_to_update():
38 self.notices.info("Generating...")
39
40 utilfunctions.make_directory_for_filename(self.outfile)
41
42 with open(self.outfile, "wb") as out:
43 git_id = self.get_git_id()
44 output = self.VERSION_TMPL.format(
45 version=git_id,
46 date=self.get_git_date())
47 out.write(output.encode('utf-8'))
48 with open(self.workfile, "wb") as work:
49 work.write(git_id.encode('utf-8'))
50
51 self.notices.info(" Version information written to %s",
52 self.outfile)
53 else:
54 self.notices.info(" Up to date")
55
56 def get_git_id(self):
57 """Return the git id string suitable for inclusion in C code.
58
59 Requires striping the newline from the end of the command output"""
60 tf = tempfile.TemporaryFile()
61 s = "describe --long --dirty --abbrev=10 --tags --always"
62 s = s.split()
63 s.insert(0, self.gitpath)
64
65 subprocess.Popen(s, stdout=tf).wait()
66 tf.seek(0)
67
68 rawidoutput = tf.readline()
69 idoutput = rawidoutput.decode('utf-8').split('\n')
70 # split on the line ending and return the first peice which will
71 # contain the entire id (all of the tags, etc) of the current
72 # repository without any newlines
73 return idoutput[0]
74
75 def get_git_date(self):
76 """Return the git date string suitable for includsion in C code.
77
78 Requires stripping the newline from the end of the command output"""
79 tf = tempfile.TemporaryFile()
80
81 datecmd = 'log --pretty=format:%cd -n 1'
82 datecmd = datecmd.split()
83 datecmd.insert(0, self.gitpath)
84
85 subprocess.Popen(datecmd, stdout=tf).wait()
86 tf.seek(0)
87
88 rawdateoutput = tf.readline()
89 dateoutput = rawdateoutput.decode('utf-8')
90 return dateoutput
91
92 def need_to_update(self):
93 """Check if we need to update the outfile
94 :return: False if outfile is up to date, True otherwise
95 """
96 if os.path.exists(self.workfile):
97 if os.path.exists(self.outfile):
98 # check the id to see if it has changed
99 with open(self.workfile, 'rb') as work:
100 if work.readline().decode('utf-8') == self.get_git_id():
101 return False
102 else:
103 self.notices.info(" git id changed")
104 else:
105 self.notices.info(" %s does not exist", self.outfile)
106 else:
107 self.notices.info(" %s does not exist", self.workfile)
108 return True
109