forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jar_task.py
499 lines (391 loc) · 18 KB
/
jar_task.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# coding=utf-8
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import shutil
import tempfile
from abc import ABC, abstractmethod
from builtins import bytes, object, open
from contextlib import contextmanager
from future.utils import iteritems, string_types
from twitter.common.collections import maybe_list
from pants.backend.jvm.argfile import safe_args
from pants.backend.jvm.subsystems.jar_tool import JarTool
from pants.backend.jvm.targets.java_agent import JavaAgent
from pants.backend.jvm.targets.jvm_binary import Duplicate, JarRules, JvmBinary, Skip
from pants.backend.jvm.tasks.classpath_util import ClasspathUtil
from pants.backend.jvm.tasks.nailgun_task import NailgunTask
from pants.base.exceptions import TaskError
from pants.java.jar.manifest import Manifest
from pants.java.util import relativize_classpath
from pants.util.contextutil import temporary_dir
from pants.util.dirutil import safe_mkdtemp
class Jar(object):
"""Encapsulates operations to build up or update a jar file.
Upon construction the jar is conceptually opened for writes. The write methods are called to
add to the jar's contents and then changes are finalized with a call to close. If close is not
called the staged changes will be lost.
:API: public
"""
class Error(Exception):
"""Indicates an error creating or updating a jar on disk."""
class Entry(ABC):
"""An entry to be written to a jar."""
def __init__(self, dest):
self._dest = dest
@property
def dest(self):
"""The destination path of the entry in the jar."""
return self._dest
def split_manifest(self):
"""Splits this entry into a jar non-manifest part and a manifest part.
Some entries represent a manifest, some do not, and others have both a manifest entry and
non-manifest entries; as such, callers must be prepared to handle ``None`` entries.
:returns: A tuple of (non-manifest Entry, manifest Entry).
:rtype: tuple of (:class:`Jar.Entry`, :class:`Jar.Entry`)
"""
if self.dest == Manifest.PATH:
return None, self
else:
return self, None
@abstractmethod
def materialize(self, scratch_dir):
"""Materialize this entry's source data into a filesystem path.
:param string scratch_dir: A temporary directory that may be used to do any work required
to materialize the entry as a source file. The caller is responsible for cleaning up
`scratch_dir` after the jar is closed.
:returns: The path to the source data.
"""
class FileSystemEntry(Entry):
"""An entry backed by an existing file on disk."""
def __init__(self, src, dest=None):
super(Jar.FileSystemEntry, self).__init__(dest)
self._src = src
def split_manifest(self):
if not os.path.isdir(self._src):
return super(Jar.FileSystemEntry, self).split_manifest()
if self.dest and self.dest == os.path.commonprefix([self.dest, Manifest.PATH]):
manifest_relpath = os.path.relpath(Manifest.PATH, self.dest)
else:
manifest_relpath = Manifest.PATH
manifest_path = os.path.join(self._src, manifest_relpath)
if os.path.isfile(manifest_path):
manifest_entry = Jar.FileSystemEntry(manifest_path, dest=Manifest.PATH)
non_manifest_chroot = os.path.join(safe_mkdtemp(), 'chroot')
shutil.copytree(self._src, non_manifest_chroot)
os.unlink(os.path.join(non_manifest_chroot, manifest_relpath))
return Jar.FileSystemEntry(non_manifest_chroot), manifest_entry
else:
return self, None
def materialize(self, _):
return self._src
class MemoryEntry(Entry):
"""An entry backed by an in-memory sequence of bytes."""
def __init__(self, dest, contents):
super(Jar.MemoryEntry, self).__init__(dest)
self._contents = contents
def materialize(self, scratch_dir):
fd, path = tempfile.mkstemp(dir=scratch_dir)
try:
os.write(fd, self._contents)
finally:
os.close(fd)
return path
def __init__(self, path):
self._path = path
self._entries = []
self._jars = []
self._manifest_entry = None
self._main = None
self._classpath = []
@property
def classpath(self):
"""The Class-Path entry of jar's Manifest."""
return self._classpath
@property
def path(self):
"""The path to jar itself."""
return self._path
def main(self, main):
"""Specifies a Main-Class entry for this jar's manifest.
:param string main: a fully qualified class name
"""
if not main or not isinstance(main, string_types):
raise ValueError('The main entry must be a non-empty string')
self._main = main
def append_classpath(self, classpath):
"""Specifies a Class-Path entry for this jar's manifest.
If called multiple times, new entry will be appended to the existing classpath.
:param iterable classpath: a list of paths
"""
self._classpath = self._classpath + maybe_list(classpath)
def write(self, src, dest=None):
"""Schedules a write of the file at ``src`` to the ``dest`` path in this jar.
If the ``src`` is a file, then ``dest`` must be specified.
If the ``src`` is a directory then by default all descendant files will be added to the jar as
entries carrying their relative path. If ``dest`` is specified it will be prefixed to each
descendant's relative path to form its jar entry path.
:param string src: the path to the pre-existing source file or directory
:param string dest: the path the source file or directory should have in this jar
"""
if not src or not isinstance(src, string_types):
raise ValueError('The src path must be a non-empty string, got {} of type {}.'.format(
src, type(src)))
if dest and not isinstance(dest, string_types):
raise ValueError('The dest entry path must be a non-empty string, got {} of type {}.'.format(
dest, type(dest)))
if not os.path.isdir(src) and not dest:
raise self.Error('Source file {} must have a jar destination specified'.format(src))
self._add_entry(self.FileSystemEntry(src, dest))
def writestr(self, path, contents):
"""Schedules a write of the file ``contents`` to the given ``path`` in this jar.
:param string path: the path to write the contents to in this jar
:param string contents: the raw byte contents of the file to write to ``path``
"""
if not path or not isinstance(path, string_types):
raise ValueError('The path must be a non-empty string')
if contents is None or not isinstance(contents, bytes):
raise ValueError('The contents must be a sequence of bytes')
self._add_entry(self.MemoryEntry(path, contents))
def _add_entry(self, entry):
non_manifest, manifest = entry.split_manifest()
if manifest:
self._manifest_entry = manifest
if non_manifest:
self._entries.append(non_manifest)
def writejar(self, jar):
"""Schedules all entries from the given ``jar``'s to be added to this jar save for the manifest.
:param string jar: the path to the pre-existing jar to graft into this jar
"""
if not jar or not isinstance(jar, string_types):
raise ValueError('The jar path must be a non-empty string')
self._jars.append(jar)
@contextmanager
def _render_jar_tool_args(self, options):
"""Format the arguments to jar-tool.
:param Options options:
"""
args = []
with temporary_dir() as manifest_stage_dir:
# relativize urls in canonical classpath, this needs to be stable too therefore
# do not follow the symlinks because symlinks may vary from platform to platform.
classpath = relativize_classpath(self.classpath,
os.path.dirname(self._path),
followlinks=False)
def as_cli_entry(entry):
src = entry.materialize(manifest_stage_dir)
return '{}={}'.format(src, entry.dest) if entry.dest else src
files = [as_cli_entry(entry) for entry in self._entries] if self._entries else []
jars = self._jars or []
with safe_args(classpath, options, delimiter=',') as classpath_args:
with safe_args(files, options, delimiter=',') as files_args:
with safe_args(jars, options, delimiter=',') as jars_args:
# If you specify --manifest to jar-tool you cannot specify --main.
if self._manifest_entry:
manifest_file = self._manifest_entry.materialize(manifest_stage_dir)
else:
manifest_file = None
if self._main and manifest_file:
main_arg = None
with open(manifest_file, 'a') as f:
f.write("Main-Class: {}\n".format(self._main))
else:
main_arg = self._main
if main_arg:
args.append('-main={}'.format(self._main))
if classpath_args:
args.append('-classpath={}'.format(','.join(classpath_args)))
if manifest_file:
args.append('-manifest={}'.format(manifest_file))
if files_args:
args.append('-files={}'.format(','.join(files_args)))
if jars_args:
args.append('-jars={}'.format(','.join(jars_args)))
yield args
class JarTask(NailgunTask):
"""A baseclass for tasks that need to create or update jars.
All subclasses will share the same underlying nailgunned jar tool and thus benefit from fast
invocations.
:API: public
"""
@classmethod
def subsystem_dependencies(cls):
return super(JarTask, cls).subsystem_dependencies() + (JarTool,)
@classmethod
def prepare(cls, options, round_manager):
super(JarTask, cls).prepare(options, round_manager)
JarTool.prepare_tools(round_manager)
@staticmethod
def _flag(bool_value):
return 'true' if bool_value else 'false'
_DUPLICATE_ACTION_TO_NAME = {
Duplicate.SKIP: 'SKIP',
Duplicate.REPLACE: 'REPLACE',
Duplicate.CONCAT: 'CONCAT',
Duplicate.CONCAT_TEXT: 'CONCAT_TEXT',
Duplicate.FAIL: 'THROW',
}
@classmethod
def _action_name(cls, action):
name = cls._DUPLICATE_ACTION_TO_NAME.get(action)
if name is None:
raise ValueError('Unrecognized duplicate action: {}'.format(action))
return name
def __init__(self, *args, **kwargs):
super(JarTask, self).__init__(*args, **kwargs)
self.set_distribution(jdk=True)
# TODO(John Sirois): Consider poking a hole for custom jar-tool jvm args - namely for Xmx
# control.
@contextmanager
def open_jar(self, path, overwrite=False, compressed=True, jar_rules=None):
"""Yields a Jar that will be written when the context exits.
:API: public
:param string path: the path to the jar file
:param bool overwrite: overwrite the file at ``path`` if it exists; ``False`` by default; ie:
update the pre-existing jar at ``path``
:param bool compressed: entries added to the jar should be compressed; ``True`` by default
:param jar_rules: an optional set of rules for handling jar exclusions and duplicates
"""
jar = Jar(path)
try:
yield jar
except jar.Error as e:
raise TaskError('Failed to write to jar at {}: {}'.format(path, e))
with jar._render_jar_tool_args(self.get_options()) as args:
if args: # Don't build an empty jar
args.append('-update={}'.format(self._flag(not overwrite)))
args.append('-compress={}'.format(self._flag(compressed)))
jar_rules = jar_rules or JarRules.default()
args.append('-default_action={}'.format(self._action_name(jar_rules.default_dup_action)))
skip_patterns = []
duplicate_actions = []
for rule in jar_rules.rules:
if isinstance(rule, Skip):
skip_patterns.append(rule.apply_pattern)
elif isinstance(rule, Duplicate):
duplicate_actions.append('{}={}'.format(
rule.apply_pattern.pattern, self._action_name(rule.action)))
else:
raise ValueError('Unrecognized rule: {}'.format(rule))
if skip_patterns:
args.append('-skip={}'.format(','.join(p.pattern for p in skip_patterns)))
if duplicate_actions:
args.append('-policies={}'.format(','.join(duplicate_actions)))
args.append(path)
if JarTool.global_instance().run(context=self.context, runjava=self.runjava, args=args):
raise TaskError('jar-tool failed')
class JarBuilderTask(JarTask):
class JarBuilder(ABC):
"""A utility to aid in adding the classes and resources associated with targets to a jar.
:API: public
"""
@staticmethod
def _add_agent_manifest(agent, manifest):
# TODO(John Sirois): refactor an agent model to support 'Boot-Class-Path' properly.
manifest.addentry(Manifest.MANIFEST_VERSION, '1.0')
if agent.premain:
manifest.addentry('Premain-Class', agent.premain)
if agent.agent_class:
manifest.addentry('Agent-Class', agent.agent_class)
if agent.can_redefine:
manifest.addentry('Can-Redefine-Classes', 'true')
if agent.can_retransform:
manifest.addentry('Can-Retransform-Classes', 'true')
if agent.can_set_native_method_prefix:
manifest.addentry('Can-Set-Native-Method-Prefix', 'true')
@staticmethod
def _add_manifest_entries(jvm_binary_target, manifest):
"""Add additional fields to MANIFEST.MF as declared in the ManifestEntries structure.
:param JvmBinary jvm_binary_target:
:param Manifest manifest:
"""
for header, value in iteritems(jvm_binary_target.manifest_entries.entries):
manifest.addentry(header, value)
@staticmethod
def prepare(round_manager):
"""Prepares the products needed to use `create_jar_builder`.
This method should be called during task preparation to ensure the classes and resources
needed for jarring targets are mapped by upstream tasks that generate these.
Later, in execute context, the `create_jar_builder` method can be called to get back a
prepared ``JarTask.JarBuilder`` ready for use.
"""
round_manager.require_data('runtime_classpath')
def __init__(self, context, jar):
self._context = context
self._jar = jar
self._manifest = Manifest()
def add_target(self, target, recursive=False):
"""Adds the classes and resources for a target to an open jar.
:param target: The target to add generated classes and resources for.
:param bool recursive: `True` to add classes and resources for the target's transitive
internal dependency closure.
:returns: `True` if the target contributed any files - manifest entries, classfiles or
resource files - to this jar.
:rtype: bool
"""
products_added = False
classpath_products = self._context.products.get_data('runtime_classpath')
# TODO(John Sirois): Manifest handling is broken. We should be tracking state and failing
# fast if any duplicate entries are added; ie: if we get a second binary or a second agent.
if isinstance(target, JvmBinary):
self._add_manifest_entries(target, self._manifest)
products_added = True
# Ensure that JavaAgent entries are added to the manifest. Either by adding all of the
# transitive JavaAgent deps, if recursive, or by adding the root target, if the root target
# is itself a JavaAgent.
if recursive:
agents = [t for t in target.closure() if isinstance(t, JavaAgent)]
if len(agents) > 1:
raise TaskError('Only 1 agent can be added to a jar, found {} for {}:\n\t{}'
.format(len(agents),
target.address.reference(),
'\n\t'.join(agent.address.reference() for agent in agents)))
elif agents:
self._add_agent_manifest(agents[0], self._manifest)
products_added = True
elif isinstance(target, JavaAgent):
self._add_agent_manifest(target, self._manifest)
products_added = True
# In the transitive case we'll gather internal resources naturally as dependencies, but in the
# non-transitive case we need to manually add these special (in the context of jarring)
# dependencies.
targets = target.closure(bfs=True) if recursive else [target]
if not recursive and target.has_resources:
targets += target.resources
# We only gather internal classpath elements per our contract.
target_classpath = ClasspathUtil.internal_classpath(targets,
classpath_products)
for entry in target_classpath:
if ClasspathUtil.is_jar(entry):
self._jar.writejar(entry)
products_added = True
elif ClasspathUtil.is_dir(entry):
for rel_file in ClasspathUtil.classpath_entries_contents([entry]):
self._jar.write(os.path.join(entry, rel_file), rel_file)
products_added = True
else:
# non-jar and non-directory classpath entries should be ignored
pass
return products_added
def commit_manifest(self, jar):
"""Updates the manifest in the jar being written to.
Typically done right before closing the .jar. This gives a chance for all targets to bundle
in their contributions to the manifest.
"""
if not self._manifest.is_empty():
jar.writestr(Manifest.PATH, self._manifest.contents())
@classmethod
def prepare(cls, options, round_manager):
super(JarBuilderTask, cls).prepare(options, round_manager)
cls.JarBuilder.prepare(round_manager)
@contextmanager
def create_jar_builder(self, jar):
"""Creates a ``JarTask.JarBuilder`` ready for use.
This method should be called during in `execute` context and only after ensuring
`JarTask.JarBuilder.prepare` has already been called in `prepare` context.
:param jar: An opened ``pants.backend.jvm.tasks.jar_task.Jar`.
"""
builder = self.JarBuilder(self.context, jar)
yield builder
builder.commit_manifest(jar)