22import click
33import random
44import string
5- import pypi2nix .wheelhouse
6- import pypi2nix .parse_wheels
7- import pypi2nix .stage3
85
9-
10- PYTHON_VERSIONS = {
11- "2.6" : "python26" ,
12- "2.7" : "python27" ,
13- "3.2" : "python32" ,
14- "3.3" : "python33" ,
15- "3.4" : "python34" ,
16- "3.5" : "python35" ,
17- "pypy" : "pypy" ,
18- }
6+ from pypi2nix .stage1 import download_wheels_and_create_wheelhouse
7+ from pypi2nix .stage2 import extract_metadata_from_wheelhouse
8+ from pypi2nix .stage3 import generate_nix_expressions
9+ from pypi2nix .utils import PYTHON_VERSIONS , compose
1910
2011
2112@click .command ()
7869)
7970def main (specification , name , requirements , buildout , nix_path ,
8071 extra_build_inputs , python_version , cache_dir ):
81- '''
82- INPUT_FILE should be requirements.txt (output of pip freeze).
83- '''
72+ """INPUT_FILE should be requirements.txt (output of pip freeze).
73+ """
8474
75+ # ensure that working folder exists
8576 home_dir = os .path .expanduser ('~/.pypi2nix' )
8677 if not os .path .isdir (home_dir ):
8778 os .makedirs (home_dir )
8879
89- _input = [specification , requirements , buildout ]
90-
91- if not any (_input ):
92- raise click .exceptions .UsageError (
93- u'Please tell what you want to be packages by specifying `-b` '
94- u'(buildout.cfg), `-r` (requirements.txt) or by providing '
95- u'package specification as a argument of pypi2nix command' )
96-
97- if len (filter (lambda x : x , _input )) >= 2 :
98- raise click .exceptions .UsageError (
99- u'Please only specify one of the options: `-b`, `-r` or package.' )
100-
80+ # adjust input_name/input_file based on the inputs
10181 if buildout :
10282 raise click .exceptions .ClickException (
10383 u'Not yet implemented!' )
@@ -115,80 +95,21 @@ def main(specification, name, requirements, buildout, nix_path,
11595 with open (input_file , 'w+' ) as f :
11696 f .write ('\n ' .join (specification ))
11797
118- if name :
119- input_name = name
120-
121- #
122- # Stage 1
123- #
124- # from setup.py or buildout.cfg we create complete list of all requirements
125- # needed.
126- #
127- click .secho ('Downloading wheels and creating wheelhouse' , fg = 'green' )
128- wheels_dir = pypi2nix .wheelhouse .do (
129- input_file , nix_path , extra_build_inputs ,
130- PYTHON_VERSIONS [python_version ], cache_dir )
131-
132- #
133- # Stage 2
134- #
135- # once we have all the metadata we can create wheels and install them, so
136- # that metadata.json is produced for each package which we process to
137- # extract dependencies for packages
138- #
139- click .secho (
140- 'Stage2: Extracting metadata from {}' .format (wheels_dir ), fg = 'green' )
141- metadata = pypi2nix .parse_wheels .do (wheels_dir )
142- click .secho (
143- 'Got metadata from {:d} packages' .format (len (metadata )), fg = 'green' )
144-
145- #
146- # Stage 3
147- #
148- # With all above we can now generate nix expressions
149- #
150- base_dir = os .getcwd ()
151- default_file = os .path .join (base_dir , '{}.nix' .format (input_name ))
152- generate_file = os .path .join (
153- base_dir , '{}_generated.nix' .format (input_name ))
154- override_file = os .path .join (
155- base_dir , '{}_override.nix' .format (input_name ))
156-
157- with open (input_file ) as f :
158- pypi2nix .stage3 .do (metadata , generate_file )
159-
160- if not os .path .exists (override_file ):
161- with open (override_file , 'wa+' ) as f :
162- write = lambda x : f .write (x + '\n ' )
163- write ("{ pkgs, python }:" )
164- write ("" )
165- write ("self: super: {" )
166- write ("}" )
98+ else :
99+ raise click .exceptions .UsageError (
100+ u'Please tell what you want to be packages by specifying `-b` '
101+ u'(buildout.cfg), `-r` (requirements.txt) or by providing '
102+ u'package specification as a argument of pypi2nix command' )
167103
168- if not os .path .exists (default_file ):
169- with open (default_file , 'wa+' ) as f :
170- write = lambda x : f .write (x + '\n ' )
171- write ("{ system ? builtins.currentSystem" )
172- write (", nixpkgs ? <nixpkgs>" )
173- write ("}:" )
174- write ("" )
175- write ("let" )
176- write ("" )
177- write (" inherit (pkgs.stdenv.lib) fix' extends;" )
178- write ("" )
179- write (" pkgs = import nixpkgs { inherit system; };" )
180- write (" pythonPackages = pkgs.%sPackages;" % (
181- PYTHON_VERSIONS [python_version ]))
182- write ("" )
183- write (" python = {" )
184- write (" interpreter = pythonPackages.python;" )
185- write (" mkDerivation = pythonPackages.buildPythonPackage;" )
186- write (" modules = pythonPackages.python.modules;" )
187- write (" overrideDerivation = drv: f: pythonPackages.buildPythonPackage (drv.drvAttrs // f drv.drvAttrs);" )
188- write (" pkgs = pythonPackages;" )
189- write (" };" )
190- write ("" )
191- write (" generated = import %s { inherit pkgs python; };" % generate_file )
192- write (" overrides = import %s { inherit pkgs python; };" % override_file )
193- write ("" )
194- write ("in fix' (extends overrides generated)" )
104+ compose (
105+ download_wheels_and_create_wheelhouse (
106+ nix_path = nix_path ,
107+ extra_build_inputs = extra_build_inputs ,
108+ python = PYTHON_VERSIONS [python_version ],
109+ cache_dir = cache_dir ),
110+ extract_metadata_from_wheelhouse ,
111+ generate_nix_expressions (
112+ input_name = input_name ,
113+ input_file = input_file ,
114+ python_version = python_version ),
115+ )(input_file = input_file )
0 commit comments