Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gen_rst.py « utils - github.com/rpm-software-management/createrepo_c.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 85541327bceb739e31e2e7e2fbcd854ed01033e3 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python

import sys
import re
import datetime
from optparse import OptionParser


class Info(object):
    def __init__(self, name, description=None, synopsis=None, copyright=None, options=None):
        self.name = name
        self.description = description
        self.synopsis = synopsis
        self.copyright = copyright
        self.options = options  # list of dictionaries with keys ["long_name", "short_name", "description", "arg_description"]

    def gen_rst(self):
        rst  = ".. -*- coding: utf-8 -*-\n\n"
        rst += "%s\n" % ("=" * len(self.name),)
        rst += "%s\n" % self.name
        rst += "%s\n\n" % ("=" * len(self.name),)

        # Add synopsis
        #if self.synopsis:
        #    rst += ":Synopsis: %s\n\n" % self.synopsis

        # Add description
        if self.description:
            rst += ":Subtitle: %s\n\n" % self.description

        # Add copyright
        if self.copyright:
            rst += ":Copyright: %s\n" % self.copyright

        # Add date
        rst += ":Date: $Date: %s $\n\n" % datetime.datetime.strftime(datetime.datetime.utcnow(), format="%F %X")

        # Add options
        rst += "-------\n"
        rst += "OPTIONS\n"
        rst += "-------\n"

        for command in self.options:
            cmd  = ""
            if command["short_name"]:
                cmd += "-%s " % command["short_name"]
            cmd += "--%s" % command["long_name"]
            if command["arg_description"]:
                cmd += " %s" % command["arg_description"]

            rst += "%s\n" % cmd
            rst += "%s\n" % ("-" * len(cmd))
            rst += "%s\n" % command["description"]

            rst += "\n"

        return rst


def parse_arguments_from_c_file(filename):
    args = []

    try:
        content = open(filename, "r").read()
    except IOError:
        print "Error: Cannot open file %s" % filename
        return args

    re_cmd_entries = re.compile(r"static[ ]+GOptionEntry[^{]*{(?P<entries>.*)\s*NULL\s*}\s*};", re.MULTILINE|re.DOTALL)
    match = re_cmd_entries.search(content)
    if not match:
        print "Warning: Cannot find GOptionEntry section in %s" % filename
        return args

    re_single_entry = re.compile(r"""{\s*"(?P<long_name>[^"]*)"\s*,        # long name
                                     \s*'?(?P<short_name>[^',]*)'?\s*,     # short name
                                     \s*[^,]*\s*,                          # flags
                                     \s*[^,]*\s*,                          # arg type
                                     \s*[^,]*\s*,                          # arg data pointer
                                     \s*("(?P<description>.*?)")\s*,       # description
                                     \s*"?(?P<arg_description>[^"}]*)"?    # arg description
                                  \s*},""", re.MULTILINE|re.DOTALL|re.VERBOSE)
    raw_entries_str = match.group("entries")

    start = 0
    entry_match = re_single_entry.search(raw_entries_str)
    i = 1
    while entry_match:
        long_name = entry_match.group("long_name").strip()
        short_name = entry_match.group("short_name").strip()
        description = entry_match.group("description").strip()
        arg_description = entry_match.group("arg_description").strip()

        # Normalize short name
        if short_name and (short_name == "NULL" or short_name == "0"):
            short_name = None

        # Normalize description
        description = description.replace('\\"', "\\\\'")
        description = description.replace("\\\\'", '"')
        description = description.replace('"', "")
        description = description.replace('\n', '')
        description = description.replace('\t', '')

        # Remove multiple whitespaces from description
        while True:
            new_description = description.replace("  ", " ")
            if new_description == description:
                break
            description = new_description
        description = description.strip()

        # Normalize arg_description
        if arg_description and (arg_description == "NULL" or arg_description == "0"):
            arg_description = None

        # Store option into list
        args.append({'long_name': long_name,
                     'short_name': short_name,
                     'description': description,
                     'arg_description': arg_description
                     })

        # Continue to next option
        i += 1
        start += entry_match.end(0)
        entry_match = re_single_entry.search(raw_entries_str[start:])
    # End while

    print >> sys.stderr, "Loaded %2d arguments" % (i,)
    return args



if __name__ == "__main__":
    parser = OptionParser('usage: %prog [options] <filename> [--mergerepo]')
    parser.add_option('-m', '--mergerepo', action="store_true", help="Gen rst for mergerepo")
    options, args = parser.parse_args()

    if len(args) < 1:
        print >> sys.stderr, "Error: Must specify a input filename. (Example: ../src/cmd_parser.c)"
        sys.exit(1)

    args = parse_arguments_from_c_file(args[0])

    if not options.mergerepo:
        NAME = "createrepo_c"
        info = Info(NAME,
                description="C implementation of createrepo",
                synopsis="%s [options] <directory>" % (NAME,),
                options=args)
    else:
        NAME = "mergerepo_c"
        info = Info(NAME,
                description="C implementation of mergerepo",
                synopsis="%s [options] <directory>" % (NAME,),
                options=args)

    ret = info.gen_rst()
    if not ret:
        print >> sys.stderr, "Error: Rst has not been generated"
        sys.exit(1)

    print ret