-
Notifications
You must be signed in to change notification settings - Fork 6
/
pip_setup.ado
330 lines (243 loc) · 8.45 KB
/
pip_setup.ado
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*==================================================
project: Run globals and setup env for pip
Author: R.Andres Castaneda
E-email: [email protected]
url:
Dependencies: The World Bank
----------------------------------------------------
Creation Date: 6 May 2023 - 06:57:11
Modification Date:
Do-file version: 01
References:
Output:
==================================================*/
/*==================================================
0: Program set up
==================================================*/
program define pip_setup, rclass
version 16.1
syntax [anything(name=subcmd)] [, * ]
pip_setup_dates
//========================================================
// Subcommands
//========================================================
// create
if ("`subcmd'" == "create") {
pip_setup_create
return add
exit
}
// Replace in pattern
if ("`subcmd'" == "replace") {
pip_setup_replace, `options'
return add
exit
}
// run pip_setup.do
if ("`subcmd'" == "run") {
cap findfile "pip_setup.do"
if (_rc) pip_setup_create // if setup.do is not found
if ("`options'" == "display") type "`r(fn)'"
run "`r(fn)'"
exit
}
// run pip_setup.do
if ("`subcmd'" == "display") {
cap findfile "pip_setup.do"
if (_rc) pip_setup_create // if setup.do is not found
type "`r(fn)'"
exit
}
// cche dir
if ("`subcmd'" == "cachedir") {
noi pip_setup_cachedir, `options'
exit
}
// server
if ("`subcmd'" == "server") {
pip_set_server, `options'
exit
}
qui {
//========================================================
// compile mata code
//========================================================
global pip_version ""
findfile "pip_fun.mata"
local pip_funmata_file = "`r(fn)'"
tempname spipmata
scalar `spipmata' = fileread("`pip_funmata_file'")
pip_cache gethash, query(`"`: disp `spipmata''"')
local pipmata_hash = "`r(piphash)'"
// To avoid MATA library to be built each time.
if ("${pip_pipmata_hash}" == "") {
cap findfile "pip_setup.do"
if (_rc) global pip_pipmata_hash "000" // if setup.do is not found
else run "`r(fn)'"
}
// If mata functions have changed, saved them again.
if ("${pip_pipmata_hash}" != "`pipmata_hash'") {
run "`pip_funmata_file'"
lmbuild lpip_fun.mlib, replace
noi disp "{res}Mata {txt}lpip_fun {res}library has been updated"
local pattern = "pip_pipmata_hash"
local newline = `"global pip_pipmata_hash = "`pipmata_hash'""'
* local newline = `"global pip_pipmata_hash = "1959982309""'
cap findfile "pip_setup.do"
if (_rc) pip_setup_create // if setup.do is not found
pip_setup replace, pattern(`"`pattern'"') new(`"`newline'"')
}
//------------Run globals
pip_setup run
//========================================================
// PIP cache directory
//========================================================
mata: st_local("ex_cachedir", strofreal(direxists("${pip_cachedir}")))
if ("${pip_cachedir}" == "" | `ex_cachedir' == 0) {
noi pip_setup_cachedir, cachedir("${pip_cachedir}")
}
//========================================================
// Set global for server
//========================================================
if ("${pip_host}" == "") {
pip_set_server
}
}
end
//========================================================
// Program to create pip_setup.do
//========================================================
program define pip_setup_create, rclass
qui {
// find folder to store setup.do
local pdirs `" "`c(sysdir_personal)'" "`c(sysdir_plus)'" "`c(pwd)'" "`c(sysdir_site)'" "'
tokenize `"`pdirs'"'
while ("`1'" != "") {
mata: st_local("setup_dir", pathjoin("`1'", "p"))
mata: st_numscalar("direxist", pip_check_folder("`setup_dir'"))
if (direxist == 1 ) {
mata: st_local("setup_file", pathjoin("`setup_dir'", "pip_setup.do"))
continue, break
}
macro shift
}
// it assumes that pip_setup.do does not exist
// because this program is called in a condition
tempfile dofile
tempname do
file open `do' using `dofile', write `replace'
file write `do' `"//"' _dup(50) "=" _n
file write `do' `"// Globals"' _n
file write `do' `"//"' _dup(50) "=" _n
file write `do' `""' _dup(2) _n
file write `do' `"global pip_pipmata_hash = """' _dup(3) _n
file write `do' `"global pip_lastupdate = """' _dup(3) _n
file write `do' `"global pip_source = """' _dup(3) _n
file write `do' `"global pip_cachedir = """' _dup(3) _n
file write `do' _dup(5) _n
file write `do' `"exit"' _n
file write `do' `"/* End of do-file */"' _n
file close `do'
// save file in dir
copy `dofile' "`setup_file'", replace
return local fn = "`setup_file'"
}
end
//========================================================
// Replace setup lines in pip_setup.do
//========================================================
program define pip_setup_replace, rclass
version 16.1
syntax [anything(name=subcmd)], [ ///
pattern(string) ///
NEWline(string) ///
]
qui {
findfile "pip_setup.do"
local setup_file = "`r(fn)'"
mata: pip_replace_in_pattern("`setup_file'", `"`pattern'"', `"`newline'"')
copy `tempf' "`origf'" , replace // locals tempf and origf are created in MATA routine
return local fn = "`setup_file'"
}
end
program define pip_setup_cachedir, rclass
version 16.1
syntax [anything(name=subcmd)] , [ ///
cachedir(string) ///
]
qui {
tempname direxist
if ("`cachedir'" == "") {
// find folder to store setup.do
if !inlist("${pip_cachedir}", "no", "") local oldcachedir `""${pip_cachedir}""'
else local oldcachedir ""
local pdirs `" `oldcachedir' "`c(sysdir_personal)'" "`c(sysdir_plus)'" "`c(pwd)'" "`c(sysdir_site)'" "'
tokenize `"`pdirs'"'
scalar `direxist' = 0
while ("`1'" != "") {
mata: st_local("cachedir", pathjoin("`1'", "pip_cache"))
mata: st_numscalar("`direxist'", pip_check_folder("`cachedir'"))
if (`direxist' == 1 ) {
cap window stopbox rusure ///
`"Do you want to use directory "`cachedir'" to store PIP cache data?"' ///
`"If you don't, click "No" and provide an alternative directory path in the console."'
if (_rc) {
db pip_setup_cachedir
noi disp "{err}NOTE: {res}Abort operation until cache directory is either set up or disabled"
error
}
continue, break // exit while
}
macro shift
} // end of while
} // if cache dir is empty
if ("`cachedir'" != "") {
if inlist(lower("`cachedir'"), "0", "no") {
local cachedir = 0
scalar `direxist' = 1 // bypass condition
}
else mata: st_numscalar("`direxist'", pip_check_folder("`cachedir'"))
if (`direxist' == 1 ) {
local pattern "pip_cachedir"
local newline `"global pip_cachedir = "`cachedir'""'
pip_setup replace, pattern(`"`pattern'"') new(`"`newline'"')
pip_setup run
if ("`cachedir'" != "0") {
noi disp "{res}Cache directory has been set up. If you want to change it, type {cmd:pip_setup cachedir}"
}
else {
noi disp "{res}Cache has been {err}disabled{res}. If you want to change it, type {cmd:pip cache, setup}"
}
}
}
}
end
//========================================================
// Program to create dates that will be used across PIP
//========================================================
program define pip_setup_dates
version 16.1
local date = date("`c(current_date)'", "DMY") // %tdDDmonCCYY
local time = clock("`c(current_time)'", "hms") // %tcHH:MM:SS
local date_time = `date'*24*60*60*1000 + `time' // %tcDDmonCCYY_HH:MM:SS
local datetimeHRF: disp %tcDDmonCCYY_HH:MM:SS `date_time'
local datetimeHRF = trim("`datetimeHRF'")
local dateHRF: disp %tdDDmonCCYY `date'
local dateHRF = trim("`dateHRF'")
local date_file: disp %tdCCYYNNDD `date'
local date_file = trim("`date_file'")
global pip_date_file = "`date_file'"
global pip_date_file_format = "%tdDDmonCCYY"
global pip_dateHRF = "`dateHRF'"
global pip_date_format = "%tdDDmonCCYY"
global pip_datetimeHRF = "`datetimeHRF'"
global pip_datetime_format = "%tcDDmonCCYY_HH:MM:SS"
end
exit
/* End of do-file */
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
Notes:
1.
2.
3.
Version Control or old (new) ideas: