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

use crate::error::*;

#[derive(StructOpt, Debug)]
#[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(
        long = "--system-site-packages",
        help = "Give the virtual environment access to the system site-packages dir"
    )]
    pub system_site_packages: bool,

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

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

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

    #[structopt(name = "create", about = "Create an empty vritualenv")]
    Create {},

    #[structopt(name = "install", about = "Install all dependencies")]
    Install {
        #[structopt(long = "--no-develop", help = "Do not run setup.py develop")]
        no_develop: 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(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", raw(required = "true"))]
        cmd: Vec<String>,
    },

    #[structopt(name = "process-scripts", help = "Process generated scripts")]
    ProcessScripts {
        #[structopt(long = "--force", help = "force override of existing files")]
        force: bool,
    },

    #[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 = "tidy", about = "Re-generate a clean lock")]
    Tidy {},

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

// 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'`".to_string(),
        ));
    }
    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();
    }
}