Skip to content

Commit

Permalink
Fix mason license source (#25062)
Browse files Browse the repository at this point in the history
In Mason, updates the source list of valid license names to the proper
spdx repository. Also adds a new option to `mason publish`,
`--refresh-licences` that will force delete and make a new clone of the
license repository that is stored in the `.mason` folder.

The feature is implemented in such a way that if it's passed, it will be
the only option executed. This felt OK to me since it should be a very
rarely if ever used option and it seemed like a distinct operation from
the publishing actions.

Includes some testing for the refresh-licenses feature, while there,
updated some tests that had malformed arguments to the corresponding
`mason` sub-function, and fixed some linter warnings for unused names
and indentation.

TESTING:

- [x] paratest `[Summary: #Successes = 17229 | #Failures = 0 | #Futures
= 926]`

[reviewed by @benharsh - thanks!]
  • Loading branch information
arezaii authored May 23, 2024
2 parents 79f7866 + fe145fb commit 10247fd
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 18 deletions.
6 changes: 3 additions & 3 deletions test/mason/publish/dryTest.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use MasonNew;
const dir = here.cwd();

proc dry() {
masonNew(['mason', 'new', 'publishCheck']);
masonNew(['new', 'publishCheck']);
here.chdir(dir + '/publishCheck');
masonPublish(['mason', 'publish', '--dry-run']);
masonPublish(['publish', '--dry-run']);
}

dry();
dry();
2 changes: 1 addition & 1 deletion test/mason/publish/dryTest.good
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Created new library project: publishCheck
Created new application project: publishCheck
Package does not gave a git origin
12 changes: 12 additions & 0 deletions test/mason/publish/licenseList.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use MasonPublish;
use MasonEnv;
use FileSystem;


proc main() {
const args = ["publish", "--refresh-licenses"];
const spdxDir = MASON_HOME + '/spdx';
const spdxTextDir = spdxDir + "/text";
if exists(spdxTextDir) then rmTree(spdxTextDir);
masonPublish(args);
}
1 change: 1 addition & 0 deletions test/mason/publish/licenseList.compopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-M ../../../tools/mason
2 changes: 2 additions & 0 deletions test/mason/publish/licenseList.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Force updating list of valid license names from SPDX repo...
Done updating license list
37 changes: 37 additions & 0 deletions test/mason/publish/licenseRefresh.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use MasonPublish;
use MasonEnv;
use MasonUtils;
use FileSystem;
use Time;


proc main() {
const args = ["publish", "--refresh-licenses"];
const spdxDir = MASON_HOME + '/spdx';
const spdxTextDir = spdxDir + "/text";

// creates a new dir and clones when it doesn't exist, without overwrite flag
if exists(spdxDir) then rmTree(spdxDir);
writeln("spdx dir does not exist, creating it...");
refreshLicenseList();

assert(exists(spdxDir));
assert(exists(spdxTextDir));
writeln("spdx dir was created, removing the text/ subdir...");
rmTree(spdxTextDir);

assert(!exists(spdxTextDir));


try! {
refreshLicenseList(); // expect this to error because we deleted the text dir
} catch e: MasonError {
assert(!exists(spdxTextDir));
writeln("spdx/text dir was not recreated because we did not force an overwrite");
}

sleep(5); // sleep a little to let git settle from the previous clone above
refreshLicenseList(true); // set the overwrite flag to force getting the repo again
assert(exists(spdxTextDir));
writeln("spdx/text dir was recreated with use of overwrite flag");
}
1 change: 1 addition & 0 deletions test/mason/publish/licenseRefresh.compopts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-M ../../../tools/mason
4 changes: 4 additions & 0 deletions test/mason/publish/licenseRefresh.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spdx dir does not exist, creating it...
spdx dir was created, removing the text/ subdir...
spdx/text dir was not recreated because we did not force an overwrite
spdx/text dir was recreated with use of overwrite flag
3 changes: 2 additions & 1 deletion test/mason/publish/publishHelp.good
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Options:
--dry-run Check to see if package is ready to be published
--check Runs check to see if package can be published successfully to <registry>
--ci-check Same as --check, except omits git origin checks
--[no-]update [Do not] Prevent registries from being updated when a package is published.
--[no-]update [Do not] Prevent registries from being updated when a package is published
--refresh-licenses Force-update the list of valid license names and immediately exit without publishing

Publishing requires the mason-registry to be forked and the package to have a remote origin.
3 changes: 2 additions & 1 deletion test/mason/publish/publishHelpShort.good
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Options:
--dry-run Check to see if package is ready to be published
--check Runs check to see if package can be published successfully to <registry>
--ci-check Same as --check, except omits git origin checks
--[no-]update [Do not] Prevent registries from being updated when a package is published.
--[no-]update [Do not] Prevent registries from being updated when a package is published
--refresh-licenses Force-update the list of valid license names and immediately exit without publishing

Publishing requires the mason-registry to be forked and the package to have a remote origin.
3 changes: 2 additions & 1 deletion tools/mason/MasonHelp.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ proc masonPublishHelp(){
writeln(' --dry-run Check to see if package is ready to be published');
writeln(' --check Runs check to see if package can be published successfully to <registry>');
writeln(' --ci-check Same as --check, except omits git origin checks');
writeln(' --[no-]update [Do not] Prevent registries from being updated when a package is published.');
writeln(' --[no-]update [Do not] Prevent registries from being updated when a package is published');
writeln(' --refresh-licenses Force-update the list of valid license names and immediately exit without publishing');
writeln();
writeln('Publishing requires the mason-registry to be forked and the package to have a remote origin.');
}
Expand Down
55 changes: 44 additions & 11 deletions tools/mason/MasonPublish.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ proc masonPublish(ref args: list(string)) throws {
var updateFlag = parser.addFlag(name="update", flagInversion=true);
var registryArg = parser.addArgument(name="registry", numArgs=0..1);

var refreshLicenseFlag = parser.addFlag(name="refresh-licenses",
defaultValue=false);

parser.parseArgs(args.toArray());

try! {
var dry = dryFlag.valueAsBool();
var checkFlag = checkArg.valueAsBool();
var refreshLicenses = refreshLicenseFlag.valueAsBool();
var registryPath = "";
if registryArg.hasValue() then registryPath = registryArg.value();
var username = getUsername();
Expand All @@ -78,7 +82,14 @@ proc masonPublish(ref args: list(string)) throws {
}
var createReg = createFlag.valueAsBool();

const badSyntaxMessage = 'Arguments does not follow "mason publish [options] <registry>" syntax';
const badSyntaxMessage = 'Arguments do not follow "mason publish [options] <registry>" syntax';

if refreshLicenses {
writeln("Force updating list of valid license names from SPDX repo...");
refreshLicenseList(true);
writeln("Done updating license list");
exit(0);
}

if createReg {
var pathReg = registryPath;
Expand Down Expand Up @@ -669,6 +680,34 @@ proc check(username : string, path : string, trueIfLocal : bool, ci : bool) thro
exit(0);
}

/*
update the list of valid licenses or pulls a new copy of the list if one
does not already exist. If overwrite is true, delete the list and pull a new
copy of the repo.
*/
proc refreshLicenseList(overwrite=false) throws {
const dest = MASON_HOME + '/spdx';
const branch = '--branch main ';
const depth = '--depth 1 ';
const url = 'https://github.com/spdx/license-list-data.git ';
const command = 'git clone -q ' + branch + depth + url + dest;
if !isDir(dest) {
runCommand(command);
} else if overwrite {
rmTree(dest);
runCommand(command);
}

if !isDir(dest + "/text") {
throw new owned MasonError("Expected to find license list data at " + dest +
"/text, but location does not exist. Try running\
'mason publish --refresh-licenses' to update the\
license list.");
}
const licenseList = listDir(dest + "/text");
return licenseList;
}

/* Validate license with that of SPDX list */
private proc checkLicense(projectHome: string) throws {
var foundValidLicense = false;
Expand All @@ -679,14 +718,8 @@ private proc checkLicense(projectHome: string) throws {
if tomlFile.pathExists("brick.license") {
defaultLicense = tomlFile["brick"]!["license"]!.s;
}
// git clone the SPDX repo and validate license identifier
const dest = MASON_HOME + '/spdx';
const branch = '--branch master ';
const depth = '--depth 1 ';
const url = 'https://github.com/spdx/license-list.git ';
const command = 'git clone -q ' + branch + depth + url + dest;
if !isDir(dest) then runCommand(command);
var licenseList = listDir(MASON_HOME + "/spdx");
// get the license list and validate license identifier
const licenseList = refreshLicenseList();
for licenses in licenseList {
const licenseName: string = licenses.strip('.txt', trailing=true);
if licenseName == defaultLicense || defaultLicense == 'None' {
Expand All @@ -705,7 +738,7 @@ private proc attemptToBuild() throws {
var sub = spawn(['mason','build','--force'], stdout=pipeStyle.pipe);
sub.wait();
if sub.exitCode == 1 {
writeln('(FAILED) Please make sure your package builds');
writeln('(FAILED) Please make sure your package builds');
}
else {
writeln('(PASSED) Package builds successfully.');
Expand Down Expand Up @@ -820,7 +853,7 @@ private proc returnMasonEnv() {

private proc falseIfRemotePath() {
var registryInEnv = MASON_REGISTRY;
for (name, registry) in registryInEnv {
for (_, registry) in registryInEnv {
if registry.find(':') != -1 {
return false;
}
Expand Down

0 comments on commit 10247fd

Please sign in to comment.