-
Notifications
You must be signed in to change notification settings - Fork 16
/
pipenvlib.py
196 lines (146 loc) · 5.61 KB
/
pipenvlib.py
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
import os
import json
import toml
import delegator
class Dependency(object):
"""A Dependency."""
def __init__(self, name, constraint, locked=False):
self.name = name
self.constraint = constraint
self.locked = locked
def __repr__(self):
return "<Dependency '{0}' constraint='{1}'>".format(
self.name, self.constraint
)
class LockedDependency(object):
"""A Locked Dependency."""
def __init__(self, name, constraint, hashes):
self.name = name
self.constraint = constraint
self.hashes = hashes
def __repr__(self):
return "<LockedDependency '{0}{1}'>".format(
self.name, self.constraint
)
class Requirement(object):
"""A Requirement."""
def __init__(self, name, constraint):
self.name = name
self.constraint = constraint
def __repr__(self):
return "<Requirement '{0}' constraint='{1}'>".format(
self.name, self.constraint
)
class PipenvProject(object):
"""A Pipenv project."""
def __init__(self, home, pipfile='Pipfile', create=False):
self.home = home
self.pipfile = pipfile
if not create:
# Assert that the Pipfile exists.
self.assert_has_pipfile()
else:
# Cheat a project-creation by installing, then uninstalling
# the Requests library. :)
self.install('requests')
self.uninstall('requests')
@property
def _pipfile_path(self):
return os.path.sep.join([self.home, self.pipfile])
@property
def _lockfile_path(self):
return os.path.sep.join([self.home, '{0}.lock'.format(self.pipfile)])
def assert_has_pipfile(self):
"""Asserts that the Pipfile exists."""
assert os.path.isfile(self._pipfile_path)
def assert_has_lockfile(self):
"""Asserts that the Pipfile.lock exists."""
assert os.path.isfile(self._lockfile_path)
def _get_section_of_pipfile(self, section, target):
def gen():
pipfile = toml.load(self._pipfile_path)
for package in pipfile[section]:
name = package
constraint = pipfile[section][package]
yield target(name, constraint)
return [p for p in gen()]
@property
def packages(self):
"""Returns a list of Dependency objects (for [packages]) for
the Pipenv project"""
return self._get_section_of_pipfile('packages', Dependency)
@property
def dev_packages(self):
"""Returns a list of Dependency objects (for [dev-packages]) for
the Pipenv project.
"""
return self._get_section_of_pipfile('dev-packages', Dependency)
@property
def requires(self):
"""Returns a list of Requirement objects for the Pipenv project.
"""
return self._get_section_of_pipfile('requires', Requirement)
@property
def locked_packages(self):
"""Returns a list of LockedDependency objects for the Pipenv
project.
"""
self.assert_has_lockfile()
def gen():
with open(self._lockfile_path) as f:
lockfile = json.load(f)
for package in lockfile['default']:
name = package
constraint = lockfile['default'][package]['version']
hashes = lockfile['default'][package]['hashes']
yield LockedDependency(name, constraint, hashes)
return [l for l in gen()]
@property
def locked_dev_packages(self):
"""Returns a list of LockedDependency objects for the Pipenv
project.
"""
self.assert_has_lockfile()
def gen():
with open(self._lockfile_path) as f:
lockfile = json.load(f)
for package in lockfile['develop']:
name = package
constraint = lockfile['develop'][package]['version']
hashes = lockfile['develop'][package]['hashes']
yield LockedDependency(name, constraint, hashes)
return [l for l in gen()]
@property
def locked_requirements(self):
"""Returns a list of Requirement objects for the Pipenv
project, from the Pipfile.lock.
"""
self.assert_has_lockfile()
def gen():
with open(self._lockfile_path) as f:
lockfile = json.load(f)
for require in lockfile['_meta']['requires']:
name = require
constraint = lockfile['_meta']['requires'][require]
yield Requirement(name, constraint)
return [l for l in gen()]
def _run(self, cmd):
"""Run a Pipenv command for the Pipenv project."""
return delegator.run('pipenv {0}'.format(cmd), cwd=self.home)
def install(self, package_name, constraint=None, dev=False):
"""Installs a given package to the Pipenv project."""
# If no constraint was
if constraint is not None:
# Append the constraint to the package name.
package_name = 'package_name{0}'.format(constraint)
dev = '' if not dev else '--dev'
return self._run('install {0} {1}'.format(package_name, dev)).return_code == 0
def uninstall(self, package_name):
"""Uninstalls a given package from the Pipenv project."""
return self._run('uninstall {0}'.format(package_name)).return_code == 0
def check(self):
"""Runs Pipenv check on the Pipenv project."""
return self._run('check').return_code == 0
@property
def virtualenv_location(self):
return self._run('--venv').out.strip()