|
| 1 | +/// This an example usage of the rustpython_compiler crate. |
| 2 | +/// This program reads, parses, and compiles a file you provide |
| 3 | +/// to RustPython bytecode, and then displays the output in the |
| 4 | +/// `dis.dis` format. |
| 5 | +/// |
| 6 | +/// example usage: |
| 7 | +/// $ cargo run --release --example dis demo.py |
| 8 | +
|
| 9 | +#[macro_use] |
| 10 | +extern crate clap; |
| 11 | +extern crate env_logger; |
| 12 | +#[macro_use] |
| 13 | +extern crate log; |
| 14 | + |
| 15 | +use clap::{App, Arg}; |
| 16 | + |
| 17 | +use rustpython_compiler::compile; |
| 18 | +use std::error::Error; |
| 19 | +use std::fs; |
| 20 | +use std::path::Path; |
| 21 | + |
| 22 | +fn main() { |
| 23 | + env_logger::init(); |
| 24 | + let app = App::new("parse_folders") |
| 25 | + .version(crate_version!()) |
| 26 | + .author(crate_authors!()) |
| 27 | + .about("Walks over all .py files in a folder, and parses them.") |
| 28 | + .arg( |
| 29 | + Arg::with_name("scripts") |
| 30 | + .help("Scripts to scan") |
| 31 | + .multiple(true) |
| 32 | + .required(true), |
| 33 | + ) |
| 34 | + .arg( |
| 35 | + Arg::with_name("mode") |
| 36 | + .help("The mode to compile the scripts in") |
| 37 | + .long("mode") |
| 38 | + .short("m") |
| 39 | + .possible_values(&["exec", "single", "eval"]) |
| 40 | + .takes_value(true), |
| 41 | + ) |
| 42 | + .arg( |
| 43 | + Arg::with_name("no_expand") |
| 44 | + .help( |
| 45 | + "Don't expand CodeObject LoadConst instructions to show \ |
| 46 | + the instructions inside", |
| 47 | + ) |
| 48 | + .long("no-expand") |
| 49 | + .short("x"), |
| 50 | + ) |
| 51 | + .arg( |
| 52 | + Arg::with_name("optimize") |
| 53 | + .help("The amount of optimization to apply to the compiled bytecode") |
| 54 | + .short("O") |
| 55 | + .multiple(true), |
| 56 | + ); |
| 57 | + let matches = app.get_matches(); |
| 58 | + |
| 59 | + let mode = matches.value_of_lossy("mode").unwrap().parse().unwrap(); |
| 60 | + let expand_codeobjects = !matches.is_present("no_expand"); |
| 61 | + let optimize = matches.occurrences_of("optimize") as u8; |
| 62 | + let scripts = matches.values_of_os("scripts").unwrap(); |
| 63 | + |
| 64 | + for script in scripts.map(Path::new) { |
| 65 | + if script.exists() && script.is_file() { |
| 66 | + let res = display_script(script, mode, optimize, expand_codeobjects); |
| 67 | + if let Err(e) = res { |
| 68 | + error!("Error while compiling {:?}: {}", script, e); |
| 69 | + } |
| 70 | + } else { |
| 71 | + eprintln!("{:?} is not a file.", script); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fn display_script( |
| 77 | + path: &Path, |
| 78 | + mode: compile::Mode, |
| 79 | + optimize: u8, |
| 80 | + expand_codeobjects: bool, |
| 81 | +) -> Result<(), Box<dyn Error>> { |
| 82 | + let source = fs::read_to_string(path)?; |
| 83 | + let code = compile::compile(&source, mode, path.to_string_lossy().into_owned(), optimize)?; |
| 84 | + println!("{}:", path.display()); |
| 85 | + if expand_codeobjects { |
| 86 | + println!("{}", code.display_expand_codeobjects()); |
| 87 | + } else { |
| 88 | + println!("{}", code); |
| 89 | + } |
| 90 | + Ok(()) |
| 91 | +} |
0 commit comments