Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathpath.lua
More file actions
598 lines (503 loc) · 13.2 KB
/
Copy pathpath.lua
File metadata and controls
598 lines (503 loc) · 13.2 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
------------------------------------------------------------------
--
-- Author: Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
-- Copyright (C) 2013-2016 Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
-- Licensed according to the included 'LICENCE' document
--
-- This file is part of lua-path library.
--
------------------------------------------------------------------
local package = require "package"
local string = require "string"
local table = require "table"
local os = require "os"
local io = require "io"
local USE_ALIEN = true
local USE_FFI = true
local USE_AFX = true
local DIR_SEP = package.config:sub(1,1)
local IS_WINDOWS = DIR_SEP == '\\'
local PATH = {}
PATH.DIR_SEP = DIR_SEP
PATH.IS_WINDOWS = IS_WINDOWS
--
-- PATH manipulation
function PATH:unquote(P)
if P:sub(1,1) == '"' and P:sub(-1,-1) == '"' then
return (P:sub(2,-2))
end
return P
end
function PATH:quote(P)
if P:find("%s") then
return '"' .. P .. '"'
end
return P
end
function PATH:has_dir_end(P)
return (string.find(P, '[\\/]$')) and true
end
function PATH:remove_dir_end(P)
return (string.gsub(P, '[\\/]+$', ''))
end
function PATH:ensure_dir_end(P)
return self:remove_dir_end(P) .. self.DIR_SEP
end
function PATH:isunc(P)
return (string.sub(P, 1, 2) == (self.DIR_SEP .. self.DIR_SEP)) and P
end
function PATH:normolize_sep(P)
return (string.gsub(P, '\\', self.DIR_SEP):gsub('/', self.DIR_SEP))
end
PATH.normalize_sep = PATH.normolize_sep
function PATH:normolize(P)
P = self:normolize_sep(P)
local DIR_SEP = self.DIR_SEP
local is_unc = self:isunc(P)
while true do -- `/./` => `/`
local n P,n = string.gsub(P, DIR_SEP .. '%.' .. DIR_SEP, DIR_SEP)
if n == 0 then break end
end
while true do -- `//` => `/`
local n P,n = string.gsub(P, DIR_SEP .. DIR_SEP, DIR_SEP)
if n == 0 then break end
end
P = string.gsub(P, DIR_SEP .. '%.$', '')
if (not IS_WINDOWS) and (P == '') then P = '/' end
if is_unc then P = DIR_SEP .. P end
local root, path = nil, P
if is_unc then
root, path = self:splitroot(P)
end
path = self:ensure_dir_end(path)
while true do
local first, last = string.find(path, DIR_SEP .. "[^".. DIR_SEP .. "]+" .. DIR_SEP .. '%.%.' .. DIR_SEP)
if not first then break end
path = string.sub(path, 1, first) .. string.sub(path, last+1)
end
P = path
if root then -- unc
assert(is_unc)
P = P:gsub( '%.%.?' .. DIR_SEP , '')
P = DIR_SEP .. DIR_SEP .. self:join(root, P)
elseif self.IS_WINDOWS then
-- c:\..\foo => c:\foo
-- \..\foo => \foo
local root, path = self:splitroot(P)
if root ~= '' or P:sub(1,1) == DIR_SEP then
path = path:gsub( '%.%.?' .. DIR_SEP , '')
P = self:join(root, path)
end
end
if self.IS_WINDOWS and #P <= 3 and P:sub(2,2) == ':' then -- c: => c:\ or c:\ => c:\
if #P == 2 then return P .. self.DIR_SEP end
return P
end
if (not self.IS_WINDOWS) and (P == DIR_SEP) then return '/' end
return self:remove_dir_end(P)
end
PATH.normalize = PATH.normolize
function PATH:join_(P1, P2)
local ch = P2:sub(1,1)
if (ch == '\\') or (ch == '/') then
return self:remove_dir_end(P1) .. P2
end
return self:ensure_dir_end(P1) .. P2
end
function PATH:join(...)
local t,n = {...}, select('#', ...)
local r = t[1]
for i = 2, #t do
if self:isfullpath(t[i]) then
r = t[i]
else
r = self:join_(r,t[i])
end
end
return r
end
function PATH:splitext(P)
local s1,s2 = string.match(P,"(.-[^\\/.])(%.[^\\/.]*)$")
if s1 then return s1,s2 end
return P, ''
end
function PATH:splitpath(P)
return string.match(P,"^(.-)[\\/]?([^\\/]*)$")
end
function PATH:splitroot(P)
if self.IS_WINDOWS then
if self:isunc(P) then
return string.match(P, [[^\\([^\/]+)[\]?(.*)$]])
end
if string.sub(P,2,2) == ':' then
return string.sub(P,1,2), string.sub(P,4)
end
return '', P
else
if string.sub(P,1,1) == '/' then
return string.match(P,[[^/([^\/]+)[/]?(.*)$]])
end
return '', P
end
end
function PATH:splitdrive(P)
if self.IS_WINDOWS then
return self:splitroot(P)
end
return '', P
end
function PATH:basename(P)
local s1,s2 = self:splitpath(P)
return s2
end
function PATH:dirname(P)
return (self:splitpath(P))
end
function PATH:extension(P)
local s1,s2 = self:splitext(P)
return s2
end
function PATH:root(P)
return (self:splitroot(P))
end
function PATH:isfullpath(P)
return (self:root(P) ~= '') and P
end
function PATH:user_home()
if IS_WINDOWS then
return os.getenv('USERPROFILE') or PATH:join(os.getenv('HOMEDRIVE'), os.getenv('HOMEPATH'))
end
return os.getenv('HOME')
end
local function prequire(m)
local ok, err = pcall(require, m)
if not ok then return nil, err end
return err
end
local fs = prequire "path.fs"
if fs then
--
-- PATH based on system
local function assert_system(self)
if PATH.IS_WINDOWS then assert(self.IS_WINDOWS) return end
assert(not self.IS_WINDOWS)
end
if fs.flags then
function PATH:flags(P, ...)
assert_system(self)
P = self:fullpath(P)
return fs.flags(P, ...)
end
end
function PATH:tmpdir()
assert_system(self)
return self:remove_dir_end(fs.tmpdir())
end
function PATH:tmpname()
local P = os.tmpname()
if self:dirname(P) == '' then
P = self:join(self:tmpdir(), P)
end
return P
end
function PATH:size(P)
assert_system(self)
return fs.size(P)
end
function PATH:fullpath(P)
if not self:isfullpath(P) then
P = self:normolize_sep(P)
local ch1, ch2 = P:sub(1,1), P:sub(2,2)
if ch1 == '~' then -- ~\temp
P = self:join(self:user_home(), P:sub(2))
elseif self.IS_WINDOWS and (ch1 == self.DIR_SEP) then -- \temp => c:\temp
local root = self:root(self:currentdir())
P = self:join(root, P)
else
P = self:join(self:currentdir(), P)
end
end
return self:normolize(P)
end
function PATH:attrib(P, ...)
assert_system(self)
return fs.attributes(P, ...)
end
function PATH:exists(P)
assert_system(self)
return fs.exists(self:fullpath(P))
end
function PATH:isdir(P)
assert_system(self)
return fs.isdir(self:fullpath(P))
end
function PATH:isfile(P)
assert_system(self)
return fs.isfile(self:fullpath(P))
end
function PATH:islink(P)
assert_system(self)
return fs.islink(self:fullpath(P))
end
function PATH:ctime(P)
assert_system(self)
return fs.ctime(self:fullpath(P))
end
function PATH:mtime(P)
assert_system(self)
return fs.mtime(self:fullpath(P))
end
function PATH:atime(P)
assert_system(self)
return fs.atime(self:fullpath(P))
end
function PATH:touch(P, ...)
assert_system(self)
return fs.touch(self:fullpath(P), ...)
end
function PATH:currentdir()
assert_system(self)
return self:normolize(fs.currentdir())
end
function PATH:chdir(P)
assert_system(self)
return fs.chdir(self:fullpath(P))
end
function PATH:isempty(P)
assert_system(self)
local ok, err = fs.each_impl{
file = self:ensure_dir_end(P),
callback = function() return 'pass' end;
}
if err then return nil, err end
return ok ~= 'pass'
end
local date = prequire "date"
if date then
local function make_getfiletime_as_date(fn)
if date then
return function(...)
local t,e = fn(...)
if not t then return nil, e end
return date(t)
end
end
end
PATH.cdate = make_getfiletime_as_date( PATH.ctime );
PATH.mdate = make_getfiletime_as_date( PATH.mtime );
PATH.adate = make_getfiletime_as_date( PATH.atime );
end
function PATH:mkdir(P)
assert_system(self)
local P = self:fullpath(P)
if self:exists(P) then return self:isdir(P) end
local p = ''
P = self:ensure_dir_end(P)
for str in string.gmatch(P, '.-' .. self.DIR_SEP) do
p = p .. str
if self:exists(p) then
if not self:isdir(p) then
return nil, 'can not create ' .. p
end
else
if IS_WINDOWS or p ~= DIR_SEP then
local ok, err = fs.mkdir(self:remove_dir_end(p))
if not ok then return nil, err .. ' ' .. p end
end
end
end
return P
end
function PATH:rmdir(P)
assert_system(self)
return fs.rmdir(self:fullpath(P))
end
function PATH:rename(from, to, force)
assert_system(self)
from = self:fullpath(from)
to = self:fullpath(to)
return fs.move(from, to, force)
end
local each = require "path.findfile".load(function(opt)
local has_dir_end = PATH:has_dir_end(opt.file)
opt.file = PATH:fullpath(opt.file)
if has_dir_end then opt.file = PATH:ensure_dir_end(opt.file) end
return fs.each_impl(opt)
end)
function PATH:each(...)
assert_system(self)
return each(...)
end
local function copy_impl_batch(self, fs, src_dir, mask, dst_dir, opt)
if not opt then opt = {} end
local overwrite = opt.overwrite
local accept = opt.accept
local onerror = opt.error
local chlen = #fs.DIR_SEP
local count = 0
local existed_dirs = {}
local ok, err = fs.each_impl{file = src_dir .. fs.DIR_SEP .. mask,
delay = opt.delay; recurse = opt.recurse; param = "pnm";
skipdirs = opt.skipdirs; skipfiles = opt.skipfiles;
callback = function(path, name, mode)
local rel = string.sub(path, #src_dir + chlen + 1)
if #rel > 0 then rel = rel .. fs.DIR_SEP .. name else rel = name end
local dst = dst_dir .. fs.DIR_SEP .. rel
local src = path .. fs.DIR_SEP .. name
if accept then
local ok = accept(src, dst, opt)
if not ok then return end
end
local ok, err = true
if mode == "directory" then
if not existed_dirs[dst] then
if not fs.isdir(dst) then
ok, err = self:mkdir(dst)
end
existed_dirs[dst] = true
end
else
local dir = self:splitpath(dst)
if not existed_dirs[dir] then
if not fs.isdir(dst) then
ok, err = self:mkdir(dir)
end
existed_dirs[dir] = true
end
if ok then
ok, err = fs.copy(src, dst, not overwrite)
end
end
if not ok and onerror then
if not onerror(err, src, dst, opt) then -- break
return true
end
else
count = count + 1
end
end;
}
if ok or err then return ok, err end
return count
end
local function remove_impl_batch(fs, src_dir, mask, opt)
if not opt then opt = {} end
local overwrite = opt.overwrite
local accept = opt.accept
local onerror = opt.error
local chlen = #fs.DIR_SEP
local count = 0
local delay = (opt.delay == nil) and true or opt.delay
local ok, err = fs.each_impl{file = src_dir .. fs.DIR_SEP .. mask,
delay = delay; recurse = opt.recurse; reverse = true; param = "fm";
skipdirs = opt.skipdirs; skipfiles = opt.skipfiles;
callback = function(src, mode)
if accept then
local ok = accept(src, opt)
if not ok then return end
end
local ok, err
if mode == "directory" then ok, err = fs.rmdir(src)
else ok, err = fs.remove(src) end
if not ok and onerror then
if not onerror(err, src, opt) then -- break
return true
end
else
count = count + 1
end
end;
}
if ok or err then return ok, err end
return count
end
function PATH:remove_impl(P)
if self:isdir(P) then return fs.rmdir(P) end
return fs.remove(P)
end
function PATH:copy(from, to, opt)
from = self:fullpath(from)
to = self:fullpath(to)
if type(opt) == "boolean" then opt = {overwrite = opt} end
local overwrite = opt and opt.overwrite
local recurse = opt and opt.recurse
local src_dir, src_name = self:splitpath(from)
if recurse or src_name:find("[*?]") then -- batch mode
return copy_impl_batch(self, fs, src_dir, src_name, to, opt)
end
if self.mkdir then self:mkdir(self:dirname(to)) end
return fs.copy(from, to, not not overwrite)
end
function PATH:remove(P, opt)
assert_system(self)
local P = self:fullpath(P)
local dir, name = self:splitpath(P)
if (opt and opt.recurse) or name:find("[*?]") then -- batch mode
return remove_impl_batch(fs, dir, name, opt)
end
return self:remove_impl(P)
end
end -- fs
do -- Python aliases
PATH.split = PATH.splitpath
PATH.isabs = PATH.isfullpath
PATH.normpath = PATH.normolize
PATH.abspath = PATH.fullpath
PATH.getctime = PATH.ctime
PATH.getatime = PATH.atime
PATH.getmtime = PATH.mtime
PATH.getsize = PATH.size
end
local function path_new(o)
o = o or {}
for k, f in pairs(PATH) do
if type(f) == 'function' then
o[k] = function(...)
if o == ... then return f(...) end
return f(o, ...)
end
else
o[k] = f
end
end
return o
end
local function lock_table(t)
return setmetatable(t,{
__newindex = function()
error("Can not change path library", 2)
end;
__metatable = "lua-path object";
})
end
local M = path_new(require "path.module")
local path_cache = setmetatable({}, {__mode='v'})
function M.new(DIR_SEP)
local is_win, sep
if type(DIR_SEP) == 'string' then
sep = DIR_SEP
is_win = (DIR_SEP == '\\')
elseif DIR_SEP ~= nil then
assert(type(DIR_SEP) == 'boolean')
is_win = DIR_SEP
sep = is_win and '\\' or '/'
else
sep = M.DIR_SEP
is_win = M.IS_WINDOWS
end
if M.DIR_SEP == sep then
assert(M.IS_WINDOWS == is_win)
return M
end
local o = path_cache[sep]
if not o then
o = path_new()
o.DIR_SEP = sep
o.IS_WINDOWS = is_win
path_cache[sep] = lock_table(o)
end
return o
end
return lock_table(M)
You can’t perform that action at this time.
