3 # Copyright (C) 2010-2013 CEA/DEN
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 In this file : all functions that do a system call,
21 like open a browser or an editor, or call a git command
30 import utilsSat as UTS
32 from . import printcolors
34 def show_in_editor(editor, filePath, logger):
35 '''open filePath using editor.
37 :param editor str: The editor to use.
38 :param filePath str: The path to the file to open.
40 # default editor is vi
41 if editor is None or len(editor) == 0:
44 if '%s' not in editor:
48 # launch cmd using subprocess.Popen
49 cmd = editor % filePath
50 logger.write('Launched command:\n' + cmd + '\n', 5)
51 p = subprocess.Popen(cmd, shell=True)
54 logger.write(printcolors.printcError(_("Unable to edit file %s\n")
58 def git_extract(from_what, tag, where, logger, environment=None):
59 '''Extracts sources from a git repository.
61 :param from_what str: The remote git repository.
62 :param tag str: The tag.
63 :param where str: The path where to extract.
64 :param logger Logger: The logger instance to use.
65 :param environment src.environment.Environ: The environment to source when extracting.
66 :return: True if the extraction is successful
69 DBG.write("git_extract", [from_what, tag, str(where)])
70 if not where.exists():
72 if tag == "master" or tag == "HEAD":
75 git clone %(remote)s %(where)s
77 cmd = cmd % {'remote': from_what, 'tag': tag, 'where': str(where)}
79 # NOTICE: this command only works with recent version of git
80 # because --work-tree does not work with an absolute path
81 where_git = os.path.join(str(where), ".git")
86 git clone %(remote)s %(where)s && \
87 git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s
89 cmd = cmd % {'remote': from_what,
92 'where_git': where_git}
95 logger.logTxtFile.write("\n" + cmd + "\n")
96 logger.logTxtFile.flush()
100 # git commands may fail sometimes for various raisons
101 # (big module, network troubles, tuleap maintenance)
102 # therefore we give several tries
104 max_number_of_tries = 3
105 sleep_delay = 30 # seconds
108 rc = UTS.Popen(cmd, cwd=str(where.dir()), env=environment.environ.environ, logger=logger)
109 if rc.isOk() or (i_try>=max_number_of_tries):
111 logger.write('\ngit command failed! Wait %d seconds and give an other try (%d/%d)\n' % \
112 (sleep_delay, i_try + 1, max_number_of_tries), 3)
113 time.sleep(sleep_delay) # wait a little
118 def git_extract_sub_dir(from_what, tag, where, sub_dir, logger, environment=None):
119 '''Extracts sources from a subtree sub_dir of a git repository.
121 :param from_what str: The remote git repository.
122 :param tag str: The tag.
123 :param where str: The path where to extract.
124 :param sub_dir str: The relative path of subtree to extract.
125 :param logger Logger: The logger instance to use.
126 :param environment src.environment.Environ: The environment to source when extracting.
127 :return: True if the extraction is successful
130 strWhere = str(where)
131 tmpWhere = strWhere + '_tmp'
132 parentWhere = os.path.dirname(strWhere)
133 if not os.path.exists(parentWhere):
134 logger.error("not existing directory: %s" % parentWhere)
136 if os.path.isdir(strWhere):
137 logger.error("do not override existing directory: %s" % strWhere)
139 aDict = {'remote': from_what,
143 'parentWhere': parentWhere,
144 'tmpWhere': tmpWhere,
146 DBG.write("git_extract_sub_dir", aDict)
150 export tmpDir=%(tmpWhere)s && \
152 git clone %(remote)s $tmpDir && \
154 git checkout %(tag)s && \
155 mv %(sub_dir)s %(where)s && \
156 git log -1 > %(where)s/README_git_log.txt && \
159 DBG.write("cmd", cmd)
161 for nbtry in range(0,3): # retries case of network problem
162 rc = UTS.Popen(cmd, cwd=parentWhere, env=environment.environ.environ, logger=logger)
164 time.sleep(30) # wait a little
168 def archive_extract(from_what, where, logger):
169 '''Extracts sources from an archive.
171 :param from_what str: The path to the archive.
172 :param where str: The path where to extract.
173 :param logger Logger: The logger instance to use.
174 :return: True if the extraction is successful
178 archive = tarfile.open(from_what)
179 for i in archive.getmembers():
180 archive.extract(i, path=str(where))
181 return True, os.path.commonprefix(archive.getnames())
182 except Exception as exc:
183 logger.write("archive_extract: %s\n" % exc)
186 def cvs_extract(protocol, user, server, base, tag, product, where,
187 logger, checkout=False, environment=None):
188 '''Extracts sources from a cvs repository.
190 :param protocol str: The cvs protocol.
191 :param user str: The user to be used.
192 :param server str: The remote cvs server.
194 :param tag str: The tag.
195 :param product str: The product.
196 :param where str: The path where to extract.
197 :param logger Logger: The logger instance to use.
198 :param checkout boolean: If true use checkout cvs.
199 :param environment src.environment.Environ: The environment to source when
201 :return: True if the extraction is successful
206 if tag is not None and len(tag) > 0:
212 elif len(opttag) == 0:
215 if len(protocol) > 0:
216 root = "%s@%s:%s" % (user, server, base)
217 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
218 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
219 'tag': opttag, 'product': product, 'command': cmd }
221 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
222 { 'root': server, 'base': base, 'where': str(where.base()),
223 'tag': opttag, 'product': product, 'command': cmd }
225 logger.write(command + "\n", 5)
227 if not where.dir().exists():
230 logger.logTxtFile.write("\n" + command + "\n")
231 logger.logTxtFile.flush()
232 res = subprocess.call(command,
233 cwd=str(where.dir()),
234 env=environment.environ.environ,
236 stdout=logger.logTxtFile,
237 stderr=subprocess.STDOUT)
240 def svn_extract(user,
247 '''Extracts sources from a svn repository.
249 :param user str: The user to be used.
250 :param from_what str: The remote git repository.
251 :param tag str: The tag.
252 :param where str: The path where to extract.
253 :param logger Logger: The logger instance to use.
254 :param checkout boolean: If true use checkout svn.
255 :param environment src.environment.Environ: The environment to source when
257 :return: True if the extraction is successful
260 if not where.exists():
264 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
265 { 'remote': from_what, 'user' : user, 'where': str(where) }
268 if os.path.exists(str(where)):
269 command = "/bin/rm -rf %(where)s && " % \
270 { 'remote': from_what, 'where': str(where) }
273 command += "svn export --username %(user)s %(remote)s %(where)s" % \
274 { 'remote': from_what, 'user' : user, 'where': str(where) }
276 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
277 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
279 logger.logTxtFile.write(command + "\n")
281 logger.write(command + "\n", 5)
282 logger.logTxtFile.write("\n" + command + "\n")
283 logger.logTxtFile.flush()
284 res = subprocess.call(command,
285 cwd=str(where.dir()),
286 env=environment.environ.environ,
288 stdout=logger.logTxtFile,
289 stderr=subprocess.STDOUT)