Salome HOME
sat source: Adding the application environment when getting sources of a product
[tools/sat.git] / src / system.py
index 7761cef53d6778730bbdcda7fd5c657e41cefd54..78e9946b6f50f4ca4c5050e9f3af9ad3731786c0 100644 (file)
@@ -50,9 +50,19 @@ def show_in_editor(editor, filePath, logger):
         logger.write(printcolors.printcError(_("Unable to edit file %s\n") 
                                              % filePath), 1)
 
-##
-# Extracts sources from a git repository.
-def git_extract(from_what, tag, where, logger):
+
+def git_extract(from_what, tag, where, logger, environment=None):
+    '''Extracts sources from a git repository.
+    
+    :param from_what str: The remote git repository.
+    :param tag str: The tag.
+    :param where str: The path where to extract.
+    :param logger Logger: The logger instance to use.
+    :param environment src.environment.Environ: The environment to source when
+                                                extracting.
+    :return: True if the extraction is successful
+    :rtype: boolean
+    '''
     if not where.exists():
         where.make()
     if tag == "master" or tag == "HEAD":
@@ -64,15 +74,32 @@ def git_extract(from_what, tag, where, logger):
         where_git = os.path.join( str(where), ".git" )
         command = "rmdir %(where)s && git clone %(remote)s %(where)s && " + \
                   "git --git-dir=%(where_git)s --work-tree=%(where)s checkout %(tag)s"
-        command = command % { 'remote': from_what, 'tag': tag, 'where': str(where), 'where_git': where_git }
+        command = command % {'remote': from_what, 
+                             'tag': tag, 
+                             'where': str(where), 
+                             'where_git': where_git }
 
     logger.write(command + "\n", 5)
 
-    res = subprocess.call(command, cwd=str(where.dir()), shell=True,
-                          stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
+    logger.logTxtFile.write("\n" + command + "\n")
+    logger.logTxtFile.flush()
+    res = subprocess.call(command,
+                          cwd=str(where.dir()),
+                          env=environment.environ.environ,
+                          shell=True,
+                          stdout=logger.logTxtFile,
+                          stderr=subprocess.STDOUT)
     return (res == 0)
 
 def archive_extract(from_what, where, logger):
+    '''Extracts sources from an archive.
+    
+    :param from_what str: The path to the archive.
+    :param where str: The path where to extract.
+    :param logger Logger: The logger instance to use.
+    :return: True if the extraction is successful
+    :rtype: boolean
+    '''
     try:
         archive = tarfile.open(from_what)
         for i in archive.getmembers():
@@ -82,8 +109,24 @@ def archive_extract(from_what, where, logger):
         logger.write("archive_extract: %s\n" % exc)
         return False, None
 
-def cvs_extract(protocol, user, server, base, tag, module, where,
-                logger, checkout=False):
+def cvs_extract(protocol, user, server, base, tag, product, where,
+                logger, checkout=False, environment=None):
+    '''Extracts sources from a cvs repository.
+    
+    :param protocol str: The cvs protocol.
+    :param user str: The user to be used.
+    :param server str: The remote cvs server.
+    :param base str: .
+    :param tag str: The tag.
+    :param product str: The product.
+    :param where str: The path where to extract.
+    :param logger Logger: The logger instance to use.
+    :param checkout boolean: If true use checkout cvs.
+    :param environment src.environment.Environ: The environment to source when
+                                                extracting.
+    :return: True if the extraction is successful
+    :rtype: boolean
+    '''
 
     opttag = ''
     if tag is not None and len(tag) > 0:
@@ -97,25 +140,49 @@ def cvs_extract(protocol, user, server, base, tag, module, where,
     
     if len(protocol) > 0:
         root = "%s@%s:%s" % (user, server, base)
-        command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(module)s" % \
+        command = "cvs -d :%(protocol)s:%(root)s %(command)s -d %(where)s %(tag)s %(product)s" % \
             { 'protocol': protocol, 'root': root, 'where': str(where.base()),
-              'tag': opttag, 'module': module, 'command': cmd }
+              'tag': opttag, 'product': product, 'command': cmd }
     else:
-        command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(module)s" % \
+        command = "cvs -d %(root)s %(command)s -d %(where)s %(tag)s %(base)s/%(product)s" % \
             { 'root': server, 'base': base, 'where': str(where.base()),
-              'tag': opttag, 'module': module, 'command': cmd }
+              'tag': opttag, 'product': product, 'command': cmd }
 
-    logger.logTxtFile.write(command + "\n")
     logger.write(command + "\n", 5)
 
     if not where.dir().exists():
         where.dir().make()
-        
-    res = subprocess.call(command, cwd=str(where.dir()), shell=True,
-                          stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
+
+    logger.logTxtFile.write("\n" + command + "\n")
+    logger.logTxtFile.flush()        
+    res = subprocess.call(command,
+                          cwd=str(where.dir()),
+                          env=environment.environ.environ,
+                          shell=True,
+                          stdout=logger.logTxtFile,
+                          stderr=subprocess.STDOUT)
     return (res == 0)
 
-def svn_extract(user, from_what, tag, where, logger, checkout=False):
+def svn_extract(user,
+                from_what,
+                tag,
+                where,
+                logger,
+                checkout=False,
+                environment=None):
+    '''Extracts sources from a svn repository.
+    
+    :param user str: The user to be used.
+    :param from_what str: The remote git repository.
+    :param tag str: The tag.
+    :param where str: The path where to extract.
+    :param logger Logger: The logger instance to use.
+    :param checkout boolean: If true use checkout svn.
+    :param environment src.environment.Environ: The environment to source when
+                                                extracting.
+    :return: True if the extraction is successful
+    :rtype: boolean
+    '''
     if not where.exists():
         where.make()
 
@@ -138,6 +205,12 @@ def svn_extract(user, from_what, tag, where, logger, checkout=False):
     logger.logTxtFile.write(command + "\n")
     
     logger.write(command + "\n", 5)
-    res = subprocess.call(command, cwd=str(where.dir()), shell=True,
-                          stdout=logger.logTxtFile, stderr=subprocess.STDOUT)
+    logger.logTxtFile.write("\n" + command + "\n")
+    logger.logTxtFile.flush()
+    res = subprocess.call(command,
+                          cwd=str(where.dir()),
+                          env=environment.environ.environ,
+                          shell=True,
+                          stdout=logger.logTxtFile,
+                          stderr=subprocess.STDOUT)
     return (res == 0)
\ No newline at end of file