Diff of /setup.py [000000] .. [6d389a]

Switch to unified view

a b/setup.py
1
# Copyright (c) OpenMMLab. All rights reserved.
2
import os
3
import os.path as osp
4
import shutil
5
import sys
6
import warnings
7
from setuptools import find_packages, setup
8
9
10
def readme():
11
    with open('README.md', encoding='utf-8') as f:
12
        content = f.read()
13
    return content
14
15
16
version_file = 'mmaction/version.py'
17
18
19
def get_version():
20
    with open(version_file, 'r') as f:
21
        exec(compile(f.read(), version_file, 'exec'))
22
    return locals()['__version__']
23
24
25
def parse_requirements(fname='requirements.txt', with_version=True):
26
    """Parse the package dependencies listed in a requirements file but strips
27
    specific versioning information.
28
29
    Args:
30
        fname (str): path to requirements file
31
        with_version (bool, default=False): if True include version specs
32
33
    Returns:
34
        List[str]: list of requirements items
35
36
    CommandLine:
37
        python -c "import setup; print(setup.parse_requirements())"
38
    """
39
    import re
40
    import sys
41
    from os.path import exists
42
    require_fpath = fname
43
44
    def parse_line(line):
45
        """Parse information from a line in a requirements text file."""
46
        if line.startswith('-r '):
47
            # Allow specifying requirements in other files
48
            target = line.split(' ')[1]
49
            for info in parse_require_file(target):
50
                yield info
51
        else:
52
            info = {'line': line}
53
            if line.startswith('-e '):
54
                info['package'] = line.split('#egg=')[1]
55
            elif '@git+' in line:
56
                info['package'] = line
57
            else:
58
                # Remove versioning from the package
59
                pat = '(' + '|'.join(['>=', '==', '>']) + ')'
60
                parts = re.split(pat, line, maxsplit=1)
61
                parts = [p.strip() for p in parts]
62
63
                info['package'] = parts[0]
64
                if len(parts) > 1:
65
                    op, rest = parts[1:]
66
                    if ';' in rest:
67
                        # Handle platform specific dependencies
68
                        # http://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-platform-specific-dependencies
69
                        version, platform_deps = map(str.strip,
70
                                                     rest.split(';'))
71
                        info['platform_deps'] = platform_deps
72
                    else:
73
                        version = rest  # NOQA
74
                    info['version'] = (op, version)
75
            yield info
76
77
    def parse_require_file(fpath):
78
        with open(fpath, 'r') as f:
79
            for line in f.readlines():
80
                line = line.strip()
81
                if line and not line.startswith('#'):
82
                    for info in parse_line(line):
83
                        yield info
84
85
    def gen_packages_items():
86
        if exists(require_fpath):
87
            for info in parse_require_file(require_fpath):
88
                parts = [info['package']]
89
                if with_version and 'version' in info:
90
                    parts.extend(info['version'])
91
                if not sys.version.startswith('3.4'):
92
                    # apparently package_deps are broken in 3.4
93
                    platform_deps = info.get('platform_deps')
94
                    if platform_deps is not None:
95
                        parts.append(';' + platform_deps)
96
                item = ''.join(parts)
97
                yield item
98
99
    packages = list(gen_packages_items())
100
    return packages
101
102
103
def add_mim_extension():
104
    """Add extra files that are required to support MIM into the package.
105
106
    These files will be added by creating a symlink to the originals if the
107
    package is installed in `editable` mode (e.g. pip install -e .), or by
108
    copying from the originals otherwise.
109
    """
110
111
    # parse installment mode
112
    if 'develop' in sys.argv:
113
        # installed by `pip install -e .`
114
        mode = 'symlink'
115
    elif 'sdist' in sys.argv or 'bdist_wheel' in sys.argv:
116
        # installed by `pip install .`
117
        # or create source distribution by `python setup.py sdist`
118
        mode = 'copy'
119
    else:
120
        return
121
122
    filenames = ['tools', 'configs', 'model-index.yml']
123
    repo_path = osp.dirname(__file__)
124
    mim_path = osp.join(repo_path, 'mmaction', '.mim')
125
    os.makedirs(mim_path, exist_ok=True)
126
127
    for filename in filenames:
128
        if osp.exists(filename):
129
            src_path = osp.join(repo_path, filename)
130
            tar_path = osp.join(mim_path, filename)
131
132
            if osp.isfile(tar_path) or osp.islink(tar_path):
133
                os.remove(tar_path)
134
            elif osp.isdir(tar_path):
135
                shutil.rmtree(tar_path)
136
137
            if mode == 'symlink':
138
                src_relpath = osp.relpath(src_path, osp.dirname(tar_path))
139
                try:
140
                    os.symlink(src_relpath, tar_path)
141
                except OSError:
142
                    # Creating a symbolic link on windows may raise an
143
                    # `OSError: [WinError 1314]` due to privilege. If
144
                    # the error happens, the src file will be copied
145
                    mode = 'copy'
146
                    warnings.warn(
147
                        f'Failed to create a symbolic link for {src_relpath}, '
148
                        f'and it will be copied to {tar_path}')
149
                else:
150
                    continue
151
            elif mode == 'copy':
152
                if osp.isfile(src_path):
153
                    shutil.copyfile(src_path, tar_path)
154
                elif osp.isdir(src_path):
155
                    shutil.copytree(src_path, tar_path)
156
                else:
157
                    warnings.warn(f'Cannot copy file {src_path}.')
158
            else:
159
                raise ValueError(f'Invalid mode {mode}')
160
161
162
if __name__ == '__main__':
163
    add_mim_extension()
164
    setup(
165
        name='mmaction2',
166
        version=get_version(),
167
        description='OpenMMLab Video Understanding Toolbox and Benchmark',
168
        long_description=readme(),
169
        long_description_content_type='text/markdown',
170
        author='MMAction2 Contributors',
171
        author_email='openmmlab@gmail.com',
172
        maintainer='MMAction2 Contributors',
173
        maintainer_email='openmmlab@gmail.com',
174
        packages=find_packages(exclude=('configs', 'tools', 'demo')),
175
        keywords='computer vision, video understanding',
176
        include_package_data=True,
177
        classifiers=[
178
            'Development Status :: 4 - Beta',
179
            'License :: OSI Approved :: Apache Software License',
180
            'Operating System :: OS Independent',
181
            'Programming Language :: Python :: 3',
182
            'Programming Language :: Python :: 3.6',
183
            'Programming Language :: Python :: 3.7',
184
            'Programming Language :: Python :: 3.8',
185
            'Programming Language :: Python :: 3.9',
186
        ],
187
        url='https://github.com/open-mmlab/mmaction2',
188
        license='Apache License 2.0',
189
        install_requires=parse_requirements('requirements/runtime.txt'),
190
        extras_require={
191
            'all': parse_requirements('requirements.txt'),
192
            'tests': parse_requirements('requirements/tests.txt'),
193
            'build': parse_requirements('requirements/build.txt'),
194
            'optional': parse_requirements('requirements/optional.txt'),
195
        },
196
        zip_safe=False)