forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.jl
254 lines (220 loc) · 6.51 KB
/
file.jl
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
# File and path name manipulation
# These do not examine the filesystem at all, they just work on strings
@unix_only begin
const os_separator = "/"
const os_separator_match = "/"
const os_separator_match_chars = "/"
end
@windows_only begin
const os_separator = "\\"
const os_separator_match = "[/\\]" # permit either slash type on Windows
const os_separator_match_chars = "/\\" # to permit further concatenation
end
# Match only the final separator
const last_separator = Regex(strcat(os_separator_match, "(?!.*", os_separator_match, ")"))
# Match the "." indicating a file extension. Must satisfy the
# following requirements:
# - It's not followed by a later "." or os_separator
# (handles cases like myfile.txt.gz, or Mail.directory/cur)
# - It's not the first character in a string, nor is it preceded by
# an os_separator (handles cases like .bashrc or /home/fred/.juliarc)
const extension_separator_match = Regex(strcat("(?<!^)(?<!",
os_separator_match, ")\\.(?!.*[", os_separator_match_chars, "\.])"))
# Match ~/filename
const plain_tilde = Regex(strcat("^~", os_separator_match))
# Match ~user/filename
const user_tilde = r"^~\w"
filesep() = os_separator
function basename(path::String)
m = match(last_separator, path)
if m == nothing
return path
else
return path[m.offset+1:end]
end
end
function dirname(path::String)
m = match(last_separator, path)
if m == nothing
return ""
else
return path[1:m.offset-1]
end
end
function dirname_basename(path::String)
m = match(last_separator, path)
if m == nothing
return "", path
else
return path[1:m.offset-1], path[m.offset+1:end]
end
end
function split_extension(path::String)
m = match(extension_separator_match, path)
if m == nothing
return path, ""
else
return path[1:m.offset-1], path[m.offset:end]
end
end
split_path(path::String) = split(path, os_separator_match)
function fileparts(filename::String)
pathname, filestr = dirname_basename(filename)
filebase, ext = split_extension(filestr)
return pathname, filebase, ext
end
function file_path(components...)
join(components, os_separator)
end
function fullfile(pathname::String, basename::String, ext::String)
if isempty(pathname)
return basename * ext
else
return pathname * os_separator * basename * ext
end
end
# Test for an absolute path
function isrooted(path::String)
# Check whether it begins with the os_separator. On Windows, matches
# \\servername syntax, so this is a relevant check for everyone
m = match(Regex(strcat("^", os_separator_match)), path)
if m != nothing
return true
end
@windows_only begin
m = match(r"^\w+:", path)
if m != nothing
return true
end
end
false
end
global tilde_expand
function tilde_expand(path::String)
@windows_only return path # on windows, ~ means "temporary file"
@unix_only begin
m = match(user_tilde, path)
if m != nothing
return "/home/"*path[2:end]
end
end
m = match(plain_tilde, path)
if m != nothing
return ENV["HOME"]*path[2:end]
end
path
end
# Get the absolute path to a file. Uses file system for cwd() when
# needed, the rest is all string manipulation. In particular, it
# doesn't check whether the file exists.
function abs_path_split(fname::String)
fname = tilde_expand(fname)
if isrooted(fname)
comp = split(fname, os_separator_match)
else
comp = [split(cwd(), os_separator_match), split(fname, os_separator_match)]
end
n = length(comp)
pmask = trues(n)
last_is_dir = false
for i = 2:n
if comp[i] == "." || comp[i] == ""
pmask[i] = false
last_is_dir = true
elseif comp[i] == ".."
pmask[i] = false
last_is_dir = true
for j = i-1:-1:2
if pmask[j]
pmask[j] = false
break
end
end
else
last_is_dir = false
end
end
comp = comp[pmask]
if last_is_dir
push(comp, "")
end
return comp
end
function abs_path(fname::String)
comp = abs_path_split(fname)
return join(comp, os_separator)
end
# The remaining commands use the file system in some way
# Get the full, real path to a file, including dereferencing
# symlinks.
function real_path(fname::String)
fname = tilde_expand(fname)
sp = ccall(:realpath, Ptr{Uint8}, (Ptr{Uint8}, Ptr{Uint8}), fname, C_NULL)
system_error(:real_path, sp == C_NULL)
s = cstring(sp)
ccall(:free, Void, (Ptr{Uint8},), sp)
return s
end
# get and set current directory
function cwd()
b = Array(Uint8,1024)
p = ccall(:getcwd, Ptr{Uint8}, (Ptr{Uint8}, Uint), b, length(b))
system_error("getcwd", p == C_NULL)
cstring(p)
end
cd(dir::String) = system_error("chdir", ccall(:chdir,Int32,(Ptr{Uint8},),real_path(dir)) == -1)
cd() = cd(ENV["HOME"])
# do stuff in a directory, then return to current directory
function cd(f::Function, dir::String)
fd = ccall(:open,Int32,(Ptr{Uint8},Int32),".",0)
system_error("open", fd == -1)
try
cd(dir)
retval = f()
system_error("fchdir", ccall(:fchdir,Int32,(Int32,),fd) != 0)
retval
catch err
system_error("fchdir", ccall(:fchdir,Int32,(Int32,),fd) != 0)
throw(err)
end
end
cd(f::Function) = cd(f, ENV["HOME"])
# The following use Unix command line facilites
# list the contents of a directory
ls() = run(`ls -l`)
ls(args::Cmd) = run(`ls -l $args`)
ls(args::String...) = run(`ls -l $args`)
function path_expand(path::String)
chomp(readlines(`bash -c "echo $path"`)[1])
end
function file_copy(source::String, destination::String)
run(`cp $source $destination`)
end
function file_create(filename::String)
run(`touch $filename`)
end
function file_remove(filename::String)
run(`rm $filename`)
end
function path_rename(old_pathname::String, new_pathname::String)
run(`mv $old_pathname $new_pathname`)
end
function dir_create(directory_name::String)
run(`mkdir $directory_name`)
end
function dir_remove(directory_name::String)
run(`rmdir $directory_name`)
end
function tempdir()
chomp(readall(`mktemp -d -t tmp`))
end
function tempfile()
chomp(readall(`mktemp -t tmp`))
end
function download_file(url::String)
filename = tempfile()
run(`curl -o $filename $url`)
new_filename = strcat(filename, ".tar.gz")
path_rename(filename, new_filename)
new_filename
end