Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

layout.rs 3.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  1. //! Utilities for working with the directory tree layout.
  2. use std::collections::HashMap;
  3. use std::path::{Path, PathBuf};
  4. use std::io;
  5. use std::fs::read_to_string;
  6. use std::env::current_dir;
  7. use text_template::{Template, Piece};
  8. use anyhow::{Result, anyhow};
  9. use log::*;
  10. /// Configuration data.
  11. pub struct Config {
  12. repr: toml::Value,
  13. }
  14. impl Config {
  15. pub fn load() -> Result<Config> {
  16. let mut path = find_path_root()?;
  17. path.push("config.toml");
  18. debug!("reading config from {:?}", path);
  19. let text = read_to_string(&path)?;
  20. Ok(Config {
  21. repr: toml::de::from_str(&text)?
  22. })
  23. }
  24. /// Lookup a configuration option by dotted path.
  25. pub fn lookup_str<'a>(&'a self, path: &str) -> Option<&'a str> {
  26. let parts = path.split(".");
  27. let mut node = &self.repr;
  28. for part in parts {
  29. if let Some(n) = node.get(part) {
  30. node = n;
  31. } else {
  32. return None
  33. }
  34. }
  35. node.as_str()
  36. }
  37. /// Interpolate a string.
  38. pub fn interpolate<'a, 'b: 'a>(&'a self, text: &'b str) -> String {
  39. let tmpl = Template::from(text);
  40. let mut vars = HashMap::new();
  41. for piece in tmpl.iter() {
  42. match piece {
  43. Piece::Placeholder { name, .. } => {
  44. vars.insert(*name, self.lookup_str(name).unwrap_or_default());
  45. },
  46. _ => (),
  47. }
  48. }
  49. tmpl.fill_in(&vars).to_string()
  50. }
  51. }
  52. /// Check the TOML manifest to see if we're bookdata.
  53. fn check_project_manifest(path: &Path) -> Result<bool> {
  54. let mut path = path.to_path_buf();
  55. path.push("Cargo.toml");
  56. let manifest = read_to_string(&path);
  57. match manifest {
  58. Ok(txt) => {
  59. let val: toml::Value = toml::de::from_str(&txt)?;
  60. if let Some(pkg) = val.get("package") {
  61. if let Some(name) = pkg.get("name") {
  62. if name.as_str().unwrap_or_default() == "bookdata" {
  63. Ok(true)
  64. } else {
  65. error!("Cargo.toml has package name {}", name);
  66. Ok(false)
  67. }
  68. } else {
  69. error!("Cargo.toml has no name");
  70. Ok(false)
  71. }
  72. } else {
  73. error!("Cargo.toml has no package section");
  74. Ok(false)
  75. }
  76. },
  77. Err(e) if e.kind() == io::ErrorKind::NotFound => {
  78. error!("Cargo.toml not found alongside .dvc");
  79. Ok(false)
  80. },
  81. Err(e) => Err(e.into()),
  82. }
  83. }
  84. fn is_bookdata_root(path: &Path) -> Result<bool> {
  85. let mut dvc = path.to_path_buf();
  86. dvc.push(".dvc");
  87. if dvc.try_exists()? {
  88. debug!("found DVC path at {}", dvc.display());
  89. if check_project_manifest(path)? {
  90. Ok(true)
  91. } else {
  92. error!("found .dvc but not bookdata, running from a weird directory?");
  93. Err(anyhow!("bookdata not found"))
  94. }
  95. } else {
  96. Ok(false)
  97. }
  98. }
  99. /// Find the root path for the repository.
  100. pub fn find_path_root() -> Result<PathBuf> {
  101. let mut path = current_dir()?;
  102. debug!("working directory: {}", path.display());
  103. loop {
  104. trace!("looking for DVC in {}", path.display());
  105. if is_bookdata_root(&path)? {
  106. info!("found bookdata root at {}", path.display());
  107. return Ok(path);
  108. }
  109. if !path.pop() {
  110. error!("scanned all parents and could not find bookdata root");
  111. return Err(anyhow!("bookdata not found"));
  112. }
  113. }
  114. }
  115. /// Require that we are in the root directory.
  116. pub fn require_working_root() -> Result<()> {
  117. let cwd = current_dir()?;
  118. if is_bookdata_root(&cwd)? {
  119. Ok(())
  120. } else {
  121. error!("working directory is not bookdata root");
  122. Err(anyhow!("incorrect working directory"))
  123. }
  124. }
  125. /// Expect that we are in a particular subdirectory.
  126. pub fn require_working_dir<P: AsRef<Path>>(path: P) -> Result<()> {
  127. let path = path.as_ref();
  128. let cwd = current_dir()?;
  129. if !cwd.ends_with(path) {
  130. error!("expected to be run in {:?}", path);
  131. Err(anyhow!("incorrect working directory"))
  132. } else {
  133. Ok(())
  134. }
  135. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...