Skip to content

Commit

Permalink
The first initial commit of Zserio PyPi
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Jan 6, 2021
0 parents commit 20aebf0
Show file tree
Hide file tree
Showing 19 changed files with 1,742 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# distribution / packaging
build/
dist/

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) 2021, Navigation Data Standard e.v.
All rights reserved.

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

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

* 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.

* 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.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
graft src
graft test
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Zserio PyPi package

Zserio PyPi package contains Zserio compiler and Zserio Python runtime. Zserio is serialization framework
available at [GitHub](http://zserio.org).

## Installation

To install Zserio compiler together with Zserio Python runtime, just run

```
pip install zserio
```

## Usage from command line

Consider the following zserio schema which is stored to the source `appl.zs`:

```
package appl;
struct TestStructure
{
int32 value;
};
```

To compile the schema by compiler and generate Python sources to the directory `gen`, you can run Zserio
compiler directly from command line by the following command:

```
zserio appl.zs -python gen
```

Then, if you run the python by the command

```
PYTHONPATH="gen" python
```

you will be able to use the generated Python sources by the following python commands

```py
import appl.api as api
testStructure = api.TestStructure()
```

## Usage from Python

Consider the following zserio schema which is stored to the source `appl.zs`:

```
package appl;
struct TestStructure
{
int32 value;
};
```

To compile the schema by compiler and generate Python sources to the directory `gen`, you can run the
following python commands:

```py
import zserio
api = zserio.generatePython("appl.zs", genDir = "gen")
testStructure = api.TestStructure()
```

For convenience, the method `generatePython` returns imported API for generated top level package.

Alternatively, you can run zserio compiler directly by the following python commands:

```py
import sys
import importlib
import zserio
completedProcess = zserio.runCompiler(["appl.zs", "-python", "gen"])
if completedProcess.returncode == 0:
sys.path.append("gen")
api = importlib.import_module("appl.api")
testStructure = api.TestStructure()
```

## Building

The easiest way how to build Zserio PyPi package is by using Bash script `build.sh` located in the project's
folder `scripts`:

```
scripts/build.sh
```

## Testing

Testing is available by using Bash script `test.sh` located in the project's folder `scripts`:

```
PYLINT_ENABLED=1 MYPY_ENABLED=1 scripts/test.sh
```
188 changes: 188 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#!/bin/bash

SCRIPT_DIR=`dirname $0`
source "${SCRIPT_DIR}/common_tools.sh"

# Build PyPi package.
build_pypi_package()
{
exit_if_argc_ne $# 3
local PYPI_PROJECT_ROOT="${1}" ; shift
local PYPI_BUILD_DIR="${1}"; shift
local PYPI_DISTR_DIR="${1}"; shift

local BUILD_PYTHON_REQUIREMENTS=("setuptools" "wheel")
activate_python_virtualenv "${PYPI_BUILD_DIR}" BUILD_PYTHON_REQUIREMENTS[@]
if [ $? -ne 0 ] ; then
return 1
fi

pushd "${PYPI_PROJECT_ROOT}" > /dev/null
PYPI_BUILD_DIR="${PYPI_BUILD_DIR}" ${PYTHON} setup.py \
build --build-base="${PYPI_BUILD_DIR}" \
sdist --dist-dir="${PYPI_DISTR_DIR}" \
bdist_wheel --dist-dir="${PYPI_DISTR_DIR}" \
egg_info --egg-base="${PYPI_BUILD_DIR}"
local BUILD_RESULT=$?
popd > /dev/null

if [ ${BUILD_RESULT} -ne 0 ] ; then
stderr_echo "PyPi package build failed with result ${BUILD_RESULT}!"
return 1
fi

return 0
}

# Print help message.
print_help()
{
cat << EOF
Description:
Builds Zserio wheel package into the distr directory.
Usage:
$0 [-h] [-e] [-c] [-p] [-o <dir>]
Arguments:
-h, --help Show this help.
-e, --help-env Show help for enviroment variables.
-c, --clean Clean build and distr directories.
-p, --purge Purge build and distr directories before build.
-o <dir>, --output-directory <dir>
Output directory where build and distr will be located.
EOF
}

# Parse all command line arguments.
#
# Return codes:
# -------------
# 0 - Success. Arguments have been successfully parsed.
# 1 - Failure. Some arguments are wrong or missing.
# 2 - Help switch is present. Arguments after help switch have not been checked.
parse_arguments()
{
local NUM_OF_ARGS=3
exit_if_argc_lt $# ${NUM_OF_ARGS}
local PARAM_OUT_DIR_OUT="$1"; shift
local SWITCH_CLEAN_OUT="$1"; shift
local SWITCH_PURGE_OUT="$1"; shift

eval ${SWITCH_PURGE_OUT}=0

local NUM_PARAMS=0
local PARAM_ARRAY=();
local ARG="$1"
while [ -n "${ARG}" ] ; do
case "${ARG}" in
"-h" | "--help")
return 2
;;

"-e" | "--help-env")
return 3
;;

