Refactor the Fetch package for non duplicated code

parent 65be4a9e
...@@ -21,6 +21,13 @@ ...@@ -21,6 +21,13 @@
"""Module providing the base class for the different code fetchers""" """Module providing the base class for the different code fetchers"""
import os
from hdlmake.util import path as path_utils
import logging
from tempfile import TemporaryFile
from subprocess import Popen, PIPE
class Fetcher(object): class Fetcher(object):
"""Base class for the code fetcher objects""" """Base class for the code fetcher objects"""
...@@ -30,8 +37,33 @@ class Fetcher(object): ...@@ -30,8 +37,33 @@ class Fetcher(object):
pass pass
@staticmethod @staticmethod
def check_id(path): def check_id(path, command):
"""Stub method, this must return a string with the version identifier """Use the provided command to get the specific ID from
for the selected fetcher (revision, commit...)""" the repository at path"""
pass cur_dir = os.getcwd()
identifier = None
stderr = TemporaryFile()
try:
is_windows = path_utils.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
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
import os import os
from hdlmake.util import path as path_utils from hdlmake.util import path as path_utils
import logging import logging
from tempfile import TemporaryFile
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from .constants import GIT from .constants import GIT
from .fetcher import Fetcher from .fetcher import Fetcher
...@@ -99,31 +98,8 @@ class Git(Fetcher): ...@@ -99,31 +98,8 @@ class Git(Fetcher):
return success return success
@staticmethod @staticmethod
def check_id(path): def check_git_commit(path):
"""Get the commit id for the Git repository at path""" """Get the revision number for the Git repository at path"""
cur_dir = os.getcwd() git_cmd = 'git log -1 --format="%H" | cut -c1-32'
commit = None return Fetcher.check_id(path, git_cmd)
stderr = TemporaryFile()
try:
is_windows = path_utils.check_windows()
os.chdir(path)
git_cmd = 'git log -1 --format="%H" | cut -c1-32'
git_out = Popen(git_cmd,
shell=True,
stdin=PIPE,
stdout=PIPE,
stderr=stderr,
close_fds=not is_windows)
errmsg = stderr.readlines()
if errmsg:
logging.debug(
"git error message (in %s): %s",
path, '\n'.join(errmsg))
try:
commit = git_out.stdout.readlines()[0].strip()
except IndexError:
pass
finally:
os.chdir(cur_dir)
stderr.close()
return commit
...@@ -35,7 +35,7 @@ class Local(Fetcher): ...@@ -35,7 +35,7 @@ class Local(Fetcher):
pass pass
@staticmethod @staticmethod
def check_id(path): def check_md5sum(path):
"""Get the ID for Local sources""" """Get the ID for Local sources... maybe sha256 or md5sum?"""
pass pass
...@@ -23,8 +23,6 @@ ...@@ -23,8 +23,6 @@
import os import os
import logging import logging
from tempfile import TemporaryFile
from subprocess import Popen, PIPE
from hdlmake.util import path as path_utils from hdlmake.util import path as path_utils
from .fetcher import Fetcher from .fetcher import Fetcher
...@@ -62,32 +60,8 @@ class Svn(Fetcher): ...@@ -62,32 +60,8 @@ class Svn(Fetcher):
return success return success
@staticmethod @staticmethod
def check_id(path): def check_svn_revision(path):
"""Get the revision number for the SVN repository at path""" """Get the revision number for the SVN repository at path"""
cur_dir = os.getcwd() svn_cmd = "svn info 2>/dev/null | awk '{if(NR == 5) {print $2}}'"
revision = None return Fetcher.check_id(path, svn_cmd)
stderr = TemporaryFile()
try:
is_windows = path_utils.check_windows()
os.chdir(path)
svn_cmd = "svn info 2>/dev/null | awk '{if(NR == 5) {print $2}}'"
svn_out = Popen(
svn_cmd,
shell=True,
stdin=PIPE,
stdout=PIPE,
stderr=stderr,
close_fds=not is_windows)
errmsg = stderr.readlines()
if errmsg:
logging.debug(
"svn error message (in %s): %s",
path, '\n'.join(errmsg))
try:
revision = svn_out.stdout.readlines()[0].strip()
except IndexError:
pass
finally:
os.chdir(cur_dir)
stderr.close()
return revision
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