ognibuild 0.2.19

Detect and run any build system
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
//! Support for Python build systems.
//!
//! This module provides functionality for building, testing, and installing
//! Python packages using various build systems such as setuptools, poetry, and pip.

use crate::analyze::{run_detecting_problems, AnalyzedError};
use crate::buildsystem::{BuildSystem, DependencyCategory, Error, InstallTarget};
use crate::dependencies::python::{PythonDependency, PythonPackageDependency};
use crate::dependency::Dependency;
use crate::dist_catcher::DistCatcher;
use crate::fix_build::BuildFixer;
use crate::installer::{Error as InstallerError, InstallationScope, Installer};
use crate::output::{BinaryOutput, Output, PythonExtensionOutput, PythonPackageOutput};
use crate::session::Session;
use pyo3::exceptions::{PyFileNotFoundError, PyImportError, PyModuleNotFoundError, PySystemExit};
use pyo3::prelude::*;
use pyo3::types::PyDict;
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::io::Seek;
use std::path::{Path, PathBuf};
use toml;

#[derive(Debug, Deserialize, Default)]
#[serde(default)]
#[allow(dead_code)]
struct Distribution {
    name: Option<String>,
    requires: Vec<String>,
    setup_requires: Vec<String>,
    install_requires: Vec<String>,
    tests_require: Vec<String>,
    scripts: Vec<String>,
    packages: Vec<String>,
    entry_points: HashMap<String, Vec<String>>,
    /// Names of the compiled extension modules declared by `ext_modules`.
    ext_modules: Vec<String>,
}

fn load_toml(path: &Path) -> Result<pyproject_toml::PyProjectToml, PyErr> {
    let path = path.join("pyproject.toml");
    let text = match std::fs::read_to_string(&path) {
        Ok(text) => text,
        Err(e) => {
            return Err(match e.kind() {
                std::io::ErrorKind::NotFound => {
                    PyFileNotFoundError::new_err(format!("File not found: {}", path.display()))
                }
                _ => pyo3::exceptions::PyIOError::new_err(format!(
                    "Failed to read {}: {}",
                    path.display(),
                    e
                )),
            })
        }
    };

    match toml::from_str(&text) {
        Ok(parsed) => Ok(parsed),
        Err(e) => Err(pyo3::exceptions::PyValueError::new_err(format!(
            "Failed to parse {}: {}",
            path.display(),
            e
        ))),
    }
}

#[derive(Debug)]
/// A wrapper around a Python setup.cfg configuration file.
///
/// This provides access to the configuration in a setup.cfg file, which is used
/// by setuptools to configure Python package builds.
pub struct SetupCfg(Py<PyAny>);

impl SetupCfg {
    fn has_section(&self, section: &str) -> bool {
        Python::attach(|py| {
            self.0
                .call_method1(py, "__contains__", (section,))
                .unwrap()
                .extract::<bool>(py)
                .unwrap()
        })
    }

    fn get_section(&self, section: &str) -> Option<SetupCfgSection> {
        Python::attach(|py| {
            if self.has_section(section) {
                let section: Option<Py<PyAny>> = self
                    .0
                    .call_method1(py, "get", (section, py.None()))
                    .unwrap()
                    .extract(py)
                    .ok();
                Some(SetupCfgSection(section.unwrap()))
            } else {
                None
            }
        })
    }
}

/// A section in a Python setup.cfg configuration file.
///
/// This provides access to a specific section in a setup.cfg file, allowing
/// access to configuration keys within that section.
pub struct SetupCfgSection(Py<PyAny>);

impl Default for SetupCfg {
    fn default() -> Self {
        Python::attach(|py| SetupCfg(py.None()))
    }
}

impl SetupCfgSection {
    fn get<T: for<'a, 'py> FromPyObject<'a, 'py>>(&self, key: &str) -> Option<T> {
        Python::attach(|py| {
            self.0
                .call_method1(py, "get", (key, py.None()))
                .ok()?
                .extract::<Option<T>>(py)
                .ok()?
        })
    }

    /// Check if a key exists in this section.
    ///
    /// # Arguments
    /// * `key` - The key to check for
    ///
    /// # Returns
    /// `true` if the key exists, `false` otherwise
    pub fn has_key(&self, key: &str) -> bool {
        Python::attach(|py| {
            self.0
                .call_method1(py, "__contains__", (key,))
                .unwrap()
                .extract::<bool>(py)
                .unwrap()
        })
    }
}

