CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Remove_impl.hh
Go to the documentation of this file.
1 #ifndef CASM_DB_Remove_impl
2 #define CASM_DB_Remove_impl
3 
4 #include <boost/filesystem/fstream.hpp>
5 
7 #include "casm/app/import.hh"
8 #include "casm/app/rm.hh"
13 #include "casm/database/Remove.hh"
15 
16 namespace CASM {
17 namespace DB {
18 
19 // --- RemoveT ---
20 
21 template <typename _ConfigType>
22 RemoveT<_ConfigType>::RemoveT(const PrimClex &primclex, std::string report_dir)
23  : ConfigData(primclex, TypeTag<ConfigType>()), m_report_dir(report_dir) {}
24 
26 template <typename _ConfigType>
28  bool dry_run) {
29  std::vector<std::string> fail;
30  for (const auto &val : selection.data()) {
31  if (!has_existing_data_or_files(val.first)) {
32  db_config<ConfigType>().erase(val.first);
33  } else {
34  log() << "skipping " << val.first << ": has existing data or files"
35  << std::endl;
36  fail.push_back(val.first);
37  }
38  }
39 
40  if (fail.size()) {
41  _erase_report(fail);
42  log() << "Skipped " << fail.size() << " " << traits<ConfigType>::name
43  << std::endl;
44  log() << " See " << fs::path(m_report_dir) / "remove_fail" << std::endl;
45  }
46  db_config<ConfigType>().commit();
47 }
48 
50 template <typename _ConfigType>
52  const DB::Selection<ConfigType> &selection, bool dry_run) {
53  // erase data
54  for (const auto &val : selection.data()) {
55  for (auto it = db_props().find_via_to(val.first); it != db_props().end();
56  it = db_props().find_via_to(val.first)) {
57  db_props().erase(it);
58  }
59  auto it = db_props().find_via_origin(
60  CASM::calc_properties_path(this->primclex(), val.first));
61  if (it != db_props().end()) {
62  db_props().erase(it);
63  }
64  rm_files(val.first, dry_run);
65  }
66  if (!dry_run) {
67  db_props().commit();
68  }
69 }
70 
76 template <typename _ConfigType>
78  bool dry_run) {
79  // erase data
80  erase_data(selection, dry_run);
81 
82  // erase configs
83  erase(selection, dry_run);
84 }
85 
86 template <typename _ConfigType>
87 void RemoveT<_ConfigType>::_erase_report(const std::vector<std::string> &fail) {
88  std::string prefix{"remove_"};
90  fs::ofstream file(fs::path(m_report_dir) / (prefix + "_fail"));
91  for (const auto &val : fail) {
92  file << val << std::endl;
93  }
94  file.close();
95 }
96 
97 // --- Remove<Configuration> ---
98 
99 template <typename ConfigType>
100 Remove<ConfigType>::Remove(const PrimClex &primclex, std::string report_dir)
101  : RemoveT<ConfigType>(primclex, report_dir) {}
102 
103 template <typename ConfigType>
105  std::string res =
106  "Remove enumerated configurations and calculation results: \n\n"
107 
108  " 'casm remove --type " +
110  "' options: \n\n"
111 
112  " - Configurations to be erased can be specified with the --names and \n"
113  " --selection options.\n"
114  " - Use without additional options to only remove enumerated "
115  "configurations\n"
116  " that do not have any associated files or data.\n"
117  " - Use --data (-d) to remove data only, not enumerated configurations. "
118  "\n"
119  " - Use --force (-f) to remove data and enumerated configurations. \n"
120  " - Use --dry-run (-n) to do a \"dry-run\". \n\n"
121 
122  " After removing a configuration it may be re-enumerated but will have "
123  "a new\n"
124  " index because indices will not be repeated.\n\n";
125 
126  return res;
127 }
128 
129 template <typename ConfigType>
131  const Completer::RmOption &opt) {
132  // -- read selection --
134 
135  // Add command-line options
136  for (const auto &name : opt.name_strs()) {
137  if (primclex.db<ConfigType>().count(name)) {
138  selection.data()[name] = true;
139  } else {
140  std::stringstream msg;
141  msg << "Invalid Configuration name: " << name;
142  throw CASM::runtime_error(msg.str(), ERR_INVALID_ARG);
143  }
144  }
145 
146  // get remove report_dir, check if exists, and create new report_dir.i if
147  // necessary
148  std::string report_dir =
149  (primclex.dir().root_dir() / "remove_report").string();
150  report_dir = create_report_dir(report_dir);
151 
152  // -- erase --
153  Remove<ConfigType> f(primclex, report_dir);
154 
155  if (opt.force()) {
156  f.erase_all(selection, opt.dry_run());
157  } else if (opt.data()) {
158  f.erase_data(selection, opt.dry_run());
159  } else {
160  f.erase(selection, opt.dry_run());
161  }
162  return 0;
163 }
164 
165 } // namespace DB
166 } // namespace CASM
167 
168 #endif
#define ERR_INVALID_ARG
Definition: errors.hh:7
const fs::path & selection_path() const
Returns the string corresponding to add_config_suboption()
Definition: Handlers.cc:338
const std::vector< std::string > & name_strs() const
Definition: Handlers.cc:721
bool data() const
Definition: rm.cc:21
bool force() const
Definition: rm.cc:19
static std::string desc()
Definition: Remove_impl.hh:104
Remove(const PrimClex &primclex, std::string report_dir)
Definition: Remove_impl.hh:100
static int run(const PrimClex &, const Completer::RmOption &opt)
Definition: Remove_impl.hh:130
Generic ConfigType-dependent part of Remove.
Definition: Remove.hh:27
void erase(const DB::Selection< ConfigType > &selection, bool dry_run)
Erase Configurations that have no data.
Definition: Remove_impl.hh:27
void erase_data(const DB::Selection< ConfigType > &selection, bool dry_run)
Erase data and files (permanently), but not Configuration.
Definition: Remove_impl.hh:51
_ConfigType ConfigType
Definition: Remove.hh:29
void _erase_report(const std::vector< std::string > &fail)
Definition: Remove_impl.hh:87
void erase_all(const DB::Selection< ConfigType > &selection, bool dry_run)
Removes Configurations and data and files (permanently)
Definition: Remove_impl.hh:77
RemoveT(const PrimClex &primclex, std::string report_dir)
Definition: Remove_impl.hh:22
map_type & data()
Definition: Selection.cc:250
PrimClex is the top-level data structure for a CASM project.
Definition: PrimClex.hh:55
std::string create_report_dir(std::string report_dir)
Create a new report directory to avoid overwriting existing results.
Definition: ConfigData.cc:58
Main CASM namespace.
Definition: APICommand.hh:8
std::string calc_properties_path(const ConfigType &config, std::string calctype="")
Definition: Calculable.cc:197
Log & log()
Definition: Log.hh:424
GenericDatumFormatter< std::string, DataObject > name()
PrimClex * primclex
Definition: settings.cc:135