fn write_modules_to_directory(py: Python, path: &PathBuf) -> Result<(), &'static str> {
    // TODO this needs better error handling all over.

    fs::create_dir_all(path).or_else(|_| Err("could not create directory for modules"))?;

    let rand = uuid::Uuid::new_v4();

    let path = path.join(format!("modules-{}", rand.to_string()));

    let sys = py
        .import("sys")
        .or_else(|_| Err("could not obtain sys module"))?;
    let modules = sys
        .get(py, "modules")
        .or_else(|_| Err("could not obtain sys.modules"))?;

    let modules = modules
        .cast_as::<PyDict>(py)
        .or_else(|_| Err("sys.modules is not a dict"))?;

    let mut names = BTreeSet::new();
    for (key, _value) in modules.items(py) {
        names.insert(
            key.extract::<String>(py)
                .or_else(|_| Err("module name is not a str"))?,
        );
    }

    let mut f = fs::File::create(path).or_else(|_| Err("could not open file for writing"))?;

    for name in names {
        f.write_fmt(format_args!("{}\n", name))
            .or_else(|_| Err("could not write"))?;
    }

    Ok(())
}
/// Write loaded Python modules to a directory.
///
/// Given a Python interpreter and a path to a directory, this will create a
/// file in that directory named ``modules-<UUID>`` and write a ``\n`` delimited
/// list of loaded names from ``sys.modules`` into that file.
Write loaded Python modules to a directory.
Write loaded Python modules to a directory .
fn write_modules_to_directory(py: Python, path: &PathBuf) -> Result<(), &'static str> {
    // TODO this needs better error handling all over.
    fs::create_dir_all(path).or_else(|_| Err("could not create directory for modules"))?;
    let rand = uuid::Uuid::new_v4();
    let path = path.join(format!("modules-{}", rand.to_string()));
    let sys = py
        .import("sys")
        .or_else(|_| Err("could not obtain sys module"))?;
    let modules = sys
        .get(py, "modules")
        .or_else(|_| Err("could not obtain sys.modules"))?;
    let modules = modules
        .cast_as::<PyDict>(py)
        .or_else(|_| Err("sys.modules is not a dict"))?;
    let mut names = BTreeSet::new();
    for (key, _value) in modules.items(py) {
        names.insert(
            key.extract::<String>(py)
                .or_else(|_| Err("module name is not a str"))?,
        );
    }
    let mut f = fs::File::create(path).or_else(|_| Err("could not open file for writing"))?;
    for name in names {
        f.write_fmt(format_args!("{}\n", name))
            .or_else(|_| Err("could not write"))?;
    }
    Ok(())
}
fn write_modules_to_directory ( py : Python , path : & PathBuf ) -> Result < ( ) , & ' static str > { // TODO this needs better error handling all over. fs :: create_dir_all ( path ) . or_else ( | _ | Err ( " " ) ) ? ; let rand = uuid :: Uuid :: new_v4 ( ) ; let path = path . join ( format ! ( " " rand to_string ( ) ) ) ; let sys = py . import ( " " ) . or_else ( | _ | Err ( " " ) ) ? ; let modules = sys . get ( py , " " ) . or_else ( | _ | Err ( " " ) ) ? ; let modules = modules . cast_as :: < PyDict > ( py ) . or_else ( | _ | Err ( " " ) ) ? ; let mut names = BTreeSet :: new ( ) ; for ( key , _value ) in modules . items ( py ) { names . insert ( key . extract :: < String > ( py ) . or_else ( | _ | Err ( " " ) ) ? , ) ; } let mut f = fs :: File :: create ( path ) . or_else ( | _ | Err ( " " ) ) ? ; for name in names { f . write_fmt ( format_args ! ( " \n " name ) ) . or_else ( | _ | Err ( " " ) ) ? ; } Ok ( ( ) ) }
Write loaded Python modules to a directory.
Write loaded Python modules to a directory .
py
Python
path
&PathBuf