File: metadata_reset.py

package info (click to toggle)
rocketcea 1.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 39,944 kB
  • sloc: fortran: 23,152; python: 9,235; pascal: 370; makefile: 168; sh: 9
file content (60 lines) | stat: -rw-r--r-- 1,931 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: ascii -*-

"""
Look for a line of code ending with a comment that starts with "# METADATA_RESET:"

Use the template after "# METADATA_RESET:" to change the code portion of the line.

For example:

__version__ = '0.0.1'  # METADATA_RESET:__version__ = '<<version>>'
"""

def render_line( line, metadataD ):
    """Change the line of code using values from dictionary, metadataD.
    
       Items in metadataD look like: {"<<version>>":"1.2.3", "<<author>>":"Buster Boop"}
    """
    
    sL = line.split("# METADATA_RESET:")
    code_str = sL[-1].rstrip()
    # print( 'code_str =', code_str)
    print('  Change:', sL[0].rstrip() )
    
    for key,val in metadataD.items():
        if code_str.find(key) >= 0:
            code_str = code_str.replace( key, val )
            
    print('      To:', code_str)
    return code_str + '  # METADATA_RESET:' + sL[-1]

def render_file( full_file_name, metadataD ):
    """Look through file for "# METADATA_RESET:"
       Render lines where it is found.
    """
    with open(full_file_name, 'r') as f:
        lineL = list(f)    # will include \n at end of line
        
    for i,line in enumerate(lineL):
        if line.find("# METADATA_RESET:") >= 0:
            print('In File:', full_file_name)
            if line.endswith('\n'):
                lineL[i] = render_line( line[:-1], metadataD ) + '\n'
            else:
                lineL[i] = render_line( line, metadataD )
            
    with open(full_file_name, 'w') as f:
        f.write( ''.join(lineL) )
    
if __name__ == "__main__":
    import os
    
    metadataD =  {"<<version>>":"1.1.35"}

    # render_file( os.path.abspath('./docs/conf.py'), metadataD )
    # render_file( os.path.abspath('./setup.py'), metadataD )
    render_file( os.path.abspath('./rocketcea/_version.py'), metadataD )
    # render_file( os.path.abspath('./rocketcea/cea_obj.py'), metadataD )