-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsetup.py
More file actions
74 lines (60 loc) · 2.42 KB
/
setup.py
File metadata and controls
74 lines (60 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
from setuptools import setup, find_packages, Command
import shutil
from pathlib import Path
import re
schema_file = os.path.join('schema','ismrmrd.xsd')
config_file = os.path.join('schema','.xsdata.xml')
import setuptools.command.build
setuptools.command.build.build.sub_commands.append(("generate_schema", None))
class GenerateSchemaCommand(Command):
description = "Generate Python code from ISMRMRD XML schema using xsdata"
user_options = []
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.build_lib = None
self.editable_mode = False
def initialize_options(self):
pass
def finalize_options(self):
# Set build_lib for non-editable installs
self.set_undefined_options("build_py", ("build_lib", "build_lib"))
def run(self):
# Use editable_mode if present (PEP 660)
if self.editable_mode:
outdir = 'ismrmrd/xsd/'
else:
outdir = os.path.join(self.build_lib, 'ismrmrd/xsd/')
self.announce(f'Generating schema to {outdir} (editable_mode={self.editable_mode})', level=3)
self.generate_schema(schema_file, config_file, 'ismrmrdschema', outdir)
def get_source_files(self):
return [schema_file, config_file]
def get_outputs(self):
return [
"{build_lib}/ismrmrd/xsd/ismrmrdschema/__init__.py",
"{build_lib}/ismrmrd/xsd/ismrmrdschema/ismrmrd.py"
]
def fix_init_file(self, package_name,filepath):
with open(filepath,'r+') as f:
text = f.read()
text = re.sub(f'from {package_name}.ismrmrd', 'from .ismrmrd',text)
f.seek(0)
f.write(text)
f.truncate()
def generate_schema(self, schema_filename, config_filename, subpackage_name, outdir):
import sys
import subprocess
# subpackage_name = 'ismrmrdschema'
args = [sys.executable, '-m', 'xsdata', 'generate', str(schema_filename), '--config', str(config_filename), '--package', subpackage_name]
subprocess.run(args)
self.fix_init_file(subpackage_name, f"{subpackage_name}/__init__.py")
destination = os.path.join(outdir, subpackage_name)
shutil.rmtree(destination, ignore_errors=True)
shutil.move(subpackage_name, destination)
setup(
version='1.14.3',
packages=find_packages(),
cmdclass={
'generate_schema': GenerateSchemaCommand
}
)