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
use std::io;
use std::path::Path;
use std::process::{Command, Stdio};
/// A source code repository
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct Source {
pub kind: String,
pub url: String,
}
impl Source {
/// Download the source code repository to the given directory
//TODO: More documentation, code example
pub fn download<P: AsRef<Path>>(&self, directory: P) -> io::Result<u64> {
match self.kind.as_str() {
"dir" => {
let status = Command::new("cp")
.arg("--preserve=all")
.arg("--recursive")
.arg(&self.url)
.arg(directory.as_ref())
.spawn()?
.wait()?;
if ! status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Copy error: {}", status)
));
}
let output = Command::new("find")
.arg(directory.as_ref())
.arg("-type")
.arg("f")
.arg("-printf")
.arg("%T@\\n")
.stdout(Stdio::piped())
.spawn()?
.wait_with_output()?;
if ! output.status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Find error: {}", output.status)
));
}
let stdout = String::from_utf8(output.stdout).map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Find output not UTF-8: {}", err)
)
})?;
let mut time_opt = None;
for line in stdout.trim().lines() {
let mut parts = line.trim().split('.');
let time_str = parts.next().ok_or(io::Error::new(
io::ErrorKind::Other,
format!("Find time not valid")
))?;
let time = time_str.parse::<u64>().map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Find time not a number: {}", err)
)
})? as u64;
time_opt = match time_opt {
Some(old_time) => if time > old_time {
Some(time)
} else {
Some(old_time)
},
None => Some(time)
};
}
match time_opt {
Some(time) => Ok(time),
None => Err(io::Error::new(
io::ErrorKind::Other,
format!("Find time not found")
))
}
},
"git" => {
let status = Command::new("git")
.arg("clone")
.arg("--recursive")
.arg(&self.url)
.arg(directory.as_ref())
.spawn()?
.wait()?;
if ! status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Git clone error: {}", status)
));
}
let output = Command::new("git")
.arg("-C")
.arg(directory.as_ref())
.arg("log")
.arg("-1")
.arg("--format=%ct")
.stdout(Stdio::piped())
.spawn()?
.wait_with_output()?;
if ! output.status.success() {
return Err(io::Error::new(
io::ErrorKind::Other,
format!("Git log error: {}", output.status)
));
}
let stdout = String::from_utf8(output.stdout).map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Git log output not UTF-8: {}", err)
)
})?;
let time = stdout.trim().parse::<u64>().map_err(|err| {
io::Error::new(
io::ErrorKind::Other,
format!("Git log time not a number: {}", err)
)
})?;
Ok(time)
},
_ => {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Unknown source kind: {}", self.kind)
))
}
}
}
}