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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! Interfaces for retrieving packages (and information about them) from different sources.
//!
//! Packages can originate from several sources, which complicates getting metadata about them.
//! This module is responsible for smoothing over that process, as well as coordinating the actual
//! retrieval of packages from various different sources (hopefully in parallel).

pub mod cache;

pub use self::cache::{Cache, Source};
use console::style;
use failure::{Error, ResultExt};
use index::Indices;
use indexmap::IndexMap;
use package::{
    resolution::{DirectRes, IndexRes, Resolution},
    version::{Constraint, Interval, Range, Relation},
    PackageId, Summary,
};
use resolve::incompat::{Incompatibility, IncompatibilityCause};
use semver::Version;
use slog::Logger;
use util::errors::{ErrorKind, Res};
use util::graph::Graph;

// TODO: Patching
/// Retrieves the best packages using both the indices available and a lockfile.
/// By default, prioritizes using a lockfile.
#[derive(Debug)]
pub struct Retriever<'cache> {
    /// The local cache of packages.
    cache: &'cache Cache,
    root: Summary,
    root_deps: Vec<(PackageId, Constraint)>,
    indices: Indices,
    lockfile: Graph<Summary>,
    pub logger: Logger,
    pub def_index: IndexRes,
    sources: IndexMap<PackageId, IndexMap<DirectRes, Source>>,
}

impl<'cache> Retriever<'cache> {
    pub fn new(
        plog: &Logger,
        cache: &'cache Cache,
        root: Summary,
        root_deps: Vec<(PackageId, Constraint)>,
        indices: Indices,
        lockfile: Graph<Summary>,
        def_index: IndexRes,
    ) -> Self {
        let logger = plog.new(o!("root" => root.to_string()));

        Retriever {
            cache,
            root,
            root_deps,
            indices,
            lockfile,
            logger,
            def_index,
            sources: indexmap!(),
        }
    }

    /// Loads all of the packages selected in a Solve into the Cache, returning a new graph of all
    /// the Sources.
    ///
    /// This downloads all the packages into the cache. If we wanted to parallelize downloads
    /// later, this is where we'd deal with all the Tokio stuff.
    pub fn retrieve_packages(&mut self, solve: &Graph<Summary>) -> Res<Graph<Source>> {
        // let mut prg = 0;
        // Until pb.println gets added, we can't use progress bars
        // let pb = ProgressBar::new(solve.inner.raw_nodes().len() as u64);
        // pb.set_style(ProgressStyle::default_bar().template("  [-->] {bar} {pos}/{len}"));

        let sources = solve.map(
            |_, sum| {
                println!("{:>7} {}", style("[rtv]").blue(), sum.to_string());

                let loc = match sum.resolution() {
                    Resolution::Direct(direct) => direct,
                    Resolution::Index(_) => &self.indices.select(sum).unwrap().location,
                };

                if let Some(s) = self.sources.get_mut(sum.id()).and_then(|x| x.remove(loc)) {
                    // prg += 1;
                    // pb.set_position(prg);
                    Ok(s)
                } else {
                    let source = self
                        .cache
                        .checkout_source(sum.id(), loc)
                        .context(format_err!("unable to retrieve package {}", sum))?;
                    // prg += 1;
                    // pb.set_position(prg);
                    Ok(source)
                }
            },
            |_| Ok(()),
        )?;

        // pb.finish_and_clear();
        println!(
            "{:>7} Packages cached in {}",
            style("[inf]").dim(),
            self.cache.layout.src.display()
        );

        Ok(sources)
    }

    /// Chooses the best version of a package given a constraint.
    pub fn best(
        &mut self,
        pkg: &PackageId,
        con: &Constraint,
        minimize: bool,
    ) -> Result<Version, Error> {
        // With stuff from lockfiles, we try to retrieve whatever version was specified in the
        // lockfile. However, if it fails, we don't want to error out; we want to try to find
        // the best version we can otherwise.
        let pkg_version = self
            .lockfile
            .find_by(|sum| sum.id == *pkg)
            .map(|meta| &meta.version);
        if let Some(v) = pkg_version {
            if con.satisfies(&v) {
                let dir = if let Resolution::Direct(loc) = pkg.resolution() {
                    Some(loc)
                } else {
                    self.indices
                        .select(&Summary::new(pkg.clone(), v.clone()))
                        .map(|e| &e.location)
                        .ok()
                };

                if let Some(dir) = dir {
                    let dir = dir.clone();
                    if let Ok(src) = self.direct_checkout(pkg, &dir) {
                        return Ok(src.meta().version().clone());
                    }
                }
            }
        }

        if let Resolution::Direct(loc) = pkg.resolution() {
            return Ok(self.direct_checkout(pkg, loc)?.meta().version().clone());
        }

        let (mut pre, mut not_pre): (Vec<Version>, Vec<Version>) = self
            .indices
            .entries(pkg)?
            .clone()
            .into_iter()
            .map(|v| v.0)
            .filter(|v| con.satisfies(v))
            .partition(|v| v.is_prerelease());

        if !not_pre.is_empty() {
            if !minimize {
                Ok(not_pre.pop().unwrap())
            } else {
                Ok(not_pre.remove(0))
            }
        } else if !pre.is_empty() {
            if !minimize {
                Ok(pre.pop().unwrap())
            } else {
                Ok(pre.remove(0))
            }
        } else {
            Err(Error::from(ErrorKind::PackageNotFound))
        }
    }

