Skip to content

Commit edfa5a2

Browse files
committed
Add file list into releases
1 parent 1840534 commit edfa5a2

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

src/db/add_package.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use time;
2323
/// Package must be built first.
2424
pub fn add_package_into_database(conn: &Connection,
2525
pkg: &Package,
26-
res: &ChrootBuilderResult)
26+
res: &ChrootBuilderResult,
27+
files: Option<Json>)
2728
-> Result<i32, DocBuilderError> {
2829
debug!("Adding package into database");
2930
let crate_id = try!(initialize_package_in_database(&conn, &pkg));
@@ -41,9 +42,10 @@ pub fn add_package_into_database(conn: &Connection,
4142
dependencies, target_name, yanked, build_status, \
4243
rustdoc_status, test_status, license, repository_url, \
4344
homepage_url, description, description_long, readme, \
44-
authors, keywords, have_examples, downloads ) VALUES ( \
45-
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, \
46-
$13, $14, $15, $16, $17, $18, $19 ) RETURNING id",
45+
authors, keywords, have_examples, downloads, files ) \
46+
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, \
47+
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20 ) \
48+
RETURNING id",
4749
&[&crate_id,
4850
&format!("{}", pkg.manifest().version()),
4951
&release_time,
@@ -62,7 +64,8 @@ pub fn add_package_into_database(conn: &Connection,
6264
&pkg.manifest().metadata().authors.to_json(),
6365
&pkg.manifest().metadata().keywords.to_json(),
6466
&res.have_examples,
65-
&downloads]));
67+
&downloads,
68+
&files]));
6669
// return id
6770
rows.get(0).get(0)
6871