fn load_setup_cfg(path: &Path) -> Result<Option<SetupCfg>, PyErr> {
    Python::attach(|py| {
        let m = py.import("setuptools.config.setupcfg")?;
        let read_configuration = m.getattr("read_configuration")?;

        let p = path.join("setup.cfg");

        if p.exists() {
            let config = read_configuration.call1((p,))?;
            Ok(Some(SetupCfg(config.unbind())))
        } else {
            Ok(None)
        }
    })
}

//  run_setup, but setting __name__
// Imported from Python's distutils.core, Copyright (C) PSF

/// run_setup mutates interpreter-global state (distutils.core._setup_stop_after
/// and ._setup_distribution, sys.argv, and the process working directory), so
/// concurrent callers would read back each other's results.
///
/// Take this before attaching to the interpreter, never while already holding
/// the GIL: a thread holding the lock has to reacquire the GIL to finish, so
/// the reverse order deadlocks.
static RUN_SETUP_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

fn run_setup(py: Python, script_name: &Path, stop_after: &str) -> PyResult<Py<PyAny>> {
    assert!(
        stop_after == "init"
            || stop_after == "config"
            || stop_after == "commandline"
            || stop_after == "run"
    );
    // Import setuptools, just in case it decides to replace distutils
    let _ = py.import("setuptools");

    let core = match py.import("distutils.core") {
        Ok(m) => m,
        Err(e) if e.is_instance_of::<PyImportError>(py) => {
            // Importing distutils failed, but that's fine.
            py.import("setuptools._distutils.core")?
        }
        Err(e) => return Err(e),
    };

    core.setattr("_setup_stop_after", stop_after)?;
    // Stale from a previous run otherwise: distutils only ever assigns this,
    // so without clearing it a setup.py that never calls setup() would appear
    // to have produced the previous caller's Distribution.
    core.setattr("_setup_distribution", py.None())?;

    let sys = py.import("sys")?;
    let os = py.import("os")?;

    let save_argv = sys.getattr("argv")?;

    let g = PyDict::new(py);
    g.set_item("__file__", script_name)?;
    g.set_item("__name__", "__main__")?;

    let old_cwd = os.getattr("getcwd")?.call0()?.extract::<String>()?;
    os.call_method1(
        "chdir",
        (os.getattr("path")?
            .call_method1("dirname", (script_name,))?,),
    )?;

    sys.setattr("argv", vec![script_name])?;

    let text = std::fs::read_to_string(script_name)?;

    let code = std::ffi::CString::new(text).unwrap();
    // run, not eval: a setup.py is a sequence of statements, and eval only
    // accepts a single expression.
    let r = py.run(&code, Some(&g), None);

    os.call_method1("chdir", (old_cwd,))?;
    sys.setattr("argv", save_argv)?;
    core.setattr("_setup_stop_after", py.None())?;

    match r {
        Ok(_) => Ok(core.getattr("_setup_distribution")?.unbind()),
        Err(e) if e.is_instance_of::<PySystemExit>(py) => {
            Ok(core.getattr("_setup_distribution")?.unbind())
        }
        Err(e) => Err(e),
    }
}

const SETUP_WRAPPER: &str = r#"""
# setuptools is what a setup.py imports, and on Python 3.12+ it is also what
# provides distutils (removed from the stdlib). Let the ImportError bubble up
# rather than swallowing it: a session missing setuptools would otherwise yield
# empty metadata that looks like a project declaring nothing, and the caller's
# build fixers can install setuptools once the failure names it.
import setuptools
from distutils import core
import os
import json
import sys
script_name = "%(script_name)s"
os.chdir(os.path.dirname(script_name))

g = {"__file__": os.path.basename(script_name), "__name__": "__main__"}
try:
    core._setup_stop_after = "init"
    sys.argv[0] = script_name
    with open(script_name, "rb") as f:
        exec(f.read(), g)
except SystemExit:
    # Hmm, should we do something if exiting with a non-zero code
    # (ie. error)?
    pass

if core._setup_distribution is None:
    raise RuntimeError(
        (
            "'distutils.core.setup()' was never called -- "
            "perhaps '%s' is not a Distutils setup script?"
        )
        % script_name
    )