    /// Returns a `Vec<Incompatibility>` corresponding to the package's dependencies.
    pub fn incompats(&mut self, pkg: &Summary) -> Result<Vec<Incompatibility>, Error> {
        if pkg == &self.root {
            let mut res = vec![];
            for dep in &self.root_deps {
                res.push(Incompatibility::from_dep(
                    pkg.clone(),
                    (dep.0.clone(), dep.1.complement()),
                ));
            }
            return Ok(res);
        }

        let def_index = self.def_index.clone();

        // If this is a DirectRes dep, we ask the cache for info.
        if let Resolution::Direct(loc) = pkg.resolution() {
            let deps = self
                .direct_checkout(pkg.id(), loc)?
                .meta()
                .deps(&def_index, false);
            let mut res = vec![];
            for dep in deps {
                res.push(Incompatibility::from_dep(
                    pkg.clone(),
                    (dep.0.clone(), dep.1.complement()),
                ));
            }
            return Ok(res);
        }

        let entries = self.indices.entries(pkg.id())?;

        let l = entries.len();

        let (ix, ver, start_deps) = entries
            .get_full(pkg.version())
            .map(|x| (x.0, x.1, &x.2.dependencies))
            .ok_or_else(|| ErrorKind::PackageNotFound)?;
        let mut res = vec![];

        for dep in start_deps {
            let mut lix = ix;
            let mut lower = ver;
            let mut rix = ix;
            let mut upper = ver;

            while lix > 0 {
                lix -= 1;
                let new = entries.get_index(lix).unwrap();
                let new_deps = &new.1.dependencies;
                let mut seen = false;
                for new_dep in new_deps {
                    if dep.name == new_dep.name && dep.index == new_dep.index {
                        let rel = dep.req.relation(&new_dep.req);
                        if rel == Relation::Equal || rel == Relation::Superset {
                            seen = true;
                            lower = &new.0;
                        } else {
                            seen = false;
                        }
                    }
                }
                if !seen {
                    lix += 1;
                    break;
                }
            }

            while rix < l - 1 {
                rix += 1;
                let new = entries.get_index(rix).unwrap();
                let new_deps = &new.1.dependencies;
                let mut seen = false;
                for new_dep in new_deps {
                    if dep.name == new_dep.name && dep.index == new_dep.index {
                        let rel = dep.req.relation(&new_dep.req);
                        if rel == Relation::Equal || rel == Relation::Superset {
                            seen = true;
                            upper = &new.0;
                        } else {
                            seen = false;
                        }
                    }
                }
                if !seen {
                    rix -= 1;
                    break;
                }
            }

            let nl = if lix == 0 && rix == l - 1 {
                Interval::Unbounded
            } else {
                Interval::Closed(lower.clone(), false)
            };

            let nu = if lix == 0 && rix == l - 1 {
                Interval::Unbounded
            } else {
                Interval::Closed(upper.clone(), false)
            };

            let dep_pkg = PackageId::new(dep.name.clone(), dep.index.clone().into());

            let cs = indexmap!(
                pkg.id().clone() => Range::new(nl, nu).unwrap().into(),
                dep_pkg => dep.req.complement(),
            );

            res.push(Incompatibility::new(cs, IncompatibilityCause::Dependency))
        }

        Ok(res)
    }

    pub fn count_versions(&self, pkg: &PackageId) -> usize {
        self.indices.count_versions(pkg)
    }

    pub fn root(&self) -> &Summary {
        &self.root
    }

    pub fn direct_checkout(&mut self, pkg: &PackageId, loc: &DirectRes) -> Res<&Source> {
        let reses = self
            .sources
            .entry(pkg.clone())
            .or_insert_with(IndexMap::new);
        if reses.contains_key(loc) {
            Ok(&reses[loc])
        } else {
            let s = self.cache.checkout_source(&pkg, &loc)?;

            reses.insert(loc.clone(), s);
            Ok(&reses[loc])
        }
    }
}