@@ -72,7 +75,7 @@ pub fn add_package_into_database(conn: &Connection,
7275
$8, test_status = $9, license = $10, repository_url = $11, \
7376
homepage_url = $12, description = $13, description_long = $14, \
7477
readme = $15, authors = $16, keywords = $17, have_examples = $18, \
75-
downloads = $19 WHERE crate_id = $1 AND version = $2",
78+
downloads = $19, files = $20 WHERE crate_id = $1 AND version = $2",
7679
&[&crate_id,
7780
&format!("{}", pkg.manifest().version()),
7881
&release_time,
@@ -91,7 +94,8 @@ pub fn add_package_into_database(conn: &Connection,
9194
&pkg.manifest().metadata().authors.to_json(),
9295
&pkg.manifest().metadata().keywords.to_json(),
9396
&res.have_examples,
94-
&downloads]));
97+
&downloads,
98+
&files]));
9599
rows.get(0).get(0)
96100
}
97101
};
@@ -399,8 +403,8 @@ fn add_owners_into_database(conn: &Connection,
399403
if rows.len() > 0 {
400404
rows.get(0).get(0)
401405
} else {
402-
try!(conn.query("INSERT INTO owners (login, avatar, name, email) \
403-
VALUES ($1, $2, $3, $4) RETURNING id",
406+
try!(conn.query("INSERT INTO owners (login, avatar, name, email) VALUES ($1, \
407+
$2, $3, $4) RETURNING id",
404408
&[&login, &avatar, &name, &email]))
405409
.get(0)
406410
.get(0)

src/db/file.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use std::path::Path;
99
use DocBuilderError;
1010
use postgres::Connection;
11+
use rustc_serialize::json::{Json, ToJson};
1112
use std::fs::File;
1213
use std::io::Read;
1314

@@ -43,7 +44,7 @@ fn get_file_list_from_dir<P: AsRef<Path>>(path: P,
4344
}
4445

4546

46-
fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>, DocBuilderError> {
47+
pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>, DocBuilderError> {
4748
let path = path.as_ref();
4849
let mut files: Vec<String> = Vec::new();
4950

@@ -61,18 +62,20 @@ fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<String>, DocBuilderError
6162
}
6263

6364

64-
/// Adds files into database
65+
/// Adds files into database and returns list of files with their mime type in Json
6566
pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
6667
prefix: &str,
6768
path: P)
68-
-> Result<(), DocBuilderError> {
69+
-> Result<Json, DocBuilderError> {
6970
use magic::{Cookie, flags};
7071
let cookie = try!(Cookie::open(flags::MIME_TYPE));
7172
// FIXME: This is linux specific but idk any alternative
7273
try!(cookie.load(&vec!["/usr/share/misc/magic.mgc"]));
7374

7475
let trans = try!(conn.transaction());
7576

77+
let mut file_list_with_mimes: Vec<(String, String)> = Vec::new();
78+
7679
for file_path_str in try!(get_file_list(&path)) {
7780
let (path, content, mime) = {
7881
let path = Path::new(path.as_ref()).join(&file_path_str);
@@ -85,6 +88,9 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
8588
let mut content: Vec<u8> = Vec::new();
8689
try!(file.read_to_end(&mut content));
8790
let mime = try!(cookie.buffer(&content));
91+
92+
file_list_with_mimes.push((mime.clone(), file_path_str.clone()));
93+
8894
(file_path(prefix, &file_path_str), content, mime)
8995
};
9096

@@ -103,7 +109,23 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
103109

104110
try!(trans.commit());
105111

106-
Ok(())
112+
file_list_to_json(file_list_with_mimes)
113+
}
114+
115+
116+
117+
fn file_list_to_json(file_list: Vec<(String, String)>) -> Result<Json, DocBuilderError> {
118+
119+
let mut file_list_json: Vec<Json> = Vec::new();
120+
121+
for file in file_list {
122+
let mut v: Vec<String> = Vec::new();
123+
v.push(file.0.clone());
124+
v.push(file.1.clone());
125+
file_list_json.push(v.to_json());
126+
}
127+
128+
Ok(file_list_json.to_json())
107129
}
108130

109131

src/db/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub fn create_tables(conn: &Connection) -> Result<(), Error> {
5858
keywords JSON, \
5959
have_examples BOOL DEFAULT FALSE, \
6060
downloads INT DEFAULT 0, \
61+
files JSON, \
6162
UNIQUE (crate_id, version) \
6263
)",
6364
"CREATE TABLE authors ( \

src/docbuilder/chroot_builder.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use cargo::core::Package;
88
use std::process::{Command, Output};
99
use std::path::PathBuf;
1010
use postgres::Connection;
11+
use rustc_serialize::json::Json;
1112

1213
use regex::Regex;
1314

@@ -69,13 +70,13 @@ impl DocBuilder {
6970
let res = self.build_package_in_chroot(&pkg);
7071

7172
// copy sources and documentation
72-
try!(self.add_sources_into_database(&conn, &pkg));
73+
let file_list = try!(self.add_sources_into_database(&conn, &pkg));
7374
if res.have_doc {
7475
try!(self.copy_documentation(&pkg, &res.rustc_version));
7576
try!(self.add_documentation_into_database(&conn, &pkg));
7677
}
7778

78-
let release_id = try!(add_package_into_database(&conn, &pkg, &res));
79+
let release_id = try!(add_package_into_database(&conn, &pkg, &res, Some(file_list)));
7980
try!(add_build_into_database(&conn, &release_id, &res));
8081

8182
// remove documentation, source and build directory after we are done
@@ -233,7 +234,7 @@ impl DocBuilder {
233234
fn add_sources_into_database(&self,
234235
conn: &Connection,
235236
package: &Package)
236-
-> Result<(), DocBuilderError> {
237+
-> Result<Json, DocBuilderError> {
237238
debug!("Adding sources into database");
238239
let prefix = format!("sources/{}/{}",
239240
package.manifest().name(),
@@ -246,7 +247,7 @@ impl DocBuilder {
246247
fn add_documentation_into_database(&self,
247248
conn: &Connection,
248249
package: &Package)
249-
-> Result<(), DocBuilderError> {
250+
-> Result<Json, DocBuilderError> {
250251
debug!("Adding documentation into database");
251252
let prefix = format!("rustdoc/{}/{}",
252253
package.manifest().name(),

0 commit comments

Comments
 (0)