forked from scrapinghub/shub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
49 lines (37 loc) · 1.07 KB
/
Copy pathauth.py
File metadata and controls
49 lines (37 loc) · 1.07 KB
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
import os
import netrc
from os.path import expanduser
from click import ClickException
OS_WIN = True if os.name == 'nt' else False
NETRC_FILE = expanduser('~/_netrc') if OS_WIN else expanduser('~/.netrc')
def find_api_key():
"""
Raises:
ClickException: if no credentials are found
"""
key = get_key_netrc()
if not key:
key = os.getenv("SHUB_APIKEY")
if not key:
err = 'Not logged in. Please login first with: shub login'
raise ClickException(err)
return key
def get_key_netrc():
"""Gets the key from the netrc file"""
try:
info = netrc.netrc(NETRC_FILE)
except IOError:
return
try:
key, account, password = info.authenticators("scrapinghub.com")
except TypeError:
return
if key:
return key
def write_key_netrc(key):
descriptor = os.open(
NETRC_FILE,
os.O_CREAT | os.O_RDWR | os.O_APPEND, 0o600)
with os.fdopen(descriptor, 'a+') as out:
line = 'machine scrapinghub.com login {0} password ""\n'.format(key)
out.write(line)