forked from giswqs/geemap-heroku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_vars.py
37 lines (29 loc) · 1.26 KB
/
config_vars.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
import os
import platform
import json
from subprocess import DEVNULL, STDOUT, check_call
def set_heroku_vars(token_name='EARTHENGINE_TOKEN'):
"""Extracts Earth Engine token from the local computer and sets it as an environment variable on heroku.
Args:
token_name (str, optional): Name of the Earth Engine token. Defaults to 'EARTHENGINE_TOKEN'.
"""
try:
ee_token_dir = os.path.expanduser("~/.config/earthengine/")
ee_token_file = os.path.join(ee_token_dir, 'credentials')
if not os.path.exists(ee_token_file):
print('The credentials file does not exist.')
else:
with open(ee_token_file) as f:
content = f.read()
content_json = json.loads(content)
token = content_json["refresh_token"]
secret = '{}={}'.format(token_name, token)
if platform.system() == 'Windows':
check_call(['heroku', 'config:set', secret], stdout=DEVNULL, stderr=STDOUT, shell=True)
else:
check_call(['heroku', 'config:set', secret], stdout=DEVNULL, stderr=STDOUT)
except Exception as e:
print(e)
return
if __name__ == '__main__':
set_heroku_vars(token_name='EARTHENGINE_TOKEN')