Commit 8d574a2f authored by PTSEFTON's avatar PTSEFTON
Browse files

Now RO-crate compatible, uses the ro-crate library, and bagging is simplified

parent 80be7a9f
......@@ -23,7 +23,7 @@ const Index = require("./lib/index_html.js");
const Bag = require("./lib/bag.js");
// const ROCrate = require("ro-crate").ROCrate;
const ROCrate = require("../../working/ro-crate-js/lib/rocrate");
const ROCrate = require("ro-crate").ROCrate;
const path = require("path");
......@@ -32,7 +32,7 @@ const program = require("commander");
const defaults = require("./lib/defaults.js");
const htmlFileName = defaults.html_file_name;
const metadata_file_name = defaults.metadata_json_file_name;
const fs = require("fs")
const fs = require("fs-extra")
var dirs = undefined;
program
......@@ -50,8 +50,8 @@ program
"-z, --zip",
"Create a zipped version of the bag - only applies with --bag"
)
.option("-d, --depth [depth]", "Maximum depth to recurse into directories")
.option("-r, --recurse", "Recurse into directories looking for ro-crate-metadata_.xslx files")
.option("-d, --depth [depth]", "Maximum depth to recurse into directories looking for or creating CATALOG_.xlsx file")
.option("-r, --recurse", "Recurse into directories looking for or creating CATALOG_.xslx files")
.option("-c, --cratescript [cratesript]", "URL of Crate-script directtory")
.option(
"-u, --url [distro]",
......@@ -136,40 +136,39 @@ async function generateHTML(metadata_path) {
}
var index_maker = new Index();
index_maker.init(
crate,
path.join(__dirname, "defaults", "metadata_template.html")
);
fs.writeFileSync(path.join(dir, htmlFileName),
await index_maker.make_index_html(program.url, program.cratescript));
if (program.bag) {
// Bag the HTML
// TODO - GET THIS WORKING
var bagger = new Bag();
var dest = path.join(program.bag, path.basename(dir));
shell.rm("-rf", dest);
dir = bagger.bag(dir, program.bag, crate);
dir = bagger.bag(dir, dest, crate);
bagger.generate_bag_info();
bagger.save_bag_info();
if (!path.isAbsolute(dir)) {
dir = path.join(process.cwd(), dir);
}
metadata_path = path.join(dir, metadata_file_name);
const index = await fs.copyFile(path.join(__dirname, "defaults", "index.html"), path.join(dest, "index.html"));
bagger.update();
// Reload new JSON
crate = new ROCrate(crate.getJson());
}
var index_maker = new Index();
index_maker.init(
crate,
path.join(__dirname, "defaults/metadata_template.html")
);
fs.writeFileSync(path.join(dir, htmlFileName),
await index_maker.make_index_html(program.url, program.cratescript));
if (program.zip && program.bag) {
shell.cd(bagger.target_dir);
var zipname = path.join("../", path.basename(bagger.target_dir) + ".zip");
shell.cd(bagger.targetDir);
var zipname = path.join("../", path.basename(bagger.targetDir) + ".zip");
shell.rm("-f", zipname);
shell.cd(bagger.target_dir);
shell.exec("zip -r " + zipname + " " + "*");
}
......
......@@ -14,7 +14,7 @@
<script src="<%= render_script %>/crate.js"></script>
<script src="<%= render_script %>"></script>
......
......@@ -25,20 +25,18 @@ const defaults = require("./defaults.js");
module.exports = function() {
return {
target_dir: this.target_dir,
targetDir: this.targetDir,
metadata_path: this.metadata_path,
generate_bag_info: function generate_bag_info() {
this.bag_meta = {
"BagIt-Profile-Identifier": defaults.BagIt_Profile_Identifier,
"DataCrate-Specification-Identifier":
defaults.DataCrate_Specification_Identifier
"ROCrate_Specification_Identifier":
this.crate.defaults.ROCrate_Specification_Identifier
};
if (this.helper.root_node["contactPoint"] && this.helper.root_node["contactPoint"]["@id"]) {
contact = this.helper.item_by_id[this.helper.root_node["contactPoint"]["@id"]];
if (this.root["contactPoint"] && this.root["contactPoint"]["@id"]) {
contact = this.crate.getItem(this.root["contactPoint"]["@id"]);
map = {
email: "Contact-Email",
phone: "Contact-Telephone",
......@@ -52,16 +50,16 @@ module.exports = function() {
}
}
if (this.helper.root_node["publisher"] && this.helper.root_node["publisher"]["@id"]) {
publisher = this.helper.item_by_id[this.helper.root_node["publisher"]["@id"]];
if (this.root["publisher"] && this.root["publisher"]["@id"]) {
publisher = this.crate.getItem(this.root["publisher"]["@id"]);
if(publisher["name"]) {
this.bag_meta["SourceOrganization"] = publisher.name
}
}
if (this.helper.root_node["description"]) {
this.bag_meta["External-Description"] = this.helper.root_node["description"];
if (this.root["description"]) {
this.bag_meta["External-Description"] = this.root["description"];
}
this.bag_meta["Bagging-Date"] = new Date().toISOString();
// Return a hash of BagIt style metadata by looking for it in the JSON-LD structure
......@@ -75,74 +73,32 @@ module.exports = function() {
for (var [k, v] of Object.entries(this.bag_meta)) {
bag_info += k + ": " + v + "\n";
}
fs.writeFileSync(path.join(this.target_dir, "bag-info.txt"), bag_info);
fs.writeFileSync(path.join(this.targetDir, "bag-info.txt"), bag_info);
},
update: function update_bag_tags() {
shell.exec("bagit updatetagmanifests " + this.target_dir);
shell.exec("bagit updatetagmanifests " + this.targetDir);
},
// Move to /data
fix_paths: function fix_paths(metadata_path) {
if (!path.isAbsolute(metadata_path)) {
metadata_path = path.join("./", metadata_path);
}
var metadata = this.helper.json_ld;
for (let item of metadata["@graph"]) {
if (item["path"]) {
var p = this.helper.value_as_array(item["path"])[0];
var new_p = item["@path"];
if (p != "./") {
new_p = path.join("data", p);
item["path"] = new_p;
}
if (item["@id"] === p) {
this.helper.update_id(item, new_p)
}
}
this.helper.update_all_ids();
}
fs.writeFileSync(
metadata_path,
JSON.stringify(this.helper.json_ld, null, 2),
function(err) {
if (err) {
return console.log(err, "Error writing in", metadata_path);
}
}
);
},
bag: function bag(source_dir, bag_dir, helper) {
bag: function bag(source_dir, bag_dir, crate) {
// TODO Generate a list of all files
// FOR NOW: delete metadata.json and index.html
// Generate bag info later
this.helper = helper;
var tmpobj = tmp.dirSync();
var bag_name = path.basename(source_dir);
var target_dir = path.join(bag_dir, bag_name);
shell.cp(
path.join(source_dir, defaults.metadata_json_file_name ),
path.join(tmpobj.name,"metadata_backup.json")
);
shell.cp(path.join(source_dir, defaults.metadata_json_file_name), tmpobj.name);
shell.rm(path.join(source_dir, defaults.html_file_name)); // Don't worry, welll make a new one
this.crate = crate;
this.crate.index();
this.root = crate.getRootDataset();
this.helper = crate.utils;
this.targetDir = bag_dir;
shell.exec(
"bagit create --excludebaginfo " +
target_dir +
this.targetDir +
" " +
path.join(source_dir, "*")
);
shell.rm(path.join(target_dir, "data", defaults.metadata_json_file_name), target_dir);
shell.exec("bagit update " + target_dir);
shell.cp(path.join(tmpobj.name, defaults.metadata_json_file_name), target_dir);
this.fix_paths(path.join(target_dir, defaults.metadata_json_file_name));
this.target_dir = target_dir;
return target_dir;
return this.targetDir;
}
};
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment