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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
extern crate appdirs;
extern crate colored;
use colored::*;
pub struct App {
venv_path: std::path::PathBuf,
requirements_lock_path: std::path::PathBuf,
}
#[derive(Debug)]
pub struct Error {
description: String,
}
impl Error {
pub fn new(description: &str) -> Error {
Error {
description: String::from(description),
}
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Error {
Error::new(&format!("I/O error: {}", error))
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", &self.description)
}
}
impl App {
pub fn new() -> Result<Self, Error> {
let current_dir = std::env::current_dir()?;
let name = current_dir.file_name();
if name.is_none() {
return Err(Error::new("current directory has no filename"));
}
let name = name.unwrap();
let data_dir = appdirs::user_data_dir(Some("dmenv"), None, false);
if data_dir.is_err() {
return Err(Error::new(
"appdirs::user_data_dir() failed. That's all we know",
));
}
let data_dir = data_dir.unwrap();
let venv_path = data_dir.join("venvs").join(name);
let requirements_lock_path = current_dir.join("requirements.lock");
let app = App {
venv_path,
requirements_lock_path,
};
Ok(app)
}
pub fn clean(&self) -> Result<(), Error> {
println!(
"{} Cleaning {}",
"::".blue(),
&self.venv_path.to_string_lossy()
);
if !self.venv_path.exists() {
return Ok(());
}
std::fs::remove_dir_all(&self.venv_path).map_err(|x| x.into())
}
pub fn install(&self) -> Result<(), Error> {
if !self.venv_path.exists() {
self.create_venv()?;
}
if !self.requirements_lock_path.exists() {
return Err(Error::new(&format!(
"{} does not exist. Please run dmenv freeze",
&self.requirements_lock_path.to_string_lossy(),
)));
}
self.install_from_lock()
}
pub fn run(&self, args: Vec<String>) -> Result<(), Error> {
let bin_path = &self.venv_path.join("bin").join(&args[0]);
let command = std::process::Command::new(bin_path)
.args(&args[1..])
.status()?;
if !command.success() {
return Err(Error::new("command failed"));
}
Ok(())
}
pub fn freeze(&self) -> Result<(), Error> {
if !self.venv_path.exists() {
self.create_venv()?;
}
println!("{} Generating requirements.txt from setup.py", "::".blue());
self.install_editable()?;
self.run_pip_freeze()?;
Ok(())
}
pub fn show(&self) -> Result<(), Error> {
println!("{}", self.venv_path.to_string_lossy());
Ok(())
}
fn create_venv(&self) -> Result<(), Error> {
let parent_venv_path = &self.venv_path.parent();
if parent_venv_path.is_none() {
return Err(Error::new("venv_path has no parent"));
}
let parent_venv_path = parent_venv_path.unwrap();
println!(
"{} Creating virtualenv in: {}",
"::".blue(),
self.venv_path.to_string_lossy()
);
std::fs::create_dir_all(&parent_venv_path)?;
let status = std::process::Command::new("python")
.args(&["-m", "venv", &self.venv_path.to_string_lossy()])
.status()?;
if !status.success() {
return Err(Error::new("Failed to create virtualenv"));
}
self.upgrade_pip()
}
fn run_pip_freeze(&self) -> Result<(), Error> {
let python = self.get_path_in_venv("python")?;
let args = vec!["-m", "pip", "freeze", "--exclude-editable"];
Self::print_cmd(python.to_path_buf(), &args);
let command = std::process::Command::new(python).args(args).output()?;
if !command.status.success() {
return Err(Error::new("pip freeze failed"));
}
std::fs::write("requirements.lock", &command.stdout)?;
println!("{} Requirements written to requirements.lock", "::".blue());
Ok(())
}
fn install_from_lock(&self) -> Result<(), Error> {
let as_str = &self.requirements_lock_path.to_string_lossy();
let args = vec![
"-m",
"pip",
"install",
"--requirement",
as_str,
"-e",
".[dev]",
];
self.run_venv_bin("python", args)
}
pub fn upgrade_pip(&self) -> Result<(), Error> {
let args = vec!["-m", "pip", "install", "pip", "--upgrade"];
self.run_venv_bin("python", args)
}
fn install_editable(&self) -> Result<(), Error> {
let args = vec!["-m", "pip", "install", "-e", ".[dev]"];
self.run_venv_bin("python", args)
}
fn run_venv_bin(&self, name: &str, args: Vec<&str>) -> Result<(), Error> {
let bin_path = &self.get_path_in_venv(name)?;
Self::print_cmd(bin_path.to_path_buf(), &args);
let command = std::process::Command::new(bin_path).args(args).status()?;
if !command.success() {
return Err(Error::new("command failed"));
}
Ok(())
}
fn get_path_in_venv(&self, name: &str) -> Result<std::path::PathBuf, Error> {
if !self.venv_path.exists() {
return Err(Error::new(&format!(
"virtualenv in '{}' does not exist",
&self.venv_path.to_string_lossy()
)));
}
let path = self.venv_path.join("bin").join(name);
if !path.exists() {
return Err(Error::new(&format!(
"Cannot run: '{}' does not exist",
&path.to_string_lossy()
)));
}
Ok(path)
}
fn print_cmd(bin_path: std::path::PathBuf, args: &Vec<&str>) {
println!(
"{} running {} {}",
"->".blue(),
bin_path.to_string_lossy().bold(),
args.join(" ")
);
}
}