"-c" | "--clean")
eval ${SWITCH_CLEAN_OUT}=1
shift
;;

"-p" | "--purge")
eval ${SWITCH_PURGE_OUT}=1
shift
;;

"-o" | "--output-directory")
eval ${PARAM_OUT_DIR_OUT}="$2"
shift 2
;;

"-"*)
stderr_echo "Invalid switch ${ARG}!"
echo
return 1
;;

*)
PARAM_ARRAY[NUM_PARAMS]=${ARG}
NUM_PARAMS=$((NUM_PARAMS + 1))
shift
;;
esac
ARG="$1"
done

local PARAM
for PARAM in "${PARAM_ARRAY[@]}" ; do
case "${PARAM}" in
*)
stderr_echo "Invalid argument ${PARAM}!"
echo
return 1
esac
done

return 0
}

main()
{
# get the project root, absolute path is necessary for python distutils
local PYPI_PROJECT_ROOT
convert_to_absolute_path "${SCRIPT_DIR}/.." PYPI_PROJECT_ROOT

# parse command line arguments
local PARAM_OUT_DIR="${PYPI_PROJECT_ROOT}"
local SWITCH_CLEAN
local SWITCH_PURGE
parse_arguments PARAM_OUT_DIR SWITCH_CLEAN SWITCH_PURGE $@
local PARSE_RESULT=$?
if [ ${PARSE_RESULT} -eq 2 ] ; then
print_help
return 0
elif [ ${PARSE_RESULT} -eq 3 ] ; then
print_global_help_env
return 0
elif [ ${PARSE_RESULT} -ne 0 ] ; then
return 1
fi

echo "Building of Zserio PyPi package."
echo

# set global variables
set_global_variables
if [ $? -ne 0 ] ; then
return 1
fi

# clean build and distr directories if requested
local PYPI_BUILD_DIR="${PARAM_OUT_DIR}/build"
local PYPI_DISTR_DIR="${PARAM_OUT_DIR}/distr"
if [[ ${SWITCH_PURGE} == 1 || ${SWITCH_CLEAN} == 1 ]] ; then
echo "Cleaning build and distr directories."
echo
rm -rf "${PYPI_BUILD_DIR}/"
rm -rf "${PYPI_DISTR_DIR}/"
fi

# continue only if cleaning was not requested
if [[ ${SWITCH_CLEAN} != 1 ]] ; then
mkdir -p "${PYPI_BUILD_DIR}"
mkdir -p "${PYPI_DISTR_DIR}"

# build PyPi package
build_pypi_package "${PYPI_PROJECT_ROOT}" "${PYPI_BUILD_DIR}" "${PYPI_DISTR_DIR}"
if [ $? -ne 0 ] ; then
return 1
fi
fi

return 0
}

# call main function
main "$@"
Loading

0 comments on commit 20aebf0

Please sign in to comment.