From 04ea94103db92d6d27230794e14606547aacf670 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 24 Sep 2015 08:08:23 +0200 Subject: Factor and reuse manifest fetching code --- bpkg/buildfile | 3 +- bpkg/fetch | 22 ++++++++++++++ bpkg/fetch.cxx | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ bpkg/rep-create.cxx | 31 ++----------------- bpkg/rep-fetch.cxx | 60 +++---------------------------------- 5 files changed, 116 insertions(+), 85 deletions(-) create mode 100644 bpkg/fetch create mode 100644 bpkg/fetch.cxx (limited to 'bpkg') diff --git a/bpkg/buildfile b/bpkg/buildfile index 4e5bd23..e57ec5e 100644 --- a/bpkg/buildfile +++ b/bpkg/buildfile @@ -9,7 +9,8 @@ import libs += libbutl%lib{butl} import libs += libodb%lib{odb} import libs += libodb-sqlite%lib{odb-sqlite} -exe{bpkg}: cxx{package package-odb database diagnostics utility} \ +exe{bpkg}: cxx{fetch package package-odb database diagnostics \ + utility} \ cli.cxx{common-options} cxx{types-parsers} \ cxx{bpkg} cli.cxx{bpkg-options} \ cxx{help} cli.cxx{help-options} \ diff --git a/bpkg/fetch b/bpkg/fetch new file mode 100644 index 0000000..5290107 --- /dev/null +++ b/bpkg/fetch @@ -0,0 +1,22 @@ +// file : bpkg/fetch -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BPKG_FETCH +#define BPKG_FETCH + +#include + +#include +#include + +namespace bpkg +{ + repository_manifests fetch_repositories (const dir_path&); + repository_manifests fetch_repositories (const repository_location&); + + package_manifests fetch_packages (const dir_path&); + package_manifests fetch_packages (const repository_location&); +} + +#endif // BPKG_FETCH diff --git a/bpkg/fetch.cxx b/bpkg/fetch.cxx new file mode 100644 index 0000000..34445a8 --- /dev/null +++ b/bpkg/fetch.cxx @@ -0,0 +1,85 @@ +// file : bpkg/fetch.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include + +#include + +#include +#include +#include + +#include + +#include + +using namespace std; +using namespace butl; + +namespace bpkg +{ + template + static M + fetch_file (const path& f) + { + if (!exists (f)) + fail << "file " << f << " does not exist"; + + try + { + ifstream ifs; + ifs.exceptions (ofstream::badbit | ofstream::failbit); + ifs.open (f.string ()); + + manifest_parser mp (ifs, f.string ()); + return M (mp); + } + catch (const manifest_parsing& e) + { + error (e.name, e.line, e.column) << e.description; + } + catch (const ifstream::failure&) + { + error << "unable to read from " << f; + } + + throw failed (); + } + + static const path repositories ("repositories"); + + repository_manifests + fetch_repositories (const dir_path& d) + { + return fetch_file (d / repositories); + } + + repository_manifests + fetch_repositories (const repository_location& rl) + { + assert (/*rl.remote () ||*/ rl.absolute ()); + + return rl.remote () + ? repository_manifests () + : fetch_file (rl.path () / repositories); + } + + static const path packages ("packages"); + + package_manifests + fetch_packages (const dir_path& d) + { + return fetch_file (d / packages); + } + + package_manifests + fetch_packages (const repository_location& rl) + { + assert (/*rl.remote () ||*/ rl.absolute ()); + + return rl.remote () + ? package_manifests () + : fetch_file (rl.path () / packages); + } +} diff --git a/bpkg/rep-create.cxx b/bpkg/rep-create.cxx index 6ee0e87..8cb1827 100644 --- a/bpkg/rep-create.cxx +++ b/bpkg/rep-create.cxx @@ -12,9 +12,9 @@ #include // dir_iterator #include -#include #include +#include #include #include #include @@ -143,33 +143,8 @@ namespace bpkg // Load the 'repositories' file to make sure it is there and // is valid. // - // @@ The same code as in rep-fetch. - // @@ Should we check for duplicates? Or should this be done at - // the manifest level? - // - path rf (d / path ("repositories")); - - if (!exists (rf)) - fail << "file " << rf << " does not exist"; - - try - { - ifstream ifs; - ifs.exceptions (ofstream::badbit | ofstream::failbit); - ifs.open (rf.string ()); - - manifest_parser mp (ifs, rf.string ()); - repository_manifests ms (mp); - level4 ([&]{trace << ms.size () - 1 << " prerequisite repository(s)";}); - } - catch (const manifest_parsing& e) - { - fail (e.name, e.line, e.column) << e.description; - } - catch (const ifstream::failure&) - { - fail << "unable to read from " << rf; - } + repository_manifests rms (fetch_repositories (d)); + level4 ([&]{trace << rms.size () - 1 << " prerequisite repository(s)";}); // While we could have serialized as we go along, the order of // packages will be pretty much random and not reproducible. By diff --git a/bpkg/rep-fetch.cxx b/bpkg/rep-fetch.cxx index 7bb5652..89107aa 100644 --- a/bpkg/rep-fetch.cxx +++ b/bpkg/rep-fetch.cxx @@ -8,8 +8,8 @@ #include #include -#include +#include #include #include #include @@ -35,42 +35,14 @@ namespace bpkg const repository_location& rl (r->location); level4 ([&]{trace << r->name () << " " << rl;}); - - assert (rl.absolute () /*|| rl.remote ()*/); + assert (rl.absolute () || rl.remote ()); r->fetched = true; // Mark as being fetched. - //@@ The same code as in rep-create. - // - // Load the 'repositories' file and use it to populate the // prerequisite and complement repository sets. // - repository_manifests rms; - { - path f (rl.path () / path ("repositories")); - - if (!exists (f)) - fail << "file " << f << " does not exist"; - - try - { - ifstream ifs; - ifs.exceptions (ofstream::badbit | ofstream::failbit); - ifs.open (f.string ()); - - manifest_parser mp (ifs, f.string ()); - rms = repository_manifests (mp); - } - catch (const manifest_parsing& e) - { - fail (e.name, e.line, e.column) << e.description; - } - catch (const ifstream::failure&) - { - fail << "unable to read from " << f; - } - } + repository_manifests rms (fetch_repositories (rl)); for (repository_manifest& rm: rms) { @@ -126,31 +98,7 @@ namespace bpkg // @@ We need to check that that 'repositories' file hasn't // changed since. // - package_manifests pms; - { - path f (rl.path () / path ("packages")); - - if (!exists (f)) - fail << "file " << f << " does not exist"; - - try - { - ifstream ifs; - ifs.exceptions (ofstream::badbit | ofstream::failbit); - ifs.open (f.string ()); - - manifest_parser mp (ifs, f.string ()); - pms = package_manifests (mp); - } - catch (const manifest_parsing& e) - { - fail (e.name, e.line, e.column) << e.description; - } - catch (const ifstream::failure&) - { - fail << "unable to read from " << f; - } - } + package_manifests pms (fetch_packages (rl)); // "Suspend" session while persisting packages to reduce memory // consumption. -- cgit v1.1