forked from twitter-archive/commons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BUILD.commons
216 lines (174 loc) · 8.33 KB
/
BUILD.commons
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# ==================================================================================================
# Copyright 2011 Twitter, Inc.
# --------------------------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this work except in compliance with the License.
# You may obtain a copy of the License in the LICENSE file, or at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==================================================================================================
from __future__ import print_function
import os
import sys
from contextlib import contextmanager
from zipfile import ZipFile
from twitter.pants import goal, is_internal
from twitter.pants.base.parse_context import ParseContext
from twitter.pants.python.python_chroot import PythonChroot
from twitter.pants.targets import JavaLibrary, JvmBinary, ScalaLibrary
from twitter.pants.tasks import Task
def reinstall(context):
pex = os.path.join(ROOT_DIR, 'pants.pex')
with ParseContext.temp(os.path.basename(__file__)):
pants_exe = pants('src/python/twitter/pants').resolve().next()
chroot = PythonChroot(pants_exe, ROOT_DIR)
chroot.dump().build(pex)
print('\nPants reinstalled from live local source.')
# TODO(John Sirois): remove 'goal' when pants.new -> pants
args = ['pants.pex', 'goal']
# When we re-run, pass on the remaining goals and flags so we can pick up where we leave off here.
index = sys.argv.index('reinstall')
args.extend(sys.argv[index+1:])
print('Respawning with: %s\n' % ' '.join(args))
sys.stdout.flush()
sys.stderr.flush()
context.lock.release()
os.execv(pex, args)
goal(
name='reinstall',
action=reinstall,
).install().with_description('Reinstalls pants from the current source tree.')
RESOURCE_RELDIR = 'com/twitter/common/args/apt'
RESOURCE_BASENAME = 'cmdline.arg.info.txt'
class ArgsResourceMapper(Task):
"""Maps resource files generated by com.twitter.common#args-apt into a binary jar."""
@classmethod
def setup_parser(cls, option_group, args, mkflag):
option_group.add_option(mkflag("include_all"), mkflag("include_all", negate=True),
dest="args_resource_mapper_include_all", default=False,
action="callback", callback=mkflag.set_bool,
help="[%default] Include all arg fields resources.")
def __init__(self, context, select_targets, transitive, main):
"""
:select_targets A predicate that selects the targets to create a trimmed cmdline args resource
file for.
:transitive If True, splits cmdline args resource info for all classes in the transitive
closure of classes depended on by the selected targets; otherwise, just
selects cmdline info for the classes owned by the selected targets directly.
:main True if the split cmdline arg resource info is for a main; False otherwise.
"""
Task.__init__(self, context)
self.select_targets = select_targets
self.transitive = transitive
# The args apt plugin uses a sequential suffix scheme to detect a family of cmdline args
# resource files available on a classpath. The 0th slot is normally skipped and reserved to
# the cmdline arg resource file of a main.
self.resource_index = 0 if main else 1
context.products.require('jars', self.select_targets)
context.products.require('classes')
self.classdirs = context.config.getlist('args-resource-mapper', 'classdirs')
self.include_all = context.options.args_resource_mapper_include_all
def execute(self, targets):
if self.classdirs:
jarmap = self.context.products.get('jars')
for target in filter(self.select_targets, targets):
mapping = jarmap.get(target)
if mapping:
for basedir, jars in mapping.items():
for jar in jars:
self._addargsresources(os.path.join(basedir, jar), target)
else:
self.context.log.warn('No classes found for target %s' % target)
def _addargsresources(self, jar, target):
lines = set()
for resourcedir in [os.path.join(classdir, RESOURCE_RELDIR) for classdir in self.classdirs]:
if os.path.exists(resourcedir):
for file in os.listdir(resourcedir):
if file.startswith(RESOURCE_BASENAME):
with open(os.path.join(resourcedir, file)) as resource:
lines.update(resource.readlines())
if lines:
class Args(object):
def __init__(self, context, transitive, class_genmap):
self.context = context
self.classnames = set()
def add_clasnames(target):
if isinstance(target, JavaLibrary) or isinstance(target, ScalaLibrary):
if class_genmap.get(target):
for base, classes in class_genmap.get(target).items():
for cls in classes:
self.classnames.add(cls.replace('.class', '').replace('/', '.'))
else:
self.context.log.debug('No mapping for %s' % target)
if transitive:
target.walk(add_clasnames, is_internal)
else:
add_clasnames(target)
def matches(self, line):
line = line.strip()
if not line:
return False
components = line.split(' ')
keyname = components[0]
if keyname in ('positional', 'field'):
# Line format: [key] class field
return components[1] in self.classnames
elif keyname == 'parser':
# Line format: [key] parsed-class parser-class
return components[2] in self.classnames
elif keyname == 'verifier':
# Line format: [key] verified-class verification-annotation-class verifier-class
return components[2] in self.classnames and components[3] in self.classnames
else:
# Unknown line (comments, ws, unknown configuration types
return True
self._addargs(lines if self.include_all
else filter(Args(self.context,
self.transitive,
self.context.products.get('classes')).matches, lines),
jar,
target)
@contextmanager
def _open_jar(self, path):
jar = ZipFile(path, 'a')
yield jar
jar.close()
def _addargs(self, lines, jarfile, target):
def is_configurationinfo(line):
line = line.strip()
return line and not line.startswith('#')
if any(filter(is_configurationinfo, lines)):
resource = os.path.join(RESOURCE_RELDIR, '%s.%d' % (RESOURCE_BASENAME, self.resource_index))
content = '# Created by pants goal args-apt\n'
content += ''.join(sorted(lines))
with self._open_jar(jarfile) as jar:
jar.writestr(resource, content)
self.context.log.debug('Added args-apt resource file %s for %s:'
'\n%s' % (resource, target, content))
class BinaryArgsResourceMapper(ArgsResourceMapper):
def __init__(self, context):
ArgsResourceMapper.__init__(self,
context,
select_targets=lambda target: isinstance(target, JvmBinary),
transitive=True,
main=True)
goal(name='args-apt', action=BinaryArgsResourceMapper).install('binary')
class JarArgsResourceMapper(ArgsResourceMapper):
def __init__(self, context):
ArgsResourceMapper.__init__(self,
context,
select_targets=lambda target: isinstance(target, JavaLibrary),
transitive=False,
main=False)
goal(name='args-apt', action=JarArgsResourceMapper).install('jar')
# We always want compile to finish with a checkstyle
from twitter.pants.tasks.checkstyle import Checkstyle
goal(name='checkstyle',
action=Checkstyle,
dependencies=['gen', 'resolve']).install('compile')