forked from danieleteti/delphimvcframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
1156 lines (989 loc) · 44 KB
/
Copy pathtasks.py
File metadata and controls
1156 lines (989 loc) · 44 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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from invoke import task, context, Exit
import os
import subprocess
from colorama import *
import glob
import shutil
from shutil import copy2, rmtree, copytree
from datetime import datetime
import pathlib
from typing import *
import time
from pathlib import Path
init()
class BuildConfig:
"""Configuration for the build process"""
def __init__(self):
self.releases_path = "releases"
self.output = "bin"
self.output_folder = "" # defined at runtime
self.version = "DEV"
# Project root directory (where tasks.py is located)
self.project_root = os.path.dirname(os.path.abspath(__file__))
self.seven_zip = os.path.join(self.project_root, "7z.exe")
@property
def output_folder_path(self):
return os.path.join(self.releases_path, self.version)
# Global config instance
config = BuildConfig()
delphi_versions = [
{"version": "10.0", "path": "17.0", "desc": "Delphi 10 Seattle"},
{"version": "10.1", "path": "18.0", "desc": "Delphi 10.1 Berlin"},
{"version": "10.2", "path": "19.0", "desc": "Delphi 10.2 Tokyo"},
{"version": "10.3", "path": "20.0", "desc": "Delphi 10.3 Rio"},
{"version": "10.4", "path": "21.0", "desc": "Delphi 10.4 Sydney"},
{"version": "11.0", "path": "22.0", "desc": "Delphi 11 Alexandria"},
{"version": "11.1", "path": "22.0", "desc": "Delphi 11.1 Alexandria"},
{"version": "11.2", "path": "22.0", "desc": "Delphi 11.2 Alexandria"},
{"version": "11.3", "path": "22.0", "desc": "Delphi 11.3 Alexandria"},
{"version": "12.0", "path": "23.0", "desc": "Delphi 12 Athens"},
{"version": "13.0", "path": "37.0", "desc": "Delphi 13 Florence"},
]
def get_package_folders():
"""Get list of package folders by scanning the packages directory.
Returns folders that match the pattern 'd*' (e.g., d100, d110, d130)"""
packages_dir = "packages"
if not os.path.isdir(packages_dir):
return []
folders = []
for item in os.listdir(packages_dir):
item_path = os.path.join(packages_dir, item)
if os.path.isdir(item_path) and item.startswith("d") and item[1:].isdigit():
folders.append(item)
return sorted(folders)
def get_delphi_projects_to_build(which=""):
projects = []
delphi_version, _ = get_best_delphi_version_available()
dversion = "d" + delphi_version["version"].replace(".", "")
if not which or which == "core":
projects += glob.glob(
r"packages\{dversion}\*.groupproj".format(dversion=dversion)
)
projects += glob.glob(r"tools\entitygenerator\MVCAREntitiesGenerator.dproj")
if not which or which == "tests":
projects += glob.glob(r"unittests\**\*.dproj")
if not which or which == "samples":
projects += glob.glob(r"samples\**\*.dproj")
projects += glob.glob(r"samples\**\**\*.dproj")
projects += glob.glob(r"samples\**\**\**\*.dproj")
return sorted(projects)
def get_best_delphi_version_available() -> tuple[dict, str]:
global delphi_version
found = False
rsvars_path = None
i = len(delphi_versions)
while (not found) and (i >= 0):
i -= 1
delphi_version = delphi_versions[i]
version_path = delphi_version["path"]
rsvars_path = f"C:\\Program Files (x86)\\Embarcadero\\Studio\\{version_path}\\bin\\rsvars.bat"
if os.path.isfile(rsvars_path):
found = True
else:
rsvars_path = f"D:\\Program Files (x86)\\Embarcadero\\Studio\\{version_path}\\bin\\rsvars.bat"
if os.path.isfile(rsvars_path):
found = True
if found:
return delphi_version, rsvars_path
else:
raise Exception("Cannot find a Delphi compiler")
def build_delphi_project(
ctx: context.Context, project_filename, config="DEBUG", platform="Win32"
):
delphi_version, rsvars_path = get_best_delphi_version_available()
print("\nBUILD WITH: " + delphi_version["desc"])
cmdline = (
'"'
+ rsvars_path
+ '"'
+ " & msbuild /t:Build /p:Config="
+ config
+ f' /p:Platform={platform} "'
+ project_filename
+ '"'
)
r = ctx.run(cmdline, hide=True, warn=True)
if r.failed:
print(r.stdout)
print(r.stderr)
raise Exit("Build failed for " + delphi_version["desc"])
def zip_samples(ctx, version):
cmdline = (
f'"{config.seven_zip}" a '
+ config.output_folder
+ f"\\..\\{version}_samples.zip -r -i@7ziplistfile.txt"
)
print("ZIPPING SAMPLES")
print("CMDLINE: " + cmdline)
result = ctx.run(cmdline, warn=True)
if result.failed:
print(Fore.RED + "ERROR: Failed to zip samples" + Fore.RESET)
return False
return True
def create_zip(ctx, version):
print("CREATING ZIP")
archive_name = "..\\" + version + ".zip"
cmdline = f'"{config.seven_zip}" a {archive_name} *'
print(cmdline)
with ctx.cd(config.output_folder):
result = ctx.run(cmdline, hide=False, warn=True)
if result.failed:
print(Fore.RED + "ERROR: Failed to create zip" + Fore.RESET)
raise Exit("Failed to create zip archive")
def copy_sources():
# Validate source directories exist
ensure_dir_exists("sources", "DMVCFramework sources")
ensure_dir_exists("ideexpert", "IDE Expert")
ensure_dir_exists("packages", "Packages")
ensure_dir_exists("tools\\entitygenerator", "Entity Generator tool")
ensure_dir_exists("tools\\certificatesgenerator", "Certificates Generator tool")
ensure_dir_exists("tools\\sample_env_file", "Sample env file tool")
os.makedirs(config.output_folder + "\\sources", exist_ok=True)
os.makedirs(config.output_folder + "\\ideexpert", exist_ok=True)
os.makedirs(config.output_folder + "\\packages", exist_ok=True)
os.makedirs(config.output_folder + "\\tools", exist_ok=True)
# copying main sources
print("Copying DMVCFramework Sources...")
src = glob.glob("sources\\*.pas") + glob.glob("sources\\*.inc")
for file in src:
print("Copying " + file + " to " + config.output_folder + "\\sources")
copy2(file, config.output_folder + "\\sources\\")
# copying tools
print("Copying tools...")
ignore_patterns = shutil.ignore_patterns("*.identcache", "*.dcu", "__history", "__recovery")
copytree("tools\\entitygenerator", config.output_folder + "\\tools\\entitygenerator", ignore=ignore_patterns)
copytree(
"tools\\certificatesgenerator",
config.output_folder + "\\tools\\certificatesgenerator",
ignore=ignore_patterns,
)
copytree("tools\\sample_env_file", config.output_folder + "\\tools\\sample_env_file", ignore=ignore_patterns)
# copying ideexperts
print("Copying DMVCFramework IDEExpert...")
src = (
glob.glob("ideexpert\\*.pas")
+ glob.glob("ideexpert\\*.dfm")
+ glob.glob("ideexpert\\*.ico")
+ glob.glob("ideexpert\\*.bmp")
+ glob.glob("ideexpert\\*.png")
+ glob.glob("ideexpert\\*.res")
)
for file in src:
print("Copying " + file + " to " + config.output_folder + "\\ideexpert")
copy2(file, config.output_folder + "\\ideexpert\\")
files = [
#"dmvcframeworkDTResource.rc",
"dmvcframework_group.groupproj",
"dmvcframeworkRT.dproj",
"dmvcframeworkRT.dpk",
"dmvcframeworkDT.dproj",
"dmvcframeworkDT.dpk",
# loggerproRT è in lib\loggerpro\packages\
# SwagDoc è in lib\swagdoc\
]
# Get package folders dynamically from packages directory
folders = get_package_folders()
if not folders:
raise Exit("No package folders found in packages directory")
for folder in folders:
print(f"Copying DMVCFramework Delphi {folder} packages...")
for file in files:
os.makedirs(config.output_folder + f"\\packages\\{folder}", exist_ok=True)
print("Copying " + file + " to " + config.output_folder + f"\\packages\\{folder}")
copy2(
rf"packages\{folder}\{file}", config.output_folder + rf"\packages\{folder}"
)
def ensure_dir_exists(path, description=""):
"""Validate that a directory exists, raise Exit if not"""
if not os.path.isdir(path):
desc = f" ({description})" if description else ""
raise Exit(f"Source directory not found{desc}: {path}")
def run_robocopy(ctx, source, dest, extra_args=""):
"""Run robocopy and handle its non-standard exit codes.
Robocopy exit codes: 0-7 = success, 8+ = error"""
ensure_dir_exists(source)
# Always exclude Delphi compilation artifacts
default_excludes = "/XF *.identcache *.dcu"
cmd = rf"robocopy {source} {dest} /E /NFL /NDL /NJH /NJS /nc /ns /np /r:1 /w:1 {default_excludes} {extra_args}"
result = ctx.run(cmd, warn=True, hide=True)
# Robocopy: exit codes 0-7 are success, 8+ are errors
if result.return_code >= 8:
print(Fore.RED + f"ERROR: robocopy failed with exit code {result.return_code}" + Fore.RESET)
print(f"Command: {cmd}")
print(f"stdout: {result.stdout}")
print(f"stderr: {result.stderr}")
raise Exit(f"Cannot copy from {source} to {dest}")
return True
def copy_libs(ctx):
# swagdoc
print("Copying libraries: SwagDoc...")
curr_folder = config.output_folder + "\\lib\\swagdoc"
os.makedirs(curr_folder, exist_ok=True)
run_robocopy(ctx, r"lib\swagdoc", curr_folder)
# loggerpro
print("Copying libraries: LoggerPro...")
curr_folder = config.output_folder + "\\lib\\loggerpro"
os.makedirs(curr_folder, exist_ok=True)
run_robocopy(ctx, r"lib\loggerpro", curr_folder,
"/XD .vscode __history __recovery samples unittests "
"/XF *.log *.png *.ico LOGGERPRO-BUILD-TIMESTAMP.TXT")
# dmustache
print("Copying libraries: dmustache...")
curr_folder = config.output_folder + "\\lib\\dmustache"
os.makedirs(curr_folder, exist_ok=True)
run_robocopy(ctx, r"lib\dmustache", curr_folder, "/XF *.log *.png *.ico")
def printkv(key, value):
print(Fore.RESET + key + ": " + Fore.GREEN + value.rjust(60) + Fore.RESET)
def init_build(version, clean_releases=False):
"""Required by all tasks"""
config.version = version
config.output_folder = config.releases_path + "\\" + config.version
print()
print(Fore.RESET + Fore.RED + "*" * 80)
print(Fore.RESET + Fore.RED + " BUILD VERSION: " + config.version + Fore.RESET)
print(Fore.RESET + Fore.RED + " OUTPUT PATH : " + config.output_folder + Fore.RESET)
print(Fore.RESET + Fore.RED + "*" * 80)
if clean_releases:
print("Cleaning releases folder...")
rmtree(config.releases_path, True)
else:
rmtree(config.output_folder, True)
os.makedirs(config.output_folder, exist_ok=True)
f = open(config.output_folder + "\\version.txt", "w")
f.write("VERSION " + config.version + "\n")
f.write("BUILD DATETIME " + datetime.now().isoformat() + "\n")
f.close()
copy2("README.md", config.output_folder)
copy2("License.txt", config.output_folder)
def build_delphi_project_list(ctx, projects, build_config="DEBUG", filter=""):
ret = True
for delphi_project in projects:
if filter and (not filter in delphi_project):
print(f"Skipped {os.path.basename(delphi_project)}")
continue
msg = f"Building: {os.path.basename(delphi_project)} ({build_config})"
print(Fore.RESET + msg.ljust(90, "."), end="")
try:
build_delphi_project(ctx, delphi_project, build_config)
print(Fore.GREEN + "OK" + Fore.RESET)
except Exception as e:
print(Fore.RED + "\n\nBUILD ERROR")
print(Fore.RESET)
print(e)
return ret
@task
def clean(ctx, folder=None):
if folder is None:
folder = config.output_folder
if not folder:
raise Exit("No folder specified for clean operation")
if not os.path.isdir(folder):
print(f"Folder does not exist, nothing to clean: {folder}")
return
print(f"Cleaning folder {folder}")
# Files to preserve (source resources, not compiled)
preserve_files = {"DMVC.Splash.Resources.res"}
to_delete = []
to_delete += glob.glob(folder + r"\**\*.exe", recursive=True)
to_delete += glob.glob(folder + r"\**\*.dcu", recursive=True)
to_delete += glob.glob(folder + r"\**\*.stat", recursive=True)
to_delete += glob.glob(folder + r"\**\*.res", recursive=True)
to_delete += glob.glob(folder + r"\**\*.map", recursive=True)
to_delete += glob.glob(folder + r"\**\*.~*", recursive=True)
to_delete += glob.glob(folder + r"\**\*.rsm", recursive=True)
to_delete += glob.glob(folder + r"\**\*.drc", recursive=True)
to_delete += glob.glob(folder + r"\**\*.log", recursive=True)
to_delete += glob.glob(folder + r"\**\*.local", recursive=True)
to_delete += glob.glob(folder + r"\**\*.gitignore", recursive=True)
to_delete += glob.glob(folder + r"\**\*.gitattributes", recursive=True)
# Filter out preserved files
to_delete = [f for f in to_delete if os.path.basename(f) not in preserve_files]
for f in to_delete:
print(f"Deleting {f}")
os.remove(f)
rmtree(folder + r"\lib\loggerpro\Win32", True)
# Clean loggerpro packages - find all package folders dynamically
loggerpro_packages = folder + r"\lib\loggerpro\packages"
if os.path.isdir(loggerpro_packages):
for pkg_folder in os.listdir(loggerpro_packages):
pkg_path = os.path.join(loggerpro_packages, pkg_folder)
if os.path.isdir(pkg_path):
rmtree(os.path.join(pkg_path, "__history"), True)
rmtree(os.path.join(pkg_path, "Win32", "Debug"), True)
rmtree(os.path.join(pkg_path, "Win64", "Debug"), True)
rmtree(folder + r"\lib\dmustache\.git", True)
rmtree(folder + r"\lib\swagdoc\lib", True)
rmtree(folder + r"\lib\swagdoc\deploy", True)
rmtree(folder + r"\lib\swagdoc\demos", True)
def _run_tests(ctx, platform, server_type="classic"):
"""Internal function to build and execute unit tests for a specific platform and server type.
server_type: 'classic' (WebBroker), 'indydirect' (TMVCIndyServer), or 'httpsys' (TMVCHttpSysServer)"""
bin_folder = "bin32" if platform == "Win32" else "bin64"
testclient = r"unittests\general\TestClient\DMVCFrameworkTests.dproj"
if server_type == "indydirect":
testserver = r"unittests\general\TestServer\TestServerIndyDirect.dproj"
server_exe = r"unittests\general\TestServer\bin\TestServerIndyDirect.exe"
server_process_name = "TestServerIndyDirect.exe"
server_label = "Indy Direct"
elif server_type == "httpsys":
testserver = r"unittests\general\TestServer\TestServerHttpSys.dproj"
server_exe = r"unittests\general\TestServer\bin\TestServerHttpSys.exe"
server_process_name = "TestServerHttpSys.exe"
server_label = "HTTP.sys"
else:
testserver = r"unittests\general\TestServer\TestServer.dproj"
server_exe = r"unittests\general\TestServer\bin\TestServer.exe"
server_process_name = "TestServer.exe"
server_label = "Classic (WebBroker)"
print(f"\n{'='*60}")
print(f"Running {platform} tests with {server_label} server")
print(f"{'='*60}")
print("\nBuilding Unit Test client")
build_delphi_project(ctx, testclient, config="CI", platform=platform)
print(f"\nBuilding Test Server ({server_label})")
build_delphi_project(ctx, testserver, config="CI", platform=platform)
print(f"\nExecuting tests against {server_label} server...")
server_proc = subprocess.Popen(
[server_exe],
shell=True
)
time.sleep(1)
r = None
try:
r = subprocess.run(
[rf"unittests\general\TestClient\{bin_folder}\DMVCFrameworkTests.exe"]
)
if r.returncode != 0:
raise Exit(f"Cannot run unit test client ({platform}): \n" + str(r.stdout))
finally:
subprocess.run(["taskkill", "/f", "/im", server_process_name],
capture_output=True)
if r.returncode > 0:
print(r)
print(f"Unit Tests Failed ({platform}, {server_label})")
raise Exit(f"Unit tests failed ({platform}, {server_label})")
@task()
def tests32(ctx):
"""Builds and execute the unit tests (Win32) with classic server"""
_run_tests(ctx, "Win32")
@task()
def tests64(ctx):
"""Builds and execute the unit tests (Win64) with classic server"""
_run_tests(ctx, "Win64")
@task(pre=[tests32, tests64])
def tests(ctx):
"""Builds and execute all unit tests (Win32 and Win64) with classic server"""
pass
@task()
def tests32_indydirect(ctx):
"""Builds and execute the unit tests (Win32) with Indy Direct server"""
_run_tests(ctx, "Win32", "indydirect")
@task()
def tests64_indydirect(ctx):
"""Builds and execute the unit tests (Win64) with Indy Direct server"""
_run_tests(ctx, "Win64", "indydirect")
@task(pre=[tests32_indydirect, tests64_indydirect])
def tests_indydirect(ctx):
"""Builds and execute all unit tests (Win32 and Win64) with Indy Direct server"""
pass
@task()
def tests32_httpsys(ctx):
"""Builds and execute the unit tests (Win32) with HTTP.sys server"""
_run_tests(ctx, "Win32", "httpsys")
@task()
def tests64_httpsys(ctx):
"""Builds and execute the unit tests (Win64) with HTTP.sys server"""
_run_tests(ctx, "Win64", "httpsys")
@task(pre=[tests32_httpsys, tests64_httpsys])
def tests_httpsys(ctx):
"""Builds and execute all unit tests (Win32 and Win64) with HTTP.sys server"""
pass
@task(pre=[tests, tests_indydirect, tests_httpsys])
def tests_all(ctx):
"""Builds and execute all unit tests with Classic, Indy Direct, and HTTP.sys servers"""
pass
# ---------------------------------------------------------------------------
# Apache 2.4 integration tests (Layer 3)
# ---------------------------------------------------------------------------
# Uses Apache Lounge portable httpd 2.4 as host. Downloaded on first run,
# cached under unittests/apache/Apache24/. The TestServerApache project
# builds as mod_dmvctest.dll (Apache module) and is dropped into Apache's
# modules folder; Apache is started in foreground, TestClient runs against
# http://localhost:8888, then Apache is killed.
APACHE_LOUNGE_URL = (
"https://www.apachelounge.com/download/VS17/binaries/"
"httpd-2.4.66-251206-Win64-VS17.zip"
)
APACHE_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "unittests", "apache"
)
APACHE_HOME = os.path.join(APACHE_DIR, "Apache24")
def _ensure_apache():
"""Download and extract Apache Lounge if not already present."""
if os.path.isdir(os.path.join(APACHE_HOME, "bin")):
return
print(f"Downloading Apache Lounge from {APACHE_LOUNGE_URL}")
os.makedirs(APACHE_DIR, exist_ok=True)
zip_path = os.path.join(APACHE_DIR, "httpd24.zip")
import urllib.request, zipfile
urllib.request.urlretrieve(APACHE_LOUNGE_URL, zip_path)
print(f"Extracting {zip_path}")
with zipfile.ZipFile(zip_path) as z:
z.extractall(APACHE_DIR)
os.remove(zip_path)
print(f"Apache ready at {APACHE_HOME}")
def _generate_apache_conf(server_root: str, modules_dir: str,
conf_path: str, port: int = 8888) -> None:
"""Generate a minimal httpd.conf wired to mod_dmvctest."""
server_root_fwd = server_root.replace("\\", "/")
modules_fwd = modules_dir.replace("\\", "/")
conf = f"""# Auto-generated by tasks.py — do not edit manually.
ServerRoot "{server_root_fwd}"
Listen 127.0.0.1:{port}
ServerName localhost
# Required modules
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule mime_module modules/mod_mime.so
LoadModule headers_module modules/mod_headers.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule rewrite_module modules/mod_rewrite.so
# DMVCFramework test module
LoadModule dmvc_module "{modules_fwd}/mod_dmvctest.dll"
PidFile "{server_root_fwd}/logs/httpd-test.pid"
ErrorLog "{server_root_fwd}/logs/error-test.log"
LogLevel warn
# A dummy DocumentRoot is required even though it is never used — Apache
# rejects requests in the URL-to-file translation phase (AH00127) before
# any handler runs if the root cannot be mapped.
DocumentRoot "{server_root_fwd}/htdocs"
<Directory />
AllowOverride None
Require all granted
</Directory>
# Web.ApacheApp generates the handler name from the DLL name:
# lowercase(ChangeFileExt(dll_name, '-handler'))
# For mod_dmvctest.dll this yields "mod_dmvctest-handler".
# RewriteRule with [H=...] fires in the Fixup phase and wins over the
# default file-based dispatcher (catches "/", encoded chars, any path).
RewriteEngine On
RewriteRule .* - [H=mod_dmvctest-handler,L]
"""
os.makedirs(os.path.dirname(conf_path), exist_ok=True)
with open(conf_path, "w", encoding="utf-8") as f:
f.write(conf)
def _run_apache_tests(ctx, platform: str):
"""Build TestServerApache (.dll), wire Apache, run TestClient."""
bin_folder = "bin32" if platform == "Win32" else "bin64"
testclient = r"unittests\general\TestClient\DMVCFrameworkTests.dproj"
testserver = r"unittests\general\TestServer\TestServerApache.dproj"
# Built filename matches Delphi project name; renamed at deploy time
# so Apache's LoadModule line stays stable.
built_dll = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"unittests", "general", "TestServer", "bin", "TestServerApache.dll"
)
print(f"\n{'='*60}")
print(f"Running {platform} tests with Apache 2.4 module")
print(f"{'='*60}")
_ensure_apache()
print("\nBuilding Unit Test client")
build_delphi_project(ctx, testclient, config="CI", platform=platform)
print("\nBuilding TestServerApache (mod_dmvctest.dll)")
# Apache 2.4 Win64 binaries are 64-bit only — force Win64 for the module
build_delphi_project(ctx, testserver, config="CI", platform="Win64")
if not os.path.isfile(built_dll):
raise Exit(f"Apache module not built at {built_dll}")
apache_modules = os.path.join(APACHE_HOME, "modules")
shutil.copy2(built_dll, os.path.join(apache_modules, "mod_dmvctest.dll"))
# DMVC's AppPath resolves to the DLL folder when hosted by Apache.
# Copy the test fixtures that classic runs find next to TestServer.exe
# (customers.json, sample.png, www/, logs/ ...) so file-backed tests
# (static files, image serving, directory traversal) work in module mode.
testserver_bin = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"unittests", "general", "TestServer", "bin"
)
for name in ("customers.json", "sample.png"):
src = os.path.join(testserver_bin, name)
if os.path.isfile(src):
shutil.copy2(src, os.path.join(apache_modules, name))
for folder in ("www",):
src = os.path.join(testserver_bin, folder)
dst = os.path.join(apache_modules, folder)
if os.path.isdir(src):
if os.path.isdir(dst):
shutil.rmtree(dst)
shutil.copytree(src, dst)
# The WebModule config uses ViewPath = AppPath + "..\templates", which
# resolves to Apache24/templates/ under the module. Place the template
# fixtures there.
testserver_root = os.path.dirname(testserver_bin)
src_templates = os.path.join(testserver_root, "templates")
if os.path.isdir(src_templates):
dst_templates = os.path.join(APACHE_HOME, "templates")
if os.path.isdir(dst_templates):
shutil.rmtree(dst_templates)
shutil.copytree(src_templates, dst_templates)
conf_path = os.path.join(APACHE_DIR, "conf", "httpd-test.conf")
_generate_apache_conf(APACHE_HOME, apache_modules, conf_path)
httpd_exe = os.path.join(APACHE_HOME, "bin", "httpd.exe")
print(f"\nStarting Apache: {httpd_exe} -f {conf_path}")
apache_proc = subprocess.Popen(
[httpd_exe, "-f", conf_path, "-X"],
cwd=os.path.join(APACHE_HOME, "bin"),
)
time.sleep(2)
if apache_proc.poll() is not None:
raise Exit(f"Apache failed to start (exit code {apache_proc.returncode})")
r = None
try:
print(f"\nExecuting tests against Apache 2.4 module...")
# Skip tests tagged [Category('NotOnApache')] — they describe
# behaviors where Apache's request pipeline (URL validator, reason-
# phrase normalization, content-encoding negotiation) diverges from
# the DMVC expectation and cannot be emulated at framework level.
r = subprocess.run(
[rf"unittests\general\TestClient\{bin_folder}\DMVCFrameworkTests.exe",
"--exclude:NotOnApache"]
)
if r.returncode != 0:
raise Exit(f"Cannot run unit test client ({platform}): \n" + str(r.stdout))
finally:
print("Stopping Apache...")
apache_proc.terminate()
try:
apache_proc.wait(timeout=5)
except subprocess.TimeoutExpired:
apache_proc.kill()
# Belt-and-suspenders: kill any leftover httpd.exe spawned by the test
subprocess.run(["taskkill", "/f", "/im", "httpd.exe"],
capture_output=True)
if r.returncode > 0:
print(r)
raise Exit(f"Unit Tests Failed ({platform}, Apache 2.4)")
@task()
def tests64_apache(ctx):
"""Builds and execute the unit tests (Win64) hosted by Apache 2.4 module"""
_run_apache_tests(ctx, "Win64")
@task(pre=[tests64_apache])
def tests_apache(ctx):
"""Builds and execute all unit tests hosted by Apache 2.4 module (Win64 only)"""
pass
# ---------------------------------------------------------------------------
# ISAPI integration tests (Layer 3)
# ---------------------------------------------------------------------------
# Uses IIS Express (already installed with Visual Studio / RAD Studio) as
# the host. The TestServerISAPI project builds as TestServerISAPI.dll and
# is wired via a wildcard script-map handler in a generated
# applicationhost.config; IIS Express is started in foreground, TestClient
# runs against http://localhost:8888, then IIS Express is killed.
IIS_EXPRESS_64 = r"C:\Program Files\IIS Express\iisexpress.exe"
IIS_TEST_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "unittests", "iis"
)
def _ensure_iis_express():
"""Fail fast with a helpful message if IIS Express is not available."""
if not os.path.isfile(IIS_EXPRESS_64):
raise Exit(
"IIS Express not found at " + IIS_EXPRESS_64 + ".\n"
"Install IIS Express (ships with Visual Studio / RAD Studio) or "
"download it from https://www.microsoft.com/en-us/download/details.aspx?id=48264")
def _generate_iis_applicationhost_conf(isapi_dll_path: str,
conf_path: str, port: int = 8888,
site_root: str = None) -> None:
"""Start from IIS Express's shipped applicationhost.config (so every
required configSection is already declared) and patch only the site
binding + wildcard ISAPI handler.
A single site listens on localhost:PORT and maps every request
(path="*", verb="*") to TestServerISAPI.dll via IsapiModule.
"""
import re
shipped = os.path.join(os.path.dirname(IIS_EXPRESS_64),
"AppServer", "applicationhost.config")
if not os.path.isfile(shipped):
raise Exit(f"Shipped applicationhost.config not found at {shipped}")
site_root = site_root or os.path.dirname(isapi_dll_path)
content = open(shipped, encoding="utf-8").read()
# Replace the default site with ours. Match the whole <site> block
# including its nested elements.
site_re = re.compile(
r'<site name="Development Web Site".*?</site>', re.DOTALL)
new_site = (
f'<site name="DMVCTest" id="1" serverAutoStart="true">\n'
f' <application path="/" applicationPool="UnmanagedClassicAppPool">\n'
f' <virtualDirectory path="/" physicalPath="{site_root}" />\n'
f' </application>\n'
f' <bindings>\n'
f' <binding protocol="http" bindingInformation=":{port}:localhost" />\n'
f' <binding protocol="http" bindingInformation=":{port}:127.0.0.1" />\n'
f' </bindings>\n'
f' </site>'
)
if not site_re.search(content):
raise Exit("Cannot find default site in shipped applicationhost.config")
# Use a lambda so backslashes in site_root are not interpreted as
# re backreferences (Python re.sub treats \d etc. in the template).
content = site_re.sub(lambda _m: new_site, content)
# Inject our wildcard handler as the first rule under <handlers>.
handler = (
f'<add name="DMVCISAPI" path="*" verb="*" modules="IsapiModule" '
f'scriptProcessor="{isapi_dll_path}" '
f'resourceType="Unspecified" requireAccess="None" '
f'preCondition="bitness64" />'
)
content = content.replace(
'<handlers accessPolicy="Read, Script">',
'<handlers accessPolicy="Read, Execute, Script">\n ' + handler
)
# Allow double-escaped URLs and every file extension so test paths
# like "/req/with/params/%25/%20/%20" are not rejected by IIS before
# reaching the module. Relax body size while we are at it.
content = content.replace(
'<requestFiltering>',
'<requestFiltering allowDoubleEscaping="true">'
)
os.makedirs(os.path.dirname(conf_path), exist_ok=True)
with open(conf_path, "w", encoding="utf-8") as f:
f.write(content)
def _run_isapi_tests(ctx, platform: str):
"""Build TestServerISAPI (.dll), wire IIS Express, run TestClient."""
_ensure_iis_express()
bin_folder = "bin32" if platform == "Win32" else "bin64"
testclient = r"unittests\general\TestClient\DMVCFrameworkTests.dproj"
testserver = r"unittests\general\TestServer\TestServerISAPI.dproj"
built_dll = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"unittests", "general", "TestServer", "bin", "TestServerISAPI.dll"
)
print(f"\n{'='*60}")
print(f"Running {platform} tests hosted by IIS Express (ISAPI)")
print(f"{'='*60}")
print("\nBuilding Unit Test client")
build_delphi_project(ctx, testclient, config="CI", platform=platform)
print("\nBuilding TestServerISAPI")
# IIS Express x64 only loads 64-bit ISAPI when the app pool has
# enable32BitAppOnWin64=false (the default above).
build_delphi_project(ctx, testserver, config="CI", platform="Win64")
if not os.path.isfile(built_dll):
raise Exit(f"ISAPI module not built at {built_dll}")
# Deploy the ISAPI DLL + fixtures into the IIS site root so paths
# resolve the same way they do next to TestServer.exe in classic runs.
site_root = os.path.join(IIS_TEST_DIR, "site")
os.makedirs(site_root, exist_ok=True)
shutil.copy2(built_dll, os.path.join(site_root, "TestServerISAPI.dll"))
testserver_bin = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"unittests", "general", "TestServer", "bin"
)
for name in ("customers.json", "sample.png"):
src = os.path.join(testserver_bin, name)
if os.path.isfile(src):
shutil.copy2(src, os.path.join(site_root, name))
for folder in ("www",):
src = os.path.join(testserver_bin, folder)
dst = os.path.join(site_root, folder)
if os.path.isdir(src):
if os.path.isdir(dst):
shutil.rmtree(dst)
shutil.copytree(src, dst)
# ViewPath = AppPath + "..\templates" → Apache/IIS site parent
testserver_root = os.path.dirname(testserver_bin)
src_templates = os.path.join(testserver_root, "templates")
if os.path.isdir(src_templates):
dst_templates = os.path.join(IIS_TEST_DIR, "templates")
if os.path.isdir(dst_templates):
shutil.rmtree(dst_templates)
shutil.copytree(src_templates, dst_templates)
conf_path = os.path.join(IIS_TEST_DIR, "config", "applicationhost.config")
deployed_dll = os.path.join(site_root, "TestServerISAPI.dll")
_generate_iis_applicationhost_conf(deployed_dll, conf_path, site_root=site_root)
print(f"\nStarting IIS Express: {IIS_EXPRESS_64} /config:{conf_path} /site:DMVCTest")
iis_proc = subprocess.Popen(
[IIS_EXPRESS_64, f"/config:{conf_path}", "/site:DMVCTest", "/trace:error"],
cwd=os.path.dirname(IIS_EXPRESS_64),
)
time.sleep(2)
if iis_proc.poll() is not None:
raise Exit(f"IIS Express failed to start (exit code {iis_proc.returncode})")
r = None
try:
print(f"\nExecuting tests against ISAPI (IIS Express)...")
# Skip tests tagged [Category('NotOnIIS')] — behaviors managed by
# IIS itself (content-encoding negotiation) rather than the module.
r = subprocess.run(
[rf"unittests\general\TestClient\{bin_folder}\DMVCFrameworkTests.exe",
"--exclude:NotOnIIS"]
)
if r.returncode != 0:
raise Exit(f"Cannot run unit test client ({platform}): \n" + str(r.stdout))
finally:
print("Stopping IIS Express...")
iis_proc.terminate()
try:
iis_proc.wait(timeout=5)
except subprocess.TimeoutExpired:
iis_proc.kill()
subprocess.run(["taskkill", "/f", "/im", "iisexpress.exe"],
capture_output=True)
if r.returncode > 0:
print(r)
raise Exit(f"Unit Tests Failed ({platform}, ISAPI)")
@task()
def tests64_isapi(ctx):
"""Builds and execute the unit tests (Win64) hosted by IIS Express ISAPI"""
_run_isapi_tests(ctx, "Win64")
@task(pre=[tests64_isapi])
def tests_isapi(ctx):
"""Builds and execute all unit tests hosted by IIS Express ISAPI (Win64 only)"""
pass
@task(pre=[tests, tests_indydirect, tests_httpsys, tests_apache, tests_isapi])
def tests_all_hosts(ctx):
"""Run the full unit test matrix against every supported host: Classic
(WebBroker+Indy bridge), Indy Direct, HTTP.sys, Apache 2.4 module,
ISAPI (IIS Express)."""
pass
def get_version_from_file():
with open(r".\sources\dmvcframeworkbuildconsts.inc") as f:
lines = f.readlines()
res = [x for x in lines if "DMVCFRAMEWORK_VERSION" in x]
if len(res) != 1:
raise Exception(
"Cannot find DMVCFRAMEWORK_VERSION in dmvcframeworkbuildconsts.inc file"
)
version_line: str = res[0]
version_line = version_line.strip(" ;\t")
pieces = version_line.split("=")
if len(pieces) != 2:
raise Exception(
"Version line in wrong format in dmvcframeworkbuildconsts.inc file: "
+ version_line
)
version = pieces[1].strip("' ")
if not "framework" in version:
version = "dmvcframework-" + version
if "beta" in version.lower():
print(Fore.RESET + Fore.RED + "WARNING - BETA VERSION: " + version + Fore.RESET)
else:
print(Fore.RESET + Fore.GREEN + "BUILDING VERSION: " + version + Fore.RESET)
return version
@task()
def release(
ctx,
skip_build=False,
skip_tests=False,
):
"""Builds all the projects, executes integration tests and prepare the release"""
version = get_version_from_file()
init_build(version, clean_releases=True)
if not skip_tests:
tests32(ctx)
tests64(ctx)
if not skip_build:
delphi_projects = get_delphi_projects_to_build("")
if not _build_projects(ctx, delphi_projects, version, ""):
return False
print(Fore.RESET)
copy_sources()
copy_libs(ctx)
clean(ctx)
zip_samples(ctx, version)
create_zip(ctx, version)
return True
def _build_projects(ctx, delphi_projects, version, filter):
return build_delphi_project_list(ctx, delphi_projects, version, filter)
@task
def build_samples(ctx, version="DEBUG", filter=""):
"""Builds samples"""
init_build(version)
delphi_projects = get_delphi_projects_to_build("samples")
return _build_projects(ctx, delphi_projects, version, filter)
@task(post=[])
def build_core(ctx, version="DEBUG"):
"""Builds core packages extensions"""
init_build(version)
delphi_projects = get_delphi_projects_to_build("core")
if not _build_projects(ctx, delphi_projects, version, ""):
raise Exit("Build failed")
def parse_template(tmpl: List[str]):
main_tmpl = []
intf_tmpl = []
impl_tmpl = []
state = "verbatim"
for row in tmpl:
if row.upper().strip() == "///INTERFACE.BEGIN":
state = "parsing.interface"
continue
if row.upper().strip() == "///IMPLEMENTATION.BEGIN":
state = "parsing.implementation"
continue
if row.upper().strip() in ["///INTERFACE.END", "///IMPLEMENTATION.END"]:
if state == "parsing.interface":
main_tmpl.append("$INTERFACE$")
if state == "parsing.implementation":
main_tmpl.append("$IMPLEMENTATION$")
state = "verbatim"
continue
if state == "parsing.interface":
intf_tmpl.append(row)
elif state == "parsing.implementation":
impl_tmpl.append(row)
elif state == "verbatim":
main_tmpl.append(row)
return main_tmpl, intf_tmpl, impl_tmpl
@task
def generate_nullables(ctx):
import pathlib
src_folder = pathlib.Path(__file__).parent.joinpath("sources")
template_unitname = src_folder.joinpath("MVCFramework.Nullables.pas.template")
output_unitname = src_folder.joinpath("MVCFramework.Nullables.pas")
with open(template_unitname, "r") as f:
rows = f.readlines()
main_tmpl, intf_tmpl, impl_tmpl = parse_template(rows)