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

first version #1

Merged
merged 14 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from 6 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
29 changes: 29 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2019, sot
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
# aca_helpers
# aca_helpers

This is a collection of utilities for ACA packages.
It currently includes:

- get_version. A function to get the version from installed package information ot git.

from aca_helpers import get_version
version = get_version('chandra_aca')

import aca_helpers.version
print(aca_helpers.version.parse_version(version))
Empty file added README.rst
Empty file.
9 changes: 9 additions & 0 deletions aca_helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
ACA Helpers is a collection of utilities for ACA packages.
It currently includes:

- get_version. A function to get the version from installed package information ot git.
"""
from .version import get_version

__version__ = get_version(__package__)
66 changes: 66 additions & 0 deletions aca_helpers/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
This module provides utilities to handle package versions. The version of a package is determined
using pkg_resources if it is installed, and
`setuptools_scm <https://github.com/pypa/setuptools_scm/>`_ otherwise.
"""

import re
import importlib
from pkg_resources import get_distribution, DistributionNotFound


def get_version(package, git_root='..'):
"""
Get version string. If there is no package info, get it from git.

:param package:
:param root: path of git root directory, relative to module file.
:return: str
The version string
"""
errors = []
try:
dist_info = get_distribution(package)
version = dist_info.version
except DistributionNotFound:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to be broad Exception. This code should never raise an exception.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

dist_info = None
version = None
errors.append(f'No pkg_resources found for {package}')

# this appears to be cyclic, but it is not.
# When called from within __init__.py, the module is not completely defined yet,
# but that is not an issue here.
module = importlib.import_module(package)
Copy link
Member

@taldcroft taldcroft Dec 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A key goal (requirement?) is to avoid import of the module just to get the version. I think you can look at testr.test() for some hints on how to get the upstream ‘file‘. Hopefully that will be fully sufficient.

Copy link
Collaborator Author

@javierggt javierggt Dec 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good points, but this one I'm not sure.

  1. In general I think explicit is better than implicit,
  2. accessing the upstream file breaks encapsulation and it is not clear from the interface.
  3. It does not use anything from the module itself (the module is partially initialized), it only gets the file. Maybe there is another way to get the file of a module without importing it?

On the other hand, what you propose depends on the context of the function call. In other words, if I call

  get_version('maude')

from some other place, I will get a wrong answer.

After I pushed this, I was thinking that I do not like the current interface because one needs to give the git root as an argument, which is something client code should not be expected to know. Only the maude module knows its relation to the git root. It is again an issue of context.

One way to make the context dependence explicit (although not to remove it) is to pass the file as argument, basically having the 'root' and 'relative_to' arguments from setuptools_scm.get_version.

I would be happy with this partial loading of the module, if I could also then determine whether the file is in a git repository and where the git root is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically I'm OK with context dependence and documenting clearly that get_version is only intended for use in a very specific context, namely being called from a package top-level __init__.py, which might be installed or it might be in a git repo. I'd like this particular get_version to be simple to use in that context, so basically just __version__ = get_version() in every package except Ska.engarchive get_version('Ska.engarchive').

It could be called get_version_in_init if that helps with being explicit. Sorry that my original description was too vague and you have implemented a more generalized function that can work in any context.

if not version or not module.__file__.lower().startswith(dist_info.location.lower()):
version = None
try:
from setuptools_scm import get_version
version = get_version(root=git_root, relative_to=module.__file__)
except Exception as err:
errors.append(err)

if not version:
error = f'Failed to find a package version for {package}'
for err in errors:
error += f'\n - Error: {err}'
raise RuntimeError(error)

return version


def parse_version(version):
"""
Parse version string and return a dictionary with version information.
This only handles the default scheme.

:param version: str
:return: dict
"""
fmt = '(?P<major>[0-9]+)(.(?P<minor>[0-9]+))?(.(?P<patch>[0-9]+))?' \
'(.dev(?P<distance>[0-9]+))?'\
'(\+(?P<letter>\S)g?(?P<hash>\S+)\.(d(?P<date>[0-9]+))?)?'
m = re.match(fmt, version)
if not m:
raise RuntimeError(f'version {version} could not be parsed')
result = m.groupdict()
return result
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = python -msphinx
SPHINXPROJ = aca_helpers
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
152 changes: 152 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# aca_helpers documentation build configuration file, created by
# sphinx-quickstart on Fri Dec 13 09:48:53 2019.
#
# This file is execfile()d with the current directory set to its
# containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import sys
import os
sys.path.insert(0, os.path.abspath('..'))
from aca_helpers import __version__


# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx.ext.autodoc',
'sphinx.ext.doctest',
'sphinx.ext.coverage']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = 'ACA Helpers'
copyright = '2019, Javier Gonzalez'
author = 'Javier Gonzalez'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = __version__
# The full version, including alpha/beta/rc tags.
release = __version__

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'

# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = False


# -- Options for HTML output ----------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'default'

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# This is required for the alabaster theme
# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars
# html_sidebars = {}


# -- Options for HTMLHelp output ------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'aca_helpers_doc'


# -- Options for LaTeX output ---------------------------------------------

latex_elements = {
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'aca_helpers.tex', 'aca\\_helpers Documentation',
'Javier Gonzalez', 'manual'),
]


# -- Options for manual page output ---------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'aca_helpers', 'aca_helpers Documentation',
[author], 1)
]


# -- Options for Texinfo output -------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'aca_helpers', 'aca_helpers Documentation',
author, 'aca_helpers', 'One line description of project.',
'Miscellaneous'),
]



65 changes: 65 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
.. aca_helpers documentation master file, created by
sphinx-quickstart on Fri Dec 13 09:48:53 2019.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

ACA Package Helpers
===================

.. automodule:: aca_helpers
:members:

Get Version
-----------

.. automodule:: aca_helpers.get_version
:members:


Default versioning scheme
^^^^^^^^^^^^^^^^^^^^^^^^^^

What follows is the scheme as described in `setuptools_scm's documentation <https://github.com/pypa/setuptools_scm/>`.

In the standard configuration ``setuptools_scm`` takes a look at three things:

1. latest tag (with a version number)
2. the distance to this tag (e.g. number of revisions since latest tag)
3. workdir state (e.g. uncommitted changes since latest tag)

and uses roughly the following logic to render the version:

no distance and clean:
``{tag}``
distance and clean:
``{next_version}.dev{distance}+{scm letter}{revision hash}``
no distance and not clean:
``{tag}+dYYYMMMDD``
distance and not clean:
``{next_version}.dev{distance}+{scm letter}{revision hash}.dYYYMMMDD``

The next version is calculated by adding ``1`` to the last numeric component of
the tag.

For Git projects, the version relies on `git describe <https://git-scm.com/docs/git-describe>`_,
so you will see an additional ``g`` prepended to the ``{revision hash}``.


Due to the default behavior it's necessary to always include a
patch version (the ``3`` in ``1.2.3``), or else the automatic guessing
will increment the wrong part of the SemVer (e.g. tag ``2.0`` results in
``2.1.devX`` instead of ``2.0.1.devX``). So please make sure to tag
accordingly.

.. toctree::
:maxdepth: 2
:caption: Contents:



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
19 changes: 19 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from distutils.core import setup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from setuptools import setup

I can't tell you exactly why, but I think setuptools is the way to go. For one thing, my astropy colleagues who obsess about infrastructure have put together this guide: https://oa-packaging-guide-preview.readthedocs.io/en/latest/minimal.html. We don't need to fully buy into this immediately, but they have their pulse on the modern idioms.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


try:
from testr.setup_helper import cmdclass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setup_helper would be a perfect candidate to migrate into this package. We do not need to remove the old one, but if we make ska_helpers a standard entry in setup_requires then one can imagine just

from ska_helpers.setup import cmdclass

The ska-helpers package can be put on PyPI to always be available for a networked machine.

except ImportError:
cmdclass = {}

setup(name='aca_helpers',
description='Utilities for SKA packages',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SKA is the Square Kilometer Array. The ska runtime environment is not an acronym! 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

author='Javier Gonzalez',
author_email='[email protected]',
url='http://cxc.harvard.edu/mta/ASPECT/tool_doc/aca_helpers.html',
packages=['aca_helpers'],
tests_require=['pytest'],
use_scm_version=True,
setup_requires=['setuptools_scm'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs also 'setuptool_scm_git_archive' to allow building from a zip file. See sparkles for the template, including those two other files .gitattributes and the other .git_<something>.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

cmdclass=cmdclass,
)