-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtasks.py
205 lines (178 loc) · 8.3 KB
/
tasks.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
from invoke import Collection, task
from invoke.tasks import Task
import platform
import os
import pathlib
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/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/',
'Other/MemoryFileSystem/',
'Other/StreamIO/',
'Security/AddRegexRedaction/',
'Security/Redactions/',
'Text/AddGlyphs/',
'Text/AddUnicodeText/',
'Text/AddVerticalText/',
'Text/ListWords/',
'Text/RegexExtractText/',
'Text/RegexTextSearch/',
'Text/TextExtract/'
]
@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(pre=[clean_samples])
def build_samples(ctx):
"""Builds the APDFL Java Maven samples"""
for sample in samples_list:
full_path = os.path.join(os.getcwd(), sample)
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('mvn 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 = str(f'java -Djava.library.path={target_dir} -jar target/{sample_name}-1.0-SNAPSHOT-jar-with-dependencies.jar')
process = subprocess.Popen(command, shell=True, 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()
def run_samples(ctx):
"""Runs the APDFL Java Maven samples"""
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 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'}})