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 # Extracts sources from a git repository.
55 def git_extract(from_what, tag, where, logger):
56 if not where.exists():
58 if tag == "master" or tag == "HEAD":
59 command = "git clone %(remote)s %(where)s" % \
60 { 'remote': from_what, 'tag': tag, 'where': str(where) }
62 # NOTICE: this command only works with recent version of git
63 # because --work-tree does not work with an absolute path
64 where_git = os.path.join( str(where), ".git" )
65 command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
66 "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
67 command = command % { 'remote': from_what, 'tag': tag, 'where': str(where), 'where_git': where_git }
69 logger.write(command + "\n", 5)
71 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
72 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
75 def archive_extract(from_what, where, logger):
77 archive = tarfile.open(from_what)
78 for i in archive.getmembers():
79 archive.extract(i, path=str(where))
80 return True, os.path.commonprefix(archive.getnames())
81 except Exception as exc:
82 logger.write("archive_extract: %s\n" % exc)
85 def cvs_extract(protocol, user, server, base, tag, module, where,
86 logger, checkout=False):
89 if tag is not None and len(tag) > 0:
95 elif len(opttag) == 0:
99 root = "%s@%s:%s" % (user, server, base)
100 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(module)s" % \
101 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
102 'tag': opttag, 'module': module, 'command': cmd }
104 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(module)s" % \
105 { 'root': server, 'base': base, 'where': str(where.base()),
106 'tag': opttag, 'module': module, 'command': cmd }
108 logger.logTxtFile.write(command + "\n")
109 logger.write(command + "\n", 5)
111 if not where.dir().exists():
114 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
115 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
118 def svn_extract(user, from_what, tag, where, logger, checkout=False):
119 if not where.exists():
123 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
124 { 'remote': from_what, 'user' : user, 'where': str(where) }
127 if os.path.exists(str(where)):
128 command = "/bin/rm -rf %(where)s && " % \
129 { 'remote': from_what, 'where': str(where) }
132 command += "svn export --username %(user)s %(remote)s %(where)s" % \
133 { 'remote': from_what, 'user' : user, 'where': str(where) }
135 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
136 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
138 logger.logTxtFile.write(command + "\n")
140 logger.write(command + "\n", 5)
141 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
142 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)