diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2015-09-24 08:08:23 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2015-09-24 08:08:23 +0200 |
commit | 04ea94103db92d6d27230794e14606547aacf670 (patch) | |
tree | e6731caab1d64375e52525fd2dec8ae33cfb50d0 | |
parent | 69c333f31373177f6809ef4532f9c0c73a1a8148 (diff) |
Factor and reuse manifest fetching code
-rw-r--r-- | bpkg/buildfile | 3 | ||||
-rw-r--r-- | bpkg/fetch | 22 | ||||
-rw-r--r-- | bpkg/fetch.cxx | 85 | ||||
-rw-r--r-- | bpkg/rep-create.cxx | 31 | ||||
-rw-r--r-- | bpkg/rep-fetch.cxx | 60 |
5 files changed, 116 insertions, 85 deletions
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 <bpkg/manifest> + +#include <bpkg/types> +#include <bpkg/utility> + +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 <bpkg/fetch> + +#include <fstream> + +#include <butl/process> +#include <butl/fdstream> +#include <butl/filesystem> + +#include <bpkg/manifest-parser> + +#include <bpkg/diagnostics> + +using namespace std; +using namespace butl; + +namespace bpkg +{ + template <typename M> + 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<repository_manifests> (d / repositories); + } + + repository_manifests + fetch_repositories (const repository_location& rl) + { + assert (/*rl.remote () ||*/ rl.absolute ()); + + return rl.remote () + ? repository_manifests () + : fetch_file<repository_manifests> (rl.path () / repositories); + } + + static const path packages ("packages"); + + package_manifests + fetch_packages (const dir_path& d) + { + return fetch_file<package_manifests> (d / packages); + } + + package_manifests + fetch_packages (const repository_location& rl) + { + assert (/*rl.remote () ||*/ rl.absolute ()); + + return rl.remote () + ? package_manifests () + : fetch_file<package_manifests> (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 <butl/filesystem> // dir_iterator #include <bpkg/manifest> -#include <bpkg/manifest-parser> #include <bpkg/manifest-serializer> +#include <bpkg/fetch> #include <bpkg/types> #include <bpkg/utility> #include <bpkg/diagnostics> @@ -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 <stdexcept> #include <bpkg/manifest> -#include <bpkg/manifest-parser> +#include <bpkg/fetch> #include <bpkg/types> #include <bpkg/package> #include <bpkg/package-odb> @@ -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. |