Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to add codemeta.json #324

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ cmake_policy(VERSION 3.12...3.15)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(FairMQLib)

get_git_version()
get_codemeta_version()
message(STATUS "At main: ${PROJECT_CODEMETA_VERSION}")
get_git_version(DEFAULT_VERSION "${PROJECT_CODEMETA_VERSION}")

project(FairMQ VERSION ${PROJECT_VERSION} LANGUAGES CXX)
message(STATUS "${BWhite}${PROJECT_NAME}${CR} ${PROJECT_GIT_VERSION} from ${PROJECT_DATE}")
Expand Down
17 changes: 17 additions & 0 deletions cmake/FairMQLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ function(get_git_version)
set(${ARGS_OUTVAR_PREFIX}_GIT_DATE ${${ARGS_OUTVAR_PREFIX}_GIT_DATE} PARENT_SCOPE)
endfunction()

function(get_codemeta_version)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19)
file(READ "${CMAKE_SOURCE_DIR}/codemeta.json" codemeta_content)
string(JSON codemeta_version ERROR_VARIABLE json_error
GET "${codemeta_content}" "softwareVersion")
if(NOT "${json_error}" STREQUAL "NOTFOUND")
string(JSON codemeta_version ERROR_VARIABLE json_error
GET "${codemeta_content}" "version")
endif()
if(NOT "${json_error}" STREQUAL "NOTFOUND")
return()
endif()
string(REGEX REPLACE "^v" "" codemeta_version "${codemeta_version}")
set(PROJECT_CODEMETA_VERSION "${codemeta_version}" PARENT_SCOPE)
endif()
endfunction()


# Set defaults
macro(set_fairmq_defaults)
Expand Down
11 changes: 11 additions & 0 deletions codemeta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"@context": "https://doi.org/10.5063/schema/codemeta-2.0",
"@type": "SoftwareSourceCode",
"name": "FairMQ",
"description": "C++ Message Queuing Library and Framework",
"license": "./COPYRIGHT",
"datePublished": "2018-04-15",
"developmentStatus": "active",
"codeRepository": "https://github.com/FairRootGroup/FairMQ/",
"issueTracker": "https://github.com/FairRootGroup/FairMQ/issues"
}
82 changes: 82 additions & 0 deletions codemeta_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#! /usr/bin/env python3
# Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
#
# SPDX-License-Identifier: LGPL-3.0-or-later


import argparse
import json
import re
from collections import OrderedDict


class CodeMetaManipulator(object):
def load(self, filename='codemeta.json'):
with open(filename, 'rb') as fp:
self.data = json.load(fp, object_pairs_hook=OrderedDict)

@staticmethod
def _dict_entry_cmp(dict1, dict2, field):
if (field in dict1) and (field in dict2):
return dict1[field] == dict2[field]
else:
return False

@classmethod
def find_person_entry(cls, person_list, matchdict):
for entry in person_list:
if cls._dict_entry_cmp(entry, matchdict, 'email'):
return entry
if cls._dict_entry_cmp(entry, matchdict, 'familyName') \
and cls._dict_entry_cmp(entry, matchdict, 'givenName'):
return entry
return None

@staticmethod
def update_person_entry(entry, matchdict):
if entry is None:
entry = OrderedDict()
entry['@type'] = 'Person'
for field in ('givenName', 'familyName', 'email'):
val = matchdict.get(field, None)
if val is not None:
entry[field] = val
return entry

def handle_person_list_file(self, filename, cm_field_name):
fp = open(filename, 'r', encoding='utf8')
findregex = re.compile(r'^(?P<familyName>[-\w\s]*[-\w]),\s*'
r'(?P<givenName>[-\w\s]*[-\w])\s*'
r'(?:<(?P<email>\S+@\S+)>)?$')
person_list = self.data.setdefault(cm_field_name, [])
for line in fp:
line = line.strip()
m = findregex.match(line)
if m is None:
raise RuntimeError("Could not analyze line %r" % line)
found_entry = self.find_person_entry(person_list, m.groupdict())
entry = self.update_person_entry(found_entry, m.groupdict())
if found_entry is None:
person_list.append(entry)

def save(self, filename='codemeta.json'):
with open('codemeta.json', 'w', encoding='utf8') as fp:
json.dump(self.data, fp, indent=2)


def main():
parser = argparse.ArgumentParser(description='Update codemeta.json')
parser.add_argument('--set-version', dest='newversion')
args = parser.parse_args()

cm = CodeMetaManipulator()
cm.load()
if args.newversion is not None:
cm.data['softwareVersion'] = args.newversion
cm.handle_person_list_file('AUTHORS', 'author')
cm.handle_person_list_file('CONTRIBUTORS', 'contributor')
cm.save()


if __name__ == '__main__':
main()