1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 fetchFromGitHub,
6 fetchpatch,
7 cargo,
8 hypothesis,
9 libiconv,
10 pytestCheckHook,
11 python,
12 pythonOlder,
13 pyyaml,
14 rustPlatform,
15 rustc,
16 setuptools-rust,
17 setuptools-scm,
18 typing-extensions,
19 typing-inspect,
20}:
21
22buildPythonPackage rec {
23 pname = "libcst";
24 version = "1.1.0";
25 format = "pyproject";
26
27 disabled = pythonOlder "3.7";
28
29 src = fetchFromGitHub {
30 owner = "instagram";
31 repo = "libcst";
32 rev = "refs/tags/v${version}";
33 hash = "sha256-kFs7edBWz0GRbgbLDmtpUVi5R+6mYXsJSvceOoPW9ck=";
34 };
35
36 cargoDeps = rustPlatform.fetchCargoTarball {
37 inherit src;
38 sourceRoot = "${src.name}/${cargoRoot}";
39 name = "${pname}-${version}";
40 hash = "sha256-fhaHiz64NH6S61fSXj4gNxxcuB+ECxWSSmG5StiFr1k=";
41 };
42
43 cargoRoot = "native";
44
45 patches = [
46 # https://github.com/Instagram/LibCST/pull/1042
47 (fetchpatch {
48 name = "remove-distutils.patch";
49 url = "https://github.com/Instagram/LibCST/commit/a6834aa0e6eb78e41549fd1087d7ba60ca4dd237.patch";
50 hash = "sha256-lyIXJhm4UMwdCOso6McDslIvtK7Ar8sF5Zy7qo1nicQ=";
51 })
52 ];
53
54 postPatch = ''
55 # avoid infinite recursion by not formatting the release files
56 substituteInPlace libcst/codegen/generate.py \
57 --replace '"ufmt"' '"true"'
58 '';
59
60 nativeBuildInputs = [
61 setuptools-rust
62 setuptools-scm
63 rustPlatform.cargoSetupHook
64 cargo
65 rustc
66 ];
67
68 buildInputs = lib.optionals stdenv.isDarwin [ libiconv ];
69
70 propagatedBuildInputs = [
71 typing-extensions
72 typing-inspect
73 pyyaml
74 ];
75
76 nativeCheckInputs = [
77 hypothesis
78 pytestCheckHook
79 ];
80
81 preCheck = ''
82 # otherwise import libcst.native fails
83 cp build/lib.*/libcst/native.* libcst/
84
85 ${python.interpreter} -m libcst.codegen.generate visitors
86 ${python.interpreter} -m libcst.codegen.generate return_types
87
88 # Can't run all tests due to circular dependency on hypothesmith -> libcst
89 rm -r {libcst/tests,libcst/codegen/tests,libcst/m*/tests}
90 '';
91
92 disabledTests = [
93 # No files are generated
94 "test_codemod_formatter_error_input"
95 ];
96
97 pythonImportsCheck = [ "libcst" ];
98
99 meta = with lib; {
100 description = "Concrete Syntax Tree (CST) parser and serializer library for Python";
101 homepage = "https://github.com/Instagram/libcst";
102 changelog = "https://github.com/Instagram/LibCST/blob/v${version}/CHANGELOG.md";
103 license = with licenses; [
104 mit
105 asl20
106 psfl
107 ];
108 maintainers = with maintainers; [ ];
109 };
110}