Skip to content

Commit

Permalink
Merge pull request #2 from pradeep-dot-jaiswar/main
Browse files Browse the repository at this point in the history
Github workflow
  • Loading branch information
Pradeep-Jaiswar-ML authored Sep 20, 2021
2 parents f0e9b4f + 0af72ac commit 643d24f
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 65 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/pypi-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Publish Python 🐍 distributions 📦 to PyPI

# on: push
on:
release:
types:
- created
workflow_dispatch:

jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to PyPI
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@master
- name: Set up Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install pypa/build
run: >-
python -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
.
- name: Publish distribution 📦 to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.PYPI_API_TOKEN }}
38 changes: 38 additions & 0 deletions .github/workflows/test-pypi-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Publish Python 🐍 distributions 📦 to TestPyPI

# on: push
on:
release:
types:
- created
workflow_dispatch:

jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions 📦 to TestPyPI
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@master
- name: Set up Python 3.8
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install pypa/build
run: >-
python -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: >-
python -m
build
--sdist
--wheel
--outdir dist/
.
- name: Publish distribution 📦 to Test PyPI
uses: pypa/gh-action-pypi-publish@master
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
22 changes: 22 additions & 0 deletions PUBLISH.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Publish sf-apm-lib pip package
==============
[![made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) [![pypi-package](https://img.shields.io/badge/pypi-green)](https://pypi.org/project/sf-apm-lib/)


Steps
--------------


- Open [SF-APM-LIB Github Repo](https://github.com/snappyflow/py_snappyflow)

- Go to [Actions](https://github.com/snappyflow/py_snappyflow/actions) tab and select <b>Build and publish Python 🐍 distributions 📦 to PyPI</b> or <b>Build and publish Python 🐍 distributions 📦 to TestPyPI</b> action and click on <i>Run workflow</i>

- Select the desired branch e.g <i>main</i> or <i>master</i> branch and finally click on <b> Run workflow</b> button.<br>
Above process will start building and then publishing your package.

- Once build result of workflow is successful, Check published version of pip package in pypi or test.pypi repo based on selected action.


<br>
Note: Please make sure package version is updated to newer version before publishing.
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
py\_snappyflow
sf-apm-lib
==============
[![made-with-python](https://img.shields.io/badge/Made%20with-Python-1f425f.svg)](https://www.python.org/) [![pypi-package](https://img.shields.io/badge/pypi-green)](https://pypi.org/project/sf-apm-lib/)

Snappyflow modules

Installation
------------
pip install py_snappyflow
pip install sf-apm-lib

Then you should import the library.

```python
from sf_apm_lib.snappyflow import Snappyflow
```

Usage
-----
* Get trace config

```
from py_snappyflow import snappyflow as sf
trace_config = sf.get_trace_config(<Snappyflow Profile Key>)
```
Get trace config

```python
sf = Snappyflow() # Initialize Snappyflow. By default intialization will pick profileKey, projectName and appName from sfagent config.yaml.

# Add below part to manually configure the initialization
project_name = '<Snappyflow Project Name>'
app_name = '<Snappyflow App Name>'
profile_key = '<Snappyflow Profile Key>'

sf.init(profile_key, project_name, app_name)
# End of manual configuration

trace_config = sf.get_trace_config() # Returns trace config

print(trace_config)
```
21 changes: 0 additions & 21 deletions README.rst

This file was deleted.

1 change: 0 additions & 1 deletion py_snappyflow-0.1.1.dist-info/dependency_links.txt

This file was deleted.

1 change: 0 additions & 1 deletion py_snappyflow-0.1.1.dist-info/top_level.txt

This file was deleted.

29 changes: 0 additions & 29 deletions py_snappyflow/snappyflow.py

This file was deleted.

10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ def read(filename):


setup(
name="py_snappyflow",
version="0.1.1",
name="sf_apm_lib",
version="0.1.2",
url="https://github.com/snappyflow/py_snappyflow",
license='MIT',

author="Maplelabs",
author_email="[email protected]",

description="Snappyflow modules",
description="Snappyflow pip package",
long_description=read("README.md"),
long_description_content_type='text/markdown',

packages=find_packages(exclude=('tests',)),

install_requires=['cryptography', 'pycryptodome'],
install_requires=['cryptography==3.4.7', 'pycryptodome==3.10.1', 'cffi==1.14.6', 'PyYAML==5.4.1'],

classifiers=[
'Development Status :: 2 - Pre-Alpha',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
Expand Down
2 changes: 1 addition & 1 deletion py_snappyflow/__init__.py → sf_apm_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""py_snappyflow - Snappyflow modules"""
"""sf_apm_lib - Snappyflow pip package"""

__version__ = '0.1.0'
__author__ = 'Maplelabs <[email protected]>'
Expand Down
81 changes: 81 additions & 0 deletions sf_apm_lib/snappyflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from hashlib import sha256
import base64
from Crypto import Random
from Crypto.Cipher import AES
import json
import platform
import yaml
from yaml.loader import SafeLoader


class Snappyflow:
def __init__(self):
self.__CP_ENCRYPTED_KEY = "U25hcHB5RmxvdzEyMzQ1Ng=="
self.project_name = None
self.app_name = None
self.profile_data = None
path = ''
os_type = platform.system().lower()
if os_type == 'windows' or os_type == 'win32':
path = 'C:\\Program Files (x86)\\Sfagent\\config.yaml'
else:
path = '/opt/sfagent/config.yaml'
# with open(path) as file:
try:
yaml_file = open(path)
data = yaml.load(yaml_file, Loader=SafeLoader)
# self.profile_key =
self.project_name = data['tags']['projectName']
self.app_name = data['tags']['appName']
self.profile_data = self._get_profile_data(data['key'])
except Exception as ex:
print('Can not read from config.yaml\nCall init method with parameters to initialize.')

def init(self, profile_key, project_name, app_name):
self.profile_data = self._get_profile_data(profile_key)
self.project_name = project_name
self.app_name = app_name

def _get_profile_data(self, profile_key):
unpad = lambda s : s[0:-ord(s[-1:])]

key = base64.b64decode(self.__CP_ENCRYPTED_KEY)

enc = base64.b64decode(profile_key)
iv = enc[:16]
cipher = AES.new(key, AES.MODE_CBC, iv )
message = unpad(cipher.decrypt( enc[16:] )).decode('utf-8')
return json.loads(message)

def get_trace_config(self):
""" Returns Snappyflow trace config
Args:
profile_key ([string]): [Snappyflow Profile Key]
project_name ([string]): [Project name]
app_name ([string]): [App name]
Raises:
ValueError: [If profile key is invalid]
Returns:
[dict]: [trace config]
"""


try:
data = self.profile_data
global_labels = "_tag_projectName={},_tag_appName={},_tag_profileId={}".format(
self.project_name, self.app_name, data['profile_id'])
trace_data = {
'SFTRACE_SERVER_URL': data['trace_server_url'],
'SFTRACE_SPAN_FRAMES_MIN_DURATION': "1ms",
'SFTRACE_STACK_TRACE_LIMIT': 2,
'SFTRACE_CAPTURE_SPAN_STACK_TRACES': False,
'SFTRACE_VERIFY_SERVER_CERT': False,
'SFTRACE_GLOBAL_LABELS': global_labels
}
return trace_data
except Exception as e:
# print("ex", e)
raise ValueError("\nPlease check config.yaml file is present. Or \nInit method is called with appropriate values.")
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
envlist = py27,py34,py35,py36,py37

[testenv]
commands = py.test py_snappyflow
commands = py.test sf_apm_lib
deps = pytest

0 comments on commit 643d24f

Please sign in to comment.