Browse Source

Drop sdist hashing. We'll come back to that someday, but YAGNI for now

default_compile_flags
vector-of-bool 5 years ago
parent
commit
1c0044d482
3 changed files with 13 additions and 56 deletions
  1. +2
    -3
      src/dds/dds.main.cpp
  2. +9
    -44
      src/dds/sdist.cpp
  3. +2
    -9
      src/dds/sdist.hpp

+ 2
- 3
src/dds/dds.main.cpp View File

for (const auto& [name, grp] : grp_by_name) { for (const auto& [name, grp] : grp_by_name) {
spdlog::info("{}:", name); spdlog::info("{}:", name);
for (const dds::sdist& sd : grp) { for (const dds::sdist& sd : grp) {
spdlog::info(" - {} [{}]",
sd.manifest.version.to_string(),
sd.md5_string());
spdlog::info(" - {}",
sd.manifest.version.to_string());
} }
} }



+ 9
- 44
src/dds/sdist.cpp View File



#include <libman/parse.hpp> #include <libman/parse.hpp>


#include <browns/md5.hpp>
#include <browns/output.hpp>
#include <neo/buffer.hpp>

#include <range/v3/algorithm/sort.hpp> #include <range/v3/algorithm/sort.hpp>
#include <range/v3/view/filter.hpp> #include <range/v3/view/filter.hpp>




namespace { namespace {


void hash_file(std::string_view prefix, path_ref fpath, browns::md5& hash) {
hash.feed(neo::buffer(prefix));
std::ifstream infile;
infile.exceptions(std::ios::failbit | std::ios::badbit);
using file_iter = std::istreambuf_iterator<char>;
infile.open(fpath, std::ios::binary);
assert(infile.good());
hash.feed(neo::buffer("\nfile_content: "));
hash.feed(file_iter(infile), file_iter());
}

void sdist_export_file(path_ref out_root, path_ref in_root, path_ref filepath, browns::md5& hash) {
void sdist_export_file(path_ref out_root, path_ref in_root, path_ref filepath) {
auto relpath = fs::relative(filepath, in_root); auto relpath = fs::relative(filepath, in_root);
spdlog::info("Export file {}", relpath.string()); spdlog::info("Export file {}", relpath.string());
hash_file("filepath:" + relpath.generic_string(), filepath, hash);
auto dest = out_root / relpath; auto dest = out_root / relpath;
fs::create_directories(dest.parent_path()); fs::create_directories(dest.parent_path());
fs::copy(filepath, dest); fs::copy(filepath, dest);
} }


void sdist_copy_library(path_ref out_root,
const library& lib,
const sdist_params& params,
browns::md5& hash) {
void sdist_copy_library(path_ref out_root, const library& lib, const sdist_params& params) {
auto sources_to_keep = // auto sources_to_keep = //
lib.all_sources() // lib.all_sources() //
| ranges::views::filter([&](const source_file& sf) { | ranges::views::filter([&](const source_file& sf) {
"Each library in a source distribution requires a library manifest (Expected [{}])", "Each library in a source distribution requires a library manifest (Expected [{}])",
lib_dds_path.string())); lib_dds_path.string()));
} }
sdist_export_file(out_root, params.project_dir, lib_dds_path, hash);
sdist_export_file(out_root, params.project_dir, lib_dds_path);


spdlog::info("sdist: Export library from {}", lib.path().string()); spdlog::info("sdist: Export library from {}", lib.path().string());
fs::create_directories(out_root); fs::create_directories(out_root);
for (const auto& source : sources_to_keep) { for (const auto& source : sources_to_keep) {
sdist_export_file(out_root, params.project_dir, source.path, hash);
sdist_export_file(out_root, params.project_dir, source.path);
} }
} }


sdist dds::create_sdist_in_dir(path_ref out, const sdist_params& params) { sdist dds::create_sdist_in_dir(path_ref out, const sdist_params& params) {
auto libs = collect_libraries(params.project_dir); auto libs = collect_libraries(params.project_dir);


browns::md5 md5;

for (const library& lib : libs) { for (const library& lib : libs) {
sdist_copy_library(out, lib, params, md5);
sdist_copy_library(out, lib, params);
} }


auto man_path = params.project_dir / "package.dds"; auto man_path = params.project_dir / "package.dds";
throw std::runtime_error(fmt::format( throw std::runtime_error(fmt::format(
"Creating a source distribution requires a package.dds file for the project")); "Creating a source distribution requires a package.dds file for the project"));
} }
sdist_export_file(out, params.project_dir, man_path, md5);
sdist_export_file(out, params.project_dir, man_path);
auto pkg_man = package_manifest::load_from_file(man_path); auto pkg_man = package_manifest::load_from_file(man_path);


md5.pad();
auto hash_str = browns::format_digest(md5.digest());
spdlog::info("Generated export as {}-{}", pkg_man.name, hash_str);

std::vector<lm::pair> pairs;
pairs.emplace_back("MD5-Hash", hash_str);
lm::write_pairs(out / "_sdist.dds", pairs);
spdlog::info("Generated export as {}_{}", pkg_man.name, pkg_man.version.to_string());


return sdist::from_directory(out); return sdist::from_directory(out);
} }


sdist sdist::from_directory(path_ref where) { sdist sdist::from_directory(path_ref where) {
auto pkg_man = package_manifest::load_from_file(where / "package.dds");
auto meta_pairs = lm::parse_file(where / "_sdist.dds");
std::string hash_str;
lm::read(fmt::format("Loading source distribution manifest from {}/_sdist.dds", where.string()),
meta_pairs,
lm::read_required("MD5-Hash", hash_str),
lm::reject_unknown());
return sdist{std::move(pkg_man),
browns::parse_digest<browns::md5::digest_type>(hash_str),
where};
auto pkg_man = package_manifest::load_from_file(where / "package.dds");
return sdist{std::move(pkg_man), where};
} }

+ 2
- 9
src/dds/sdist.hpp View File

#include <dds/package_manifest.hpp> #include <dds/package_manifest.hpp>
#include <dds/util/fs.hpp> #include <dds/util/fs.hpp>


#include <browns/md5.hpp>
#include <browns/output.hpp>

namespace dds { namespace dds {


struct sdist_params { struct sdist_params {


struct sdist { struct sdist {
package_manifest manifest; package_manifest manifest;
browns::md5::digest_type md5;
fs::path path; fs::path path;


sdist(package_manifest man, browns::md5::digest_type hash, path_ref path_)
sdist(package_manifest man, path_ref path_)
: manifest(std::move(man)) : manifest(std::move(man))
, md5(hash)
, path(path_) {} , path(path_) {}


static sdist from_directory(path_ref p); static sdist from_directory(path_ref p);


std::string md5_string() const noexcept { return browns::format_digest(md5); }

std::string ident() const noexcept { std::string ident() const noexcept {
return manifest.name + "." + manifest.version.to_string() + "." + md5_string();
return manifest.name + "_" + manifest.version.to_string();
} }
}; };



Loading…
Cancel
Save