Merge ubuntu-dev-tools:ubuntutools-misc into ubuntu-dev-tools:master

Proposed by Stefano Rivera
Status: Merged
Merged at revision: c8602ba8a2a5ef625c8492b306fa47852e3513a6
Proposed branch: ubuntu-dev-tools:ubuntutools-misc
Merge into: ubuntu-dev-tools:master
Diff against target: 105 lines (+29/-25)
2 files modified
debian/changelog (+6/-0)
ubuntutools/misc.py (+23/-25)
Reviewer Review Type Date Requested Status
Mattia Rizzolo Approve
Review via email: mp+372627@code.launchpad.net

Commit message

ubuntutools.misc: Replace Popen() calls with check_output()

Fixes a crash in pbuilder-dist

Description of the change

Missed an encoding=utf-8 in the python3 branch.

But really, we can refactor this all and make it a bit better.

To post a comment you must log in.
Revision history for this message
Mattia Rizzolo (mapreri) :
review: Approve
Revision history for this message
Mattia Rizzolo (mapreri) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/changelog b/debian/changelog
2index b7c564d..e322d5c 100644
3--- a/debian/changelog
4+++ b/debian/changelog
5@@ -1,3 +1,9 @@
6+ubuntu-dev-tools (0.174) UNRELEASED; urgency=medium
7+
8+ * ubuntutools.misc: Replace Popen() calls with check_output()
9+
10+ -- Stefano Rivera <stefanor@debian.org> Wed, 11 Sep 2019 13:30:54 -0300
11+
12 ubuntu-dev-tools (0.173) unstable; urgency=medium
13
14 [ Stefano Rivera ]
15diff --git a/ubuntutools/misc.py b/ubuntutools/misc.py
16index 8409108..62f105a 100644
17--- a/ubuntutools/misc.py
18+++ b/ubuntutools/misc.py
19@@ -23,10 +23,10 @@
20 # ##################################################################
21
22 # Modules.
23-from subprocess import Popen, PIPE
24 import locale
25 import os
26 import sys
27+from subprocess import check_output, CalledProcessError
28
29 import distro_info
30
31@@ -47,29 +47,22 @@ def system_distribution_chain():
32 global _system_distribution_chain
33 if len(_system_distribution_chain) == 0:
34 try:
35- p = Popen(('dpkg-vendor', '--query', 'Vendor'),
36- stdout=PIPE, encoding='utf-8')
37- _system_distribution_chain.append(p.communicate()[0].strip())
38- except OSError:
39+ vendor = check_output(('dpkg-vendor', '--query', 'Vendor'),
40+ encoding='utf-8').strip()
41+ _system_distribution_chain.append(vendor)
42+ except CalledProcessError:
43 print('Error: Could not determine what distribution you are running.')
44 return []
45
46 while True:
47 try:
48- p = Popen(('dpkg-vendor',
49- '--vendor', _system_distribution_chain[-1],
50- '--query', 'Parent'),
51- stdout=PIPE, encoding='utf-8')
52- parent = p.communicate()[0].strip()
53- # Don't check return code, because if a vendor has no
54- # parent, dpkg-vendor returns 1
55- if not parent:
56- break
57- _system_distribution_chain.append(parent)
58- except Exception:
59- print(('Error: Could not determine the parent of the '
60- 'distribution %s' % _system_distribution_chain[-1]))
61- return []
62+ parent = check_output((
63+ 'dpkg-vendor', '--vendor', _system_distribution_chain[-1],
64+ '--query', 'Parent'), encoding='utf-8').strip()
65+ except CalledProcessError:
66+ # Vendor has no parent
67+ break
68+ _system_distribution_chain.append(parent)
69
70 return _system_distribution_chain
71
72@@ -91,14 +84,18 @@ def host_architecture():
73 architecture can't be determined, print an error message and return None.
74 """
75
76- arch = Popen(['dpkg', '--print-architecture'], stdout=PIPE,
77- stderr=PIPE).communicate()[0].split()
78+ try:
79+ arch = check_output(('dpkg', '--print-architecture'),
80+ encoding='utf-8').strip()
81+ except CalledProcessError:
82+ arch = None
83
84- if not arch or 'not found' in arch[0]:
85- print('Error: Not running on a Debian based system; could not detect its architecture.')
86+ if not arch or 'not found' in arch:
87+ print('Error: Not running on a Debian based system; '
88+ 'could not detect its architecture.')
89 return None
90
91- return arch[0]
92+ return arch
93
94
95 def readlist(filename, uniq=True):
96@@ -112,7 +109,8 @@ def readlist(filename, uniq=True):
97 print('File "%s" does not exist.' % filename)
98 return False
99
100- content = open(filename).read().replace('\n', ' ').replace(',', ' ')
101+ with open(filename) as f:
102+ content = f.read().replace('\n', ' ').replace(',', ' ')
103
104 if not content.strip():
105 print('File "%s" is empty.' % filename)

Subscribers

People subscribed via source and target branches