11const std = @import ("std" );
2- const builtin = @import ("builtin" );
32const pyoz = @import ("PyOZ" );
43const version = @import ("version" );
54
65const project = @import ("project.zig" );
76const builder = @import ("builder.zig" );
7+ const commands = @import ("commands.zig" );
88const wheel = @import ("wheel.zig" );
9- const symreader = @import ("symreader.zig" );
109
1110fn init_project (name : ? []const u8 , in_current_dir : ? bool , local_pyoz_path : ? []const u8 , package_layout : ? bool ) ! void {
1211 try project .create (std .heap .page_allocator , name , in_current_dir orelse false , local_pyoz_path , package_layout orelse false );
@@ -29,137 +28,22 @@ fn publish_wheels(test_pypi: ?bool) !void {
2928}
3029
3130fn run_tests (release : ? bool , verbose : ? bool ) ! void {
32- const alloc = std .heap .page_allocator ;
33- const rel = release orelse false ;
34- const verb = verbose orelse false ;
35-
36- // Build the module
37- var build_result = try builder .buildModule (alloc , rel );
38- defer build_result .deinit (alloc );
39-
40- // Extract tests from the compiled module
41- const test_content = try symreader .extractTests (alloc , build_result .module_path );
42- if (test_content == null or test_content .? .len == 0 ) {
43- std .debug .print ("\n No tests found in module.\n " , .{});
44- std .debug .print ("Add .tests to your pyoz.module() config:\n\n " , .{});
45- std .debug .print (" .tests = &.{{\n " , .{});
46- std .debug .print (" pyoz.@\" test\" (\" my test\" ,\n " , .{});
47- std .debug .print (" \\\\ assert mymod.add(2, 3) == 5\n " , .{});
48- std .debug .print (" ),\n " , .{});
49- std .debug .print (" }},\n " , .{});
50- return ;
51- }
52- defer alloc .free (test_content .? );
53-
54- // Write test file
55- const test_file = "zig-out/lib/__pyoz_test.py" ;
56- {
57- const f = try std .fs .cwd ().createFile (test_file , .{});
58- defer f .close ();
59- try f .writeAll (test_content .? );
60- }
61-
62- std .debug .print ("\n Running tests...\n\n " , .{});
63-
64- const python_cmd = builder .getPythonCommand ();
65- const path_sep = if (builtin .os .tag == .windows ) ";" else ":" ;
66-
67- const existing_pp = std .process .getEnvVarOwned (alloc , "PYTHONPATH" ) catch "" ;
68- defer if (existing_pp .len > 0 ) alloc .free (existing_pp );
69-
70- const new_pp = if (existing_pp .len > 0 )
71- try std .fmt .allocPrint (alloc , "zig-out/lib{s}{s}" , .{ path_sep , existing_pp })
72- else
73- try alloc .dupe (u8 , "zig-out/lib" );
74- defer alloc .free (new_pp );
75-
76- var argv_buf : [6 ][]const u8 = undefined ;
77- var argc : usize = 0 ;
78- argv_buf [argc ] = python_cmd ;
79- argc += 1 ;
80- argv_buf [argc ] = "-m" ;
81- argc += 1 ;
82- argv_buf [argc ] = "unittest" ;
83- argc += 1 ;
84- argv_buf [argc ] = test_file ;
85- argc += 1 ;
86- if (verb ) {
87- argv_buf [argc ] = "-v" ;
88- argc += 1 ;
31+ var args_buf : [2 ][]const u8 = undefined ;
32+ var args_len : usize = 0 ;
33+ if (release orelse false ) {
34+ args_buf [args_len ] = "--release" ;
35+ args_len += 1 ;
8936 }
90-
91- var env_map = try std .process .getEnvMap (alloc );
92- defer env_map .deinit ();
93- try env_map .put ("PYTHONPATH" , new_pp );
94-
95- var child = std .process .Child .init (argv_buf [0.. argc ], alloc );
96- child .env_map = & env_map ;
97- child .stderr_behavior = .Inherit ;
98- child .stdout_behavior = .Inherit ;
99-
100- const term = try child .spawnAndWait ();
101- if (term .Exited != 0 ) {
102- std .process .exit (1 );
37+ if (verbose orelse false ) {
38+ args_buf [args_len ] = "--verbose" ;
39+ args_len += 1 ;
10340 }
41+ try commands .runTests (std .heap .page_allocator , args_buf [0.. args_len ]);
10442}
10543
10644fn run_bench () ! void {
107- const alloc = std .heap .page_allocator ;
108-
109- // Always build in release mode for benchmarks
110- var build_result = try builder .buildModule (alloc , true );
111- defer build_result .deinit (alloc );
112-
113- // Extract benchmarks from the compiled module
114- const bench_content = try symreader .extractBenchmarks (alloc , build_result .module_path );
115- if (bench_content == null or bench_content .? .len == 0 ) {
116- std .debug .print ("\n No benchmarks found in module.\n " , .{});
117- std .debug .print ("Add .benchmarks to your pyoz.module() config:\n\n " , .{});
118- std .debug .print (" .benchmarks = &.{{\n " , .{});
119- std .debug .print (" pyoz.bench(\" my benchmark\" ,\n " , .{});
120- std .debug .print (" \\\\ mymod.add(100, 200)\n " , .{});
121- std .debug .print (" ),\n " , .{});
122- std .debug .print (" }},\n " , .{});
123- return ;
124- }
125- defer alloc .free (bench_content .? );
126-
127- // Write benchmark file
128- const bench_file = "zig-out/lib/__pyoz_bench.py" ;
129- {
130- const f = try std .fs .cwd ().createFile (bench_file , .{});
131- defer f .close ();
132- try f .writeAll (bench_content .? );
133- }
134-
135- std .debug .print ("\n Running benchmarks...\n " , .{});
136-
137- const python_cmd = builder .getPythonCommand ();
138- const path_sep = if (builtin .os .tag == .windows ) ";" else ":" ;
139-
140- const existing_pp = std .process .getEnvVarOwned (alloc , "PYTHONPATH" ) catch "" ;
141- defer if (existing_pp .len > 0 ) alloc .free (existing_pp );
142-
143- const new_pp = if (existing_pp .len > 0 )
144- try std .fmt .allocPrint (alloc , "zig-out/lib{s}{s}" , .{ path_sep , existing_pp })
145- else
146- try alloc .dupe (u8 , "zig-out/lib" );
147- defer alloc .free (new_pp );
148-
149- var env_map = try std .process .getEnvMap (alloc );
150- defer env_map .deinit ();
151- try env_map .put ("PYTHONPATH" , new_pp );
152-
153- const argv = [_ ][]const u8 { python_cmd , bench_file };
154- var child = std .process .Child .init (& argv , alloc );
155- child .env_map = & env_map ;
156- child .stderr_behavior = .Inherit ;
157- child .stdout_behavior = .Inherit ;
158-
159- const term = try child .spawnAndWait ();
160- if (term .Exited != 0 ) {
161- std .process .exit (1 );
162- }
45+ const args = [_ ][]const u8 {};
46+ try commands .runBench (std .heap .page_allocator , & args );
16347}
16448
16549fn get_version () []const u8 {
0 commit comments