-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add an example usecase of the parser. #1177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||
| /// This an example usage of the rustpython_parser crate. | ||||||
| /// This program crawls over a directory of python files and | ||||||
| /// tries to parse them into an abstract syntax tree (AST) | ||||||
| /// | ||||||
| /// example usage: | ||||||
| /// $ RUST_LOG=info cargo run --release parse_folder /usr/lib/python3.7 | ||||||
|
|
||||||
| #[macro_use] | ||||||
| extern crate clap; | ||||||
| extern crate env_logger; | ||||||
| #[macro_use] | ||||||
| extern crate log; | ||||||
|
|
||||||
| use clap::{App, Arg}; | ||||||
|
|
||||||
| use rustpython_parser::{ast, parser}; | ||||||
| use std::path::Path; | ||||||
|
|
||||||
| fn main() { | ||||||
| env_logger::init(); | ||||||
| let app = App::new("RustPython") | ||||||
| .version(crate_version!()) | ||||||
| .author(crate_authors!()) | ||||||
| .about("Walks over all .py files in a folder, and parses them.") | ||||||
| .arg( | ||||||
| Arg::with_name("folder") | ||||||
| .help("Folder to scan") | ||||||
| .required(true), | ||||||
| ); | ||||||
| let matches = app.get_matches(); | ||||||
|
|
||||||
| let folder = Path::new(matches.value_of("folder").unwrap()); | ||||||
| if folder.exists() && folder.is_dir() { | ||||||
| println!("Parsing folder of python code: {:?}", folder); | ||||||
| let res = parse_folder(&folder).unwrap(); | ||||||
| println!("Processed {:?} files", res.len()); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it print the parsed files or something?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have some ideas for metrics:
For now, I just use it to try and scan my cpython lib folder |
||||||
| } else { | ||||||
| println!("{:?} is not a folder.", folder); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn parse_folder(path: &Path) -> std::io::Result<Vec<ast::Program>> { | ||||||
| let mut res = vec![]; | ||||||
| info!("Parsing folder of python code: {:?}", path); | ||||||
| for entry in path.read_dir()? { | ||||||
| debug!("Entry: {:?}", entry); | ||||||
| let entry = entry?; | ||||||
| let metadata = entry.metadata()?; | ||||||
|
|
||||||
| let path = entry.path(); | ||||||
| if metadata.is_dir() { | ||||||
| let x = parse_folder(&path)?; | ||||||
| res.extend(x); | ||||||
| } | ||||||
|
|
||||||
| if metadata.is_file() && path.extension().map(|s| s.to_str().unwrap()) == Some("py") { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||||||
| match parse_python_file(&path) { | ||||||
| Ok(x) => res.push(x), | ||||||
| Err(y) => error!("Erreur in file {:?} {:?}", path, y), | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Ok(res) | ||||||
| } | ||||||
|
|
||||||
| fn parse_python_file(filename: &Path) -> Result<ast::Program, String> { | ||||||
| info!("Parsing file {:?}", filename); | ||||||
| let source = std::fs::read_to_string(filename).map_err(|e| e.to_string())?; | ||||||
| parser::parse_program(&source).map_err(|e| e.to_string()) | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
RustPythonis the name of the whole project and, presumably, its interpreter as a stand-alone application – shouldn’t this be called something else (or at least – more specific)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, copy paste error, thanks!