diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-09-05 11:12:52 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-09-05 11:45:41 +0100 |
commit | 3d84ca76fec491015030daa53e9122b03032ddfd (patch) | |
tree | 322bb535f9745fc0efced977402be4db9469d835 /generator/pr.ml | |
parent | 169ac913e9930f7e4a27f395e6c06901f7c849c3 (diff) | |
download | libguestfs-3d84ca76fec491015030daa53e9122b03032ddfd.tar.gz libguestfs-3d84ca76fec491015030daa53e9122b03032ddfd.tar.xz libguestfs-3d84ca76fec491015030daa53e9122b03032ddfd.zip |
generator: Remove generated and unused files from previous runs of the generator.
If you go back in time in git (eg. git reset, git bisect) then you can
end up in a situation where a file that was generated by a later
version is left around unused in the earlier version.
This isn't a problem for most things, but gobject documentation
generation picks up any file in a directory, even unreferenced ones,
and breaks. So the correct thing to do is to remove these files.
Diffstat (limited to 'generator/pr.ml')
-rw-r--r-- | generator/pr.ml | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/generator/pr.ml b/generator/pr.ml index fd927206..1c3812a1 100644 --- a/generator/pr.ml +++ b/generator/pr.ml @@ -31,6 +31,7 @@ let lines = ref 0 (* Name of each file generated. *) let files = ref [] +let fileshash = Hashtbl.create 13 (* Print-to-current-output function, used everywhere. It has * printf-like semantics. @@ -45,6 +46,7 @@ let pr fs = let output_to ?(perm = 0o444) filename k = files := filename :: !files; + Hashtbl.add fileshash filename (); let filename_new = filename ^ ".new" in chan := open_out filename_new; @@ -63,6 +65,36 @@ let output_to ?(perm = 0o444) filename k = printf "written %s\n%!" filename; ) +let delete_except_generated ?(skip = []) glob = + let cmd = sprintf "ls -1 %s" glob in + let chan = open_process_in cmd in + let lines = ref [] in + let rec loop () = + lines := input_line chan :: !lines; + loop () + in + let lines = try loop () with End_of_file -> List.rev !lines in + (match close_process_in chan with + | WEXITED 0 -> () + | WEXITED i -> + failwithf "command exited with non-zero status (%d)" i + | WSIGNALED i | WSTOPPED i -> + failwithf "command signalled or stopped with non-zero status (%d)" i + ); + + (* Build the final skip hash. *) + let skiphash = Hashtbl.copy fileshash in + List.iter (fun filename -> Hashtbl.add skiphash filename ()) skip; + + (* Remove the files. *) + List.iter ( + fun filename -> + if not (Hashtbl.mem skiphash filename) then ( + unlink filename; + printf "deleted %s\n%!" filename + ) + ) lines + let get_lines_generated () = !lines |