File: port_utilities.py

package info (click to toggle)
python-line-profiler 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,256 kB
  • sloc: python: 8,119; sh: 810; ansic: 297; makefile: 14
file content (92 lines) | stat: -rw-r--r-- 2,585 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Statically port utilities from ubelt and xdocest need for the autoprofile
features.

Similar Scripts:
    ~/code/xdoctest/dev/maintain/port_ubelt_utils.py
    ~/code/mkinit/dev/maintain/port_ubelt_code.py
    ~/code/line_profiler/dev/maintain/port_utilities.py
"""
import ubelt as ub
import liberator
import re


def generate_util_static():
    lib = liberator.Liberator(verbose=0)

    import ubelt
    import xdoctest

    if 1:
        lib.add_dynamic(ubelt.util_import.modpath_to_modname)
        lib.add_dynamic(ubelt.util_import.modname_to_modpath)
        lib.expand(['ubelt'])

    if 1:
        lib.add_dynamic(xdoctest.static_analysis.package_modpaths)
        lib.expand(['xdoctest'])

        # # Hack because ubelt and xdoctest define this
        del lib.body_defs['xdoctest.utils.util_import._platform_pylib_exts']

    # lib.expand(['ubelt', 'xdoctest'])
    text = lib.current_sourcecode()

    """
    pip install rope
    pip install parso
    """

    prefix = ub.codeblock(
        '''
        """
        This file was autogenerated based on code in :py:mod:`ubelt` and
        :py:mod:`xdoctest` via dev/maintain/port_utilities.py in the
        line_profiler repo.
        """
        ''')

    # Remove doctest references to ubelt
    new_lines = []
    for line in text.split('\n'):
        if line.strip().startswith('>>> from ubelt'):
            continue
        if line.strip().startswith('>>> import ubelt as ub'):
            line = re.sub('>>> .*', '>>> # xdoctest: +SKIP("ubelt dependency")', line)
        new_lines.append(line)

    text = '\n'.join(new_lines)
    text = prefix + '\n' + text + '\n'
    return text


def main():
    text = generate_util_static()
    print(ub.highlight_code(text, backend='rich'))

    import parso
    import line_profiler
    target_fpath = ub.Path(line_profiler.__file__).parent / 'autoprofile' / 'util_static.py'

    new_module = parso.parse(text)
    if target_fpath.exists():
        old_module = parso.parse(target_fpath.read_text())
        new_names = [child.name.value for child in new_module.children if child.type in {'funcdef', 'classdef'}]
        old_names = [child.name.value for child in old_module.children if child.type in {'funcdef', 'classdef'}]
        print(set(old_names) - set(new_names))
        print(set(new_names) - set(old_names))

    target_fpath.write_text(text)

    # Fixup formatting
    if 1:
        ub.cmd(['black', target_fpath])


if __name__ == '__main__':
    """
    CommandLine:
        python ~/code/line_profiler/dev/maintain/port_utilities.py
    """
    main()