|
1 | 1 | import os |
2 | | -from setuptools import setup, find_packages |
3 | | -from distutils.command.build import build |
4 | | -from distutils.command.build_py import build_py |
| 2 | +from setuptools import setup, find_packages, Command |
5 | 3 |
|
6 | | -import logging |
7 | 4 | import shutil |
8 | 5 | from pathlib import Path |
9 | | -import re |
10 | | - |
11 | | -logging.basicConfig() |
12 | | -log_ = logging.getLogger(__name__) |
| 6 | +import re |
13 | 7 |
|
14 | 8 | schema_file = os.path.join('schema','ismrmrd.xsd') |
15 | 9 | config_file = os.path.join('schema','.xsdata.xml') |
16 | 10 |
|
17 | | -class my_build_py(build_py): |
18 | | - def run(self): |
19 | | - # honor the --dry-run flag |
20 | | - if not self.dry_run: |
21 | | - outloc = self.build_lib |
22 | | - outloc = os.path.join(outloc,'ismrmrd/xsd/ismrmrdschema') |
23 | 11 |
|
24 | | - |
25 | | - generate_schema(schema_file, config_file, outloc) |
26 | | - # distutils uses old-style classes, so no super() |
27 | | - build_py.run(self) |
| 12 | +import setuptools.command.build |
| 13 | +setuptools.command.build.build.sub_commands.append(("generate_schema", None)) |
28 | 14 |
|
| 15 | +class GenerateSchemaCommand(Command): |
| 16 | + description = "Generate Python code from ISMRMRD XML schema using xsdata" |
| 17 | + user_options = [] |
29 | 18 |
|
30 | | -def fix_init_file(package_name,filepath): |
| 19 | + def __init__(self, *args, **kwargs): |
| 20 | + super().__init__(*args, **kwargs) |
| 21 | + self.build_lib = None |
| 22 | + self.editable_mode = False |
31 | 23 |
|
32 | | - with open(filepath,'r+') as f: |
33 | | - text = f.read() |
34 | | - text = re.sub(f'from {package_name}.ismrmrd', 'from .ismrmrd',text) |
35 | | - f.seek(0) |
36 | | - f.write(text) |
37 | | - f.truncate() |
| 24 | + def initialize_options(self): |
| 25 | + pass |
38 | 26 |
|
| 27 | + def finalize_options(self): |
| 28 | + # Set build_lib for non-editable installs |
| 29 | + self.set_undefined_options("build_py", ("build_lib", "build_lib")) |
39 | 30 |
|
| 31 | + def run(self): |
| 32 | + # Use editable_mode if present (PEP 660) |
| 33 | + if self.editable_mode: |
| 34 | + outdir = 'ismrmrd/xsd/' |
| 35 | + else: |
| 36 | + outdir = os.path.join(self.build_lib, 'ismrmrd/xsd/') |
| 37 | + self.announce(f'Generating schema to {outdir} (editable_mode={self.editable_mode})', level=3) |
| 38 | + self.generate_schema(schema_file, config_file, 'ismrmrdschema', outdir) |
40 | 39 |
|
| 40 | + def get_source_files(self): |
| 41 | + return [schema_file, config_file] |
41 | 42 |
|
42 | | -def generate_schema(schema_filename, config_filename, outloc ): |
43 | | - def to_uri(filename): |
44 | | - return Path(filename).absolute().as_uri() |
| 43 | + def get_outputs(self): |
| 44 | + return [ |
| 45 | + "{build_lib}/ismrmrd/xsd/ismrmrdschema/__init__.py", |
| 46 | + "{build_lib}/ismrmrd/xsd/ismrmrdschema/ismrmrd.py" |
| 47 | + ] |
45 | 48 |
|
46 | | - import sys |
47 | | - import subprocess |
48 | | - |
49 | | - subpackage_name = 'ismrmrdschema' |
50 | | - args = [sys.executable,'-m','xsdata', str(schema_filename), '--config',str(config_filename), '--package',subpackage_name] |
51 | | - subprocess.run(args) |
52 | | - fix_init_file(subpackage_name,f"{subpackage_name}/__init__.py") |
53 | | - shutil.rmtree(os.path.join(outloc,subpackage_name),ignore_errors=True) |
54 | | - shutil.move(subpackage_name,outloc) |
| 49 | + def fix_init_file(self, package_name,filepath): |
| 50 | + with open(filepath,'r+') as f: |
| 51 | + text = f.read() |
| 52 | + text = re.sub(f'from {package_name}.ismrmrd', 'from .ismrmrd',text) |
| 53 | + f.seek(0) |
| 54 | + f.write(text) |
| 55 | + f.truncate() |
55 | 56 |
|
56 | | -this_directory = Path(__file__).parent |
57 | | -long_description = (this_directory / "README").read_text() |
| 57 | + def generate_schema(self, schema_filename, config_filename, subpackage_name, outdir): |
| 58 | + import sys |
| 59 | + import subprocess |
| 60 | + # subpackage_name = 'ismrmrdschema' |
| 61 | + args = [sys.executable, '-m', 'xsdata', str(schema_filename), '--config', str(config_filename), '--package', subpackage_name] |
| 62 | + subprocess.run(args) |
| 63 | + self.fix_init_file(subpackage_name, f"{subpackage_name}/__init__.py") |
| 64 | + destination = os.path.join(outdir, subpackage_name) |
| 65 | + shutil.rmtree(destination, ignore_errors=True) |
| 66 | + shutil.move(subpackage_name, destination) |
58 | 67 |
|
59 | 68 | setup( |
60 | | - name='ismrmrd', |
61 | 69 | version='1.14.1', |
62 | | - author='ISMRMRD Developers', |
63 | | - description='Python implementation of the ISMRMRD', |
64 | | - license='Public Domain', |
65 | | - keywords='ismrmrd', |
66 | | - url='https://ismrmrd.github.io', |
67 | | - long_description = long_description, |
68 | | - long_description_content_type='text/markdown', |
69 | 70 | packages=find_packages(), |
70 | | - classifiers=[ |
71 | | - 'Development Status :: 5 - Production/Stable', |
72 | | - 'Intended Audience :: Science/Research', |
73 | | - 'License :: Public Domain', |
74 | | - 'Operating System :: OS Independent', |
75 | | - 'Topic :: Scientific/Engineering :: Medical Science Apps.' |
76 | | - ], |
77 | | - install_requires=['xsdata>=22.12', 'numpy>=1.22.0', 'h5py>=2.3'], |
78 | | - setup_requires=['nose>=1.0', 'xsdata[cli]>=22.12', 'jinja2 >= 2.11'], |
79 | | - test_suite='nose.collector', |
80 | | - cmdclass={'build_py':my_build_py} |
| 71 | + cmdclass={ |
| 72 | + 'generate_schema': GenerateSchemaCommand |
| 73 | + } |
81 | 74 | ) |
0 commit comments