Create a function to execute processes in the O.S. specific shell

parent e773bd4b
...@@ -26,7 +26,6 @@ from __future__ import print_function ...@@ -26,7 +26,6 @@ from __future__ import print_function
from __future__ import absolute_import from __future__ import absolute_import
import os import os
import logging import logging
from subprocess import PIPE, Popen
import sys import sys
from hdlmake.util import shell from hdlmake.util import shell
...@@ -192,23 +191,11 @@ class Action(list): ...@@ -192,23 +191,11 @@ class Action(list):
"""Guess origin (git, svn, local) of a module at given path""" """Guess origin (git, svn, local) of a module at given path"""
cwd = self.top_module.path cwd = self.top_module.path
try: try:
is_windows = shell.check_windows()
os.chdir(path) os.chdir(path)
git_out = Popen("git config --get remote.origin.url", url = shell.run("git config --get remote.origin.url")
stdout=PIPE, shell=True, close_fds=not is_windows)
lines = git_out.stdout.readlines()
if len(lines) == 0:
return None
url = lines[0].strip()
if not url: # try svn if not url: # try svn
svn_out = Popen( return shell.run("svn info | grep 'Repository Root' | " +
"svn info | grep 'Repository Root' | awk '{print $NF}'", "awk '{print $NF}'")
stdout=PIPE, shell=True, close_fds=not is_windows)
url = svn_out.stdout.readlines()[0].strip()
if url:
return url
else:
return None
else: else:
return url return url
finally: finally:
......
...@@ -24,9 +24,6 @@ ...@@ -24,9 +24,6 @@
from __future__ import absolute_import from __future__ import absolute_import
import os import os
from hdlmake.util import shell from hdlmake.util import shell
import logging
from tempfile import TemporaryFile
from subprocess import Popen, PIPE
class Fetcher(object): class Fetcher(object):
...@@ -42,28 +39,7 @@ class Fetcher(object): ...@@ -42,28 +39,7 @@ class Fetcher(object):
"""Use the provided command to get the specific ID from """Use the provided command to get the specific ID from
the repository at path""" the repository at path"""
cur_dir = os.getcwd() cur_dir = os.getcwd()
identifier = None os.chdir(path)
stderr = TemporaryFile() identifier = shell.run(command)
try: os.chdir(cur_dir)
is_windows = shell.check_windows()
os.chdir(path)
command_out = Popen(
command,
shell=True,
stdin=PIPE,
stdout=PIPE,
stderr=stderr,
close_fds=not is_windows)
errmsg = stderr.readlines()
if errmsg:
logging.debug(
"ID error message (in %s): %s",
path, '\n'.join(errmsg))
try:
identifier = command_out.stdout.readlines()[0].strip()
except IndexError:
pass
finally:
os.chdir(cur_dir)
stderr.close()
return identifier return identifier
...@@ -26,7 +26,6 @@ import os ...@@ -26,7 +26,6 @@ import os
from hdlmake.util import path as path_utils from hdlmake.util import path as path_utils
from hdlmake.util import shell from hdlmake.util import shell
import logging import logging
from subprocess import Popen, PIPE, CalledProcessError
from .constants import GIT from .constants import GIT
from .fetcher import Fetcher from .fetcher import Fetcher
...@@ -42,43 +41,17 @@ class Git(Fetcher): ...@@ -42,43 +41,17 @@ class Git(Fetcher):
@staticmethod @staticmethod
def get_git_toplevel(): def get_git_toplevel():
"""Get the top level for the Git repository""" """Get the top level for the Git repository"""
try: return shell.run("git rev-parse --show-toplevel")
tree_root_cmd = Popen("git rev-parse --show-toplevel",
stdout=PIPE,
stdin=PIPE,
close_fds=not shell.check_windows(),
shell=True)
tree_root_line = tree_root_cmd.stdout.readlines()[0].strip()
return tree_root_line
except CalledProcessError as process_error:
logging.error("Cannot get the top level!: %s",
process_error.output)
quit()
@staticmethod @staticmethod
def get_submodule_commit(submodule_dir): def get_submodule_commit(submodule_dir):
"""Get the commit for a repository if defined in Git submodules""" """Get the commit for a repository if defined in Git submodules"""
try: status_line = shell.run("git submodule status %s" % submodule_dir)
command_tmp = "git submodule status %s" % submodule_dir status_line = status_line.split()
status_cmd = Popen(command_tmp, if len(status_line) == 2:
stdout=PIPE, return status_line[0][1:]
stdin=PIPE, else:
stderr=PIPE, return None
close_fds=not shell.check_windows(),
shell=True)
status_output = status_cmd.stdout.readlines()
if len(status_output) == 1:
status_line = status_output[0].split()
if len(status_line) == 2:
return status_line[0][1:]
else:
return None
else:
return None
except CalledProcessError as process_error:
logging.error("Cannot get the submodule status!: %s",
process_error.output)
quit()
def fetch(self, module): def fetch(self, module):
"""Get the code from the remote Git repository""" """Get the code from the remote Git repository"""
......
...@@ -31,7 +31,6 @@ from __future__ import print_function ...@@ -31,7 +31,6 @@ from __future__ import print_function
from __future__ import absolute_import from __future__ import absolute_import
import os import os
import logging import logging
from subprocess import Popen, PIPE, CalledProcessError
from hdlmake.util import path as path_mod from hdlmake.util import path as path_mod
from hdlmake.util import shell from hdlmake.util import shell
...@@ -103,18 +102,8 @@ class Module(ModuleContent): ...@@ -103,18 +102,8 @@ class Module(ModuleContent):
if not self.isfetched: if not self.isfetched:
return return
logging.debug("Removing " + self.path) logging.debug("Removing " + self.path)
try: command_tmp = shell.rmdir_command() + " " + self.path
command_tmp = shell.rmdir_command() + " " + self.path shell.run(command_tmp)
Popen(command_tmp,
stdout=PIPE,
stdin=PIPE,
stderr=PIPE,
close_fds=not shell.check_windows(),
shell=True)
except CalledProcessError as process_error:
logging.error("Cannot clean the module: %s",
process_error.output)
quit()
def process_manifest(self): def process_manifest(self):
""" """
......
...@@ -27,6 +27,27 @@ from __future__ import absolute_import ...@@ -27,6 +27,27 @@ from __future__ import absolute_import
import os import os
import sys import sys
import platform import platform
import logging
from subprocess import PIPE, Popen, CalledProcessError
def run(command):
"""Execute a command in the shell and print the output lines as a list"""
try:
command_out = Popen(command,
stdout=PIPE,
stdin=PIPE,
stderr=PIPE,
close_fds=not check_windows(),
shell=True)
lines = command_out.stdout.readlines()
if len(lines) == 0:
return None
return lines[0].strip()
except CalledProcessError as process_error:
logging.error("Cannot clean the module: %s",
process_error.output)
quit()
def tclpath(path): def tclpath(path):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment