-
Notifications
You must be signed in to change notification settings - Fork 200
/
defines.py
174 lines (133 loc) · 5.1 KB
/
defines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Generate a defines file.
"""
import os
import time
import argparse
import inspect
import datetime
from subprocess import check_output, CalledProcessError
from distutils.version import StrictVersion
from collections import namedtuple
class Defines(object):
"""
Handles defines.
"""
def __init__(self):
self._repository_root = None
self._repository_version = None
self._version = None
self._date = None
@property
def no_git(self):
return int(os.environ.get('FREELAN_NO_GIT', '0'))
@property
def local_path(self):
return os.path.dirname(os.path.realpath(__file__))
@property
def repository_root(self):
if self._repository_root is None:
try:
if ('APPVEYOR' in os.environ) or self.no_git:
self._repository_root = self.local_path
else:
self._repository_root = os.path.abspath(check_output(['git', 'rev-parse', '--show-toplevel']).rstrip()).decode('utf-8')
except (CalledProcessError, OSError):
self._repository_root = os.path.abspath(os.path.dirname(inspect.getfile(inspect.currentframe())))
return self._repository_root
@property
def repository_version(self):
if self._repository_version is None:
try:
if 'APPVEYOR_REPO_COMMIT' in os.environ:
self._repository_version = os.environ['APPVEYOR_REPO_COMMIT'].rstrip()
elif self.no_git:
if 'FREELAN_NO_GIT_VERSION' not in os.environ:
raise RuntimeError(errstr='You must specify FREELAN_NO_GIT_VERSION when FREELAN_NO_GIT is specified.')
self._repository_version = os.environ['FREELAN_NO_GIT_VERSION'].rstrip()
else:
self._repository_version = check_output(['git', 'describe', '--dirty=-modified']).rstrip()
except (CalledProcessError, OSError):
self._repository_version = "<unspecified version>"
return self._repository_version
@property
def version_file_path(self):
return os.path.join(self.repository_root, 'VERSION')
@property
def version(self):
if self._version is None:
self._version = namedtuple('Version', ['major', 'minor', 'patch'])(*StrictVersion(open(self.version_file_path).read()).version)
return self._version
@property
def version_str(self):
return '%s.%s' % (self.version.major, self.version.minor)
@property
def date(self):
if self._date is None:
self._date = datetime.datetime.utcfromtimestamp(int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))).strftime('%a %d %b %Y')
return self._date
@property
def template_file_path(self):
return os.path.join(self.repository_root, 'defines.hpp.template')
@property
def defines_file_name(self):
return os.path.splitext(os.path.basename(self.template_file_path))[0]
def replace_template_variables(self, content):
"""
Replace the template variables.
Return the content.
"""
if isinstance(content, str):
return content.format(defines=self)
else:
return content.decode().format(defines=self).encode()
def emitter(self, target, source, env):
"""
Modifies the dependencies.
"""
env.Depends(target, env.Value(self.repository_version))
env.Depends(target, env.Value(self.version))
env.Depends(target, env.Value(self.date))
return target, source
def action(self, target, source, env):
"""
Generate the defines file.
"""
output = self.replace_template_variables(source[0].get_contents())
flag = 'wb'
if isinstance(output, str):
flag = 'w'
with open(target[0].abspath, flag) as out:
out.write(output)
def register_into(self, env):
"""
Register into the specified environment.
"""
env.Append(BUILDERS={'GenerateDefines': env.Builder(
action=self.action,
emitter=self.emitter,
)})
env.defines = self
def generate_defines(self, target):
"""
Generate the defines.hpp file.
"""
with open(self.template_file_path, 'r') as source_file:
output = self.replace_template_variables(source_file.read())
try:
with open(target, 'r') as target_file:
current_content = target_file.read()
except IOError:
current_content = None
if output != current_content:
flag = 'wb'
if isinstance(output, str):
flag = 'w'
with open(target, flag) as target_file:
target_file.write(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate a defines file.')
parser.add_argument('target', help='The target file to generate from the template.')
args = parser.parse_args()
defines = Defines()
defines.generate_defines(args.target)