Skip to content

Commit 3067ed0

Browse files
authored
travis (#4)
* travis * modify yml * py3 support
1 parent d6d7e1f commit 3067ed0

File tree

7 files changed

+178
-55
lines changed

7 files changed

+178
-55
lines changed

.gitignore

+51-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,54 @@
1-
*.log
2-
*.pot
3-
*.pyc
4-
*~
5-
*.swp
6-
*tmp
7-
*.ini
8-
*.pid
9-
*.txt
10-
*DS_Store
11-
settings.py
12-
rundev.sh
13-
*.csv
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
bin/
12+
build/
13+
develop-eggs/
14+
dist/
15+
eggs/
16+
lib/
17+
lib64/
18+
parts/
19+
sdist/
20+
var/
21+
*.egg-info/
22+
.installed.cfg
23+
*.egg
24+
25+
# Installer logs
26+
pip-log.txt
27+
pip-delete-this-directory.txt
28+
29+
# Unit test / coverage reports
30+
htmlcov/
31+
.tox/
32+
.coverage
33+
.cache
34+
nosetests.xml
35+
coverage.xml
36+
37+
# Translations
38+
*.mo
39+
40+
# Mr Developer
41+
.mr.developer.cfg
1442
.project
1543
.pydevproject
16-
.settings/*
44+
45+
# Rope
46+
.ropeproject
47+
48+
# Django stuff:
49+
*.log
50+
*.pot
51+
52+
# Sphinx documentation
53+
docs/_build/
1754
.idea/*
18-
tags
19-
cscope.*
20-
[sS]itemap.xml
21-
secure_settings.py
22-
/static/payment_flow_attachments/
23-
secure_settings.py

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# https://travis-ci.org/lovedboy/pwm
2+
language: python
3+
python:
4+
- 2.7.8
5+
- 2.7
6+
- 3.3
7+
- 3.4
8+
- 3.5
9+
- nightly
10+
11+
script:
12+
- python -m unittest discover
13+
- python setup.py install
14+
- cd travis
15+
- export PWM_DB_PATH="pwm.db"
16+
- /bin/sh setup.sh

pwm/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from pwm import *
1+
from .pwm import *

pwm/pwm.py

+33-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#!coding:utf-8
1+
# coding:utf-8
22

33
from hashlib import sha1
4+
from base64 import b64encode
45
import sys
56
import hmac
67
import re
@@ -11,7 +12,7 @@
1112

1213

1314
__author = 'lovedb0y'
14-
__version = '0.1'
15+
__version = '0.2'
1516
__package = 'pwm'
1617

1718

@@ -22,12 +23,15 @@ def __init__(self, key, db_path=None):
2223
self.passwd_length = 15
2324
self.db_path = db_path
2425
self.table = 'pwm'
25-
self._create_table()
2626

2727
def gen_passwd(self, raw):
2828

29-
h = hmac.new(self.key, raw, sha1)
30-
base64 = h.digest().encode("base64")
29+
if sys.version_info > (3, 0):
30+
h = hmac.new(self.key.encode(), raw.encode(), sha1)
31+
base64 = b64encode(h.digest()).decode()
32+
else:
33+
h = hmac.new(self.key, raw, sha1)
34+
base64 = h.digest().encode("base64")
3135
_passwd = base64[0: self.passwd_length]
3236
return self._format_passwd(_passwd)
3337

@@ -55,13 +59,12 @@ def _format_passwd(self, passwd):
5559

5660
def _get_conn(self):
5761
if self.db_path is None:
58-
print "You didn't set you PWD_DB_PATH ENV"
62+
print ("You didn't set you PWD_DB_PATH ENV")
5963
sys.exit(1)
6064
try:
6165
conn = sqlite3.connect(self.db_path)
6266
except sqlite3.OperationalError:
63-
print "PWD_DB_PATH: %s is invalid! "\
64-
"(Is it a directory or a file?)" % self.db_path
67+
print ("PWD_DB_PATH: {} is invalid! (Is it a directory or a file?)".format(self.db_path))
6568
sys.exit(1)
6669
return conn
6770

@@ -129,12 +132,14 @@ def _delete(self, id):
129132
sql = "delete from {} where id={}".format(self.table, id)
130133
with self:
131134
cur = self.conn.cursor()
132-
raw_count = cur.execute(sql)
133-
return raw_count
135+
cur.execute(sql)
136+
row_count = cur.rowcount
137+
return row_count
134138

135139
def insert(self, domain, account, batch):
140+
self._create_table()
136141
self._insert_account(domain, account, batch or '')
137-
print "save success"
142+
print ("save success")
138143

139144
@staticmethod
140145
def gen_sign_raw(domain, account, batch):
@@ -149,35 +154,36 @@ def gen_account_passwd(self, domain, account, batch):
149154
return self.gen_passwd(raw)
150155

151156
def delete(self, id):
157+
self._create_table()
152158
row_count = self._delete(id)
153-
print "remove success, %s record(s) removed" % (row_count)
159+
print ("remove success, {} record(s) removed".format(row_count, ))
154160

155161
def search(self, keyword):
156162

163+
self._create_table()
157164
if keyword == '*':
158165
keyword = ''
159166

160167
result = self._query_account(keyword)
161-
fmt = "%-5s|%-40s|%-35s|%-20s|%-5s"
162-
print fmt % ("ID", "DOMAIN", "ACCOUNT", "PASWORD", "BATCH")
163-
print fmt % ("-"*5, "-"*40, "-"*35, "-"*20, "-"*5)
168+
fmt = "{:5s}|{:40s}|{:35s}|{:20s}|{:5s}"
169+
print (fmt.format("ID", "DOMAIN", "ACCOUNT", "PASWORD", "BATCH"))
170+
print (fmt.format("-"*5, "-"*40, "-"*35, "-"*20, "-"*5))
164171
for item in result:
165-
print fmt % (item[0], item[1], item[2],
166-
self.gen_account_passwd(item[1], item[2], item[3]),
167-
item[3])
172+
passwd = self.gen_account_passwd(item[1], item[2], item[3])
173+
print (fmt.format(str(item[0]), item[1], item[2], passwd, item[3]))
168174

169-
print "\n{} records found.\n".format(len(result))
175+
print ("\n{} records found.\n".format(len(result)))
170176

171177

172178
def main():
173179

174180
db_path = os.getenv("PWM_DB_PATH", None)
175181
if db_path is None:
176-
print "##########WARNING:############"
177-
print "You didn't set you PWD_DB_PATH ENV"
178-
print "echo \"export PWM_DB_PATH=your_path\" >> ~/.bashrc"
179-
print "source ~/.bashrc"
180-
print "###############################"
182+
print ("##########WARNING:############")
183+
print ("You didn't set you PWD_DB_PATH ENV")
184+
print ("echo \"export PWM_DB_PATH=your_path\" >> ~/.bashrc")
185+
print ("source ~/.bashrc")
186+
print ("###############################")
181187
parse = OptionParser(version="{} {}".format(__package, __version))
182188

183189
parse.add_option('-k', '--key', help="your secret key", nargs=0)
@@ -194,7 +200,7 @@ def main():
194200
help="remove your account and domain by id", nargs=1, type=int)
195201
parse.add_option(
196202
'-b', '--batch',
197-
help="add batch to generate diffrent passwd with same domain and account", # noqa
203+
help="add batch to generate different passwd with same domain and account", # noqa
198204
nargs=1, type=int)
199205
(options, args) = parse.parse_args()
200206

@@ -220,9 +226,9 @@ def main():
220226
parse.print_help()
221227
return
222228

223-
print "passwd is :\n{}".format(
229+
print ("passwd is :{}".format(
224230
pwm.gen_account_passwd(
225-
options.domain, options.account, options.batch))
231+
options.domain, options.account, options.batch)))
226232

227233
# 保存
228234
if options.save is not None:

pwm/test_pwm.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# coding:utf-8
2+
3+
from unittest import TestCase
4+
from pwm import PWM
5+
import re
6+
import os
7+
8+
9+
class TestPwm(TestCase):
10+
11+
def setUp(self):
12+
self.db_path = os.path.join(os.path.dirname(__file__), "tmp.dat")
13+
self.p = PWM(key='123456', db_path= self.db_path)
14+
15+
def tearDown(self):
16+
if os.path.exists(self.db_path):
17+
os.unlink(os.path.join(os.path.dirname(__file__), "tmp.dat"))
18+
19+
def test_gen_passwd(self):
20+
21+
passwd = self.p.gen_passwd('1234')
22+
self.assertTrue(re.search(r"[0-9]", passwd) is not None)
23+
self.assertTrue(re.search(r"[a-z]", passwd) is not None)
24+
self.assertTrue(re.search(r"[A-Z]", passwd) is not None)
25+
26+
def test_insert(self):
27+
self.p.insert(domain='github.com', account='lovedboy', batch='')
28+
res = self.p._query_account('github.com')
29+
self.assertEqual(res, [(1, 'github.com', 'lovedboy', '')])
30+
31+
def test_search(self):
32+
self.p.insert(domain='github.com', account='lovedboy', batch='')
33+
self.p.search('*')
34+
35+
def test_delete(self):
36+
self.p.insert(domain='github.com', account='lovedboy', batch='')
37+
res = self.p._query_account('github.com')
38+
self.assertEqual(res, [(1, 'github.com', 'lovedboy', '')])
39+
self.p.delete(1)
40+
res = self.p._query_account('github.com')
41+
self.assertEqual(res, [])
42+
43+
def test_gen_account_password(self):
44+
45+
p = self.p.gen_account_passwd('github.com', 'lovedboy', '')
46+
self.assertEqual(p, 'lcrv5vttjqOk7c3')
47+
48+

setup.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#coding:utf-8
2-
from setuptools import setup,find_packages
1+
# coding:utf-8
2+
3+
from setuptools import setup, find_packages
34

45
PACKAGE = "pwm-tool"
56
NAME = "pwm-tool"
6-
DESCRIPTION = "passwd manager tool"
7+
DESCRIPTION = "password manager tool"
78
AUTHOR = "lovedboy"
89
AUTHOR_EMAIL = "[email protected]"
910
URL = "https://github.com/lovedboy/pwm"
10-
VERSION = '0.1.1'
11+
VERSION = '0.2'
1112

1213
setup(
1314
name=NAME,
@@ -17,10 +18,10 @@
1718
author_email=AUTHOR_EMAIL,
1819
license="BSD",
1920
url=URL,
20-
include_package_data = True,
21+
include_package_data=True,
2122
scripts=['bin/pwm'],
22-
packages = find_packages(),
23-
classifiers = [
23+
packages=find_packages(),
24+
classifiers=[
2425
'License :: OSI Approved :: MIT License',
2526
'Programming Language :: Python',
2627
'Intended Audience :: Developers',

travis/setup.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -e;
4+
5+
# check pwm exists;
6+
pwm;
7+
8+
# save;
9+
10+
pwm -d github.com -a lovedboy -w;
11+
12+
# search;
13+
14+
pwm -s github | grep github.com;
15+
pwm -s github | grep LNXStZoEGuHi9rb;
16+
pwm -s github | grep "1 records";
17+
18+
# delete;
19+
20+
pwm -r 1 | grep '1 record(s) removed';
21+
pwm -s github | grep "0 records";

0 commit comments

Comments
 (0)