tools: Safely handle Ctrl-C and ignore some additional files

Don't want to scan files we don't own. Not the greatest solution, but it does work.
This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-03-01 08:39:18 +01:00
parent bd8a574e7a
commit d1ffa2c942

View file

@ -10,8 +10,33 @@ const OS = require("os");
const SECTION_START = "AUTOGENERATED COPYRIGHT HEADER START"; const SECTION_START = "AUTOGENERATED COPYRIGHT HEADER START";
const SECTION_END = "AUTOGENERATED COPYRIGHT HEADER END"; const SECTION_END = "AUTOGENERATED COPYRIGHT HEADER END";
const IGNORED = [
".git",
"cmake/clang",
"cmake/version",
"third-party",
]
let abortAllWork = false;
async function isIgnored(path) {
let rpath = PATH.relative(process.cwd(), path).replaceAll(PATH.sep, PATH.posix.sep);
for (let ignore of IGNORED) {
if (ignore instanceof RegExp) {
if (ignore.global) {
if (!rpath.matchAll(ignore).done) {
return true;
}
} else {
if (rpath.match(ignore) !== null) {
return true;
}
}
} else if (rpath.startsWith(ignore)) {
return true;
}
}
async function git_isIgnored(path) {
return await new Promise((resolve, reject) => { return await new Promise((resolve, reject) => {
try { try {
let proc = CHILD_PROCESS.spawn("git", [ let proc = CHILD_PROCESS.spawn("git", [
@ -245,13 +270,24 @@ function makeHeader(file, copyright) {
async function addCopyright(file) { async function addCopyright(file) {
try { try {
if (abortAllWork) {
return;
}
// Async/Promises // Async/Promises
// Copyright information.
let copyright = await generateCopyright(file);
let header = undefined;
try {
header = makeHeader(file, copyright);
} catch (ex) {
return;
}
console.log(`Updating file '${file}'...`);
// File contents.
let content = await FSPROMISES.readFile(file); let content = await FSPROMISES.readFile(file);
let eol = (content.indexOf("\r\n") != -1 ? OS.EOL : "\n"); let eol = (content.indexOf("\r\n") != -1 ? OS.EOL : "\n");
let copyright = await generateCopyright(file);
let header = makeHeader(file, copyright);
let insert = Buffer.from(header.join(eol) + eol); let insert = Buffer.from(header.join(eol) + eol);
// Find the starting point. // Find the starting point.
@ -264,7 +300,11 @@ async function addCopyright(file) {
endHeader = content.indexOf(eol, endHeader); endHeader = content.indexOf(eol, endHeader);
endHeader += Buffer.from(eol).byteLength; endHeader += Buffer.from(eol).byteLength;
let fd = await FSPROMISES.open(file, "w+"); if (abortAllWork) {
return;
}
let fd = await FSPROMISES.open(file, "w");
let fp = []; let fp = [];
if ((startHeader >= 0) && (endHeader >= 0)) { if ((startHeader >= 0) && (endHeader >= 0)) {
let pos = 0; let pos = 0;
@ -318,7 +358,10 @@ async function addCopyright(file) {
} }
async function addCopyrights(path) { async function addCopyrights(path) {
if (await git_isIgnored(path)) { if (abortAllWork) {
return;
}
if (await isIgnored(path)) {
return; return;
} }
@ -326,16 +369,20 @@ async function addCopyrights(path) {
let files = await FSPROMISES.readdir(path, { "withFileTypes": true }); let files = await FSPROMISES.readdir(path, { "withFileTypes": true });
for (let file of files) { for (let file of files) {
if (abortAllWork) {
break;
}
let fullname = PATH.join(path, file.name); let fullname = PATH.join(path, file.name);
if (await git_isIgnored(fullname)) { if (await isIgnored(fullname)) {
console.log(`Ignoring path '${fullname}'...`); console.log(`Ignoring path '${fullname}'...`);
continue; continue;
} }
if (file.isDirectory()) { if (file.isDirectory()) {
console.log(`Scanning path '${fullname}'...`); //console.log(`Scanning path '${fullname}'...`);
promises.push(addCopyrights(fullname)); promises.push(addCopyrights(fullname));
} else { } else {
console.log(`Updating file '${fullname}'...`);
promises.push(addCopyright(fullname)); promises.push(addCopyright(fullname));
} }
} }
@ -344,12 +391,36 @@ async function addCopyrights(path) {
} }
(async function () { (async function () {
let file = PROCESS.argv[2]; PROCESS.on("SIGINT", (ev) => {
let pathStat = await FSPROMISES.stat(file); abortAllWork = true;
if (pathStat.isDirectory()) { console.log("Sanely aborting all pending work...");
await addCopyrights(PATH.resolve(file)); })
let path = PATH.resolve(PROCESS.argv[2]);
{ // Bootstrap to actually be in the directory where '.git' is.
let is_git_directory = false;
while (!is_git_directory) {
if (abortAllWork) {
return;
}
let entries = await FSPROMISES.readdir(PROCESS.cwd());
if (entries.includes(".git")) {
console.log(`Found .git at '${process.cwd()}'.`);
is_git_directory = true;
} else { } else {
await addCopyright(PATH.resolve(file)); PROCESS.chdir(PATH.resolve(PATH.join(PROCESS.cwd(), "..")));
}
}
path = PATH.normalize(PATH.relative(process.cwd(), path));
}
let pathStat = await FSPROMISES.stat(path);
if (pathStat.isDirectory()) {
await addCopyrights(PATH.resolve(path));
} else {
await addCopyright(PATH.resolve(path));
} }
console.log("Done"); console.log("Done");
})(); })();