forked from ParisiLabs/wire-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedBuildInfo
executable file
·201 lines (172 loc) · 7.72 KB
/
embedBuildInfo
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python
#
# Wire
# Copyright (C) 2016 Wire Swiss GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
import os
import os.path
import Foundation
import sys
import subprocess
import argparse
import json
import re
import plistlib
from subprocess import Popen, PIPE
def update_and_merge_build_info(info, source_root, repositories):
build_info_key = "ZCBuildInfo"
build_info = Foundation.NSMutableDictionary.dictionary()
merge_build_info_from_zetacore(build_info, source_root)
update_build_info(build_info, source_root, repositories)
info[build_info_key] = build_info
carthage_info_key = "CarthageBuildInfo"
carthage_info = Foundation.NSMutableDictionary.dictionary()
merge_build_info_from_carthage(carthage_info, source_root)
info[carthage_info_key] = carthage_info
def plist_to_dictionary(filename):
with open(filename, "rb") as f:
content = f.read()
args = ["plutil", "-convert", "json", "-o", "-", "--", "-"]
p = Popen(args, stdin=PIPE, stdout=PIPE)
p.stdin.write(content)
out, err = p.communicate()
return json.loads(out)
def update_build_info(build_info, source_root, paths):
dev_base_dir = os.path.join(source_root, "..", "..")
build_time = Foundation.NSDate.date()
for path in paths:
name = os.path.basename(os.path.abspath(path))
if os.path.exists(path):
repo_info = Foundation.NSMutableDictionary.dictionary()
repo_info["sha"] = SHA_for_repo(path)
if 'USER' in os.environ:
repo_info['user'] = os.environ['USER']
if 'BUILD_NUMBER' in os.environ:
repo_info['build_number'] = os.environ['BUILD_NUMBER']
if 'JOB_NAME' in os.environ:
repo_info['job_name'] = os.environ['JOB_NAME']
repo_info['time'] = build_time
build_info[name] = repo_info
def parse_dependency_version_numbers(source_root):
dependencies = {}
carfile_resolved_path = os.path.join(source_root, "Cartfile.resolved")
cartfile_resolved = open(carfile_resolved_path, "r")
stripQuotes = lambda string: re.sub('"', '', string)
for line in cartfile_resolved.readlines():
if line.strip() == "":
continue
dependency = line.split()
full_name = stripQuotes(dependency[1])
version = stripQuotes(dependency[2])
name = full_name.split("/")[1]
dependencies[name] = version
return dependencies
def merge_build_info_from_carthage(build_info, source_root):
dependencies = parse_dependency_version_numbers(source_root)
for name, version in dependencies.iteritems():
build_info[name] = version
def merge_build_info_from_zetacore(build_info, source_root):
for name in ["wire-avs-ios"]:
build_info_path = os.path.join(os.environ["SRCROOT"], "Libraries", name, "version.buildinfo")
if not os.path.isfile(build_info_path):
sys.stderr.write("warning: Unable to read '{0}'\n".format(build_info_path))
continue
build_info_data = open (build_info_path, "r").read()
#build_info_data = Foundation.NSData.dataWithContentsOfFile_(build_info_path)
if not build_info_data:
sys.stderr.write("warning: Unable to read '{0}'\n".format(build_info_path))
continue
project_build_info = json.loads(build_info_data)
if not project_build_info:
sys.stderr.write("warning: Unable to parse '{0}'\n".format(build_info_path))
continue
sys.stderr.write("project_build_info parsed as {0}, ({1})".format(project_build_info, type(project_build_info)));
project_info = {}
build_info[name] = project_info
for top_key, top_value in project_build_info.iteritems():
if top_key == name:
info = project_info
else:
if "subprojects" in project_info:
d = project_info["subprojects"]
if not top_key in d:
d[top_key] = {}
else:
project_info["subprojects"] = {}
project_info["subprojects"][top_key] = {}
info = project_info["subprojects"][top_key]
for key, value in top_value.iteritems():
if key == "time_rfc3339":
continue
if key == "time":
# Convert to NSDate object:
since_epoch = long(value)
value = Foundation.NSDate.dateWithTimeIntervalSince1970_(since_epoch)
if key == "build_number":
value = int(value)
info[key] = value
def workspace_is_dirty(path):
# Check if the working space is dirty. Of course git is super stupid and doesn't give us an easy way to do this...
git_status = subprocess.Popen(["/usr/bin/xcrun", "git", "status", "--porcelain"], cwd=path, stdout=subprocess.PIPE)
wc = subprocess.Popen(["/usr/bin/wc", "-l"], stdin=git_status.stdout, stdout=subprocess.PIPE)
line_count = wc.communicate()[0]
git_status.communicate()
assert 0 <= git_status.returncode, "git returned {0}".format(git_status.returncode)
assert wc.returncode == 0
return line_count and (0 < int(line_count))
def SHA_for_repo(path):
git_rev_parse = subprocess.Popen(["/usr/bin/xcrun", "git", "rev-parse", "HEAD"], cwd=path, stdout=subprocess.PIPE)
sha = git_rev_parse.communicate()[0].strip()
if workspace_is_dirty(path):
sha += "-dirty"
return sha
def write_property_list_to_path(plist, path, plist_format=Foundation.NSPropertyListBinaryFormat_v1_0):
(data, error) = Foundation.NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
plist,
plist_format,
0,
None
)
if not data:
sys.stderr.write("error: Unable to serialize proerty list: {0}\n".format(error))
sys.stderr.write("note: plist: {0}\n".format(plist))
sys.exit(-1)
(success, error) = data.writeToFile_options_error_(path, 0, None)
if not success:
sys.stderr.write("error: Unable to write to '{0}': {1}\n".format(path, error))
sys.exit(-1)
def check_environment(names):
for name in names:
if not name in os.environ:
sys.stderr.write("error: {0} not found in the environment.\n".format(name))
sys.exit(-1)
def update_info_plist(repositories, output_path=None):
check_environment(["SRCROOT"])
source_root = os.environ["SRCROOT"]
sys.stderr.write("note: Using SRCROOT = {0}\n".format(source_root))
info = Foundation.NSMutableDictionary.dictionary()
update_and_merge_build_info(info, source_root, repositories)
write_property_list_to_path(info, output_path)
return info
def main():
parser = argparse.ArgumentParser(description="Add additional build info to the Info.plist")
parser.add_argument('--output', help="The plist file to write to")
parser.add_argument('repositories', metavar='repository', type=str, nargs='*', help="Path to repository to recorded into ZCBuildInfo")
args = parser.parse_args()
print args
info = update_info_plist(repositories=args.repositories, output_path=args.output)
if __name__ == "__main__":
main()