-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach.py
144 lines (107 loc) · 4.22 KB
/
attach.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
"""
This script adds the the containing package as a valid debug adapter in the Debugger's settings
"""
from os.path import join, abspath, dirname, expanduser, exists
from Debugger.modules.debugger.debugger import Debugger
from threading import Timer
from shutil import copy
import sublime
import time
import sys
import os
adapter_type = "Nuke" # NOTE: type name must be unique to each adapter
package_path = dirname(abspath(__file__))
adapter_path = join(package_path, "adapter")
# The version is only used for display in the GUI
version = "1.0"
# You can have several configurations here depending on your adapter's offered functionalities,
# but they all need a "label", "description", and "body"
config_snippets = [
{
"label": "Nuke: Python 2 Debugging",
"description": "Run and Debug Python 2 code in Nuke",
"body": {
"name": "Nuke: Python 2 Debugging",
"type": adapter_type,
"program": "\${file\}",
"request": "attach", # can only be attach or launch
"interpreter": sys.executable,
"debugpy": # The host/port used to communicate with debugpy in Nuke
{
"host": "localhost",
"port": 7004
},
}
},
]
# The settings used by the Debugger to run the adapter.
settings = {
"type": adapter_type,
"command": [sys.executable, adapter_path]
}
# Instantiate variables needed for checking thread
running = False
check_speed = 3 # number of seconds to wait between checks for adapter presence in debugger instances
# Add server file to nuke if not present
user_nuke_path = join(expanduser("~"), ".nuke")
setup = join(adapter_path, 'resources', 'setup')
srv = join(user_nuke_path, "script_debug_server.py")
menu = join(user_nuke_path, "menu.py")
first_setup = False
if exists(join(user_nuke_path, 'debug_server.py')):
os.remove(join(user_nuke_path, 'debug_server.py'))
if not exists(srv):
copy(join(setup, 'script_debug_server.py'), srv)
first_setup = True
if not exists(menu):
with open(menu, 'w') as f:
f.write('import script_debug_server')
else:
with open(menu, 'r+') as f:
contents = f.read()
if "import script_debug_server" not in contents:
f.write("\nimport script_debug_server")
def check_for_adapter():
"""
Gets run in a thread to inject configuration snippets and version information
into adapter objects of type adapter_type in each debugger instance found
"""
while running:
for instance in Debugger.instances.values():
adapter = getattr(instance, "adapters", {}).get(adapter_type, None)
if adapter and not adapter.version:
adapter.version = version
adapter.snippets = config_snippets
time.sleep(check_speed)
def plugin_loaded():
""" Add adapter to debugger settings for it to be recognized """
# Add entry to debugger settings
debugger_settings = sublime.load_settings('debugger.sublime-settings')
adapters_custom = debugger_settings.get('adapters_custom', {})
adapters_custom[adapter_type] = settings
debugger_settings.set('adapters_custom', adapters_custom)
sublime.save_settings('debugger.sublime-settings')
# Start checking thread
global running
running = True
Timer(1, check_for_adapter).start()
if first_setup:
sublime.message_dialog(
"Thanks for installing the Nuke debug adapter!\n"
"Because this is your first time using the adapter, a one-time "
"setup was performed. Please restart Nuke before continuing."
)
def plugin_unloaded():
""" This is done every unload just in case this adapter is being uninstalled """
# Wait for checking thread to finish
global running
running = False
time.sleep(check_speed + .1)
# Remove entry from debugger settings
debugger_settings = sublime.load_settings('debugger.sublime-settings')
adapters_custom = debugger_settings.get('adapters_custom', {})
adapters_custom.pop(adapter_type, "")
debugger_settings.set('adapters_custom', adapters_custom)
sublime.save_settings('debugger.sublime-settings')
if exists(srv):
os.remove(srv)