Repository navigation
Expand file tree
/
Copy pathtasks.py
More file actions
367 lines (313 loc) · 15.1 KB
/
Copy pathtasks.py
File metadata and controls
367 lines (313 loc) · 15.1 KB
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
from invoke import Collection, Exit, task
from invoke.tasks import Task
import platform
import os
import pathlib
import shutil
import subprocess
samples_list = [
'Annotations/Annotations/',
'Annotations/InkAnnotations/',
'Annotations/LinkAnnotations/',
'Annotations/PolygonAnnotations/',
'Annotations/PolyLineAnnotations/',
'ContentCreation/AddElements/',
'ContentCreation/AddHeaderFooter/',
'ContentCreation/Clips/',
'ContentCreation/CreateBookmarks/',
'ContentCreation/GradientShade/',
'ContentCreation/MakeDocWithCalGrayColorSpace/',
'ContentCreation/MakeDocWithCalRGBColorSpace/',
'ContentCreation/MakeDocWithDeviceNColorSpace/',
'ContentCreation/MakeDocWithICCBasedColorSpace/',
'ContentCreation/MakeDocWithIndexedColorSpace/',
'ContentCreation/MakeDocWithLabColorSpace/',
'ContentCreation/MakeDocWithSeparationColorSpace/',
'ContentCreation/NameTrees/',
'ContentCreation/NumberTrees/',
'ContentCreation/RemoteGoToActions/',
'ContentCreation/WriteNChannelTiff/',
'ContentModification/Actions/',
'ContentModification/AddCollection/',
'ContentModification/AddQRCode/',
'ContentModification/ChangeLayerConfiguration/',
'ContentModification/ChangeLinkColors/',
'ContentModification/CreateLayer/',
'ContentModification/ExtendedGraphicStates/',
'ContentModification/FlattenTransparency/',
'ContentModification/LaunchActions/',
'ContentModification/MergePDF/',
'ContentModification/PageLabels/',
'ContentModification/PDFObject/',
'ContentModification/UnderlinesAndHighlights/',
'ContentModification/Watermark/',
'DocumentConversion/ColorConvertDocument/',
'DocumentConversion/ConvertToOffice/',
'DocumentConversion/ConvertWordToPDF/',
'DocumentConversion/CreateDocFromWebPage/',
'DocumentConversion/CreateDocFromXPS/',
'DocumentConversion/FacturXConverter/',
'DocumentConversion/PDFAConverter/',
'DocumentConversion/PDFXConverter/',
'DocumentConversion/ZUGFeRDConverter/',
'DocumentOptimization/PDFOptimize/',
'Forms/ConvertXFAToAcroForms/',
'Forms/ExportFormsData/',
'Forms/FlattenForms/',
'Forms/ImportFormsData/',
'Images/DocToImages/',
'Images/DrawSeparations/',
'Images/EPSSeparations/',
'Images/GetSeparatedImages/',
'Images/ImageDisplayByteArray',
'Images/ImageEmbedICCProfile/',
'Images/ImageExport/',
'Images/ImageExtraction/',
'Images/ImageFromBufferedImage/',
'Images/ImageImport/',
'Images/ImageResampling/',
'Images/OutputPreview/',
'Images/RasterizePage/',
'InformationExtraction/ListBookmarks/',
'InformationExtraction/ListFonts/',
'InformationExtraction/ListInfo/',
'InformationExtraction/ListLayers/',
'InformationExtraction/ListPaths/',
'InformationExtraction/Metadata/',
'OpticalCharacterRecognition/AddTextToDocument/',
'OpticalCharacterRecognition/AddTextToImage/',
'OpticalCharacterRecognition/OCRDocument/',
'Other/MemoryFileSystem/',
'Other/StreamIO/',
'Security/AddBasicPAdESElectronicSignature/',
'Security/AddPAdESPolicySignature/',
'Security/AddDigitalSignatureCMS/',
'Security/AddDigitalSignatureRFC3161/',
'Security/AddRegexRedaction/',
'Security/Redactions/',
'Text/AddGlyphs/',
'Text/AddUnicodeText/',
'Text/AddVerticalText/',
'Text/ListWords/',
'Text/RegexExtractText/',
'Text/RegexTextSearch/',
'Text/TextExtract/'
]
# The package sources build-samples can test against:
# Nightly - apdfl-internal, where maven-builder publishes every night. Needs
# the RepoFlow credentials in REPOFLOW_USER / REPOFLOW_PASS.
# Public - apdfl-public, the anonymous repository the sample POMs name and
# customers resolve from. This is the pass that fails when a
# release is approved but not actually usable.
package_sources = ('Nightly', 'Public')
repoflow_url = 'https://repo.datalogics.com/api/maven/datalogics-enterprise'
# Samples for features that are in the nightly packages but not yet in a public
# release. They are built and run against Nightly only, so adding a sample for a
# new feature cannot stop the rest of the samples building against Public.
# Remove the entry once the release that carries the feature is on apdfl-public.
nightly_only_samples = (
'DocumentConversion/ConvertWordToPDF/', # OfficeToPDF, after 21.4.0
)
# Generated settings files, one per package source. build/ is git-ignored.
settings_dir = 'build'
def maven_settings(pkg_source):
"""Writes the settings.xml for pkg_source and returns its absolute path.
Each pass uses only its own settings, never the node's ~/.m2/settings.xml,
so a mirror configured on a node cannot change which repository a pass
tests. Nightly mirrors apdfl-public onto apdfl-internal: the sample POMs
stay exactly as customers get them, and nothing that only exists on
apdfl-public can satisfy the nightly build. The credentials are left as
${env.*} references so the password is never written to disk.
"""
if pkg_source == 'Nightly':
missing = [v for v in ('REPOFLOW_USER', 'REPOFLOW_PASS') if not os.environ.get(v)]
if missing:
raise Exit(f'{" and ".join(missing)} not set -- the Nightly packages on '
f'apdfl-internal need the RepoFlow credentials')
body = f""" <servers>
<server>
<id>apdfl-internal</id>
<username>${{env.REPOFLOW_USER}}</username>
<password>${{env.REPOFLOW_PASS}}</password>
</server>
</servers>
<mirrors>
<mirror>
<id>apdfl-internal</id>
<mirrorOf>apdfl-public</mirrorOf>
<url>{repoflow_url}/apdfl-internal</url>
</mirror>
</mirrors>
"""
else:
body = ''
os.makedirs(settings_dir, exist_ok=True)
settings = os.path.abspath(os.path.join(settings_dir, f'settings-{pkg_source.lower()}.xml'))
with open(settings, 'w', encoding='utf-8', newline='\n') as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n'
'<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0">\n'
f'{body}</settings>\n')
return settings
def check_pkg_source(pkg_source):
# An unrecognized value would otherwise build or run every sample against
# whatever happened to be resolved last, which passes without testing anything.
if pkg_source not in package_sources:
raise Exit(f'unknown package source {pkg_source!r}, '
f'expected one of {list(package_sources)}')
def skip_for_pkg_source(sample, pkg_source):
return pkg_source != 'Nightly' and sample in nightly_only_samples
def skip_on_mac_intel(sample):
# The OfficeToPDF plugin ships for macOS on Apple silicon only.
return (platform.system() == 'Darwin' and
platform.machine().lower() not in ('arm64', 'aarch64') and
'ConvertWordToPDF' in sample)
def skip_on_windows_arm(sample):
# These samples use APDFL features that are not implemented on Windows ARM
# for APDFL 21 (Forms Extension, Office conversion and the OfficeToPDF
# plugin).
unsupported = ('ConvertXFAToAcroForms', 'ExportFormsData', 'FlattenForms',
'ImportFormsData', 'ConvertToOffice', 'ConvertWordToPDF')
is_windows_arm = (platform.system() == 'Windows' and
platform.machine().lower() in ('arm64', 'aarch64'))
return is_windows_arm and any(s in sample for s in unsupported)
@task()
def clean_samples(ctx):
"""Cleans files that were generated from building the samples"""
for sample in samples_list:
full_path = os.path.join(os.getcwd(), sample)
with ctx.cd(full_path):
ctx.run('mvn clean')
ctx.run('git clean -fdx')
@task()
def clean_maven_packages(ctx):
"""Removes the APDFL artifacts from the local Maven repository
The nightly and public packages can share version numbers, and Maven reuses
an artifact already in the local repository without asking a remote. A copy
left by the nightly pass would then stand in for one that was never
published, so the two passes each start without any com.datalogics.pdfl
artifacts. Everything else stays so third-party dependencies are not
downloaded twice.
"""
# Asked of Maven rather than assumed, because CI sets a job-local
# repository through MAVEN_OPTS.
result = ctx.run('mvn -q -DforceStdout '
'org.apache.maven.plugins:maven-help-plugin:3.5.1:evaluate '
'-Dexpression=settings.localRepository', hide='out')
local_repo = result.stdout.strip()
if not local_repo or not os.path.isabs(local_repo):
raise Exit(f'could not determine the local Maven repository, got {local_repo!r}')
apdfl_dir = os.path.join(local_repo, 'com', 'datalogics', 'pdfl')
print(f'... removing {apdfl_dir}')
shutil.rmtree(apdfl_dir, ignore_errors=True)
# Defaulted so a bare `invoke build-samples` still works; invoke never takes an
# argument with a default positionally, so it is `--pkg-source Public`.
@task(pre=[clean_samples],
help={'pkg_source': f'Packages to build against: {" or ".join(package_sources)}'})
def build_samples(ctx, pkg_source='Nightly'):
"""Builds the APDFL Java Maven samples against the Nightly or Public packages"""
check_pkg_source(pkg_source)
settings = maven_settings(pkg_source)
for sample in samples_list:
full_path = os.path.join(os.getcwd(), sample)
if skip_for_pkg_source(sample, pkg_source):
print(f'{sample} not yet in the {pkg_source} packages')
continue
if skip_on_windows_arm(sample):
print(f'{sample} not available on Windows ARM')
continue
if skip_on_mac_intel(sample):
print(f'{sample} not available on this OS')
continue
if platform.system() == 'Darwin' and ('ConvertToOffice' in sample or
'CreateDocFromXPS' in sample or
'ConvertXFAToAcroForms' in sample
or 'ExportFormsData' in sample or
'FlattenForms' in sample or
'ImportFormsData' in sample):
output = ""
if ('ConvertToOffice' in sample or 'CreateDocFromXPS' in sample):
output = "not available on this OS"
else:
output = "cannot be run in CI on this OS"
print(f'{sample} {output}')
continue
with ctx.cd(full_path):
ctx.run(f'mvn -s "{settings}" package')
def remove_last_path_entry():
# Determine the platform-specific delimiter
delimiter = ";" if platform.system() == "Windows" else ":"
path_entries = os.environ["PATH"].split(delimiter)
# Remove the last entry from the PATH environment variable
if path_entries:
path_entries.pop()
new_path = delimiter.join(path_entries)
os.environ["PATH"] = new_path
def execute_java_sample(target_dir, sample_name, full_path, apdfl_key):
command = [
'java',
f'-Djava.library.path={target_dir}',
'-jar',
f'target/{sample_name}-1.0-SNAPSHOT-jar-with-dependencies.jar'
]
process = subprocess.Popen(command, shell=False, cwd=full_path,
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
process.stdin.write(apdfl_key.encode() + b'\n')
process.stdin.flush()
stdout, stderr = process.communicate()
if process.returncode == 0:
print(f"{sample_name} sample ran successfully.")
print(stdout.decode(errors='ignore'))
else:
print(stderr.decode())
raise RuntimeError(f"{sample_name} sample failed to run.")
@task(help={'pkg_source': f'Packages the samples were built against: {" or ".join(package_sources)}'})
def run_samples(ctx, pkg_source='Nightly'):
"""Runs the APDFL Java Maven samples"""
check_pkg_source(pkg_source)
apdfl_key = os.environ.get("APDFL_KEY")
if apdfl_key is None:
raise ValueError("APDFL_KEY environment variable not set.")
for sample in samples_list:
if skip_for_pkg_source(sample, pkg_source):
print(f'{sample} not yet in the {pkg_source} packages')
continue
if skip_on_windows_arm(sample):
print(f'{sample} not available on Windows ARM')
continue
if skip_on_mac_intel(sample):
print(f'{sample} not available on this OS')
continue
if platform.system() == "Windows":
os.environ["PATH"] += ";"
full_path = os.path.join(os.getcwd(), sample)
# Add samples' target\lib directory to PATH environment variable
target_dir = pathlib.Path(full_path, 'target', 'lib')
if platform.system() == 'Windows':
os.environ["PATH"] += str(target_dir)
if 'DocToImages' in sample or 'ImageDisplayByteArray' in sample:
continue
if platform.system() == 'Darwin' and ('ConvertToOffice' in sample or
'CreateDocFromXPS' in sample or
'ConvertXFAToAcroForms' in sample
or 'ExportFormsData' in sample or
'FlattenForms' in sample or
'ImportFormsData' in sample):
output = ""
if ('ConvertToOffice' in sample or 'CreateDocFromXPS' in sample):
output = "not available on this OS"
else:
output = "cannot be run in CI on this OS"
print(f'{sample} {output}')
continue
elif platform.system() == 'Linux' and 'ConvertToOffice' in sample:
continue
else:
sample_name = sample.split("/")[1]
execute_java_sample(target_dir, sample_name, full_path, apdfl_key)
if platform.system() == "Windows":
remove_last_path_entry()
tasks = []
tasks.extend([v for v in locals().values() if isinstance(v, Task)])
ns = Collection(*tasks)
ns.configure({'run': {'echo': 'true'}})