<commit_before>(***********************************************************************)
(*                                                                     *)
(*    Copyright 2012 OCamlPro                                          *)
(*    Copyright 2012 INRIA                                             *)
(*                                                                     *)
(*  All rights reserved.  This file is distributed under the terms of  *)
(*  the GNU Public License version 3.0.                                *)
(*                                                                     *)
(*  OPAM is distributed in the hope that it will be useful,            *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(*  GNU General Public License for more details.                       *)
(*                                                                     *)
(***********************************************************************)

open OpamTypes
open OpamMisc.OP
open OpamFilename.OP

let log fmt = OpamGlobals.log "REPOSITORY" fmt

let compare r1 r2 =
  match compare r2.repo_priority r1.repo_priority with
  | 0 -> compare r2.repo_name r1.repo_name
  | x -> x

let to_string r =
  Printf.sprintf "%s(%d %s %s)"
    (OpamRepositoryName.to_string r.repo_name)
    r.repo_priority
    (OpamTypes.string_of_repository_kind r.repo_kind)
    (OpamFilename.Dir.to_string r.repo_address)

let default_address =
  OpamFilename.raw_dir OpamGlobals.default_repository_address

let default = {
  repo_name     = OpamRepositoryName.default;
  repo_kind     = `http;
  repo_address  = default_address;
  repo_priority = 0;
}

let repository_address address =
  if Sys.file_exists address
  then OpamFilename.Dir.of_string address
  else OpamFilename.raw_dir address

module O = struct
  type tmp = repository
  type t = tmp
  let compare = compare
  let to_string = to_string
end

module Set = OpamMisc.Set.Make(O)

module Map = OpamMisc.Map.Make(O)

module type BACKEND = sig
  val init: address:dirname -> unit
  val update: address:dirname -> OpamFilename.Set.t
  val download_archive: address:dirname -> package -> filename download
  val download_file: ?checksum:string -> package -> filename -> filename download
  val download_dir: package -> ?dst:dirname -> dirname -> dirname download
  val upload_dir: address:dirname -> dirname -> OpamFilename.Set.t
end

exception Unknown_backend

let backends = Hashtbl.create 8

let find_backend r =
  try
    Hashtbl.find backends r.repo_kind
  with Not_found -> raise Unknown_backend

let find_backend_by_kind k =
  try
    Hashtbl.find backends k
  with Not_found -> raise Unknown_backend

let register_backend name backend =
  Hashtbl.replace backends name backend

let local_repo () =
  OpamFilename.cwd ()

let repo r =
  OpamPath.Repository.create (OpamPath.default ()) r.repo_name

(* initialize the current directory *)
let init r =
  log "init %s" (to_string r);
  let module B = (val find_backend r: BACKEND) in
  let open OpamPath.Repository in
  let repo = repo r in
  OpamFilename.mkdir (root repo);<commit_msg>Clean-up left-over repositories when creating new ones with the same names.<commit_after>(***********************************************************************)
(*                                                                     *)
(*    Copyright 2012 OCamlPro                                          *)
(*    Copyright 2012 INRIA                                             *)
(*                                                                     *)
(*  All rights reserved.  This file is distributed under the terms of  *)
(*  the GNU Public License version 3.0.                                *)
(*                                                                     *)
(*  OPAM is distributed in the hope that it will be useful,            *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(*  GNU General Public License for more details.                       *)
(*                                                                     *)
(***********************************************************************)

open OpamTypes
open OpamMisc.OP
open OpamFilename.OP

let log fmt = OpamGlobals.log "REPOSITORY" fmt

let compare r1 r2 =
  match compare r2.repo_priority r1.repo_priority with
  | 0 -> compare r2.repo_name r1.repo_name
  | x -> x

let to_string r =
  Printf.sprintf "%s(%d %s %s)"
    (OpamRepositoryName.to_string r.repo_name)
    r.repo_priority
    (OpamTypes.string_of_repository_kind r.repo_kind)
    (OpamFilename.Dir.to_string r.repo_address)

let default_address =
  OpamFilename.raw_dir OpamGlobals.default_repository_address

let default = {
  repo_name     = OpamRepositoryName.default;
  repo_kind     = `http;
  repo_address  = default_address;
  repo_priority = 0;
}

let repository_address address =
  if Sys.file_exists address
  then OpamFilename.Dir.of_string address
  else OpamFilename.raw_dir address

module O = struct
  type tmp = repository
  type t = tmp
  let compare = compare
  let to_string = to_string
end

module Set = OpamMisc.Set.Make(O)

module Map = OpamMisc.Map.Make(O)

module type BACKEND = sig
  val init: address:dirname -> unit
  val update: address:dirname -> OpamFilename.Set.t
  val download_archive: address:dirname -> package -> filename download
  val download_file: ?checksum:string -> package -> filename -> filename download
  val download_dir: package -> ?dst:dirname -> dirname -> dirname download
  val upload_dir: address:dirname -> dirname -> OpamFilename.Set.t
end

exception Unknown_backend

let backends = Hashtbl.create 8

let find_backend r =
  try
    Hashtbl.find backends r.repo_kind
  with Not_found -> raise Unknown_backend

let find_backend_by_kind k =
  try
    Hashtbl.find backends k
  with Not_found -> raise Unknown_backend

let register_backend name backend =
  Hashtbl.replace backends name backend

let local_repo () =
  OpamFilename.cwd ()

let repo r =
  OpamPath.Repository.create (OpamPath.default ()) r.repo_name

(* initialize the current directory *)
let init r =
  log "init %s" (to_string r);
  let module B = (val find_backend r: BACKEND) in
  let open OpamPath.Repository in
  let repo = repo r in
  let repodir = root repo in
  OpamFilename.rmdir repodir;
  OpamFilename.mkdir repodir;
{'fraction_non_alphanumeric': 0.11575860472295108, 'fraction_numerical': 0.005865102639296188, 'mean_word_length': 2.296032553407935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1176983', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1730, 'n_words': 545}