Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / tools / gitwash_dumper.py @ 5cef0f13

History | View | Annotate | Download (7.85 KB)

1
#!/usr/bin/env python
2
''' Checkout gitwash repo into directory and do search replace on name '''
3

    
4
from __future__ import (absolute_import, division, print_function)
5

    
6
import os
7
from os.path import join as pjoin
8
import shutil
9
import sys
10
import re
11
import glob
12
import fnmatch
13
import tempfile
14
from subprocess import call
15
from optparse import OptionParser
16

    
17
verbose = False
18

    
19

    
20
def clone_repo(url, branch):
21
    cwd = os.getcwd()
22
    tmpdir = tempfile.mkdtemp()
23
    try:
24
        cmd = 'git clone %s %s' % (url, tmpdir)
25
        call(cmd, shell=True)
26
        os.chdir(tmpdir)
27
        cmd = 'git checkout %s' % branch
28
        call(cmd, shell=True)
29
    except:
30
        shutil.rmtree(tmpdir)
31
        raise
32
    finally:
33
        os.chdir(cwd)
34
    return tmpdir
35

    
36

    
37
def cp_files(in_path, globs, out_path):
38
    try:
39
        os.makedirs(out_path)
40
    except OSError:
41
        pass
42
    out_fnames = []
43
    for in_glob in globs:
44
        in_glob_path = pjoin(in_path, in_glob)
45
        for in_fname in glob.glob(in_glob_path):
46
            out_fname = in_fname.replace(in_path, out_path)
47
            pth, _ = os.path.split(out_fname)
48
            if not os.path.isdir(pth):
49
                os.makedirs(pth)
50
            shutil.copyfile(in_fname, out_fname)
51
            out_fnames.append(out_fname)
52
    return out_fnames
53

    
54

    
55
def filename_search_replace(sr_pairs, filename, backup=False):
56
    ''' Search and replace for expressions in files
57

58
    '''
59
    with open(filename, 'rt') as in_fh:
60
        in_txt = in_fh.read(-1)
61
    out_txt = in_txt[:]
62
    for in_exp, out_exp in sr_pairs:
63
        in_exp = re.compile(in_exp)
64
        out_txt = in_exp.sub(out_exp, out_txt)
65
    if in_txt == out_txt:
66
        return False
67
    with open(filename, 'wt') as out_fh:
68
        out_fh.write(out_txt)
69
    if backup:
70
        with open(filename + '.bak', 'wt') as bak_fh:
71
            bak_fh.write(in_txt)
72
    return True
73

    
74

    
75
def copy_replace(replace_pairs,
76
                 repo_path,
77
                 out_path,
78
                 cp_globs=('*',),
79
                 rep_globs=('*',),
80
                 renames = ()):
81
    out_fnames = cp_files(repo_path, cp_globs, out_path)
82
    renames = [(re.compile(in_exp), out_exp) for in_exp, out_exp in renames]
83
    fnames = []
84
    for rep_glob in rep_globs:
85
        fnames += fnmatch.filter(out_fnames, rep_glob)
86
    if verbose:
87
        print('\n'.join(fnames))
88
    for fname in fnames:
89
        filename_search_replace(replace_pairs, fname, False)
90
        for in_exp, out_exp in renames:
91
            new_fname, n = in_exp.subn(out_exp, fname)
92
            if n:
93
                os.rename(fname, new_fname)
94
                break
95

    
96

    
97
def make_link_targets(proj_name,
98
                      user_name,
99
                      repo_name,
100
                      known_link_fname,
101
                      out_link_fname,
102
                      url=None,
103
                      ml_url=None):
104
    """ Check and make link targets
105

106
    If url is None or ml_url is None, check if there are links present for these
107
    in `known_link_fname`.  If not, raise error.  The check is:
108

109
    Look for a target `proj_name`.
110
    Look for a target `proj_name` + ' mailing list'
111

112
    Also, look for a target `proj_name` + 'github'.  If this exists, don't write
113
    this target into the new file below.
114

115
    If we are writing any of the url, ml_url, or github address, then write new
116
    file with these links, of form:
117

118
    .. _`proj_name`
119
    .. _`proj_name`: url
120
    .. _`proj_name` mailing list: url
121
    """
122
    with open(known_link_fname, 'rt') as link_fh:
123
        link_contents = link_fh.readlines()
124
    have_url = not url is None
125
    have_ml_url = not ml_url is None
126
    have_gh_url = None
127
    for line in link_contents:
128
        if not have_url:
129
            match = re.match(r'..\s+_`%s`:\s+' % proj_name, line)
