diff --git a/bindep/depends.py b/bindep/depends.py index faa2fe0..2ed841f 100644 --- a/bindep/depends.py +++ b/bindep/depends.py @@ -15,8 +15,11 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging +import os.path from parsley import makeGrammar import subprocess +import sys debversion_grammar = """ @@ -53,6 +56,50 @@ blank = ws? '\n' -> None """ +def get_depends(filename=None): + fd = get_depends_file(filename) + if not fd: + return None + return Depends(fd.read()) + + +def get_depends_file(filename=None): + log = logging.getLogger(__name__) + if filename == "-": + return sys.stdin + elif filename: + try: + fd = open(filename, 'rt') + except IOError: + log.error('Error reading file %s.' % filename) + return None + else: + if (os.path.isfile('bindep.txt') and + os.path.isfile('other-requirements.txt')): + log.error( + 'Both bindep.txt and other-requirements.txt ' + 'files exist, choose one.') + return None + if os.path.isfile('bindep.txt'): + try: + fd = open('bindep.txt', 'rt') + except IOError: + log.error('Error reading file bindep.txt.') + return None + elif os.path.isfile('other-requirements.txt'): + try: + fd = open('other-requirements.txt', 'rt') + except IOError: + log.error('Error reading file other-requirements.txt.') + return None + else: + log.error( + 'Neither file bindep.txt nor file ' + 'other-requirements.txt exist.') + return None + return fd + + class Depends(object): """Project dependencies.""" diff --git a/bindep/main.py b/bindep/main.py index 6733859..5fd8280 100644 --- a/bindep/main.py +++ b/bindep/main.py @@ -17,7 +17,6 @@ import logging import optparse -import os.path import sys import bindep.depends @@ -44,38 +43,9 @@ def main(depends=None): help="List the platform and configuration profiles.") opts, args = parser.parse_args() if depends is None: - if opts.filename == "-": - fd = sys.stdin - elif opts.filename: - try: - fd = open(opts.filename, 'rt') - except IOError: - logging.error('Error reading file %s.' % opts.filename) - return 1 - else: - if (os.path.isfile('bindep.txt') and - os.path.isfile('other-requirements.txt')): - logging.error('Both bindep.txt and other-requirements.txt ' - 'files exist, choose one.') - return 1 - if os.path.isfile('bindep.txt'): - try: - fd = open('bindep.txt', 'rt') - except IOError: - logging.error('Error reading file bindep.txt.') - return 1 - elif os.path.isfile('other-requirements.txt'): - try: - fd = open('other-requirements.txt', 'rt') - except IOError: - logging.error('Error reading file other-requirements.txt.') - return 1 - else: - logging.error('Neither file bindep.txt nor file ' - 'other-requirements.txt exist.') - return 1 - - depends = bindep.depends.Depends(fd.read()) + depends = bindep.depends.get_depends(opts.filename) + if not depends: + return 1 if opts.profiles: logging.info("Platform profiles:") for profile in depends.platform_profiles():