13
13
$ python scripts/generate_pip_deps_from_conda.py --compare
14
14
"""
15
15
import argparse
16
- import os
16
+ import pathlib
17
17
import re
18
18
import sys
19
19
23
23
RENAME = {"pytables" : "tables" , "pyqt" : "pyqt5" , "dask-core" : "dask" }
24
24
25
25
26
- def conda_package_to_pip (package ):
26
+ def conda_package_to_pip (package : str ):
27
27
"""
28
28
Convert a conda package to its pip equivalent.
29
29
@@ -36,17 +36,13 @@ def conda_package_to_pip(package):
36
36
package = re .sub ("(?<=[^<>])=" , "==" , package ).strip ()
37
37
38
38
for compare in ("<=" , ">=" , "==" ):
39
- if compare not in package :
40
- continue
39
+ if compare in package :
40
+ pkg , version = package .split (compare )
41
+ if pkg in EXCLUDE :
42
+ return
41
43
42
- pkg , version = package .split (compare )
43
- if pkg in EXCLUDE :
44
- return
45
-
46
- if pkg in RENAME :
47
- return "" .join ((RENAME [pkg ], compare , version ))
48
-
49
- break
44
+ if pkg in RENAME :
45
+ return "" .join ((RENAME [pkg ], compare , version ))
50
46
51
47
if package in EXCLUDE :
52
48
return
@@ -57,16 +53,18 @@ def conda_package_to_pip(package):
57
53
return package
58
54
59
55
60
- def main (conda_fname , pip_fname , compare = False ):
56
+ def generate_pip_from_conda (
57
+ conda_path : pathlib .Path , pip_path : pathlib .Path , compare : bool = False
58
+ ) -> bool :
61
59
"""
62
60
Generate the pip dependencies file from the conda file, or compare that
63
61
they are synchronized (``compare=True``).
64
62
65
63
Parameters
66
64
----------
67
- conda_fname : str
65
+ conda_path : pathlib.Path
68
66
Path to the conda file with dependencies (e.g. `environment.yml`).
69
- pip_fname : str
67
+ pip_path : pathlib.Path
70
68
Path to the pip file with dependencies (e.g. `requirements-dev.txt`).
71
69
compare : bool, default False
72
70
Whether to generate the pip file (``False``) or to compare if the
@@ -78,8 +76,8 @@ def main(conda_fname, pip_fname, compare=False):
78
76
bool
79
77
True if the comparison fails, False otherwise
80
78
"""
81
- with open (conda_fname ) as conda_fd :
82
- deps = yaml .safe_load (conda_fd )["dependencies" ]
79
+ with conda_path . open () as file :
80
+ deps = yaml .safe_load (file )["dependencies" ]
83
81
84
82
pip_deps = []
85
83
for dep in deps :
@@ -88,24 +86,23 @@ def main(conda_fname, pip_fname, compare=False):
88
86
if conda_dep :
89
87
pip_deps .append (conda_dep )
90
88
elif isinstance (dep , dict ) and len (dep ) == 1 and "pip" in dep :
91
- pip_deps += dep ["pip" ]
89
+ pip_deps . extend ( dep ["pip" ])
92
90
else :
93
91
raise ValueError (f"Unexpected dependency { dep } " )
94
92
95
- fname = os .path .split (conda_fname )[1 ]
96
93
header = (
97
- f"# This file is auto-generated from { fname } , do not modify.\n "
94
+ f"# This file is auto-generated from { conda_path . name } , do not modify.\n "
98
95
"# See that file for comments about the need/usage of each dependency.\n \n "
99
96
)
100
97
pip_content = header + "\n " .join (pip_deps ) + "\n "
101
98
102
99
if compare :
103
- with open (pip_fname ) as pip_fd :
104
- return pip_content != pip_fd .read ()
105
- else :
106
- with open (pip_fname , "w" ) as pip_fd :
107
- pip_fd .write (pip_content )
108
- return False
100
+ with pip_path . open () as file :
101
+ return pip_content != file .read ()
102
+
103
+ with pip_path . open ("w" ) as file :
104
+ file .write (pip_content )
105
+ return False
109
106
110
107
111
108
if __name__ == "__main__" :
@@ -117,25 +114,20 @@ def main(conda_fname, pip_fname, compare=False):
117
114
action = "store_true" ,
118
115
help = "compare whether the two files are equivalent" ,
119
116
)
120
- argparser .add_argument (
121
- "--azure" , action = "store_true" , help = "show the output in azure-pipelines format"
122
- )
123
117
args = argparser .parse_args ()
124
118
125
- repo_path = os .path .dirname (os .path .abspath (os .path .dirname (__file__ )))
126
- res = main (
127
- os .path .join (repo_path , "environment.yml" ),
128
- os .path .join (repo_path , "requirements-dev.txt" ),
119
+ conda_fname = "environment.yml"
120
+ pip_fname = "requirements-dev.txt"
121
+ repo_path = pathlib .Path (__file__ ).parent .parent .absolute ()
122
+ res = generate_pip_from_conda (
123
+ pathlib .Path (repo_path , conda_fname ),
124
+ pathlib .Path (repo_path , pip_fname ),
129
125
compare = args .compare ,
130
126
)
131
127
if res :
132
128
msg = (
133
- f"`requirements-dev.txt ` has to be generated with `{ sys . argv [ 0 ] } ` after "
134
- "`environment.yml ` is modified.\n "
129
+ f"`{ pip_fname } ` has to be generated with `{ __file__ } ` after "
130
+ f"` { conda_fname } ` is modified.\n "
135
131
)
136
- if args .azure :
137
- msg = (
138
- f"##vso[task.logissue type=error;sourcepath=requirements-dev.txt]{ msg } "
139
- )
140
132
sys .stderr .write (msg )
141
133
sys .exit (res )
0 commit comments