130
            if match:
131
                have_url = True
132
        if not have_ml_url:
133
            match = re.match(r'..\s+_`%s mailing list`:\s+' % proj_name, line)
134
            if match:
135
                have_ml_url = True
136
        if not have_gh_url:
137
            match = re.match(r'..\s+_`%s github`:\s+' % proj_name, line)
138
            if match:
139
                have_gh_url = True
140
    if not have_url or not have_ml_url:
141
        raise RuntimeError('Need command line or known project '
142
                           'and / or mailing list URLs')
143
    lines = []
144
    if not url is None:
145
        lines.append('.. _`%s`: %s\n' % (proj_name, url))
146
    if not have_gh_url:
147
        gh_url = 'https://github.com/%s/%s\n' % (user_name, repo_name)
148
        lines.append('.. _`%s github`: %s\n' % (proj_name, gh_url))
149
    if not ml_url is None:
150
        lines.append('.. _`%s mailing list`: %s\n' % (proj_name, ml_url))
151
    if len(lines) == 0:
152
        # Nothing to do
153
        return
154
    # A neat little header line
155
    lines = ['.. %s\n' % proj_name] + lines
156
    with open(out_link_fname, 'wt') as out_links:
157
        out_links.writelines(lines)
158

    
159

    
160
USAGE = ''' <output_directory> <project_name>
161

162
If not set with options, the repository name is the same as the <project
163
name>
164

165
If not set with options, the main github user is the same as the
166
repository name.'''
167

    
168

    
169
GITWASH_CENTRAL = 'git://github.com/matthew-brett/gitwash.git'
170
GITWASH_BRANCH = 'master'
171

    
172

    
173
def main():
174
    parser = OptionParser()
175
    parser.set_usage(parser.get_usage().strip() + USAGE)
176
    parser.add_option("--repo-name", dest="repo_name",
177
                      help="repository name - e.g. nitime",
178
                      metavar="REPO_NAME")
179
    parser.add_option("--github-user", dest="main_gh_user",
180
                      help="github username for main repo - e.g fperez",
181
                      metavar="MAIN_GH_USER")
182
    parser.add_option("--gitwash-url", dest="gitwash_url",
183
                      help="URL to gitwash repository - default %s"
184
                      % GITWASH_CENTRAL,
185
                      default=GITWASH_CENTRAL,
186
                      metavar="GITWASH_URL")
187
    parser.add_option("--gitwash-branch", dest="gitwash_branch",
188
                      help="branch in gitwash repository - default %s"
189
                      % GITWASH_BRANCH,
190
                      default=GITWASH_BRANCH,
191
                      metavar="GITWASH_BRANCH")
192
    parser.add_option("--source-suffix", dest="source_suffix",
193
                      help="suffix of ReST source files - default '.rst'",
194
                      default='.rst',
195
                      metavar="SOURCE_SUFFIX")
196
    parser.add_option("--project-url", dest="project_url",
197
                      help="URL for project web pages",
198
                      default=None,
199
                      metavar="PROJECT_URL")
200
    parser.add_option("--project-ml-url", dest="project_ml_url",
201
                      help="URL for project mailing list",
202
                      default=None,
203
                      metavar="PROJECT_ML_URL")
204
    (options, args) = parser.parse_args()
205
    if len(args) < 2:
206
        parser.print_help()
207
        sys.exit()
208
    out_path, project_name = args
209
    if options.repo_name is None:
210
        options.repo_name = project_name
211
    if options.main_gh_user is None:
212
        options.main_gh_user = options.repo_name
213
    repo_path = clone_repo(options.gitwash_url, options.gitwash_branch)
214
    try:
215
        copy_replace((('PROJECTNAME', project_name),
216
                      ('REPONAME', options.repo_name),
217
                      ('MAIN_GH_USER', options.main_gh_user)),
218
                     repo_path,
219
                     out_path,
220
                     cp_globs=(pjoin('gitwash', '*'),),
221
                     rep_globs=('*.rst',),
222
                     renames=(('\.rst$', options.source_suffix),))
223
        make_link_targets(project_name,
224
                          options.main_gh_user,
225
                          options.repo_name,
226
                          pjoin(out_path, 'gitwash', 'known_projects.inc'),
227
                          pjoin(out_path, 'gitwash', 'this_project.inc'),
228
                          options.project_url,
229
                          options.project_ml_url)
230
    finally:
231
        shutil.rmtree(repo_path)
232

    
233

    
234
if __name__ == '__main__':
235
    main()