##// END OF EJS Templates
Merge pull request #3384 from ugurthemaster/master...
Min RK -
r10827:36b95ed6 merge
parent child Browse files
Show More
@@ -1,151 +1,151 b''
1 1 #!/usr/bin/env python
2 ''' Checkout gitwash repo into directory and do search replace on name '''
2 """ Checkout gitwash repo into directory and do search replace on name """
3 3
4 4 import os
5 5 from os.path import join as pjoin
6 6 import shutil
7 7 import sys
8 8 import re
9 9 import glob
10 10 import fnmatch
11 11 import tempfile
12 12 from subprocess import call
13 13
14 14
15 15 verbose = False
16 16
17 17
18 18 def clone_repo(url, branch):
19 19 cwd = os.getcwdu()
20 20 tmpdir = tempfile.mkdtemp()
21 21 try:
22 22 cmd = 'git clone %s %s' % (url, tmpdir)
23 23 call(cmd, shell=True)
24 24 os.chdir(tmpdir)
25 25 cmd = 'git checkout %s' % branch
26 26 call(cmd, shell=True)
27 27 except:
28 28 shutil.rmtree(tmpdir)
29 29 raise
30 30 finally:
31 31 os.chdir(cwd)
32 32 return tmpdir
33 33
34 34
35 35 def cp_files(in_path, globs, out_path):
36 36 try:
37 37 os.makedirs(out_path)
38 38 except OSError:
39 39 pass
40 40 out_fnames = []
41 41 for in_glob in globs:
42 42 in_glob_path = pjoin(in_path, in_glob)
43 43 for in_fname in glob.glob(in_glob_path):
44 44 out_fname = in_fname.replace(in_path, out_path)
45 45 pth, _ = os.path.split(out_fname)
46 46 if not os.path.isdir(pth):
47 47 os.makedirs(pth)
48 48 shutil.copyfile(in_fname, out_fname)
49 49 out_fnames.append(out_fname)
50 50 return out_fnames
51 51
52 52
53 53 def filename_search_replace(sr_pairs, filename, backup=False):
54 ''' Search and replace for expressions in files
54 """ Search and replace for expressions in files
55 55
56 '''
56 """
57 57 in_txt = open(filename, 'rt').read(-1)
58 58 out_txt = in_txt[:]
59 59 for in_exp, out_exp in sr_pairs:
60 60 in_exp = re.compile(in_exp)
61 61 out_txt = in_exp.sub(out_exp, out_txt)
62 62 if in_txt == out_txt:
63 63 return False
64 64 open(filename, 'wt').write(out_txt)
65 65 if backup:
66 66 open(filename + '.bak', 'wt').write(in_txt)
67 67 return True
68 68
69 69
70 70 def copy_replace(replace_pairs,
71 71 out_path,
72 72 repo_url,
73 73 repo_branch = 'master',
74 74 cp_globs=('*',),
75 75 rep_globs=('*',),
76 76 renames = ()):
77 77 repo_path = clone_repo(repo_url, repo_branch)
78 78 try:
79 79 out_fnames = cp_files(repo_path, cp_globs, out_path)
80 80 finally:
81 81 shutil.rmtree(repo_path)
82 82 renames = [(re.compile(in_exp), out_exp) for in_exp, out_exp in renames]
83 83 fnames = []
84 84 for rep_glob in rep_globs:
85 85 fnames += fnmatch.filter(out_fnames, rep_glob)
86 86 if verbose:
87 87 print '\n'.join(fnames)
88 88 for fname in fnames:
89 89 filename_search_replace(replace_pairs, fname, False)
90 90 for in_exp, out_exp in renames:
91 91 new_fname, n = in_exp.subn(out_exp, fname)
92 92 if n:
93 93 os.rename(fname, new_fname)
94 94 break
95 95
96 96
97 97 USAGE = ''' <output_directory> <project_name>
98 98
99 99 If not set with options, the repository name is the same as the <project
100 100 name>
101 101
102 102 If not set with options, the main github user is the same as the
103 103 repository name.'''
104 104
105 105
106 106 GITWASH_CENTRAL = 'git://github.com/matthew-brett/gitwash.git'
107 107 GITWASH_BRANCH = 'master'
108 108
109 109
110 110 if __name__ == '__main__':
111 111 from optparse import OptionParser
112 112 parser = OptionParser()
113 113 parser.set_usage(parser.get_usage().strip() + USAGE)
114 114 parser.add_option("--repo-name", dest="repo_name",
115 115 help="repository name - e.g. nitime",
116 116 metavar="REPO_NAME")
117 117 parser.add_option("--github-user", dest="main_gh_user",
118 118 help="github username for main repo - e.g fperez",
119 119 metavar="MAIN_GH_USER")
120 120 parser.add_option("--gitwash-url", dest="gitwash_url",
121 121 help="URL to gitwash repository - default %s"
122 122 % GITWASH_CENTRAL,
123 123 default=GITWASH_CENTRAL,
124 124 metavar="GITWASH_URL")
125 125 parser.add_option("--gitwash-branch", dest="gitwash_branch",
126 126 help="branch in gitwash repository - default %s"
127 127 % GITWASH_BRANCH,
128 128 default=GITWASH_BRANCH,
129 129 metavar="GITWASH_BRANCH")
130 130 parser.add_option("--source-suffix", dest="source_suffix",
131 131 help="suffix of ReST source files - default '.rst'",
132 132 default='.rst',
133 133 metavar="SOURCE_SUFFIX")
134 134 (options, args) = parser.parse_args()
135 135 if len(args) < 2:
136 136 parser.print_help()
137 137 sys.exit()
138 138 out_path, project_name = args
139 139 if options.repo_name is None:
140 140 options.repo_name = project_name
141 141 if options.main_gh_user is None:
142 142 options.main_gh_user = options.repo_name
143 143 copy_replace((('PROJECTNAME', project_name),
144 144 ('REPONAME', options.repo_name),
145 145 ('MAIN_GH_USER', options.main_gh_user)),
146 146 out_path,
147 147 options.gitwash_url,
148 148 options.gitwash_branch,
149 149 cp_globs=(pjoin('gitwash', '*'),),
150 150 rep_globs=('*.rst',),
151 151 renames=(('\.rst$', options.source_suffix),))
General Comments 0
You need to be logged in to leave comments. Login now