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 logger.logTxtFile.write("\n" + command + "\n")
72 logger.logTxtFile.flush()
73 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
74 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
77 def archive_extract(from_what, where, logger):
79 archive = tarfile.open(from_what)
80 for i in archive.getmembers():
81 archive.extract(i, path=str(where))
82 return True, os.path.commonprefix(archive.getnames())
83 except Exception as exc:
84 logger.write("archive_extract: %s\n" % exc)
87 def cvs_extract(protocol, user, server, base, tag, product, where,
88 logger, checkout=False):
91 if tag is not None and len(tag) > 0:
97 elif len(opttag) == 0:
100 if len(protocol) > 0:
101 root = "%s@%s:%s" % (user, server, base)
102 command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
103 { 'protocol': protocol, 'root': root, 'where': str(where.base()),
104 'tag': opttag, 'product': product, 'command': cmd }
106 command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
107 { 'root': server, 'base': base, 'where': str(where.base()),
108 'tag': opttag, 'product': product, 'command': cmd }
110 logger.write(command + "\n", 5)
112 if not where.dir().exists():
115 logger.logTxtFile.write("\n" + command + "\n")
116 logger.logTxtFile.flush()
117 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
118 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
121 def svn_extract(user, from_what, tag, where, logger, checkout=False):
122 if not where.exists():
126 command = "svn checkout --username %(user)s %(remote)s %(where)s" % \
127 { 'remote': from_what, 'user' : user, 'where': str(where) }
130 if os.path.exists(str(where)):
131 command = "/bin/rm -rf %(where)s && " % \
132 { 'remote': from_what, 'where': str(where) }
135 command += "svn export --username %(user)s %(remote)s %(where)s" % \
136 { 'remote': from_what, 'user' : user, 'where': str(where) }
138 command += "svn export -r %(tag)s --username %(user)s %(remote)s %(where)s" % \
139 { 'tag' : tag, 'remote': from_what, 'user' : user, 'where': str(where) }
141 logger.logTxtFile.write(command + "\n")
143 logger.write(command + "\n", 5)
144 logger.logTxtFile.write("\n" + command + "\n")
145 logger.logTxtFile.flush()
146 res = subprocess.call(command, cwd=str(where.dir()), shell=True,
147 stdout=logger.logTxtFile, stderr=subprocess.STDOUT)