Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Snawoot committed Sep 29, 2018
0 parents commit 40d721c
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .gitignore
@@ -0,0 +1,104 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Vladislav Yarmak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions Makefile
@@ -0,0 +1,18 @@
PYTHON = python3
RM = rm

PRJ_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
VENV ?= $(PRJ_DIR)venv

install: $(VENV) setup.py
$(VENV)/bin/pip install -U .

$(VENV):
$(PYTHON) -m venv $(VENV)

uninstall: $(VENV)
$(VENV)/bin/pip uninstall -y clitoolboilerplate

clean:
$(RM) -rf $(VENV)

71 changes: 71 additions & 0 deletions README.md
@@ -0,0 +1,71 @@
python-cli-tool-boilerplate
===========================

Boilerplate of python3 package which provides command line tool capable to:

* be installed in venv
* be installed using `setup.py`
* be runned directly from source directory if environment dependencies already satisfied

In order to convert this boilerplate to real project you have to:

1. Edit setup.py: version, name, dependencies
2. Rename `clitoolboilerplate` module directory
3. Adjust imports
4. (Optional) Add dependencies to `requirements.txt`


## Installation

### Method 1. System-wide install

Run in project directory:

```bash
python3 -m pip install .
```

Package scripts shall be available in standard executable locations upon completion.

### Method 2. Running from project directory

Installing dependencies:


```bash
python3 -m pip install -r requirements.txt
```

Now script can be run right from source directory.

#### Пользовательская установка pip

Both previous methods can be run with `--user` option of `pip` installer. In this case superuser privileges are not required and package shall be installed to user home directory. So, for first method script executabled will appear in `~/.local/bin`.

### Method 3. Install into virtualenv

See "Building virtualenv"


## Building virtualenv

Run `make` in project directory in order to build virtualenv. As result of it, new directory `venv` shall appear. `venv` contains interpreter and all required dependencies, i.e. encloses package with depencencies in separate environment. It is possible to specify alternative path where virtualenv directory shall be placed. Specify VENV variable for `make` command. Example:

```bash
make VENV=~/myapp-venv
```

Such virtual environment can be moved to another machine of similar type (as far python interpreter is compatible with new environment). If virtualenv is placed into same location on new machine, application can be runned this way:

```bash
venv/bin/cli-tool
```

Otherwise, some hacks required. First option - explicitly call virtualenv interpreter:

```bash
venv/bin/python venv/bin/cli-tool
```

Second option - specify new path in shebang of scripts installed in virtualenv. It is recommended to build virtualenv at same location which app shall occupy on target system.

28 changes: 28 additions & 0 deletions cli-tool
@@ -0,0 +1,28 @@
#!/usr/bin/env python3

import sys
import argparse
from clitoolboilerplate.utils import *
from clitoolboilerplate.enums import *


def parse_args():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("-v", "--verbosity",
help="logging verbosity",
type=LogLevel.__getitem__,
choices=list(LogLevel),
default=LogLevel.warn)

return parser.parse_args()


def main():
args = parse_args()
mainLogger = setup_logger('MAIN', args.verbosity)
mainLogger.info("Hello World!")


if __name__ == '__main__':
main()
Empty file added clitoolboilerplate/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions clitoolboilerplate/enums.py
@@ -0,0 +1,29 @@
import enum
import logging


@enum.unique
class Protocol(enum.Enum):
tcp = 1
udp = 2

def __str__(self):
return self.name

def __contains__(self, e):
return e in self.__members__


class LogLevel(enum.IntEnum):
debug = logging.DEBUG
info = logging.INFO
warn = logging.WARN
error = logging.ERROR
fatal = logging.FATAL
crit = logging.CRITICAL

def __str__(self):
return self.name

def __contains__(self, e):
return e in self.__members__
40 changes: 40 additions & 0 deletions clitoolboilerplate/utils.py
@@ -0,0 +1,40 @@
import logging


def setup_logger(name, verbosity):
logger = logging.getLogger(name)
logger.setLevel(verbosity)
handler = logging.StreamHandler()
handler.setLevel(verbosity)
handler.setFormatter(logging.Formatter('%(asctime)s '
'%(levelname)-8s '
'%(name)s: %(message)s',
'%Y-%m-%d %H:%M:%S'))
logger.addHandler(handler)
return logger


def check_port(value):
ivalue = int(value)
if not (0 < ivalue < 65536):
raise argparse.ArgumentTypeError(
"%s is not a valid port number" % value)
return ivalue


def check_positive_float(value):
fvalue = float(value)
if fvalue <= 0:
raise argparse.ArgumentTypeError(
"%s is not a valid value" % value)
return fvalue


def enable_uvloop():
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
except:
return False
else:
return True
Empty file added requirements.txt
Empty file.
20 changes: 20 additions & 0 deletions setup.py
@@ -0,0 +1,20 @@
from setuptools import setup

setup(name='postfix_mta_sts_resolver',
version='0.1',
description='Daemon which provides TLS client policy for Postfix via socketmap, according to domain MTA-STS policy',
url='https://github.com/Snawoot/postfix-mta-sts-resolver',
author='Vladislav Yarmak',
author_email='vladislav-ex-src@vm-0.com',
license='MIT',
packages=['postfix_mta_sts_resolver'],
setup_requires=[
'wheel',
],
install_requires=[
],
scripts=[
'mta-sts-daemon',
'mta-sts-query',
],
zip_safe=True)

0 comments on commit 40d721c

Please sign in to comment.