Browse Source

Merge "Extract file finding and processing to functions"

Jenkins 2 years ago
parent
commit
4a96f38637
2 changed files with 50 additions and 33 deletions
  1. 47
    0
      bindep/depends.py
  2. 3
    33
      bindep/main.py

+ 47
- 0
bindep/depends.py View File

@@ -15,8 +15,11 @@
15 15
 # See the License for the specific language governing permissions and
16 16
 # limitations under the License.
17 17
 
18
+import logging
19
+import os.path
18 20
 from parsley import makeGrammar
19 21
 import subprocess
22
+import sys
20 23
 
21 24
 
22 25
 debversion_grammar = """
@@ -53,6 +56,50 @@ blank = ws? '\n' -> None
53 56
 """
54 57
 
55 58
 
59
+def get_depends(filename=None):
60
+    fd = get_depends_file(filename)
61
+    if not fd:
62
+        return None
63
+    return Depends(fd.read())
64
+
65
+
66
+def get_depends_file(filename=None):
67
+    log = logging.getLogger(__name__)
68
+    if filename == "-":
69
+        return sys.stdin
70
+    elif filename:
71
+        try:
72
+            fd = open(filename, 'rt')
73
+        except IOError:
74
+            log.error('Error reading file %s.' % filename)
75
+            return None
76
+    else:
77
+        if (os.path.isfile('bindep.txt') and
78
+            os.path.isfile('other-requirements.txt')):
79
+            log.error(
80
+                'Both bindep.txt and other-requirements.txt '
81
+                'files exist, choose one.')
82
+            return None
83
+        if os.path.isfile('bindep.txt'):
84
+            try:
85
+                fd = open('bindep.txt', 'rt')
86
+            except IOError:
87
+                log.error('Error reading file bindep.txt.')
88
+                return None
89
+        elif os.path.isfile('other-requirements.txt'):
90
+            try:
91
+                fd = open('other-requirements.txt', 'rt')
92
+            except IOError:
93
+                log.error('Error reading file other-requirements.txt.')
94
+                return None
95
+        else:
96
+            log.error(
97
+                'Neither file bindep.txt nor file '
98
+                'other-requirements.txt exist.')
99
+            return None
100
+    return fd
101
+
102
+
56 103
 class Depends(object):
57 104
     """Project dependencies."""
58 105
 

+ 3
- 33
bindep/main.py View File

@@ -17,7 +17,6 @@
17 17
 
18 18
 import logging
19 19
 import optparse
20
-import os.path
21 20
 import sys
22 21
 
23 22
 import bindep.depends
@@ -44,38 +43,9 @@ def main(depends=None):
44 43
         help="List the platform and configuration profiles.")
45 44
     opts, args = parser.parse_args()
46 45
     if depends is None:
47
-        if opts.filename == "-":
48
-            fd = sys.stdin
49
-        elif opts.filename:
50
-            try:
51
-                fd = open(opts.filename, 'rt')
52
-            except IOError:
53
-                logging.error('Error reading file %s.' % opts.filename)
54
-                return 1
55
-        else:
56
-            if (os.path.isfile('bindep.txt') and
57
-                os.path.isfile('other-requirements.txt')):
58
-                logging.error('Both bindep.txt and other-requirements.txt '
59
-                              'files exist, choose one.')
60
-                return 1
61
-            if os.path.isfile('bindep.txt'):
62
-                try:
63
-                    fd = open('bindep.txt', 'rt')
64
-                except IOError:
65
-                    logging.error('Error reading file bindep.txt.')
66
-                    return 1
67
-            elif os.path.isfile('other-requirements.txt'):
68
-                try:
69
-                    fd = open('other-requirements.txt', 'rt')
70
-                except IOError:
71
-                    logging.error('Error reading file other-requirements.txt.')
72
-                    return 1
73
-            else:
74
-                logging.error('Neither file bindep.txt nor file '
75
-                              'other-requirements.txt exist.')
76
-                return 1
77
-
78
-        depends = bindep.depends.Depends(fd.read())
46
+        depends = bindep.depends.get_depends(opts.filename)
47
+        if not depends:
48
+            return 1
79 49
     if opts.profiles:
80 50
         logging.info("Platform profiles:")
81 51
         for profile in depends.platform_profiles():

Loading…
Cancel
Save