Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 732fb4b

Browse files
committed
cleaning up the code
1 parent dc6ba72 commit 732fb4b

File tree

12 files changed

+289
-277
lines changed

12 files changed

+289
-277
lines changed

default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ in mkDerivation rec {
1717
version = builtins.readFile ./VERSION;
1818
name = "pypi2nix-${version}";
1919
srcs = with deps; [ pypi2nix pip click setuptools zcbuildout zcrecipeegg ];
20-
buildInputs = with pkgs; [ zip makeWrapper ];
20+
buildInputs = with pkgs; [ pythonFull zip makeWrapper ];
2121
sourceRoot = ".";
2222

2323
postUnpack = ''

src/pypi2nix/cli.py

Lines changed: 25 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22
import click
33
import random
44
import 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()
@@ -78,26 +69,15 @@
7869
)
7970
def 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)

src/pypi2nix/cmd.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/pypi2nix/json2wheels.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/pypi2nix/pip.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ in pkgs.stdenv.mkDerivation rec {
3232
3333
echo "${requirements}" > requirements.txt
3434
35-
PYTHONPATH=${pypi2nix_bootstrap}/extra:$PYTHONPATH pip wheel -r requirements.txt --wheel-dir ${cache} --find-links ${cache}
35+
PYTHONPATH=${pypi2nix_bootstrap}/extra:$PYTHONPATH pip wheel -r requirements.txt --no-binary :all: --wheel-dir ${cache} --find-links ${cache}
3636
PYTHONPATH=${cache}:${pypi2nix_bootstrap}/extra:$PYTHONPATH pip freeze > $out/requirements.txt
3737
3838
cd $out/wheelhouse

src/pypi2nix/py2txt.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/pypi2nix/wheelhouse.py renamed to src/pypi2nix/stage1.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import click
22
import hashlib
33
import os
4-
import pypi2nix.cmd
54
import stat
65

6+
from pypi2nix.utils import curry, cmd
77

8-
def do(input_file, nix_path=None, extra_build_inputs=None, python="python27",
8+
9+
@curry
10+
def download_wheels_and_create_wheelhouse(
11+
input_file, nix_path=None, extra_build_inputs=None, python="python27",
912
cache_dir=None):
13+
'''from setup.py or buildout.cfg we create complete list of all
14+
requirements needed.
15+
'''
1016

11-
if not input_file.endswith('.txt'):
12-
raise click.ClickException('You need to provide correct <input_file>.')
17+
click.secho('Downloading wheels and creating wheelhouse', fg='green')
1318

1419
if not os.path.exists(input_file):
1520
raise click.ClickException(
@@ -53,7 +58,7 @@ def do(input_file, nix_path=None, extra_build_inputs=None, python="python27",
5358
' --arg extraBuildInputs \'{extra_build_inputs}\''\
5459
' {nix_path} -o {output} --show-trace'.format(**locals())
5560

56-
returncode = pypi2nix.cmd.do(command)
61+
returncode = cmd(command)
5762
if returncode != 0:
5863
raise click.ClickException(
5964
u'While trying to run the command something went wrong.')

src/pypi2nix/parse_wheels.py renamed to src/pypi2nix/stage2.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88
import pip.index
99
import pip.req
1010

11+
from pypi2nix.utils import curry
12+
13+
1114
SESSION = pip.download.PipSession()
1215
URL = 'https://pypi.python.org/simple/'
1316

1417

18+
def find_homepage(item):
19+
homepage = ''
20+
if 'extensions' in item and \
21+
'python.details' in item['extensions'] and \
22+
'project_urls' in item['extensions']['python.details']:
23+
homepage = item['extensions']['python.details'].get('Home', '')
24+
return homepage
25+
26+
1527
def extract_deps(metadata):
1628
"""Get dependent packages from metadata.
1729
@@ -53,6 +65,9 @@ def parse(metadata_json):
5365
'url': link.url_without_fragment,
5466
'md5': link.hash,
5567
'deps': extract_deps(metadata),
68+
'homepage': find_homepage(metadata),
69+
'license': metadata.get('license', ''),
70+
'description': metadata.get('summary', ''),
5671
}
5772

5873
except:
@@ -72,7 +87,17 @@ def try_candidates(distinfo):
7287
raise click.ClickException('unable to find json in %s' % distinfo)
7388

7489

75-
def do(wheel_dir):
90+
@curry
91+
def extract_metadata_from_wheelhouse(wheel_dir):
92+
'''
93+
once we have all the metadata we can create wheels and install them, so
94+
that metadata.json is produced for each package which we process to
95+
extract dependencies for packages
96+
'''
97+
98+
click.secho(
99+
'Stage2: Extracting metadata from {}'.format(wheel_dir), fg='green')
100+
76101
res = []
77102
for distinfo in glob.glob(p.join(wheel_dir, '*.dist-info')):
78103
click.secho('|-> %s' % p.basename(distinfo), fg='blue')

0 commit comments

Comments
 (0)