forked from facebook/pyre-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyrePath.ml
257 lines (186 loc) · 6.65 KB
/
pyrePath.ml
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
(* Copyright (c) 2016-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree. *)
open Core
type path = string [@@deriving compare, eq, show, sexp, hash]
module AbsolutePath = struct
type t = path [@@deriving compare, eq, show, sexp, hash]
end
module RelativePath = struct
type t = {
root: path;
relative: path;
}
[@@deriving compare, eq, show, sexp, hash]
let relative { relative; _ } = relative
end
type t =
| Absolute of AbsolutePath.t
| Relative of RelativePath.t
[@@deriving sexp, hash]
let absolute = function
| Absolute path -> path
| Relative { root; relative } -> root ^/ relative
let relative = function
| Absolute _ -> None
| Relative { relative; _ } -> Some relative
let uri path = "file://" ^ absolute path
let show = absolute
let to_yojson path = `String (show path)
let equal left right = String.equal (absolute left) (absolute right)
let compare left right = String.compare (absolute left) (absolute right)
let pp format path = Format.fprintf format "%s" (absolute path)
let create_absolute ?(follow_symbolic_links = true) path =
if follow_symbolic_links then
Absolute (Filename.realpath path)
else
Absolute path
let create_relative ~root ~relative =
let root =
let root = absolute root in
if not (String.is_suffix ~suffix:"/" root) then root ^ "/" else root
in
let relative = String.chop_prefix ~prefix:root relative |> Option.value ~default:relative in
Relative { root; relative }
let get_relative_to_root ~root ~path =
let root =
let root = absolute root in
if not (String.is_suffix ~suffix:"/" root) then root ^ "/" else root
in
String.chop_prefix ~prefix:root (absolute path)
let from_uri uri = String.chop_prefix ~prefix:"file://" uri |> Option.map ~f:create_absolute
let current_working_directory () = create_absolute (Sys.getcwd ())
let append path ~element =
match path with
| Absolute path -> Absolute (path ^/ element)
| Relative { root; relative } ->
let relative =
match relative with
| "" -> element
| _ -> relative ^/ element
in
Relative { root; relative }
module AppendOperator = struct
let ( ^| ) path element = append path ~element
end
let is_directory path = absolute path |> fun path -> Sys.is_directory path = `Yes
let get_suffix_path = function
| Absolute path -> path
| Relative { relative; _ } -> relative
let is_path_python_stub path = String.is_suffix ~suffix:".pyi" path
let is_path_python_init path =
String.is_suffix ~suffix:"__init__.pyi" path || String.is_suffix ~suffix:"__init__.py" path
let is_python_stub path = get_suffix_path path |> is_path_python_stub
let is_python_init path = get_suffix_path path |> is_path_python_init
let file_exists path = absolute path |> fun path -> Sys.file_exists path = `Yes
let last path =
let absolute = absolute path in
String.split ~on:'/' absolute |> List.last |> Option.value ~default:absolute
let real_path path =
match path with
| Absolute _ -> path
| Relative _ -> absolute path |> create_absolute
let follow_symbolic_link path =
try absolute path |> create_absolute ~follow_symbolic_links:true |> Option.some with
| Unix.Unix_error _ -> None
(* Variant of Sys.readdir where names are sorted in alphabetical order *)
let read_directory_ordered path =
let entries = Core.Sys.readdir path in
Array.sort ~compare:String.compare entries;
entries
let list ?(file_filter = fun _ -> true) ?(directory_filter = fun _ -> true) ~root () =
let rec list sofar path =
if Core.Sys.is_directory path = `Yes then
if directory_filter path then (
match read_directory_ordered path with
| entries ->
let collect sofar entry = list sofar (path ^/ entry) in
Array.fold ~init:sofar ~f:collect entries
| exception Sys_error _ ->
Log.error "Could not list `%s`" path;
sofar )
else
sofar
else if file_filter path then
create_relative ~root ~relative:path :: sofar
else
sofar
in
list [] (absolute root)
let directory_contains ~directory path =
let path = absolute path in
let directory = absolute directory in
String.is_prefix ~prefix:directory path
(* Walk up from the root to try and find a directory/target. *)
let search_upwards ~target ~root =
let rec directory_has_target directory =
if Sys.is_file (directory ^/ target) = `Yes then
Some (create_absolute directory)
else if Filename.dirname directory = directory then
None
else
directory_has_target (Filename.dirname directory)
in
directory_has_target (absolute root)
let remove path =
try Sys.remove (absolute path) with
| Sys_error _ -> Log.debug "Unable to remove file at %a" pp path
let readlink path =
try Unix.readlink (absolute path) |> Option.some with
| Unix.Unix_error _ -> None
module Map = Map.Make (struct
type nonrec t = t
let compare left right = String.compare (absolute left) (absolute right)
let sexp_of_t = sexp_of_t
let t_of_sexp = t_of_sexp
end)
module Set = Set.Make (struct
type nonrec t = t
let compare left right = String.compare (absolute left) (absolute right)
let sexp_of_t = sexp_of_t
let t_of_sexp = t_of_sexp
end)
let build_symlink_map ~links =
let add_symlink map path =
try
let key = real_path path in
Map.set map ~key ~data:path
with
| Unix.Unix_error (error, name, parameters) ->
Log.log_unix_error ~section:`Warning (error, name, parameters);
map
in
List.fold links ~init:Map.empty ~f:add_symlink
let with_suffix path ~suffix =
match path with
| Absolute prefix -> Absolute (prefix ^ suffix)
| Relative { root; relative } -> Relative { root; relative = relative ^ suffix }
let get_directory path =
absolute path |> Filename.dirname |> create_absolute ~follow_symbolic_links:false
let project_directory ~local_root ~filter_directories =
if String.is_substring ~substring:"/scratch/" local_root then
match filter_directories with
| [project_directory] -> project_directory
| _ -> ""
else
local_root
let get_matching_files_recursively ~suffix ~paths =
let rec expand path =
if is_directory path then
let expand_directory_entry entry =
let path = append path ~element:entry in
if is_directory path then
expand path
else if String.is_suffix ~suffix entry then
[path]
else
[]
in
Sys.readdir (absolute path) |> Array.to_list |> List.concat_map ~f:expand_directory_entry
else if String.is_suffix ~suffix (absolute path) then
[path]
else
[]
in
List.concat_map ~f:expand paths