Skip to content

Commit 9020cf2

Browse files
authored
Fix the pub encoding issue (#207)
Signed-off-by: jiyeong.seok <[email protected]>
1 parent 52659f5 commit 9020cf2

File tree

1 file changed

+28
-19
lines changed
  • src/fosslight_dependency/package_manager

1 file changed

+28
-19
lines changed

src/fosslight_dependency/package_manager/Pub.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,35 @@ def parse_direct_dependencies(self):
179179
self.direct_dep = True
180180
tmp_pub_deps_file = 'tmp_deps.json'
181181
tmp_no_dev_deps_file = 'tmp_no_dev_deps.txt'
182-
182+
encoding_list = ['utf8', 'utf16']
183183
if os.path.exists(tmp_pub_deps_file) and os.path.exists(tmp_no_dev_deps_file):
184-
try:
185-
with open(tmp_pub_deps_file, 'r+', encoding='utf8') as deps_f:
186-
lines = deps_f.readlines()
187-
deps_f.seek(0)
188-
deps_f.truncate()
189-
for num, line in enumerate(lines):
190-
if line.startswith('{'):
191-
first_line = num
192-
break
193-
deps_f.writelines(lines[first_line:])
194-
deps_f.seek(0)
195-
deps_l = json.load(deps_f)
196-
self.parse_pub_deps_file(deps_l)
197-
with open(tmp_no_dev_deps_file, 'r', encoding='utf8') as no_dev_f:
198-
self.parse_no_dev_command_file(no_dev_f.read())
199-
logger.info('Parse tmp pub deps file.')
200-
except Exception as e:
201-
logger.error(f'Fail to parse tmp pub deps result file: {e}')
184+
for encode in encoding_list:
185+
try:
186+
logger.info(f'Try to encode with {encode}.')
187+
with open(tmp_pub_deps_file, 'r+', encoding=encode) as deps_f:
188+
lines = deps_f.readlines()
189+
deps_f.seek(0)
190+
deps_f.truncate()
191+
for num, line in enumerate(lines):
192+
if line.startswith('{'):
193+
first_line = num
194+
break
195+
deps_f.writelines(lines[first_line:])
196+
deps_f.seek(0)
197+
deps_l = json.load(deps_f)
198+
self.parse_pub_deps_file(deps_l)
199+
with open(tmp_no_dev_deps_file, 'r', encoding=encode) as no_dev_f:
200+
self.parse_no_dev_command_file(no_dev_f.read())
201+
logger.info('Parse tmp pub deps file.')
202+
except UnicodeDecodeError as e1:
203+
logger.info(f'Fail to encode with {encode}: {e1}')
204+
pass
205+
except Exception as e:
206+
logger.error(f'Fail to parse tmp pub deps result file: {e}')
207+
return False
208+
else:
209+
logger.info(f'Success to encode with {encode}.')
210+
break
202211
else:
203212
try:
204213
cmd = "flutter pub get"

0 commit comments

Comments
 (0)