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
33 from . import printcolors
35 def show_in_editor(editor, filePath, logger):
36 '''open filePath using editor.
38 :param editor str: The editor to use.
39 :param filePath str: The path to the file to open.
41 # default editor is vi
42 if editor is None or len(editor) == 0:
45 if '%s' not in editor:
49 # launch cmd using subprocess.Popen
50 cmd = editor % filePath
51 logger.write('Launched command:\n' + cmd + '\n', 5)
52 p = subprocess.Popen(cmd, shell=True)
55 logger.write(printcolors.printcError(_("Unable to edit file %s\n")
58 def git_describe(repo_path):
59 '''Use git describe --tags command to return tag description of the git repository"
60 :param repo_path str: The git repository to describe
62 git_cmd="cd %s;git describe --tags" % repo_path
63 p = subprocess.Popen(git_cmd, shell=True,
64 stdin=subprocess.PIPE,
65 stdout=subprocess.PIPE,
66 stderr=subprocess.PIPE)
71 tag_description=p.stdout.readlines()[0].strip()
72 # with python3 this utf8 bytes should be decoded
73 if isinstance(tag_description, bytes):
74 tag_description=tag_description.decode("utf-8", "ignore")
75 return tag_description
78 def git_extract(from_what, tag, where, logger, environment=None):
79 '''Extracts sources from a git repository.
81 :param from_what str: The remote git repository.
82 :param tag str: The tag.
83 :param where str: The path where to extract.
84 :param logger Logger: The logger instance to use.
85 :param environment src.environment.Environ: The environment to source when extracting.
86 :return: True if the extraction is successful
89 DBG.write("git_extract", [from_what, tag, str(where)])
90 if not where.exists():
92 if tag == "master" or tag == "HEAD":
93 if src.architecture.is_windows():
94 cmd = "git clone %(remote)s %(where)s"
98 git clone %(remote)s %(where)s
100 cmd = cmd % {'remote': from_what, 'tag': tag, 'where': str(where)}
102 # NOTICE: this command only works with recent version of git
103 # because --work-tree does not work with an absolute path
104 where_git = os.path.join(str(where), ".git")
105 if src.architecture.is_windows():
106 cmd = "rmdir %(where)s && git clone %(remote)s %(where)s && git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
111 git clone %(remote)s %(where)s && \
112 git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s
114 cmd = cmd % {'remote': from_what,
117 'where_git': where_git}
120 logger.logTxtFile.write("\n" + cmd + "\n")
121 logger.logTxtFile.flush()
123 DBG.write("cmd", cmd)
125 # git commands may fail sometimes for various raisons
126 # (big module, network troubles, tuleap maintenance)
127 # therefore we give several tries
129 max_number_of_tries = 3
130 sleep_delay = 30 # seconds
133 rc = UTS.Popen(cmd, cwd=str(where.dir()), env=environment.environ.environ, logger=logger)
134 if rc.isOk() or (i_try>=max_number_of_tries):
136 logger.write('\ngit command failed! Wait %d seconds and give an other try (%d/%d)\n' % \
137 (sleep_delay, i_try + 1, max_number_of_tries), 3)
138 time.sleep(sleep_delay) # wait a little
143 def git_extract_sub_dir(from_what, tag, where, sub_dir, logger, environment=None):
144 '''Extracts sources from a subtree sub_dir of a git repository.
146 :param from_what str: The remote git repository.
147 :param tag str: The tag.
148 :param where str: The path where to extract.
149 :param sub_dir str: The relative path of subtree to extract.
150 :param logger Logger: The logger instance to use.
151 :param environment src.environment.Environ: The environment to source when extracting.
152 :return: True if the extraction is successful
155 strWhere = str(where)
156 tmpWhere = strWhere + '_tmp'
157 parentWhere = os.path.dirname(strWhere)
158 if not os.path.exists(parentWhere):
159 logger.error("not existing directory: %s" % parentWhere)
161 if os.path.isdir(strWhere):
162 logger.error("do not override existing directory: %s" % strWhere)
164 aDict = {'remote': from_what,
168 'parentWhere': parentWhere,
169 'tmpWhere': tmpWhere,
171 DBG.write("git_extract_sub_dir", aDict)
172 if not src.architecture.is_windows():
175 export tmpDir=%(tmpWhere)s && \
177 git clone %(remote)s $tmpDir && \
179 git checkout %(tag)s && \
180 mv %(sub_dir)s %(where)s && \
181 git log -1 > %(where)s/README_git_log.txt && \
187 set tmpDir=%(tmpWhere)s && \
189 git clone %(remote)s $tmpDir && \
191 git checkout %(tag)s && \
192 mv %(sub_dir)s %(where)s && \
193 git log -1 > %(where)s/README_git_log.txt && \
197 DBG.write("cmd", cmd)
199 for nbtry in range(0,3): # retries case of network problem
200 rc = UTS.Popen(cmd, cwd=parentWhere, env=environment.environ.environ, logger=logger)
202 time.sleep(30) # wait a little
206 def archive_extract(from_what, where, logger):
207 '''Extracts sources from an archive.
209 :param from_what str: The path to the archive.
210 :param where str: The path where to extract.
211 :param logger Logger: The logger instance to use.
212 :return: True if the extraction is successful
216 archive = tarfile.open(from_what)
217 for i in archive.getmembers():
218 archive.extract(i, path=str(where))
219 return True, os.path.commonprefix(archive.getnames())
220 except Exception as exc:
221 logger.write("archive_extract: %s\n" % exc)
224 def cvs_extract(protocol, user, server, base, tag, product, where,
225 logger, checkout=False, environment=None):
226 '''Extracts sources from a cvs repository.
228 :param protocol str: The cvs protocol.
229 :param user str: The user to be used.
230 :param server str: The remote cvs server.
232 :param tag str: The tag.
233 :param product str: The product.
234 :param where str: The path where to extract.
235 :param logger Logger: The logger instance to use.
236 :param checkout boolean: If true use checkout cvs.
237 :param environment src.environment.Environ: The environment to source when
239 :return: True if the extraction is successful
244 if tag is not None and len(tag) > 0:
250 elif len(opttag) == 0:
253 if len(protocol) > 0:
254 root = "%s@%s:%s" % (user, server, base)
255 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
256 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
257 'tag': opttag, 'product': product, 'command': cmd }
259 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
260 { 'root': server, 'base': base, 'where': str(where.base()),
261 'tag': opttag, 'product': product, 'command': cmd }
263 logger.write(command + "\n", 5)
265 if not where.dir().exists():
268 logger.logTxtFile.write("\n" + command + "\n")
269 logger.logTxtFile.flush()
270 res = subprocess.call(command,
271 cwd=str(where.dir()),
272 env=environment.environ.environ,
274 stdout=logger.logTxtFile,
275 stderr=subprocess.STDOUT)
278 def svn_extract(user,
285 '''Extracts sources from a svn repository.
287 :param user str: The user to be used.
288 :param from_what str: The remote git repository.
289 :param tag str: The tag.
290 :param where str: The path where to extract.
291 :param logger Logger: The logger instance to use.
292 :param checkout boolean: If true use checkout svn.
293 :param environment src.environment.Environ: The environment to source when
295 :return: True if the extraction is successful
298 if not where.exists():
302 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
303 { 'remote': from_what, 'user' : user, 'where': str(where) }
306 if os.path.exists(str(where)):
307 command = "/bin/rm -rf %(where)s && " % \
308 { 'remote': from_what, 'where': str(where) }
311 command += "svn export --username %(user)s %(remote)s %(where)s" % \
312 { 'remote': from_what, 'user' : user, 'where': str(where) }
314 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
315 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
317 logger.logTxtFile.write(command + "\n")
319 logger.write(command + "\n", 5)
320 logger.logTxtFile.write("\n" + command + "\n")
321 logger.logTxtFile.flush()
322 res = subprocess.call(command,
323 cwd=str(where.dir()),
324 env=environment.environ.environ,
326 stdout=logger.logTxtFile,
327 stderr=subprocess.STDOUT)
330 def get_pkg_check_cmd(dist_name):
331 '''Build the command to use for checking if a linux package is installed or not.'''
333 if dist_name in ["CO","FD","MG","MD","CO","OS"]: # linux using rpm
335 manager_msg_err="Error : command failed because sat was not able to find apt command"
338 manager_msg_err="Error : command failed because sat was not able to find rpm command"
340 # 1- search for an installed package manager (rpm on rh, apt on db)
341 cmd_which_rpm=["which", "rpm"]
342 cmd_which_apt=["which", "apt"]
343 with open(os.devnull, 'w') as devnull:
344 # 1) we search for apt (debian based systems)
345 completed=subprocess.call(cmd_which_apt,stdout=devnull, stderr=subprocess.STDOUT)
346 if completed==0 and linux=="DB":
347 cmd_is_package_installed=["apt", "list", "--installed"]
349 # 2) if apt not found search for rpm (redhat)
350 completed=subprocess.call(cmd_which_rpm,stdout=devnull, stderr=subprocess.STDOUT) # only 3.8! ,capture_output=True)
351 if completed==0 and linux=="RH":
352 cmd_is_package_installed=["rpm", "-q"]
354 # no package manager was found corresponding to dist_name
355 raise src.SatException(manager_msg_err)
356 return cmd_is_package_installed
358 def check_system_pkg(check_cmd,pkg):
359 '''Check if a package is installed
360 :param check_cmd list: the list of command to use system package manager
361 :param user str: the pkg name to check
363 :return: a string with package name with status un message
366 cmd_is_package_installed=[]
367 for cmd in check_cmd:
368 cmd_is_package_installed.append(cmd)
369 cmd_is_package_installed.append(pkg)
370 if check_cmd[0]=="apt":
371 # special treatment for apt
372 # (some debian packages have version numbers in their name, and also
373 # apt do not return status)
374 cmd_is_package_installed[-1]+="*" # we don't specify in pyconf the exact name because of version numbers
375 cmd_is_package_installed.append('|')
376 cmd_is_package_installed.append('grep') # add a grep to get an exit status
377 cmd_is_package_installed.append(cmd)
379 p=subprocess.Popen(cmd_is_package_installed,
380 stdout=subprocess.PIPE,
381 stderr=subprocess.PIPE)
382 output, err = p.communicate()
386 msg_status=src.printcolors.printcSuccess("OK")
387 if check_cmd[0]=="rpm": # apt output is too messy for being used
388 # in python3 output is a byte and should be decoded
389 if isinstance(output, bytes):
390 output = output.decode("utf-8", "ignore")
391 msg_status+=" (" + output.replace('\n',' ') + ")\n" # remove output trailing \n
393 msg_status=src.printcolors.printcError("KO")
394 msg_status+=" (package is not installed!)\n"