-
Notifications
You must be signed in to change notification settings - Fork 2
/
executepscommand.py
235 lines (197 loc) · 9.03 KB
/
executepscommand.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from __future__ import with_statement
import os.path
import subprocess
import codecs
import ctypes
import tempfile
import functools
from xml.etree.ElementTree import ElementTree
import base64
import sublime, sublime_plugin
import sublimepath
from sublime_lib.view import append
# The PoSh pipeline provided by the user and the input values (regions)
# are merged with this template.
PoSh_SCRIPT_TEMPLATE = u"""
function collectData { "<out><![CDATA[$([string]::join('`n', $input))]]></out>`n" }
$script:pathToOutPutFile ="%s"
"<outputs>" | out-file $pathToOutPutFile -encoding utf8 -force
$script:regionTexts = %s
$script:regionTexts | foreach-object {
%s | out-string | collectData | out-file `
-filepath $pathToOutPutFile `
-append `
-encoding utf8
}
"</outputs>" | out-file $pathToOutPutFile -encoding utf8 -append -force
"""
THIS_PACKAGE_NAME = "PowershellUtils"
THIS_PACKAGE_DEV_NAME = "XXX" + THIS_PACKAGE_NAME
POSH_SCRIPT_FILE_NAME = "psbuff.ps1"
POSH_HISTORY_DB_NAME = "pshist.txt"
OUTPUT_SINK_NAME = "out.xml"
DEBUG = os.path.exists(sublime.packages_path() + "/" + THIS_PACKAGE_DEV_NAME)
class CantAccessScriptFileError(Exception):
pass
def regions_to_posh_array(view, rgs):
"""
Return a PoSh array: 'x', 'y', 'z' ... and escape single quotes like
this : 'escaped ''sinqle quoted text'''
"""
return ",".join("'%s'" % view.substr(r).replace("'", "''") for r in rgs)
def get_outputs():
tree = ElementTree()
tree.parse(get_path_to_output_sink())
return [el.text[:-1] for el in tree.findall("out")]
def get_this_package_name():
"""
Name varies depending on the name of the folder containing this code.
TODO: Is __name__ accurate in Sublime? __file__ doesn't seem to be.
"""
return THIS_PACKAGE_NAME if not DEBUG else THIS_PACKAGE_DEV_NAME
def get_path_to_posh_script():
return sublimepath.rootAtPackagesDir(get_this_package_name(), POSH_SCRIPT_FILE_NAME)
def get_path_to_posh_history_db():
return sublimepath.rootAtPackagesDir(get_this_package_name(), POSH_HISTORY_DB_NAME)
def get_path_to_output_sink():
return sublimepath.rootAtPackagesDir(get_this_package_name(), OUTPUT_SINK_NAME)
def get_posh_saved_history():
# If the command history file doesn't exist now, it will be created when
# the user chooses to persist the current history for the first time.
try:
with open(get_path_to_posh_history_db(), 'r') as f:
return [command[:-1].decode('utf-8') for command in f.readlines()]
except IOError:
return []
def get_oem_cp():
# Windows OEM/Ansi codepage mismatch issue.
# We need the OEM cp, because powershell is a console program.
codepage = ctypes.windll.kernel32.GetOEMCP()
return str(codepage)
def build_script(values, userPoShCmd):
with codecs.open(get_path_to_posh_script(), 'w', 'utf_8_sig') as f:
f.write( PoSh_SCRIPT_TEMPLATE % (get_path_to_output_sink(), values, userPoShCmd) )
def build_posh_cmd_line():
return ["powershell",
"-noprofile",
"-nologo",
"-noninteractive",
# PoSh 2.0 lets you specify an ExecutionPolicy
# from the cmdline, but 1.0 doesn't.
"-executionpolicy", "remotesigned",
"-file", get_path_to_posh_script(), ]
def filter_thru_posh(values, userPoShCmd):
try:
build_script(values, userPoShCmd)
except IOError:
raise CantAccessScriptFileError
# Hide the child process window.
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
PoShOutput, PoShErrInfo = subprocess.Popen(build_posh_cmd_line(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
startupinfo=startupinfo).communicate()
return ( PoShOutput.decode(get_oem_cp()),
PoShErrInfo.decode(get_oem_cp()), )
def run_posh_command(cmd):
"""Runs a command without taking into account Sublime regions for filtering.
Output should be output to console.
"""
encoded_cmd = cmd
posh_cmdline = [
"powershell.exe",
"-noprofile",
"-noninteractive",
"-nologo",
"-sta",
"-outputformat", "text",
"-command", encoded_cmd
]
out, error = subprocess.Popen(posh_cmdline,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
return (out.decode(get_oem_cp()),
error.decode(get_oem_cp()),)
class RunPowershell(sublime_plugin.TextCommand):
"""
This plugin provides an interface to filter text through a Windows
Powershell (PoSh) pipeline. See README.TXT for instructions.
"""
PoSh_HISTORY_MAX_LENGTH = 50
PSHistory = get_posh_saved_history()
lastFailedCommand = ""
def _add_to_posh_history(self, command):
# Comment out 'till the API is complete.
#if not command in self.PSHistory:
# self.PSHistory.insert(0, command)
#if len(self.PSHistory) > self.PowershellSh_HISTORY_MAX_LENGTH:
# self.PSHistory.pop()
pass
def _show_posh_history(self, view):
sublime.error_message("Not implemented due to incomplete API.")
# view.window().showQuickPanel('', "runExternalPSCommand", self.PSHistory,
# sublime.QUICK_PANEL_MONOSPACE_FONT)
def _parse_intrinsic_commands(self, userPoShCmd, view):
if userPoShCmd == '!h':
if self.PSHistory:
self._show_posh_history(view)
else:
sublime.status_message("Powershell command history is empty.")
return True
if userPoShCmd == '!mkh':
try:
with open(get_path_to_posh_history_db(), 'w') as f:
cmds = [(cmd + '\n').encode('utf-8') for cmd in self.PSHistory]
f.writelines(cmds)
sublime.status_message("Powershell command history saved.")
return True
except IOError:
sublime.status_message("ERROR: Could not save Powershell command history.")
else:
return False
def run(self, edit, initial_text='', command='', as_filter=True):
if command:
self.on_done(self.view, edit, command, as_filter)
return
# Open cmd line.
initialText = initial_text or self.lastFailedCommand
inputPanel = self.view.window().show_input_panel("PoSh cmd:", initialText, functools.partial(self.on_done, self.view, edit), None, None)
def on_done(self, view, edit, userPoShCmd, as_filter=True):
# Exit if user doesn't actually want to filter anything.
if self._parse_intrinsic_commands(userPoShCmd, view): return
# Run command, don't modify the buffer, output to output panel.
if not as_filter:
self.output_view = self.view.window().new_file()
self.output_view.set_scratch(True)
self.output_view.set_name("Powershell - Output")
out, error = run_posh_command(userPoShCmd)
if out or error:
if out: append(self.output_view, out)
if error: append(self.output_view, error)
return
try:
PoShOutput, PoShErrInfo = filter_thru_posh(regions_to_posh_array(view, view.sel()), userPoShCmd)
except EnvironmentError, e:
sublime.error_message("Windows error. Possible causes:\n\n" +
"* Is Powershell in your %PATH%?\n" +
"* Use Start-Process to start ST from Powershell.\n\n%s" % e)
return
except CantAccessScriptFileError:
sublime.error_message("Cannot access script file.")
return
# Inform the user that something went wrong in his PoSh code or
# perform substitutions and do house-keeping.
if PoShErrInfo:
print PoShErrInfo
sublime.status_message("PowerShell error.")
self.view.window().run_command("show_panel", {"panel": "console"})
self.lastFailedCommand = userPoShCmd
return
else:
self.lastFailedCommand = ''
self._add_to_posh_history(userPoShCmd)
# Cannot do zip(regs, outputs) because view.sel() maintains
# regions up-to-date if any of them changes.
for i, txt in enumerate(get_outputs()):
view.replace(edit, view.sel()[i], txt)