From eb9378fe900db7a859f0d71ab99bf8ce9071e932 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 6 Apr 2019 18:20:18 +0300 Subject: [PATCH 1/5] Add os.mkdir --- vm/src/stdlib/os.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index ae487598a..312b79549 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -151,6 +151,15 @@ fn os_remove(path: PyStringRef, vm: &VirtualMachine) -> PyResult { Ok(vm.get_none()) } +fn os_mkdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { + match fs::create_dir(&path.value) { + Ok(_) => (), + Err(s) => return Err(vm.new_os_error(s.to_string())), + } + + Ok(vm.get_none()) +} + pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; @@ -168,6 +177,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "write" => ctx.new_rustfunc(os_write), "remove" => ctx.new_rustfunc(os_remove), "unlink" => ctx.new_rustfunc(os_remove), + "mkdir" => ctx.new_rustfunc(os_mkdir), "name" => ctx.new_str(os_name), "O_RDONLY" => ctx.new_int(0), "O_WRONLY" => ctx.new_int(1), From 2ec6afeb11a4a400584c000b6606dbd0640f09a0 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 6 Apr 2019 18:24:06 +0300 Subject: [PATCH 2/5] Add os.mkdirs --- vm/src/stdlib/os.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 312b79549..fc7651027 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -160,6 +160,15 @@ fn os_mkdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { Ok(vm.get_none()) } +fn os_mkdirs(path: PyStringRef, vm: &VirtualMachine) -> PyResult { + match fs::create_dir_all(&path.value) { + Ok(_) => (), + Err(s) => return Err(vm.new_os_error(s.to_string())), + } + + Ok(vm.get_none()) +} + pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; @@ -178,6 +187,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "remove" => ctx.new_rustfunc(os_remove), "unlink" => ctx.new_rustfunc(os_remove), "mkdir" => ctx.new_rustfunc(os_mkdir), + "mkdirs" => ctx.new_rustfunc(os_mkdirs), "name" => ctx.new_str(os_name), "O_RDONLY" => ctx.new_int(0), "O_WRONLY" => ctx.new_int(1), From bf6b12266a9b3996badd01f2ef36aac1b35ca4b7 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 6 Apr 2019 18:30:40 +0300 Subject: [PATCH 3/5] Add os.rmdir --- vm/src/stdlib/os.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index fc7651027..6ffb36ca5 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -169,6 +169,15 @@ fn os_mkdirs(path: PyStringRef, vm: &VirtualMachine) -> PyResult { Ok(vm.get_none()) } +fn os_rmdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { + match fs::remove_dir(&path.value) { + Ok(_) => (), + Err(s) => return Err(vm.new_os_error(s.to_string())), + } + + Ok(vm.get_none()) +} + pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; @@ -188,6 +197,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "unlink" => ctx.new_rustfunc(os_remove), "mkdir" => ctx.new_rustfunc(os_mkdir), "mkdirs" => ctx.new_rustfunc(os_mkdirs), + "rmdir" => ctx.new_rustfunc(os_rmdir), "name" => ctx.new_str(os_name), "O_RDONLY" => ctx.new_int(0), "O_WRONLY" => ctx.new_int(1), From a21aa6eac9ca8016e13c54689c83120eb57b482c Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 6 Apr 2019 19:14:23 +0300 Subject: [PATCH 4/5] Add os.listdir --- vm/src/stdlib/os.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 6ffb36ca5..f0f5c3dd5 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -178,6 +178,21 @@ fn os_rmdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { Ok(vm.get_none()) } +fn os_listdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { + match fs::read_dir(&path.value) { + Ok(iter) => { + let res: PyResult> = iter + .map(|entry| match entry { + Ok(path) => Ok(vm.ctx.new_str(path.file_name().into_string().unwrap())), + Err(s) => Err(vm.new_os_error(s.to_string())), + }) + .collect(); + Ok(vm.ctx.new_list(res?)) + } + Err(s) => Err(vm.new_os_error(s.to_string())), + } +} + pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { let ctx = &vm.ctx; @@ -198,6 +213,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { "mkdir" => ctx.new_rustfunc(os_mkdir), "mkdirs" => ctx.new_rustfunc(os_mkdirs), "rmdir" => ctx.new_rustfunc(os_rmdir), + "listdir" => ctx.new_rustfunc(os_listdir), "name" => ctx.new_str(os_name), "O_RDONLY" => ctx.new_int(0), "O_WRONLY" => ctx.new_int(1), From 401ca08b659d137200a80caf3bc87f08c3dc2642 Mon Sep 17 00:00:00 2001 From: Aviv Palivoda Date: Sat, 6 Apr 2019 21:11:58 +0300 Subject: [PATCH 5/5] Use map_err --- vm/src/stdlib/os.rs | 36 ++++++++---------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index f0f5c3dd5..25937cd5c 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -142,40 +142,20 @@ fn os_write(fd: PyIntRef, data: PyBytesRef, vm: &VirtualMachine) -> PyResult { Ok(vm.ctx.new_int(written)) } -fn os_remove(path: PyStringRef, vm: &VirtualMachine) -> PyResult { - match fs::remove_file(&path.value) { - Ok(_) => (), - Err(s) => return Err(vm.new_os_error(s.to_string())), - } - - Ok(vm.get_none()) +fn os_remove(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { + fs::remove_file(&path.value).map_err(|s| vm.new_os_error(s.to_string())) } -fn os_mkdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { - match fs::create_dir(&path.value) { - Ok(_) => (), - Err(s) => return Err(vm.new_os_error(s.to_string())), - } - - Ok(vm.get_none()) +fn os_mkdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { + fs::create_dir(&path.value).map_err(|s| vm.new_os_error(s.to_string())) } -fn os_mkdirs(path: PyStringRef, vm: &VirtualMachine) -> PyResult { - match fs::create_dir_all(&path.value) { - Ok(_) => (), - Err(s) => return Err(vm.new_os_error(s.to_string())), - } - - Ok(vm.get_none()) +fn os_mkdirs(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { + fs::create_dir_all(&path.value).map_err(|s| vm.new_os_error(s.to_string())) } -fn os_rmdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult { - match fs::remove_dir(&path.value) { - Ok(_) => (), - Err(s) => return Err(vm.new_os_error(s.to_string())), - } - - Ok(vm.get_none()) +fn os_rmdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult<()> { + fs::remove_dir(&path.value).map_err(|s| vm.new_os_error(s.to_string())) } fn os_listdir(path: PyStringRef, vm: &VirtualMachine) -> PyResult {