-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
171 lines (156 loc) · 5.16 KB
/
setup.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import sys
import textwrap
import select
import platform
from setuptools import setup, find_packages
from setuptools.command.install import install as SetupToolsInstaller
import gettext
t = gettext.translation(
'yubikey_totp_gui',
os.path.join(os.path.dirname(__file__), 'lang'),
fallback=True
)
_ = t.ugettext
def timed_raw_input(timeout, prompt=""):
sys.stderr.write(prompt + '\n') # pip seems to linebuffer the output somehow
sys.stderr.flush()
import time
time.sleep(1)
ready_fds = select.select([sys.stdin], [], [], timeout)
if not len(ready_fds[0]):
raise IOError("timeout waiting for user input")
return sys.stdin.readline().strip('\n')
def write_files(os_rules):
"""
For systems that use individual configuration files, use the 'files' permissions target
and write the content of each file in the list into the first directory that
exists.
If the files already exist, we leave them alone.
If none of the directories exist, we fail loudly.
"""
dirs = os_rules['try_dirs']
files = os_rules['files']
for directory in dirs:
try:
os.stat(directory)
break
except OSError:
continue
else:
raise ValueError(_("None of the expected installation directories were found"))
for name in files:
filepath = os.path.join(directory, name)
try:
os.stat(filepath)
sys.stderr.write(
_("'%(filepath)s' already exists. Skipping\n") % {'filepath': filepath}
)
continue
except OSError:
# OSError means the file doesn't yet exist.
pass
try:
with open(filepath, 'w') as f:
f.write(files[name])
f.flush()
except IOError:
# need to retry with `sudo`
import subprocess
ps = subprocess.Popen(
['sudo', 'tee', filepath],
stdin=subprocess.PIPE,
)
ps.stdin.write(files[name])
ps.stdin.close()
ps.wait()
USB_RULES = {
'Linux': {
'action': write_files,
'try_dirs':['/etc/udev/rules.d', '/lib/udev/rules.d'],
'files': {
'69-yubikey.rules': textwrap.dedent(
"""
ACTION!="add|change", GOTO="yubico_end"
ATTRS{idVendor}=="1050",
ATTRS{idProduct}=="0010|0110|0111|0114|0116|0401|0403|0405|0407|0410", \\
ENV{ID_SECURITY_TOKEN}="1"
LABEL="yubico_end"
"""),
'70-yubikey.rules': textwrap.dedent(
"""
ACTION=="add|change", SUBSYSTEM=="usb", \\
ATTRS{idVendor}=="1050", \\
ATTRS{idProduct}=="0010|0110|0111|0114|0116|0401|0403|0405|0407|0410", \\
TAG+="uaccess", TAG+="udev-acl"
"""),
},
},
#TODO
# 'openbsd': {
# 'try_dirs': [],
# 'files': {
# }
# },
# 'freebsd': {
# 'action': append_conf,
# 'try_dirs': ['devd.conf'],
# 'blocks': [textwrap.dedent("""
# attach 100 {
# match "vendor" "1050";
# match "product" "0010|0110|0111|0114|0116|0401|0403|0405|0407|0410"
# action
# """)],
#};
# }
# },
}
class Installer(SetupToolsInstaller):
"""
Override the standard setuptools installer to do some permissions-related
extra business before exit.
"""
def run(self):
SetupToolsInstaller.run(self)
if os.isatty(sys.stdin.fileno()):
try:
if timed_raw_input(30, _(
"Do you want to add the necessary USB permissions (as root)?\n"
"Will skip automatically in %(timeout)ss:[N/y]" % {'timeout': 30}
)) in _('Yy'):
os_name = platform.system()
try:
func = USB_RULES[os_name]['action']
except KeyError:
sys.stderr.write("Your OS isn't yet supported. Please file a bug report")
sys.exit(False)
func(USB_RULES[os_name])
except IOError:
# timeout
print _("skipping permissions installation...")
setup(
cmdclass = {'install' : Installer},
package_dir={'yubikey_totp_gui': './src'},
packages=['yubikey_totp_gui'],
name='yubikey-totp-gui',
version='0.3',
author_email='[email protected]',
url='https://github.com/ldrumm/yubikey-totp-gui',
author='Luke Drummond',
long_description=open(os.path.join(os.path.dirname(__file__), 'README.rst')).read(),
license = "2 clause BSD",
classifiers=[
"Development Status :: 3 - Alpha",
"Topic :: Utilities",
"Topic :: Security",
"Intended Audience :: End Users/Desktop",
"Operating System :: POSIX",
"License :: OSI Approved :: BSD License",
],
install_requires=['python-yubico', 'pyusb==1.0.0b2'],
entry_points = {
'console_scripts': [
'yubikey-totp-gui = yubikey_totp_gui:main'
]
},
)