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
29 import utilsSat as UTS
31 from . import printcolors
33 def show_in_editor(editor, filePath, logger):
34 '''open filePath using editor.
36 :param editor str: The editor to use.
37 :param filePath str: The path to the file to open.
39 # default editor is vi
40 if editor is None or len(editor) == 0:
43 if '%s' not in editor:
47 # launch cmd using subprocess.Popen
48 cmd = editor % filePath
49 logger.write('Launched command:\n' + cmd + '\n', 5)
50 p = subprocess.Popen(cmd, shell=True)
53 logger.write(printcolors.printcError(_("Unable to edit file %s\n")
57 def git_extract(from_what, tag, where, logger, environment=None):
58 '''Extracts sources from a git repository.
60 :param from_what str: The remote git repository.
61 :param tag str: The tag.
62 :param where str: The path where to extract.
63 :param logger Logger: The logger instance to use.
64 :param environment src.environment.Environ: The environment to source when extracting.
65 :return: True if the extraction is successful
68 DBG.write("git_extract", [from_what, tag, str(where)])
69 if not where.exists():
71 if tag == "master" or tag == "HEAD":
74 git clone %(remote)s %(where)s
76 cmd = cmd % {'remote': from_what, 'tag': tag, 'where': str(where)}
78 # NOTICE: this command only works with recent version of git
79 # because --work-tree does not work with an absolute path
80 where_git = os.path.join(str(where), ".git")
85 git clone %(remote)s %(where)s && \
86 git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s
88 cmd = cmd % {'remote': from_what,
91 'where_git': where_git}
94 logger.logTxtFile.write("\n" + cmd + "\n")
95 logger.logTxtFile.flush()
98 rc = UTS.Popen(cmd, cwd=str(where.dir()), env=environment.environ.environ, logger=logger)
102 res = subprocess.call(command,
103 cwd=str(where.dir()),
104 env=environment.environ.environ,
106 stdout=logger.logTxtFile,
107 stderr=subprocess.STDOUT)
113 def git_extract_sub_dir(from_what, tag, where, sub_dir, logger, environment=None):
114 '''Extracts sources from a subtree sub_dir of a git repository.
116 :param from_what str: The remote git repository.
117 :param tag str: The tag.
118 :param where str: The path where to extract.
119 :param sub_dir str: The relative path of subtree to extract.
120 :param logger Logger: The logger instance to use.
121 :param environment src.environment.Environ: The environment to source when extracting.
122 :return: True if the extraction is successful
125 strWhere = str(where)
126 tmpWhere = strWhere + '_tmp'
127 parentWhere = os.path.dirname(strWhere)
128 if not os.path.exists(parentWhere):
129 logger.error("not existing directory: %s" % parentWhere)
131 if os.path.isdir(strWhere):
132 logger.error("do not override existing directory: %s" % strWhere)
134 aDict = {'remote': from_what,
138 'parentWhere': parentWhere,
139 'tmpWhere': tmpWhere,
141 DBG.write("git_extract_sub_dir", aDict)
145 export tmpDir=%(tmpWhere)s && \
147 git clone %(remote)s $tmpDir && \
149 git checkout %(tag)s && \
150 mv %(sub_dir)s %(where)s && \
151 git log -1 > %(where)s/README_git_log.txt && \
154 DBG.write("cmd", cmd)
155 rc = UTS.Popen(cmd, cwd=parentWhere, env=environment.environ.environ, logger=logger)
159 def archive_extract(from_what, where, logger):
160 '''Extracts sources from an archive.
162 :param from_what str: The path to the archive.
163 :param where str: The path where to extract.
164 :param logger Logger: The logger instance to use.
165 :return: True if the extraction is successful
169 archive = tarfile.open(from_what)
170 for i in archive.getmembers():
171 archive.extract(i, path=str(where))
172 return True, os.path.commonprefix(archive.getnames())
173 except Exception as exc:
174 logger.write("archive_extract: %s\n" % exc)
177 def cvs_extract(protocol, user, server, base, tag, product, where,
178 logger, checkout=False, environment=None):
179 '''Extracts sources from a cvs repository.
181 :param protocol str: The cvs protocol.
182 :param user str: The user to be used.
183 :param server str: The remote cvs server.
185 :param tag str: The tag.
186 :param product str: The product.
187 :param where str: The path where to extract.
188 :param logger Logger: The logger instance to use.
189 :param checkout boolean: If true use checkout cvs.
190 :param environment src.environment.Environ: The environment to source when
192 :return: True if the extraction is successful
197 if tag is not None and len(tag) > 0:
203 elif len(opttag) == 0:
206 if len(protocol) > 0:
207 root = "%s@%s:%s" % (user, server, base)
208 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
209 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
210 'tag': opttag, 'product': product, 'command': cmd }
212 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
213 { 'root': server, 'base': base, 'where': str(where.base()),
214 'tag': opttag, 'product': product, 'command': cmd }
216 logger.write(command + "\n", 5)
218 if not where.dir().exists():
221 logger.logTxtFile.write("\n" + command + "\n")
222 logger.logTxtFile.flush()
223 res = subprocess.call(command,
224 cwd=str(where.dir()),
225 env=environment.environ.environ,
227 stdout=logger.logTxtFile,
228 stderr=subprocess.STDOUT)
231 def svn_extract(user,
238 '''Extracts sources from a svn repository.
240 :param user str: The user to be used.
241 :param from_what str: The remote git repository.
242 :param tag str: The tag.
243 :param where str: The path where to extract.
244 :param logger Logger: The logger instance to use.
245 :param checkout boolean: If true use checkout svn.
246 :param environment src.environment.Environ: The environment to source when
248 :return: True if the extraction is successful
251 if not where.exists():
255 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
256 { 'remote': from_what, 'user' : user, 'where': str(where) }
259 if os.path.exists(str(where)):
260 command = "/bin/rm -rf %(where)s && " % \
261 { 'remote': from_what, 'where': str(where) }
264 command += "svn export --username %(user)s %(remote)s %(where)s" % \
265 { 'remote': from_what, 'user' : user, 'where': str(where) }
267 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
268 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
270 logger.logTxtFile.write(command + "\n")
272 logger.write(command + "\n", 5)
273 logger.logTxtFile.write("\n" + command + "\n")
274 logger.logTxtFile.flush()
275 res = subprocess.call(command,
276 cwd=str(where.dir()),
277 env=environment.environ.environ,
279 stdout=logger.logTxtFile,
280 stderr=subprocess.STDOUT)