MCPcopy Index your code
hub / github.com/RustPython/RustPython / compile_dir

Method compile_dir

crates/derive-impl/src/compile_bytecode.rs:125–224  ·  view source on GitHub ↗
(
        &self,
        base: &Path,
        path: &Path,
        parent: String,
        mode: Mode,
        compiler: &dyn Compiler,
    )

Source from the content-addressed store, hash-verified

123 }
124
125 fn compile_dir(
126 &self,
127 base: &Path,
128 path: &Path,
129 parent: String,
130 mode: Mode,
131 compiler: &dyn Compiler,
132 ) -> Result<HashMap<String, CompiledModule>, Diagnostic> {
133 let mut code_map = HashMap::new();
134 let paths = fs::read_dir(path)
135 .or_else(|e| {
136 if cfg!(windows)
137 && let Ok(real_path) = fs::read_to_string(path.canonicalize().unwrap())
138 {
139 return fs::read_dir(real_path.trim());
140 }
141 Err(e)
142 })
143 .map_err(|err| {
144 Diagnostic::spans_error(self.span, format!("Error listing dir {path:?}: {err}"))
145 })?;
146 for path in paths {
147 let path = path.map_err(|err| {
148 Diagnostic::spans_error(self.span, format!("Failed to list file: {err}"))
149 })?;
150 let path = path.path();
151 let file_name = path.file_name().unwrap().to_str().ok_or_else(|| {
152 Diagnostic::spans_error(self.span, format!("Invalid UTF-8 in file name {path:?}"))
153 })?;
154 if path.is_dir() {
155 code_map.extend(self.compile_dir(
156 base,
157 &path,
158 if parent.is_empty() {
159 file_name.to_string()
160 } else {
161 format!("{parent}.{file_name}")
162 },
163 mode,
164 compiler,
165 )?);
166 } else if file_name.ends_with(".py") {
167 let stem = path.file_stem().unwrap().to_str().unwrap();
168 let is_init = stem == "__init__";
169 let module_name = if is_init {
170 parent.clone()
171 } else if parent.is_empty() {
172 stem.to_owned()
173 } else {
174 format!("{parent}.{stem}")
175 };
176
177 let compile_path = |src_path: &Path| {
178 let source = fs::read_to_string(src_path).map_err(|err| {
179 Diagnostic::spans_error(
180 self.span,
181 format!("Error reading file {path:?}: {err}"),
182 )

Callers 1

compileMethod · 0.45

Calls 15

newFunction · 0.85
compile_pathFunction · 0.85
trimMethod · 0.80
ok_or_elseMethod · 0.80
to_strMethod · 0.80
to_stringMethod · 0.80
ends_withMethod · 0.80
compile_stringMethod · 0.80
okMethod · 0.80
strip_prefixMethod · 0.80
starts_withMethod · 0.80
ErrClass · 0.50

Tested by

no test coverage detected