Skip to content

Commit

Permalink
📝 update setup.py and split the code to two file to simplify developm…
Browse files Browse the repository at this point in the history
…ent process
  • Loading branch information
alivx committed Jan 5, 2021
1 parent b579de1 commit 079b75f
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 93 deletions.
10 changes: 5 additions & 5 deletions Generator/SampleDataInserter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ for i in $(seq 1 "${numberOfInsert}"); do
redis-cli set "${key1}_${i}_json_ran" "$key4"
redis-cli set "${key1}_${i}_image_ran" $key5

redis-cli hset "${key2}_${i}_uuid_ran" ${i} "$key1"
redis-cli hset "${key2}_${i}_date_ran" ${i} "$key2"
redis-cli hset "${key2}_${i}_date2_ran" ${i} "$key3"
redis-cli hset "${key2}_${i}_json_ran" $i "$key4"
redis-cli hset "${key2}_${i}_image_ran" $i $key5
redis-cli hset ${key2}_${i}_uuid_ran ${i} "$key1"
redis-cli hset ${key2}_${i}_date_ran ${i} "$key2"
redis-cli hset ${key2}_${i}_date2_ran ${i} "$key3"
redis-cli hset ${key2}_${i}_json_ran $i "$key4"
redis-cli hset ${key2}_${i}_image_ran $i $key5
sleep "$insertSleepTime"
echo "$key1 $key2 $key3"
done
5 changes: 2 additions & 3 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
recursive-include *.py
include setup.cfg
include README.md CHANGELOG.md LICENSE.md
include *.txt
recursive-include redismirror/templates *
include README.md LICENSE
include *.md
Empty file added redismirror/__init__.py
Empty file.
Empty file added redismirror/helper.py
Empty file.
72 changes: 72 additions & 0 deletions redismirror/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import click
import sys
import redis
import re
from redisCommand import *
# TO DO:
# 1. Add timer to mirror process
# 2. Add auth option
# 3. Support to split it to multi node
# 4. Support all redis command
# 5. Add number of item to mirror limit


@click.command()
@click.option("--host", default="127.0.0.1", help="Destination redis host/IP.")
@click.option("--port", default=6379, help="Destination redis port.")
@click.option("--db", default="0", required=False, help="Destination redis DB.")
@click.option("--auth", required=False, help="Destination redis auth info.")
def main(host, port, db, auth):
r = makeConnection(host, port, db, auth)
getSTDOUT(r)


def makeConnection(host, port, db, auth):
"""Function to create redis connection
Note:
If the connection failed, the program will exit 1
Args:
host (str): redis connection string
port (int): redis port number
db (int): redis database name
auth (str): auth info for redis
Returns:
connection: connection object for redis
"""

pool = redis.ConnectionPool(host=host, port=port, db=db)
r = redis.StrictRedis(connection_pool=pool)
try:
r.ping()
print("Redis is connected..")
except Exception as e:
print(f"Redis connection error ({e})")
sys.exit(1)
return r


def getSTDOUT(connection):
if not sys.stdin.isatty():
input_stream = sys.stdin
else:
print("There is no stdin, check help for more info. exit 1")
sys.exit(1)
for line in input_stream:
try:
tmpLine = line.split(" ", 5)
keyType = str(tmpLine[3]).strip('"')
if keyType == "set":
setFunc(line, connection)
elif keyType == "hset":
hsetFunc(line, connection)
else:
print(f"key type ({keyType}) if not supported yet.")
except:
print("Skip line | {0} ....".format(line[:100]))


if __name__ == "__main__":
main()
22 changes: 22 additions & 0 deletions redismirror/redisCommand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

def setFunc(rLine, connection):
line = rLine.split(" ", 5)
keyName = line[4].strip('"')
keyValue = line[5].strip('"')
try:
connection.set(keyName, keyValue)
print(f"Key mirroed sucessfully | {keyName}")
except Exception as e:
print(f"Failed to set key | {keyName} | {e}")


def hsetFunc(rLine, connection):
line = rLine.split(" ", 6)
keyName = line[4].strip('"')
hkeyName = line[5].strip('"')
keyValue = str(line[6].strip('"')).lstrip('"\n')
try:
connection.hset(keyName, hkeyName, keyValue)
print(f"Key mirroed sucessfully | {keyName}")
except Exception as e:
print(f"Failed to set key | {keyName} | {hkeyName} | {e} ")
83 changes: 0 additions & 83 deletions run.py

This file was deleted.

2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from setuptools import setup, find_packages

VERSION = get_version()

f = open("README.md", "r")
LONG_DESCRIPTION = f.read()
Expand All @@ -27,8 +26,9 @@
packages=find_packages(exclude=["ez_setup", "tests*"]),
package_data={"redismirror": ["templates/*"]},
include_package_data=True,
keywords=["traffic", "mirror", "redis", "migration", "cli"],
entry_points="""
[console_scripts]
redismirror = run:main
redismirror = redismirror.main:main
""",
)

0 comments on commit 079b75f

Please sign in to comment.