d = core._setup_distribution
r = {
    # d.name is the --name display flag, not the metadata; use d.metadata.
    'name': getattr(d.metadata, "name", None) or None,
    'setup_requires': getattr(d, "setup_requires", []),
    'install_requires': getattr(d, "install_requires", []),
    'tests_require': getattr(d, "tests_require", []) or [],
    'scripts': getattr(d, "scripts", []) or [],
    'entry_points': getattr(d, "entry_points", None) or {},
    'packages': getattr(d, "packages", []) or [],
    'requires': d.get_requires() or [],
    'ext_modules': [e.name for e in (getattr(d, "ext_modules", None) or [])],
    }
with open(%(output_path)s, 'w') as f:
    json.dump(r, f)
"""#;

/// Read an optional attribute off a distutils Distribution, treating both a
/// missing attribute and `None` as absent.
///
/// Distribution.__getattr__ falls back to the command-option namespace, so
/// e.g. `name` resolves to the `--name` display flag (a bool) rather than the
/// metadata. Anything that does not extract to the expected type is therefore
/// treated as absent rather than trusted.
fn attr_or_default<'py, T>(d: &Bound<'py, PyAny>, name: &str) -> T
where
    T: for<'a> FromPyObject<'a, 'py> + Default,
{
    d.getattr(name)
        .ok()
        .and_then(|v| v.extract::<T>().ok())
        .unwrap_or_default()
}

fn distribution_from_object(d: &Bound<'_, PyAny>) -> PyResult<Distribution> {
    // The real name lives on the metadata object; d.name is the display flag.
    let name: Option<String> = d
        .getattr("metadata")
        .ok()
        .and_then(|m| m.getattr("name").ok())
        .and_then(|n| n.extract::<Option<String>>().ok())
        .flatten();

    // ext_modules holds Extension objects, not strings.
    let ext_modules: Vec<String> = d
        .getattr("ext_modules")
        .ok()
        .and_then(|v| v.extract::<Vec<Bound<PyAny>>>().ok())
        .unwrap_or_default()
        .iter()
        .filter_map(|ext| ext.getattr("name").ok()?.extract().ok())
        .collect();

    let requires: Vec<String> = d
        .call_method0("get_requires")?
        .extract()
        .unwrap_or_default();

    Ok(Distribution {
        name,
        setup_requires: attr_or_default(d, "setup_requires"),
        install_requires: attr_or_default(d, "install_requires"),
        tests_require: attr_or_default(d, "tests_require"),
        scripts: attr_or_default(d, "scripts"),
        entry_points: attr_or_default(d, "entry_points"),
        packages: attr_or_default(d, "packages"),
        requires,
        ext_modules,
    })
}

#[derive(Debug)]
/// A Python setuptools-based build system.
///
/// This build system handles Python packages that use setup.py for building,
/// which is the traditional approach for Python packages.
pub struct SetupPy {
    path: PathBuf,
    has_setup_py: bool,
    config: Option<SetupCfg>,
    pyproject: Option<pyproject_toml::PyProjectToml>,
    #[allow(dead_code)]
    buildsystem: Option<String>,
}

impl SetupPy {
    /// Create a new SetupPy build system with the specified path.
    ///
    /// This will load and parse setup.cfg and pyproject.toml if they exist.
    ///
    /// # Arguments
    /// * `path` - The path to the Python project directory
    ///
    /// # Returns
    /// A new SetupPy build system instance
    pub fn new(path: &Path) -> Self {
        let has_setup_py = path.join("setup.py").exists();

        Python::attach(|py| {
            let config = match load_setup_cfg(path) {
                Ok(config) => config,
                Err(e) if e.is_instance_of::<PyFileNotFoundError>(py) => None,
                Err(e) if e.is_instance_of::<PyModuleNotFoundError>(py) => {
                    log::warn!("Error parsing setup.cfg: {}", e);
                    None
                }
                Err(e) => {
                    panic!("Error parsing setup.cfg: {}", e);
                }
            };

            let (pyproject, buildsystem) = match load_toml(path) {
                Ok(pyproject) => {
                    let buildsystem = pyproject
                        .build_system
                        .as_ref()
                        .and_then(|bs| bs.build_backend.clone());
                    (Some(pyproject), buildsystem)
                }
                Err(e) if e.is_instance_of::<PyFileNotFoundError>(py) => (None, None),
                Err(e) => {
                    panic!("Error parsing pyproject.toml: {}", e);
                }
            };

            Self {
                has_setup_py,
                path: path.to_owned(),
                config,
                pyproject,
                buildsystem,
            }
        })
    }

    /// Probe a directory for a Python setuptools build system.
    ///
    /// # Arguments
    /// * `path` - The path to check
    ///
    /// # Returns
    /// A SetupPy build system if one exists at the path, `None` otherwise
    pub fn probe(path: &Path) -> Option<Box<dyn BuildSystem>> {
        if path.join("setup.py").exists() {
            log::debug!("Found setup.py, assuming python project.");
            return Some(Box::new(Self::new(path)));
        }
        if path.join("pyproject.toml").exists() {
            log::debug!("Found pyproject.toml, assuming python project.");
            return Some(Box::new(Self::new(path)));
        }
        None
    }

    fn extract_setup_direct(&self) -> Result<Distribution, Error> {
        let p = self
            .path
            .join("setup.py")
            .canonicalize()
            .map_err(Error::IoError)?;

        let _guard = RUN_SETUP_LOCK.lock().unwrap_or_else(|e| e.into_inner());

        Python::attach(|py| {
            let d = run_setup(py, &p, "init")
                .map_err(|e| Error::Other(format!("Unable to load setup.py metadata: {}", e)))?
                .into_bound(py);

            if d.is_none() {
                return Err(Error::Other(format!(
                    "'distutils.core.setup()' was never called -- perhaps {} is not a Distutils setup script?",
                    p.display()
                )));
            }

            distribution_from_object(&d)
                .map_err(|e| Error::Other(format!("Unable to read setup.py metadata: {}", e)))
        })
    }

    fn determine_interpreter(&self) -> String {
        if let Some(config) = self.config.as_ref() {
            let python_requires: Option<String> = config
                .get_section("options")
                .and_then(|s| s.get::<String>("python_requires"));
            if python_requires
                .map(|pr| !pr.contains("2.7"))
                .unwrap_or(true)
            {
                return "python3".to_owned();
            }
        }
        let path = self.path.join("setup.py");
        let shebang_binary = crate::shebang::shebang_binary(&path).unwrap();

        shebang_binary.unwrap_or("python3".to_owned())
    }

    fn extract_setup_in_session(
        &self,
        session: &dyn Session,
        fixers: Option<&[&dyn BuildFixer<InstallerError>]>,
    ) -> Result<Distribution, Error> {
        let interpreter = self.determine_interpreter();

        let mut output_f = tempfile::NamedTempFile::new_in(session.location().join("tmp")).unwrap();
        let argv: Vec<String> = vec![
            interpreter,
            "-c".to_string(),
            SETUP_WRAPPER
                .replace(
                    "%(script_name)s",
                    session.pwd().join("setup.py").to_str().unwrap(),
                )
                .replace(
                    "%(output_path)s",
                    &format!(
                        "\"/{}\"",
                        output_f
                            .path()
                            .to_str()
                            .unwrap()
                            .strip_prefix(session.location().to_str().unwrap())
                            .unwrap()
                    ),
                ),
        ];
        let r = if let Some(fixers) = fixers {
            session
                .command(argv.iter().map(|x| x.as_str()).collect::<Vec<_>>())
                .quiet(true)
                .run_fixing_problems::<_, Error>(fixers)
                .map(|_| ())
                .map_err(|e| e.to_string())
        } else {
            session
                .command(argv.iter().map(|x| x.as_str()).collect())
                .check_call()
                .map_err(|e| e.to_string())
        };
        r.map_err(|e| Error::Other(format!("Unable to load setup.py metadata: {}", e)))?;

        output_f
            .seek(std::io::SeekFrom::Start(0))
            .map_err(Error::IoError)?;
        serde_json::from_reader(output_f)
            .map_err(|e| Error::Other(format!("Unable to parse setup.py metadata: {}", e)))
    }

    /// Extract the metadata declared by setup.py.
    ///
    /// Returns `Ok(None)` when there is no setup.py to read; a setup.py that
    /// exists but cannot be introspected is an error, not an absence, since
    /// callers would otherwise mistake a failed extraction for a project that
    /// declares nothing.
    fn extract_setup(
        &self,
        session: Option<&dyn Session>,
        fixers: Option<&[&dyn BuildFixer<InstallerError>]>,
    ) -> Result<Option<Distribution>, Error> {
        if !self.has_setup_py {
            return Ok(None);
        }
        if let Some(session) = session {
            self.extract_setup_in_session(session, fixers).map(Some)
        } else {
            self.extract_setup_direct().map(Some)
        }
    }

    fn setup_requires(&self) -> Vec<PythonPackageDependency> {
        let mut ret = vec![];
        if let Some(build_system) = self
            .pyproject
            .as_ref()
            .and_then(|p| p.build_system.as_ref())
        {
            let requires = &build_system.requires;
            for require in requires {
                ret.push(PythonPackageDependency::from(require.clone()));
            }
        }

        if let Some(config) = &self.config {
            let options = config.get_section("options");
            let setup_requires = options
                .and_then(|os| os.get::<Vec<String>>("setup_requires"))
                .unwrap_or(vec![]);
            for require in &setup_requires {
                ret.push(PythonPackageDependency::try_from(require.clone()).unwrap());
            }
        }
        ret
    }

    fn run_setup(
        &self,
        session: &dyn Session,
        installer: &dyn Installer,
        args: Vec<&str>,
    ) -> Result<(), Error> {
        // Install the setup_requires beforehand, since otherwise
        // setuptools might fetch eggs instead of our preferred installer.
        let setup_requires = self
            .setup_requires()
            .into_iter()
            .map(|x| Box::new(x) as Box<dyn Dependency>)
            .collect::<Vec<_>>();
        crate::installer::install_missing_deps(
            session,
            installer,
            &[crate::installer::InstallationScope::Global],
            setup_requires
                .iter()
                .map(|x| x.as_ref())
                .collect::<Vec<_>>()
                .as_slice(),
        )?;
        let interpreter = self.determine_interpreter().clone();
        let mut args = args.clone();
        args.insert(0, &interpreter);
        args.insert(1, "setup.py");
        // TODO(jelmer): Perhaps this should be additive?
        session.command(args).run_detecting_problems()?;
        Ok(())
    }
}

impl BuildSystem for SetupPy {
    fn test(&self, session: &dyn Session, installer: &dyn Installer) -> Result<(), Error> {
        if self.path.join("tox.ini").exists() {
            run_detecting_problems(
                session,
                vec!["tox", "--skip-missing-interpreters"],
                None,
                false,
                None,
                None,
                None,
                None,
            )?;
            return Ok(());
        }
        if self
            .config
            .as_ref()
            .map(|c| c.has_section("tool:pytest") || c.has_section("pytest"))
            .unwrap_or(false)
        {
            session.command(vec!["pytest"]).run_detecting_problems()?;
            return Ok(());
        }
        if self.has_setup_py {
            // Pre-emptively install setuptools, since distutils doesn't provide
            // a 'test' subcommand and some packages fall back to distutils
            // if setuptools is not available.
            let setuptools_dep = PythonPackageDependency::simple("setuptools");
            if !setuptools_dep.present(session) {
                installer.install(&setuptools_dep, InstallationScope::Global)?;
            }
            match self.run_setup(session, installer, vec!["test"]) {
                Ok(_) => {
                    return Ok(());
                }
                Err(Error::Error(AnalyzedError::Unidentified { lines, .. }))
                    if lines.contains(&"error: invalid command 'test'".to_string()) =>
                {
                    return Ok(());
                }
                Err(e) => {
                    return Err(e);
                }
            }
        }
        unimplemented!();
    }

    fn build(&self, session: &dyn Session, installer: &dyn Installer) -> Result<(), Error> {
        if self.has_setup_py {
            self.run_setup(session, installer, vec!["build"])
        } else {
            unimplemented!();
        }
    }

    fn dist(
        &self,
        session: &dyn Session,
        installer: &dyn Installer,
        target_directory: &Path,
        quiet: bool,
    ) -> Result<std::ffi::OsString, Error> {
        // TODO(jelmer): Look at self.build_backend
        let dc = DistCatcher::new(vec![session.external_path(Path::new("dist"))]);
        if self.has_setup_py {
            let mut preargs = vec![];
            if quiet {
                preargs.push("--quiet");
            }
            // Preemptively install setuptools since some packages fail in some way without it.
            let setuptools_req = PythonPackageDependency::simple("setuptools");
            if !setuptools_req.present(session) {
                installer.install(&setuptools_req, InstallationScope::Global)?;
            }
            preargs.push("sdist");
            self.run_setup(session, installer, preargs)?;
        } else if self.pyproject.is_some() {
            run_detecting_problems(
                session,
                vec!["python3", "-m", "build", "--sdist", "."],
                None,
                false,
                None,
                None,
                None,
                None,
            )?;
        } else {
            panic!("No setup.py or pyproject.toml");
        }
        Ok(dc.copy_single(target_directory).unwrap().unwrap())
    }

    fn clean(&self, session: &dyn Session, installer: &dyn Installer) -> Result<(), Error> {
        if self.has_setup_py {
            self.run_setup(session, installer, vec!["clean"])
        } else {
            unimplemented!();
        }
    }

    fn install(
        &self,
        session: &dyn Session,
        installer: &dyn Installer,
        install_target: &InstallTarget,
    ) -> Result<(), Error> {
        if self.has_setup_py {
            let mut args = vec![];
            if install_target.scope == InstallationScope::User {
                args.push("--user".to_string());
            }
            if let Some(prefix) = install_target.prefix.as_ref() {
                args.push(format!("--prefix={}", prefix.to_str().unwrap()));
            }
            args.insert(0, "install".to_owned());
            self.run_setup(
                session,
                installer,
                args.iter().map(|x| x.as_str()).collect(),
            )?;
            Ok(())
        } else {
            unimplemented!();
        }
    }

    fn get_declared_dependencies(
        &self,
        session: &dyn Session,
        fixers: std::option::Option<&[&dyn BuildFixer<InstallerError>]>,
    ) -> Result<Vec<(DependencyCategory, Box<dyn Dependency>)>, Error> {
        let mut ret: Vec<(DependencyCategory, Box<dyn Dependency>)> = vec![];
        let distribution = self.extract_setup(Some(session), fixers)?;
        if let Some(distribution) = distribution {
            for require in &distribution.requires {
                ret.push((
                    DependencyCategory::Universal,
                    Box::new(PythonPackageDependency::try_from(require.clone()).unwrap()),
                ));
            }
            // Not present for distutils-only packages
            for require in &distribution.setup_requires {
                ret.push((
                    DependencyCategory::Build,
                    Box::new(PythonPackageDependency::try_from(require.clone()).unwrap()),
                ));
            }
            // Not present for distutils-only packages
            for require in &distribution.install_requires {
                ret.push((
                    DependencyCategory::Universal,
                    Box::new(PythonPackageDependency::try_from(require.clone()).unwrap()),
                ));
            }
            // Not present for distutils-only packages
            for require in &distribution.tests_require {
                ret.push((
                    DependencyCategory::Test,
                    Box::new(PythonPackageDependency::try_from(require.clone()).unwrap()),
                ));
            }
        }
        if let Some(pyproject) = self.pyproject.as_ref() {
            if let Some(build_system) = pyproject.build_system.as_ref() {
                for require in &build_system.requires {
                    ret.push((
                        DependencyCategory::Build,
                        Box::new(PythonPackageDependency::from(require.clone())),
                    ));
                }
            }
        }
        if let Some(options) = self.config.as_ref().and_then(|c| c.get_section("options")) {
            for require in options
                .get::<Vec<String>>("setup_requires")
                .unwrap_or_default()
            {
                ret.push((
                    DependencyCategory::Build,
                    Box::new(PythonPackageDependency::try_from(require).unwrap()),
                ));
            }
            for require in options
                .get::<Vec<String>>("install_requires")
                .unwrap_or_default()
            {
                ret.push((
                    DependencyCategory::Universal,
                    Box::new(PythonPackageDependency::try_from(require).unwrap()),
                ));
            }
        }

        if let Some(pyproject_toml) = self.pyproject.as_ref() {
            if let Some(build_system) = pyproject_toml.build_system.as_ref() {
                for require in &build_system.requires {
                    ret.push((
                        DependencyCategory::Build,
                        Box::new(PythonPackageDependency::from(require.clone())),
                    ));
                }
            }

            if let Some(dependencies) = pyproject_toml
                .project
                .as_ref()
                .and_then(|p| p.dependencies.as_ref())
            {
                for dep in dependencies {
                    ret.push((
                        DependencyCategory::Universal,
                        Box::new(PythonPackageDependency::from(dep.clone())),
                    ));
                }
            }

            if let Some(extras) = pyproject_toml
                .project
                .as_ref()
                .and_then(|p| p.optional_dependencies.as_ref())
            {
                for (name, deps) in extras {
                    for dep in deps {
                        ret.push((
                            DependencyCategory::RuntimeExtra(name.clone()),
                            Box::new(PythonPackageDependency::from(dep.clone())),
                        ));
                    }
                }
            }

            if let Some(requires_python) = pyproject_toml
                .project
                .as_ref()
                .and_then(|p| p.requires_python.as_ref())
            {
                ret.push((
                    DependencyCategory::Universal,
                    Box::new(PythonDependency::from(requires_python)),
                ));
            }
        }

        Ok(ret)
    }

    fn get_declared_outputs(
        &self,
        session: &dyn Session,
        fixers: Option<&[&dyn BuildFixer<InstallerError>]>,
    ) -> Result<Vec<Box<dyn Output>>, Error> {
        let mut ret: Vec<Box<dyn Output>> = vec![];
        let distribution = self.extract_setup(Some(session), fixers)?;
        let mut all_packages = HashSet::new();
        if let Some(distribution) = distribution {
            for script in &distribution.scripts {
                ret.push(Box::new(BinaryOutput(
                    Path::new(script)
                        .file_name()
                        .unwrap()
                        .to_str()
                        .unwrap()
                        .to_owned(),
                )));
            }
            for script in distribution
                .entry_points
                .get("console_scripts")
                .unwrap_or(&vec![])
            {
                ret.push(Box::new(BinaryOutput(
                    script.split_once('=').unwrap().0.to_string().to_owned(),
                )));
            }
            for ext_module in &distribution.ext_modules {
                ret.push(Box::new(PythonExtensionOutput::new(ext_module)));
            }
            all_packages.extend(distribution.packages);
        }
        if let Some(options) = self.config.as_ref().and_then(|c| c.get_section("options")) {
            all_packages.extend(options.get::<Vec<String>>("packages").unwrap_or_default());
            for script in options.get::<Vec<String>>("scripts").unwrap_or_default() {
                let p = Path::new(&script);
                ret.push(Box::new(BinaryOutput(
                    p.file_name().unwrap().to_str().unwrap().to_owned(),
                )));
            }
            let entry_points = options
                .get::<HashMap<String, Vec<String>>>("entry_points")
                .unwrap_or_default();
            for script in entry_points.get("console_scripts").unwrap_or(&vec![]) {
                ret.push(Box::new(BinaryOutput(
                    script.split_once('=').unwrap().0.to_string().to_owned(),
                )));
            }
        }

        for package in all_packages {
            ret.push(Box::new(PythonPackageOutput::new(
                &package,
                Some("cpython3"),
            )));
        }

        if let Some(pyproject) = self.pyproject.as_ref().and_then(|p| p.project.as_ref()) {
            if let Some(scripts) = pyproject.scripts.as_ref() {
                for (script, _from) in scripts {
                    ret.push(Box::new(BinaryOutput(script.to_string())));
                }
            }

            if let Some(gui_scripts) = pyproject.gui_scripts.as_ref() {
                for (script, _from) in gui_scripts {
                    ret.push(Box::new(BinaryOutput(script.to_string())));
                }
            }

            ret.push(Box::new(PythonPackageOutput::new(
                &pyproject.name,
                pyproject.version.as_ref().map(|v| v.to_string()).as_deref(),
            )));
        }

        Ok(ret)
    }

    fn name(&self) -> &str {
        "setup.py"
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    /// Whether setuptools can be imported. Tests that introspect a setup.py
    /// depend on it and are skipped when it is unavailable (e.g. on a minimal
    /// CI Python without setuptools bundled).
    fn setuptools_available() -> bool {
        pyo3::Python::initialize();
        Python::attach(|py| py.import("setuptools").is_ok())
    }

    #[test]
    fn test_python_project_without_pyproject_toml() {
        pyo3::Python::initialize();

        let temp_dir = tempdir().unwrap();
        let path = temp_dir.path();

        // Create only setup.py, no pyproject.toml
        fs::write(
            path.join("setup.py"),
            "from setuptools import setup\nsetup(name='test')",
        )
        .unwrap();

        // This should not panic
        let setup_py = SetupPy::new(path);
        assert!(setup_py.has_setup_py);
        assert!(setup_py.pyproject.is_none());
    }

    #[test]
    fn test_extract_setup_ext_modules() {
        if !setuptools_available() {
            return;
        }

        let temp_dir = tempdir().unwrap();
        let path = temp_dir.path();

        fs::write(
            path.join("setup.py"),
            r#"from setuptools import setup, Extension
setup(
    name='test',
    packages=['test'],
    install_requires=['requests'],
    ext_modules=[Extension('test._speedups', ['src/_speedups.c'])],
)
"#,
        )
        .unwrap();

        let setup_py = SetupPy::new(path);
        let distribution = setup_py.extract_setup_direct().unwrap();
        assert_eq!(distribution.name, Some("test".to_string()));
        assert_eq!(distribution.packages, vec!["test".to_string()]);
        assert_eq!(distribution.install_requires, vec!["requests".to_string()]);
        assert_eq!(distribution.ext_modules, vec!["test._speedups".to_string()]);
    }

    #[test]
    fn test_extract_setup_without_ext_modules() {
        if !setuptools_available() {
            return;
        }

        let temp_dir = tempdir().unwrap();
        let path = temp_dir.path();

        fs::write(
            path.join("setup.py"),
            "from setuptools import setup\nsetup(name='test')\n",
        )
        .unwrap();

        let setup_py = SetupPy::new(path);
        let distribution = setup_py.extract_setup_direct().unwrap();
        assert_eq!(distribution.name, Some("test".to_string()));
        assert_eq!(distribution.ext_modules, Vec::<String>::new());
    }

    #[test]
    fn test_setup_wrapper_does_not_swallow_missing_setuptools() {
        // A session without setuptools must surface the ImportError so the
        // caller's fixers can install it, rather than emitting empty metadata
        // that looks like a project declaring nothing. Both are guarded against
        // reintroduction: the imports must be unguarded, and there must be no
        // empty-metadata escape hatch.
        assert!(!SETUP_WRAPPER.contains("except ImportError"));
        assert!(!SETUP_WRAPPER.contains("json.dump({}"));
        // The imports the extraction depends on run at top level, not inside a
        // try that could swallow their failure.
        assert!(SETUP_WRAPPER.contains("\nimport setuptools\n"));
        assert!(SETUP_WRAPPER.contains("\nfrom distutils import core\n"));
    }

    #[test]
    fn test_extract_setup_direct_not_a_setup_script() {
        pyo3::Python::initialize();

        let temp_dir = tempdir().unwrap();
        let path = temp_dir.path();

        fs::write(path.join("setup.py"), "print('not a setup script')\n").unwrap();

        // A setup.py that never calls setup() is a failure to introspect, not a
        // project that declares nothing.
        let setup_py = SetupPy::new(path);
        assert!(setup_py.extract_setup_direct().is_err());
    }

    #[test]
    fn test_extract_setup_direct_under_main_guard() {
        if !setuptools_available() {
            return;
        }

        let temp_dir = tempdir().unwrap();
        let path = temp_dir.path();

        fs::write(
            path.join("setup.py"),
            "from setuptools import setup\nif __name__ == '__main__':\n    setup(name='guarded')\n",
        )
        .unwrap();

        let setup_py = SetupPy::new(path);
        let distribution = setup_py.extract_setup_direct().unwrap();
        assert_eq!(distribution.name, Some("guarded".to_string()));
    }

    #[test]
    fn test_load_toml_file_not_found() {
        pyo3::Python::initialize();
        Python::attach(|py| {
            let temp_dir = tempdir().unwrap();
            let path = temp_dir.path();

            // Don't create pyproject.toml
            let result = load_toml(path);
            assert!(result.is_err());

            let err = result.unwrap_err();
            assert!(err.is_instance_of::<PyFileNotFoundError>(py));
        });
    }

    #[test]
    fn test_load_toml_invalid_content() {
        pyo3::Python::initialize();
        Python::attach(|py| {
            let temp_dir = tempdir().unwrap();
            let path = temp_dir.path();

            // Create invalid pyproject.toml
            fs::write(path.join("pyproject.toml"), "invalid toml content").unwrap();

            let result = load_toml(path);
            assert!(result.is_err());

            let err = result.unwrap_err();
            assert!(err.is_instance_of::<pyo3::exceptions::PyValueError>(py));
        });
    }
}