Extract file finding and processing to functions

There is good logic for finding and processing the list of files that we
care about that doens't exist in a form that's easily accessible if
someone wants to use bindep as a library (like, say, for instance, if
they were going to implement an ansible module)

Pull them out from inside of the command line argument processing.

Change-Id: I69bd2275f55c7b792ad316b2289f534e363af9f1
This commit is contained in:
Monty Taylor 2017-02-02 13:31:28 -06:00
parent 2474c17d16
commit 9f8e8121c1
No known key found for this signature in database
GPG Key ID: 7BAE94BC7141A594
2 changed files with 50 additions and 33 deletions

View File

@ -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."""

View File

@ -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():