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
use colored::*;
use regex::Regex;
use structopt::StructOpt;

use crate::error::*;

#[derive(StructOpt)]
#[structopt(
    name = "dmenv",
    about = "Simple and practical virtualenv manager for Python"
)]
pub struct Command {
    #[structopt(long = "python", help = "python binary")]
    pub python_binary: Option<String>,

    #[structopt(long = "project", help = "path to use as the project directory")]
    pub project_path: Option<String>,

    #[structopt(long = "production", help = "Ignore dev dependencies")]
    pub production: bool,

    #[structopt(subcommand)]
    pub sub_cmd: SubCommand,
}

#[derive(StructOpt)]
pub enum SubCommand {
    #[structopt(name = "clean", about = "Clean existing virtualenv")]
    Clean {},

    #[structopt(name = "develop", about = "Run setup.py develop")]
    Develop {},

    #[structopt(name = "install", about = "Install all dependencies")]
    Install {
        #[structopt(long = "--no-develop", help = "Do not run setup.py develop")]
        no_develop: bool,
        #[structopt(
            long = "--system-site-packages",
            help = "Give the virtual environment access to the system site-packages dir"
        )]
        system_site_packages: bool,
    },

    #[structopt(name = "bump-in-lock", about = "Bump a dependency in the lock file")]
    BumpInLock {
        #[structopt(help = "name")]
        name: String,

        #[structopt(long = "--git")]
        git: bool,

        #[structopt(help = "version")]
        version: String,
    },

    #[structopt(name = "init", about = "Initialize a new project")]
    Init {
        #[structopt(help = "Project name")]
        name: String,

        #[structopt(long = "version", help = "Project version", default_value = "0.1.0")]
        version: String,

        #[structopt(long = "author", help = "Author name")]
        author: Option<String>,

        #[structopt(
            long = "no-setup-cfg",
            help = "Keep all code in the `setup.py` file, do not use `setup.cfg`"
        )]
        no_setup_cfg: bool,
    },

    #[structopt(name = "lock", about = "(Re)-generate requirements.lock")]
    Lock {
        #[structopt(
            long = "python-version",
            help = "Restrict Python version",
            parse(try_from_str = "parse_python_version")
        )]
        python_version: Option<String>,

        #[structopt(long = "platform", help = "Restrict platform")]
        sys_platform: Option<String>,

        #[structopt(
            long = "--system-site-packages",
            help = "Give the virtual environment access to the system site-packages dir"
        )]
        system_site_packages: bool,
    },

    #[structopt(name = "run", about = "Run the given binary from the virtualenv")]
    Run {
        #[structopt(
            long = "--no-exec",
            help = "On Unix, fork a new process instead of using exec(). On Windows, this is a no op"
        )]
        no_exec: bool,

        #[structopt(name = "command")]
        cmd: Vec<String>,
    },

    #[structopt(name = "show:deps", about = "Show installed dependencies information")]
    ShowDeps {},

    #[structopt(
        name = "show:outdated",
        about = "Show outdated dependencies information"
    )]
    ShowOutDated {},

    #[structopt(name = "show:venv_path", about = "Show path of the virtualenv")]
    ShowVenvPath {},

    #[structopt(
        name = "show:bin_path",
        about = "Show path of the virtualenv's binaries"
    )]
    ShowVenvBin {},

    #[structopt(name = "upgrade-pip", about = "Upgrade pip in the virtualenv")]
    UpgradePip {},
}

pub fn print_error(description: &str) {
    eprintln!("{}: {}", "Error".bold().red(), description);
}

pub fn print_warning(description: &str) {
    eprintln!("{}: {}", "Warning".bold().yellow(), description);
}

pub fn print_info_1(message: &str) {
    println!("{} {}", "::".blue(), message);
}

pub fn print_info_2(message: &str) {
    println!("{} {}", "->".blue(), message);
}

// Make sure the `--python-version` option used in `dmenv lock`
// can be written as marker in the lock file
fn parse_python_version(string: &str) -> Result<String, Error> {
    // Note: parsing *all* the possible syntaxes is a hard problem
    // (see https://www.python.org/dev/peps/pep-0508/#grammar for details),
    // so we use a regex that matches a *subset* of what is possible
    // instead.
    let re = Regex::new("^(==|<|<=|>|>=) (('.*?')|(\".*?\"))$").unwrap();
    if !re.is_match(string) {
        return Err(new_error("should match something like `<= '3.6'`"));
    }
    Ok(string.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_python_version_ok() {
        assert_eq!("< '3.6'", parse_python_version("< '3.6'").unwrap());
    }

    #[test]
    fn test_parse_python_version_no_comparison() {
        parse_python_version("3.6").unwrap_err();
    }

    #[test]
    fn test_parse_python_version_not_quoted() {
        parse_python_version("<= 3.6").unwrap_err();
    }
}