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):
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 :return: True if the extraction is successful
64 if not where.exists():
66 if tag == "master" or tag == "HEAD":
67 command = "git clone %(remote)s %(where)s" % \
68 { 'remote': from_what, 'tag': tag, 'where': str(where) }
70 # NOTICE: this command only works with recent version of git
71 # because --work-tree does not work with an absolute path
72 where_git = os.path.join( str(where), ".git" )
73 command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
74 "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
75 command = command % {'remote': from_what,
78 'where_git': where_git }
80 logger.write(command + "\n", 5)
82 logger.logTxtFile.write("\n" + command + "\n")
83 logger.logTxtFile.flush()
84 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
85 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
88 def archive_extract(from_what, where, logger):
89 '''Extracts sources from an archive.
91 :param from_what str: The path to the archive.
92 :param where str: The path where to extract.
93 :param logger Logger: The logger instance to use.
94 :return: True if the extraction is successful
98 archive = tarfile.open(from_what)
99 for i in archive.getmembers():
100 archive.extract(i, path=str(where))
101 return True, os.path.commonprefix(archive.getnames())
102 except Exception as exc:
103 logger.write("archive_extract: %s\n" % exc)
106 def cvs_extract(protocol, user, server, base, tag, product, where,
107 logger, checkout=False):
108 '''Extracts sources from a cvs repository.
110 :param protocol str: The cvs protocol.
111 :param user str: The user to be used.
112 :param server str: The remote cvs server.
114 :param tag str: The tag.
115 :param product str: The product.
116 :param where str: The path where to extract.
117 :param logger Logger: The logger instance to use.
118 :param checkout boolean: If true use checkout cvs.
119 :return: True if the extraction is successful
124 if tag is not None and len(tag) > 0:
130 elif len(opttag) == 0:
133 if len(protocol) > 0:
134 root = "%s@%s:%s" % (user, server, base)
135 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
136 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
137 'tag': opttag, 'product': product, 'command': cmd }
139 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
140 { 'root': server, 'base': base, 'where': str(where.base()),
141 'tag': opttag, 'product': product, 'command': cmd }
143 logger.write(command + "\n", 5)
145 if not where.dir().exists():
148 logger.logTxtFile.write("\n" + command + "\n")
149 logger.logTxtFile.flush()
150 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
151 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
154 def svn_extract(user, from_what, tag, where, logger, checkout=False):
155 '''Extracts sources from a svn repository.
157 :param user str: The user to be used.
158 :param from_what str: The remote git repository.
159 :param tag str: The tag.
160 :param where str: The path where to extract.
161 :param logger Logger: The logger instance to use.
162 :param checkout boolean: If true use checkout svn.
163 :return: True if the extraction is successful
166 if not where.exists():
170 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
171 { 'remote': from_what, 'user' : user, 'where': str(where) }
174 if os.path.exists(str(where)):
175 command = "/bin/rm -rf %(where)s && " % \
176 { 'remote': from_what, 'where': str(where) }
179 command += "svn export --username %(user)s %(remote)s %(where)s" % \
180 { 'remote': from_what, 'user' : user, 'where': str(where) }
182 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
183 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
185 logger.logTxtFile.write(command + "\n")
187 logger.write(command + "\n", 5)
188 logger.logTxtFile.write("\n" + command + "\n")
189 logger.logTxtFile.flush()
190 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
191 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)