-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfeet.rs
149 lines (126 loc) · 4.98 KB
/
feet.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::env;
use std::ffi::OsString;
use std::fs;
use std::io;
use std::io::{Error, Write, stdout};
use std::path::Path;
use std::process::{Command, Stdio};
fn get_exec_stem() -> OsString {
let current_exe = std::env::current_exe().expect("cannot get current process name");
let path = Path::new(¤t_exe);
let stem = path.file_stem().expect("cannot get file stem");
return stem.to_owned();
}
fn extract_runtime(exec_name: OsString, data_dir: OsString) -> Result<(), Error> {
println!("Extracting the Python Feet Runtime... (one-time operation)");
let file = fs::File::open(&exec_name).unwrap();
let mut archive = zip::ZipArchive::new(file).unwrap();
let total = archive.len();
for i in 0..total {
let mut file = archive.by_index(i).unwrap();
let outpath = file.sanitized_name();
if (&*file.name()).ends_with('/') {
fs::create_dir_all(&outpath).unwrap();
} else {
if i % 100 == 0 && i != 0 {
println!(" {}% ", ((i as f64 / total as f64) * 100.0) as i32);
} else if i != 0 {
print!(".");
stdout().flush()?;
}
if let Some(p) = outpath.parent() {
if !p.exists() {
fs::create_dir_all(&p).unwrap();
}
}
let mut outfile = fs::File::create(&outpath).unwrap();
io::copy(&mut file, &mut outfile).unwrap();
}
// Get and Set permissions
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
if let Some(mode) = file.unix_mode() {
fs::set_permissions(&outpath, fs::Permissions::from_mode(mode)).unwrap();
}
}
}
std::fs::rename("feet", &data_dir)?;
println!(" done!");
Ok(())
}
fn main() -> Result<(), Error> {
let exec = std::env::current_exe()?;
let exec_path = Path::new(&exec);
let exec_name = exec_path.file_name().unwrap();
let exec_base = get_exec_stem();
let data_dir = format!("{}_data", exec_base.to_str().unwrap());
let mut args: Vec<String> = env::args().collect();
args.remove(0);
// Is there a sub-command? most are implemented in the python utility, but check if we
// need to process one.
if args.len() > 0 {
match args[0].as_ref() {
// the "clean" command is implemented here, because the unpacked Python
// can't delete itself
"clean" => {
fs::remove_dir_all(Path::new(&data_dir))?;
std::process::exit(0);
},
_ => {
},
}
}
// Make sure we don't run inside the feet repo, only in test places.
if Path::new("./feetmaker.py").exists() {
println!("Do not run {:?} in its own source directory", exec_name);
std::process::exit(1);
}
// If a pre-existing extracted runtime directory exists, clear it if
// it is older than this executable
if Path::new(&data_dir).exists() {
let mod_exec = Path::new(&exec_name).metadata()?.modified()?;
let mod_runtime = Path::new(&data_dir).metadata()?.modified()?;
if mod_exec > mod_runtime {
fs::remove_dir_all(Path::new(&data_dir))?;
}
}
// If there is no runtime directory, either because this is a first-run
// or the directory was cleared for a new version, extract the whole thing
if !Path::new(&data_dir).exists() {
match extract_runtime(exec_name.to_owned(), OsString::from(data_dir.to_owned())) {
Ok(()) => (),
Err(err) => panic!(err)
}
}
// Next, if there is a requirements file, install that
if Path::new("./requirements.txt").exists() && !Path::new(&data_dir).join("requirements_installed.txt").exists() {
let script = &format!("{}/feet.py", data_dir);
println!("Installing requirements... {}", script);
let mut child = Command::new(format!("{}/cpython/python", data_dir))
.args(&[script, "library", "--update"])
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()?;
child.wait().expect("Invoking the Feet runtime script failed.");
let _f = fs::File::create(Path::new(&data_dir).join("requirements_installed.txt"))?;
}
// Now, runtime is either extracted or already was, so run the commands
// println!("{}/feet.py", data_dir);
let mut child = Command::new(format!("{}/cpython/python", data_dir))
.arg(format!("{}/feet.py", data_dir))
.args(args)
.stderr(Stdio::inherit())
.stdout(Stdio::inherit())
.stdin(Stdio::inherit())
.spawn()?;
let status = child.wait().expect("Invoking the Feet runtime script failed.");
match status.code() {
Some(code) => std::process::exit(code),
None => {
println!("Process terminated by signal");
std::process::exit(255);
},
}
}