This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
/
__init__.py
executable file
·158 lines (130 loc) · 3.42 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
"""
This script takes a string (usually a URL) on the commandline and asks for a
passphrase, generating a password specific for the given string by combining
the two and returning a substring of the md5 of the result.
By default, the script does not print the generated password - only copies it
to the clipboard.
It records the the string in a ~/.spg file which can be used for
autocompletion.
The script takes the following options, given below in the short and long
formats:
-p --print prints the password instead of copying
-n --no-record does not record the string
Copyright (c) 2008, Devendra Gera
This program is free software and is released under the terms of the GPL
version 2.
"""
import os
import platform
import sys
import getopt
import getpass
import hashlib
from base64 import b64encode
def usage():
print __doc__
def record_to_file(url, filename):
"""
records the url to the given filename, if it does not already exist
in it.
"""
contents = []
if os.path.exists(filename):
contents = open(filename, "r").readlines()
s = set(contents)
s.add("%s\n" % url)
open(filename, "w").writelines(s)
def copy_to_clipboard(string, win32=False):
"""
copies the string to the clipboard
"""
if win32:
try:
import win32clipboard as wcb
wcb.OpenClipboard(0)
wcb.EmptyClipboard()
wcb.SetClipboardText(string)
wcb.CloseClipboard()
except:
print >> sys.stderr, "Can't copy to win32 clipboard"
sys.exit(2)
else:
try:
import gtk
cb = gtk.Clipboard()
cb.set_text(string)
cb.store()
except:
print >> sys.stderr, "Can't copy to gtk clipboard"
sys.exit(2)
def main():
argv = sys.argv[1:]
no_copy = False
record = True
win32 = (platform.system().lower() == "windows")
if win32:
key = 'USERPROFILE'
else:
key = 'HOME'
filename = os.path.join(os.environ[key], ".spg")
try:
opts, args = getopt.getopt(argv, "pn", ["print", "no-record"])
except getopt.GetoptError:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-p", "--print"):
no_copy = True
elif opt in ("-n", "--no-record"):
record = False
else:
usage()
sys.exit(2)
if args == []:
# this is primarily for the Win32 'click-on.exe' use case.
input = get_input_with_readline(filename)
args.append(input.strip())
url = args[0]
passphrase = getpass.getpass("Passphrase: ")
passwd = b64encode(hashlib.md5(passphrase + " " + url).digest())[:10]
if record:
record_to_file(url, filename)
if no_copy:
print passwd
return
copy_to_clipboard(passwd, win32)
return
def get_input_with_readline(filename):
"""
sets up readline support for entering sitenames, completed from the
existing list and accepts a line of input.
"""
if not os.path.exists(filename):
file(filename, "w").close()
all_lines = map(lambda x: x.strip(), file(filename).readlines())
def completer(text, state):
"""
custom readline completer
"""
candidates = filter(lambda x: x.startswith(text), all_lines)
if state <= len(candidates):
return candidates[state-1]
else:
return None
try:
import readline
readline.set_completer(completer)
readline.read_history_file(filename)
readline.parse_and_bind('tab: complete')
if hasattr(readline, 'readline'):
print "sitename: ",
readline._issued = True
return readline.readline(history=all_lines, histfile=None)
else:
return raw_input("sitename: ")
except:
# no readline?
return raw_input("sitename: ")
if __name__ == "__main__":
main()