Commit 2823c740 authored by PTSEFTON's avatar PTSEFTON
Browse files

Removed dependency on external bagit code

parent f3702c0a
......@@ -170,7 +170,7 @@ async function generateHTML(metadataPath) {
var dest = path.join(program.bag, path.basename(dir));
shell.rm("-rf", dest);
dir = bagger.bag(dir, dest, crate);
dir = await bagger.bag(dir, dest, crate);
console.log("bagged", dir)
bagger.generate_bag_info();
bagger.save_bag_info();
......@@ -179,7 +179,7 @@ async function generateHTML(metadataPath) {
}
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();
//bagger.update();
if (program.zip){
var zipname = path.resolve(path.join(program.bag, path.basename(path.dirname(metadataPath)).replace(" ","_") + ".zip"));
......
......@@ -18,24 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
/* Bags a datacrate wich already has a metadata.json file */
const path = require("path");
const shell = require("shelljs");
const fs = require("fs");
const fs = require("fs-extra");
const hasha = require('hasha');
const _ = require('lodash');
const DIGEST_ALGORITHM = 'sha512';
module.exports = function() {
return {
targetDir: this.targetDir,
metadata_path: this.metadata_path,
generate_bag_info: function generate_bag_info() {
class Bag {
generate_bag_info() {
this.bag_meta = {
"ROCrate_Specification_Identifier":
this.crate.defaults.ROCrate_Specification_Identifier
};
}
if (this.root["contactPoint"] && this.root["contactPoint"]["@id"]) {
contact = this.crate.getItem(this.root["contactPoint"]["@id"]);
map = {
var contact = this.crate.getItem(this.root["contactPoint"]["@id"]);
var map = {
email: "Contact-Email",
phone: "Contact-Telephone",
name: "Contact-Name"
......@@ -49,7 +47,7 @@ module.exports = function() {
}
if (this.root["publisher"] && this.root["publisher"]["@id"]) {
publisher = this.crate.getItem(this.root["publisher"]["@id"]);
var publisher = this.crate.getItem(this.root["publisher"]["@id"]);
if(publisher["name"]) {
this.bag_meta["SourceOrganization"] = publisher.name
......@@ -63,22 +61,23 @@ module.exports = function() {
// Return a hash of BagIt style metadata by looking for it in the JSON-LD structure
},
}
save_bag_info: function save_bag_info() {
save_bag_info() {
var bag_info = "";
for (var [k, v] of Object.entries(this.bag_meta)) {
bag_info += k + ": " + v + "\n";
}
fs.writeFileSync(path.join(this.targetDir, "bag-info.txt"), bag_info);
},
update: function update_bag_tags() {
shell.exec(`bagit updatetagmanifests "${this.targetDir}"`);
},
}
update_bag_tags() {
//shell.exec(`bagit updatetagmanifests "${this.targetDir}"`);
};
bag: function bag(source_dir, bag_dir, crate) {
async bag(sourceDir, bag_dir, crate) {
// TODO Generate a list of all files
// FOR NOW: delete metadata.json and index.html
// Generate bag info later
......@@ -88,12 +87,46 @@ module.exports = function() {
this.root = crate.getRootDataset();
this.helper = crate.utils;
this.targetDir = bag_dir;
shell.exec(
`bagit create --excludebaginfo "${this.targetDir}" "${path.join(source_dir, "*")}"`
);
console.log("TARGET", this.targetDir, "SOURCE", sourceDir)
var dataPath = path.join(this.targetDir, "data");
if (fs.existsSync(this.targetDir)) {
await fs.remove(this.targetDir);
}
await fs.mkdirs(this.targetDir)
//TODO remove target
await fs.copy(sourceDir, path.resolve(dataPath));
var items = {};
await this.digest_dir(path.resolve(this.targetDir), items);
var manifestPath = path.resolve(path.join(this.targetDir, `manifest-${DIGEST_ALGORITHM}.txt`));
var manifest = "";
for (let item of Object.keys(items)){
manifest += `${items[item]} ${path.relative(this.targetDir, item)}\n`;
}
await fs.writeFile(manifestPath, manifest);
var bagFilePath = path.resolve(path.join(this.targetDir, "bagit.txt"));
await fs.writeFile(bagFilePath, "BagIt-Version: 1.0\nTag-File-Character-Encoding: UTF-8");
return this.targetDir;
}
};
};
async digest_dir(dir, items) {
const contents = await fs.readdir(dir);
items = _.flatten(await Promise.all(contents.map(async (p1) => {
const p = path.join(dir, p1);
const stats = await fs.stat(p);
if (stats.isDirectory()) {
await this.digest_dir(p, items);
} else {
const h = await this.hash_file(p);
items[p] = h;
}
})));
}
async hash_file(p) {
const hash = await hasha.fromFile(p, { algorithm: DIGEST_ALGORITHM })
return hash;
}
}
module.exports = Bag;
// Load a wrapper
$ = require('cheerio');
Preview = require("./ro-crate-preview.js");
module.exports = Preview;
\ No newline at end of file
......@@ -772,6 +772,22 @@
"integrity": "sha1-6CB68cx7MNRGzHC3NLXovhj4jVE=",
"dev": true
},
"hasha": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/hasha/-/hasha-5.1.0.tgz",
"integrity": "sha512-OFPDWmzPN1l7atOV1TgBVmNtBxaIysToK6Ve9DK+vT6pYuklw/nPNT+HJbZi0KDcI6vWB+9tgvZ5YD7fA3CXcA==",
"requires": {
"is-stream": "^2.0.0",
"type-fest": "^0.8.0"
},
"dependencies": {
"is-stream": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz",
"integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw=="
}
}
},
"he": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/he/-/he-1.1.1.tgz",
......@@ -1725,6 +1741,11 @@
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz",
"integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g=="
},
"type-fest": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
"integrity": "sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA=="
},
"union": {
"version": "0.4.6",
"resolved": "https://registry.npmjs.org/union/-/union-0.4.6.tgz",
......
......@@ -61,7 +61,7 @@
}
},
{
"@id": "#a250c512-16b5-4e0b-b226-7b231acd0987",
"@id": "#cfd6609e-a69f-4ec5-93cc-3162e86d5e49",
"@type": "GeoCoordinates",
"latitude": "-33.7152",
"longitude": "150.30119",
......@@ -2626,7 +2626,7 @@
"address": "Katoomba, NSW",
"description": "Catalina Park is a disused motor racing venue, located at Katoomba, in the Blue Mountains, New South Wales, Australia, and is recognised as an Aboriginal Place due to the long association of the local Gundungarra and Darug clans to the area.",
"geo": {
"@id": "#a250c512-16b5-4e0b-b226-7b231acd0987"
"@id": "#cfd6609e-a69f-4ec5-93cc-3162e86d5e49"
},
"name": "Catalina Park"
},
......
This diff is collapsed.
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