Refactor the Fetch package for non duplicated code

parent 65be4a9e
......@@ -21,6 +21,13 @@
"""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):
"""Base class for the code fetcher objects"""
......@@ -30,8 +37,33 @@ class Fetcher(object):
pass
@staticmethod
def check_id(path):
"""Stub method, this must return a string with the version identifier
for the selected fetcher (revision, commit...)"""
pass
def check_id(path, command):
"""Use the provided command to get the specific ID from
the repository at path"""
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 @@
import os
from hdlmake.util import path as path_utils
import logging
from tempfile import TemporaryFile
from subprocess import Popen, PIPE
from .constants import GIT
from .fetcher import Fetcher
......@@ -99,31 +98,8 @@ class Git(Fetcher):
return success
@staticmethod
def check_id(path):
"""Get the commit id for the Git repository at path"""
cur_dir = os.getcwd()
commit = None
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
def check_git_commit(path):
"""Get the revision number for the Git repository at path"""
git_cmd = 'git log -1 --format="%H" | cut -c1-32'
return Fetcher.check_id(path, git_cmd)
......@@ -35,7 +35,7 @@ class Local(Fetcher):
pass
@staticmethod
def check_id(path):
"""Get the ID for Local sources"""
def check_md5sum(path):
"""Get the ID for Local sources... maybe sha256 or md5sum?"""
pass
......@@ -23,8 +23,6 @@
import os
import logging
from tempfile import TemporaryFile
from subprocess import Popen, PIPE
from hdlmake.util import path as path_utils
from .fetcher import Fetcher
......@@ -62,32 +60,8 @@ class Svn(Fetcher):
return success
@staticmethod
def check_id(path):
def check_svn_revision(path):
"""Get the revision number for the SVN repository at path"""
cur_dir = os.getcwd()
revision = None
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
svn_cmd = "svn info 2>/dev/null | awk '{if(NR == 5) {print $2}}'"
return Fetcher.check_id(path, svn_cmd)
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