Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/output-citation' into citation-c…
Browse files Browse the repository at this point in the history
…ff-zenodo
  • Loading branch information
alexlancaster committed Nov 14, 2024
2 parents b6d8c9d + ecec313 commit 7fde845
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 566 deletions.
53 changes: 0 additions & 53 deletions .github/actions/update_citations/action.yml

This file was deleted.

29 changes: 24 additions & 5 deletions .github/workflows/build_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ on:
- '.github/workflows/buildjet_arm64.yml'
- '.github/workflows/release-drafter.yml'
- '.github/workflows/codeql.yml'
- '.github/workflows/run_citations.yml'
- '.github/actions/update_citations/action.yml'
- '.gitattributes'
push:
paths-ignore:
Expand All @@ -37,8 +35,6 @@ on:
- '.github/workflows/buildjet_arm64.yml'
- '.github/workflows/release-drafter.yml'
- '.github/workflows/codeql.yml'
- '.github/workflows/run_citations.yml'
- '.github/actions/update_citations/action.yml'
- '.gitattributes'
release:
types:
Expand Down Expand Up @@ -215,7 +211,30 @@ jobs:
if: runner.os == 'Linux'
uses: docker/setup-qemu-action@v3
with:
platforms: all
platforms: all
- name: Install toml and remove cffconvert from pyproject.toml
run: |
python -m pip install toml
python -c "
import toml
with open('pyproject.toml', 'r') as f:
config = toml.load(f)
if 'build-system' in config and 'requires' in config['build-system']:
config['build-system']['requires'] = [
dep for dep in config['build-system']['requires'] if 'cffconvert' not in dep.lower()
]
with open('pyproject.toml', 'w') as f:
toml.dump(config, f)
"
- name: Install a recent stable Python to handle cffconvert
uses: actions/setup-python@v4
with:
python-version: 3.12
- name: Generate citation formats
run: |
python --version
python -m pip install git+https://github.com/alexlancaster/cffconvert.git@combine_features#egg=cffconvert
python src/PyPop/citation.py
- name: Build and test wheels
uses: pypa/cibuildwheel@v2.21.3
env:
Expand Down
34 changes: 0 additions & 34 deletions .github/workflows/run_citations.yml

This file was deleted.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ write_to = "src/PyPop/_version.py" # matches the path where version will be wri
build-backend = "setuptools.build_meta:__legacy__"
requires = ["setuptools>=42",
"setuptools_scm[toml]>=6.2",
# "cffconvert @ git+https://github.com/alexlancaster/cffconvert.git@combine_features#egg=cffconvert",
"cffconvert @ git+https://github.com/alexlancaster/cffconvert.git@combine_features#egg=cffconvert",
"importlib-metadata; python_version <= '3.8'"
]

Expand Down
49 changes: 14 additions & 35 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
# UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

import sys, os
import shutil
from glob import glob
from setuptools import setup
from setuptools.extension import Extension
Expand Down Expand Up @@ -221,50 +220,29 @@ def path_to_src(source_path_list):

citation_data_file_paths = []
# citation files are in a subdirectory of PyPop, but not a separate module
from src.PyPop import citation_output_formats
from src.PyPop.citation import citation_output_formats, convert_citation_formats
citation_files = [os.path.join("citation", 'CITATION.' + suffix) for suffix in citation_output_formats]
citation_data_file_paths.extend(citation_files)

# currently disabled (these are built in a github action)
class CustomBuildPy(_build_py):
def run(self):

from cffconvert import Citation

# do standard build process
super().run()

# source citation path (single-source of truth)
citation_path = "CITATION.cff"

# then copy CITATION.cff to temp build directory
# use setuptools' temp build directory
build_lib = self.get_finalized_command('build').build_lib

# target directory for the CITATION file within the build directory
target_dir = os.path.join(build_lib, "PyPop", "citation")
# if not running from a CIBUILDWHEEL environment variable
# we need to create the citations
if os.environ.get('CIBUILDWHEEL') != '1':

# source citation path (single-source of truth)
citation_path = "CITATION.cff"

# create the citation directory if it doesn’t exist
os.makedirs(target_dir, exist_ok=True)
shutil.copy(citation_path, target_dir)
# then copy CITATION.cff to temp build directory
# use setuptools' temp build directory
build_lib = self.get_finalized_command('build').build_lib

# load the CITATION.cff content
cff = Citation(cffstr=open(citation_path).read())

# remove 'cff' from generated list - since we don't generate that
citation_output_formats.remove('cff')

for fmt in citation_output_formats:
# use getattr to get the method based on the format string
convert_method = getattr(cff, f"as_{fmt}", None)
if callable(convert_method):
converted_content = convert_method()
else:
print(f"Conversion format '{fmt}' not supported.")

# save the converted output (e.g., as CITATION.json)
with open(os.path.join(target_dir, "CITATION." + fmt), "w") as f:
f.write(converted_content)
convert_citation_formats(build_lib, citation_path)

# read the contents of your README file
from pathlib import Path
Expand Down Expand Up @@ -318,7 +296,8 @@ def run(self):
},
ext_modules=extensions,
cmdclass={'clean': CleanCommand,
# disable the custom build
# 'build_py': CustomBuildPy,
# enable the custom build
'build_py': CustomBuildPy,
},

)
13 changes: 8 additions & 5 deletions src/PyPop/CommandLineInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import os, sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter, RawDescriptionHelpFormatter, FileType, Action
from pathlib import Path
from PyPop import platform_info, citation_output_formats # global info and citation formats
from PyPop import platform_info # global info
from PyPop.citation import citation_output_formats # and citation formats

"""Command-line interface for PyPop scripts
"""
Expand All @@ -61,16 +62,18 @@ def __call__(self, parser, namespace, values, option_string=None):
from importlib_resources import files
citation_file = files('PyPop').joinpath(citation_file_name)
citation_text = citation_file.read_text()
except (ModuleNotFoundError, ImportError, FileNotFoundError): # fallback to looking in current directory if running from repo
pypop_dir = Path(__file__).resolve().parent
citation_file = pypop_dir / citation_file_name
except (ModuleNotFoundError, ImportError, FileNotFoundError): # fallback to looking in top-level directory if running from repo
top_level_dir = Path(__file__).resolve().parent.parent.parent
citation_file = top_level_dir / 'CITATION.cff' # only output CFF

if citation_file.exists():
print("only CITATION.cff is available")
print()
citation_text = citation_file.read_text()
else:
print("could not locate the specified citation format.")
parser.exit()

print(citation_text)
parser.exit() # exit after printing the file

Expand Down
36 changes: 0 additions & 36 deletions src/PyPop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
#!/usr/bin/env python

# This file is part of PyPop

# Copyright (C) 2017.
# All Rights Reserved.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
# INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
# LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
# DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.

# REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
# DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
# IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
# UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

import platform

__pkgname__ = 'pypop-genomics'
Expand All @@ -56,7 +21,6 @@

platform_info="[Python {python_version} | {system} | {arch}]".format(python_version=platform.python_version(), system=platform.platform(), arch=platform.machine())

citation_output_formats = ['apalike', 'bibtex', 'endnote', 'ris', 'codemeta', 'cff']

import locale
import logging
Expand Down
73 changes: 73 additions & 0 deletions src/PyPop/citation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env python

# This file is part of PyPop

# Copyright (C) 2017.
# All Rights Reserved.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

# IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
# INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
# LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
# DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.

# REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
# DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
# IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
# UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

import os
import shutil

citation_output_formats = ['apalike', 'bibtex', 'endnote', 'ris', 'codemeta', 'cff']

def convert_citation_formats(build_lib, citation_path):

from cffconvert import Citation

# target directory for the CITATION file within the build directory
target_dir = os.path.join(build_lib, "PyPop", "citation")

# create the citation directory if it doesn’t exist
os.makedirs(target_dir, exist_ok=True)
shutil.copy(citation_path, target_dir)

# load the CITATION.cff content
cff = Citation(cffstr=open(citation_path).read())

# remove 'cff' from generated list - since we don't generate that
citation_output_formats.remove('cff')

for fmt in citation_output_formats:
# use getattr to get the method based on the format string
convert_method = getattr(cff, f"as_{fmt}", None)
if callable(convert_method):
converted_content = convert_method()
else:
print(f"Conversion format '{fmt}' not supported.")

# save the converted output (e.g., as CITATION.json)
with open(os.path.join(target_dir, "CITATION." + fmt), "w") as f:
f.write(converted_content)

if __name__ == "__main__":

convert_citation_formats("src", "CITATION.cff")

1 change: 0 additions & 1 deletion src/PyPop/citation/CITATION.apalike

This file was deleted.

7 changes: 0 additions & 7 deletions src/PyPop/citation/CITATION.bibtex

This file was deleted.

Loading

0 comments on commit 7fde845

Please sign in to comment.