-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirenv.py
199 lines (157 loc) · 5.76 KB
/
direnv.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import concurrent.futures
import hashlib
import json
import os
import re
import shutil
import subprocess
import sublime
import sublime_plugin
from .progressbar import progressbar
ANSI_ESCAPE_RE = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
def get_output(cmd, cwd, env=None):
try:
process = subprocess.Popen(
cmd,
cwd=cwd,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except OSError as e:
return (
e.errno,
None,
"Executing %s has failed: %s" % (cmd, e)
)
else:
return (
process.wait(),
process.stdout.read().decode(),
ANSI_ESCAPE_RE.sub('', process.stderr.read().decode())
)
class DirenvCache(object):
def __init__(self, cache_path):
self._cache = {}
self._cache_path = cache_path
def _get_cache_file_path(self, file_path):
name = '%s-%s.cache' % (
hashlib.md5(file_path.encode()).hexdigest(),
os.path.basename(file_path))
return os.path.join(self._cache_path, name)
def get(self, file_path):
path = self._get_cache_file_path(file_path)
if path not in self._cache:
if os.path.isfile(path):
with open(path, 'r') as f:
self._cache[path] = json.load(f)
return {
k: v
for k, v in self._cache.get(path, {}).items()
if v is not None}
def set(self, file_path, value):
os.makedirs(self._cache_path, exist_ok=True)
path = self._get_cache_file_path(file_path)
with open(path, 'w') as f:
json.dump(value, f)
self._cache[path] = value
def clear(self):
shutil.rmtree(self._cache_path, ignore_errors=True)
self._cache.clear()
class Direnv(object):
def __init__(self, cache):
self._cache = cache
self._executor = concurrent.futures.ThreadPoolExecutor(max_workers=1)
self._current_path = None
self._previous_env = {}
@staticmethod
def _find_envrc_directory(file_name):
while file_name and file_name != '/':
envrc_path = os.path.join(file_name, '.envrc')
if os.path.isfile(envrc_path):
return file_name
file_name = os.path.dirname(file_name)
def _update_environment(self, file_path):
def rollback_env():
self._previous_env, previous_env = {}, self._previous_env
for key, value in previous_env.items():
if value is None:
os.environ.pop(key)
else:
os.environ[key] = value
direnv_path = self._find_envrc_directory(file_path)
direnv_path_prev = self._current_path
self._current_path = direnv_path
if direnv_path is None:
if direnv_path_prev is not None:
rollback_env()
sublime.status_message(
"direnv: unloaded %s" % direnv_path_prev)
return
environment = self._cache.get(direnv_path)
with progressbar(lambda tick: sublime.status_message(
"direnv: loading %s %s" % (direnv_path, tick))):
returncode, stdout, stderr = get_output(
['direnv', 'export', 'json'],
direnv_path,
dict(os.environ, **environment))
if returncode != 0:
self._current_path = None
rollback_env()
sublime.status_message(stderr)
return
if stdout:
environment = dict(environment, **json.loads(stdout))
self._cache.set(direnv_path, environment)
rollback_env()
for key, value in environment.items():
if key.startswith('DIRENV_') or value is None:
continue
prev = os.environ.get(key)
if prev != value:
self._previous_env[key] = prev
os.environ[key] = value
if stdout or direnv_path != direnv_path_prev:
sublime.status_message("direnv: loaded %s" % direnv_path)
def push(self, file_path):
if shutil.which('direnv') is not None:
future = self._executor.submit(self._update_environment, file_path)
future.add_done_callback(lambda f: f.result())
direnv_cache = DirenvCache(os.path.join(sublime.cache_path(), 'Direnv'))
direnv = Direnv(direnv_cache)
class DirenvEventListener(sublime_plugin.ViewEventListener):
def on_load(self):
direnv.push(self.view.file_name())
def on_activated(self):
direnv.push(self.view.file_name())
def on_post_save(self):
direnv.push(self.view.file_name())
class DirenvAllow(sublime_plugin.TextCommand):
def run(self, edit):
returncode, stdout, stderr = get_output(
['direnv', 'allow'],
os.path.dirname(self.view.file_name()))
if returncode != 0:
sublime.status_message(stderr)
else:
direnv.push(self.view.file_name())
class DirenvDeny(sublime_plugin.TextCommand):
def run(self, edit):
returncode, stdout, stderr = get_output(
['direnv', 'deny'],
os.path.dirname(self.view.file_name()))
if returncode != 0:
sublime.status_message(stderr)
else:
direnv.push(self.view.file_name())
class DirenvClear(sublime_plugin.TextCommand):
def run(self, edit):
direnv_cache.clear()
direnv.push(self.view.file_name())
def plugin_loaded():
if shutil.which('direnv') is None:
sublime.status_message(
"direnv: No direnv executable found, "
"follow https://direnv.net for installation instructions")
return
def plugin_unloaded():
direnv.push(None)