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
28 from . import printcolors
30 def show_in_editor(editor, filePath, logger):
31 '''open filePath using editor.
33 :param editor str: The editor to use.
34 :param filePath str: The path to the file to open.
36 # default editor is vi
37 if editor is None or len(editor) == 0:
40 if '%s' not in editor:
44 # launch cmd using subprocess.Popen
45 cmd = editor % filePath
46 logger.write('Launched command:\n' + cmd + '\n', 5)
47 p = subprocess.Popen(cmd, shell=True)
50 logger.write(printcolors.printcError(_("Unable to edit file %s\n")
54 def git_extract(from_what, tag, where, logger, environment=None):
55 '''Extracts sources from a git repository.
57 :param from_what str: The remote git repository.
58 :param tag str: The tag.
59 :param where str: The path where to extract.
60 :param logger Logger: The logger instance to use.
61 :param environment src.environment.Environ: The environment to source when
63 :return: True if the extraction is successful
66 if not where.exists():
68 if tag == "master" or tag == "HEAD":
69 command = "git clone %(remote)s %(where)s" % \
70 { 'remote': from_what, 'tag': tag, 'where': str(where) }
72 # NOTICE: this command only works with recent version of git
73 # because --work-tree does not work with an absolute path
74 where_git = os.path.join( str(where), ".git" )
75 command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
76 "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
77 command = command % {'remote': from_what,
80 'where_git': where_git }
82 logger.write(command + "\n", 5)
84 logger.logTxtFile.write("\n" + command + "\n")
85 logger.logTxtFile.flush()
86 res = subprocess.call(command,
88 env=environment.environ.environ,
90 stdout=logger.logTxtFile,
91 stderr=subprocess.STDOUT)
94 def archive_extract(from_what, where, logger):
95 '''Extracts sources from an archive.
97 :param from_what str: The path to the archive.
98 :param where str: The path where to extract.
99 :param logger Logger: The logger instance to use.
100 :return: True if the extraction is successful
104 archive = tarfile.open(from_what)
105 for i in archive.getmembers():
106 archive.extract(i, path=str(where))
107 return True, os.path.commonprefix(archive.getnames())
108 except Exception as exc:
109 logger.write("archive_extract: %s\n" % exc)
112 def cvs_extract(protocol, user, server, base, tag, product, where,
113 logger, checkout=False, environment=None):
114 '''Extracts sources from a cvs repository.
116 :param protocol str: The cvs protocol.
117 :param user str: The user to be used.
118 :param server str: The remote cvs server.
120 :param tag str: The tag.
121 :param product str: The product.
122 :param where str: The path where to extract.
123 :param logger Logger: The logger instance to use.
124 :param checkout boolean: If true use checkout cvs.
125 :param environment src.environment.Environ: The environment to source when
127 :return: True if the extraction is successful
132 if tag is not None and len(tag) > 0:
138 elif len(opttag) == 0:
141 if len(protocol) > 0:
142 root = "%s@%s:%s" % (user, server, base)
143 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
144 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
145 'tag': opttag, 'product': product, 'command': cmd }
147 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
148 { 'root': server, 'base': base, 'where': str(where.base()),
149 'tag': opttag, 'product': product, 'command': cmd }
151 logger.write(command + "\n", 5)
153 if not where.dir().exists():
156 logger.logTxtFile.write("\n" + command + "\n")
157 logger.logTxtFile.flush()
158 res = subprocess.call(command,
159 cwd=str(where.dir()),
160 env=environment.environ.environ,
162 stdout=logger.logTxtFile,
163 stderr=subprocess.STDOUT)
166 def svn_extract(user,
173 '''Extracts sources from a svn repository.
175 :param user str: The user to be used.
176 :param from_what str: The remote git repository.
177 :param tag str: The tag.
178 :param where str: The path where to extract.
179 :param logger Logger: The logger instance to use.
180 :param checkout boolean: If true use checkout svn.
181 :param environment src.environment.Environ: The environment to source when
183 :return: True if the extraction is successful
186 if not where.exists():
190 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
191 { 'remote': from_what, 'user' : user, 'where': str(where) }
194 if os.path.exists(str(where)):
195 command = "/bin/rm -rf %(where)s && " % \
196 { 'remote': from_what, 'where': str(where) }
199 command += "svn export --username %(user)s %(remote)s %(where)s" % \
200 { 'remote': from_what, 'user' : user, 'where': str(where) }
202 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
203 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
205 logger.logTxtFile.write(command + "\n")
207 logger.write(command + "\n", 5)
208 logger.logTxtFile.write("\n" + command + "\n")
209 logger.logTxtFile.flush()
210 res = subprocess.call(command,
211 cwd=str(where.dir()),
212 env=environment.environ.environ,
214 stdout=logger.logTxtFile,
215 stderr=subprocess.STDOUT)