Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

render-pipeline.tcl 6.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. # Render the DVC pipeline from source files.
  2. #
  3. # This TCL program interprets a TCL-based DSL for generating reconfigurable
  4. # DVC pipelines. The pipelines themselves are defined in 'pipeline.tcl' files.
  5. package require huddle
  6. package require yaml
  7. package require cmdline
  8. set _show_dbg 0
  9. proc dbg_msg {msg} {
  10. if {$::_show_dbg} {
  11. puts stderr "D: $msg"
  12. }
  13. }
  14. proc info_msg {msg} {
  15. puts stderr "I: $msg"
  16. }
  17. namespace eval dvcpipe {
  18. set stages [huddle false]
  19. set stack {}
  20. proc _ensure_list {name} {
  21. set keys [huddle keys $::dvcpipe::current]
  22. if {[lsearch $name $keys] < 0} {
  23. huddle append ::dvcpipe::current $name [huddle list]
  24. }
  25. }
  26. proc init {name} {
  27. dbg_msg "initializing stage $name"
  28. set ::dvcpipe::name $name
  29. if {[array exists ::dvcpipe::current]} {
  30. array unset ::dvcpipe::current
  31. }
  32. array set ::dvcpipe::current {}
  33. set ::dvcpipe::targets [list]
  34. }
  35. proc save {} {
  36. if {[info exists ::dvcpipe::name]} {
  37. dbg_msg "saving $::dvcpipe::name"
  38. set obj [dict create name $::dvcpipe::name obj [array get ::dvcpipe::current] targets $::dvcpipe::targets]
  39. lappend ::dvcpipe::stack $obj
  40. }
  41. }
  42. proc restore {} {
  43. if {[llength $::dvcpipe::stack] > 0} {
  44. set obj [lindex $::dvcpipe::stack ::dvcpipe::stack end]
  45. set ::dvcpipe::stack [lreplace $::dvcpipe::stack end-1 end]
  46. set ::dvcpipe::name [dict get $obj name]
  47. dbg_msg "restoring $::dvcpipe::name"
  48. if {[array exists ::dvcpipe::current]} {
  49. array unset ::dvcpipe::current
  50. }
  51. array set ::dvcpipe::current [dict get $obj obj]
  52. set ::dvcpipe::targets [dict get $obj targets]
  53. }
  54. }
  55. proc set_field {key val} {
  56. dbg_msg "key: $key"
  57. dbg_msg "val: $val"
  58. array set ::dvcpipe::current [list $key $val]
  59. }
  60. proc push_field {key val} {
  61. if {[info exists ::dvcpipe::current($key)]} {
  62. huddle append ::dvcpipe::current($key) $val
  63. } else {
  64. array set ::dvcpipe::current [list $key [huddle list $val]]
  65. }
  66. }
  67. proc finish {} {
  68. dbg_msg "finishing $::dvcpipe::name"
  69. set var [huddle create]
  70. foreach v {cmd wdir deps outs metrics} {
  71. if {[info exists ::dvcpipe::current($v)]} {
  72. huddle append var $v $::dvcpipe::current($v)
  73. }
  74. }
  75. if {[llength $::dvcpipe::targets] > 0} {
  76. set tgts [huddle list]
  77. foreach t $::dvcpipe::targets {
  78. huddle append tgts [huddle string $t]
  79. }
  80. set var [huddle create foreach $tgts do $var]
  81. }
  82. unset ::dvcpipe::name
  83. unset ::dvcpipe::current
  84. unset ::dvcpipe::targets
  85. return $var
  86. }
  87. }
  88. namespace eval dvcstage {
  89. namespace export target
  90. namespace export cmd
  91. namespace export wdir
  92. namespace export dep
  93. namespace export out
  94. proc mkout {nocache dep} {
  95. if {$nocache} {
  96. return [huddle create $dep [huddle create cache false]]
  97. } else {
  98. return [huddle string $dep]
  99. }
  100. }
  101. proc target {tgt} {
  102. dbg_msg "task $::dvcpipe::name has subtarget $tgt"
  103. lappend ::dvcpipe::targets $tgt
  104. }
  105. proc cmd {s} {
  106. ::dvcpipe::set_field cmd [huddle string $s]
  107. }
  108. proc wdir {s} {
  109. ::dvcpipe::set_field wdir [huddle string $s]
  110. }
  111. proc dep args {
  112. set rl 0
  113. foreach a $args {
  114. if {[string equal $a -l]} {
  115. set rl 1
  116. } else {
  117. if $rl {
  118. foreach e $a {
  119. ::dvcpipe::push_field deps [huddle string $e]
  120. }
  121. } else {
  122. ::dvcpipe::push_field deps [huddle string $a]
  123. }
  124. set rl 0
  125. }
  126. }
  127. }
  128. proc out args {
  129. set rl 0
  130. set nocache 0
  131. foreach a $args {
  132. if {[string equal $a -l]} {
  133. set rl 1
  134. } elseif {[string equal $a -nocache]} {
  135. set nocache 1
  136. } else {
  137. if $rl {
  138. foreach e $a {
  139. ::dvcpipe::push_field outs [mkout $nocache $e]
  140. }
  141. } else {
  142. ::dvcpipe::push_field outs [mkout $nocache $a]
  143. }
  144. set rl 0
  145. set nocache 0
  146. }
  147. }
  148. }
  149. proc metric args {
  150. set nocache 0
  151. foreach a $args {
  152. if {[string equal $a -nocache]} {
  153. set nocache 1
  154. } else {
  155. ::dvcpipe::push_field metrics [mkout $nocache $a]
  156. set nocache 0
  157. }
  158. }
  159. }
  160. }
  161. proc stage {name block} {
  162. ::dvcpipe::save
  163. ::dvcpipe::init $name
  164. namespace eval dvcstage $block
  165. huddle append ::dvcpipe::stages $name [::dvcpipe::finish]
  166. ::dvcpipe::restore
  167. }
  168. proc subdir {dir} {
  169. info_msg "processing subdirectory $dir"
  170. _run_pipeline_script "$dir/pipeline.tcl" "$dir/dvc.yaml"
  171. }
  172. proc _run_pipeline_script {tclfn ymlfn} {
  173. set saved $::dvcpipe::stages
  174. set ::dvcpipe::stages [huddle create]
  175. info_msg "evaluating $tclfn"
  176. uplevel "source \"$tclfn\""
  177. set stages $::dvcpipe::stages
  178. set ::dvcpipe::stages $saved
  179. set pipeline [huddle create stages $stages]
  180. dbg_msg $pipeline
  181. info_msg "writing $ymlfn with [llength [huddle keys $stages]] stages"
  182. if {$::_cmd_params(out)} {
  183. puts [::yaml::huddle2yaml $pipeline 2 250]
  184. } else {
  185. set fp [open $ymlfn w]
  186. puts $fp [::yaml::huddle2yaml $pipeline 2 250]
  187. close $fp
  188. }
  189. }
  190. array set _cmd_params [::cmdline::getoptions argv {
  191. {v "verbose logging output"}
  192. {out "write to stdout instead of file"}
  193. } {: [options]}]
  194. set _show_dbg $_cmd_params(v)
  195. dbg_msg "finished option parsing"
  196. _run_pipeline_script "pipeline.tcl" "dvc.yaml"
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...