Skip to content

Commit

Permalink
Clean up legacy stock config files
Browse files Browse the repository at this point in the history
CHIRP-legacy used to copy the distribution stock_configs into the
user's directory, which was always messy. CHIRP-next stopped doing
that, but many users still have these files there. Instead of ignoring
distribution files if they're already in the user's directory (as was
the previous behavior), try to clean them up. If they hash to the same
value, assume they're the same and remove them.

Related to #10688
  • Loading branch information
kk7ds committed Jun 28, 2023
1 parent a6b3e2d commit 4caaf1a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions chirp/wxui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def add_stock_menu(self):
os.makedirs(user_stock_dir, exist_ok=True)
dist_stock_confs = sorted(
[
conf.name for conf
(conf.name, hashlib.md5(conf.read_bytes())) for conf
in importlib_resources.files('chirp.stock_configs').iterdir()
if conf.is_file()
]
Expand All @@ -606,12 +606,31 @@ def add_stock(fn):

stock.Append(wx.MenuItem(stock, wx.ID_SEPARATOR))

for fn in dist_stock_confs:
if os.path.basename(fn) not in found:
add_stock(fn)
else:
LOG.info('Ignoring dist stock conf %s because same name found '
'in user dir', os.path.basename(fn))
for fn, hash in dist_stock_confs:
if os.path.basename(fn) in found:
# Remove old stock configs that were copied to the user's
# directory by legacy chirp.
try:
user_fn = os.path.join(get_stock_configs(),
os.path.basename(fn))
with open(user_fn, 'rb') as f:
user_hash = hashlib.md5(f.read())
if hash.digest() == user_hash.digest():
LOG.info('Removing stale legacy stock config %s',
os.path.basename(fn))
os.remove(user_fn)
# Since we already added it to the user area, just
# don't add it to system this time. At next startup,
# it will move to the system section.
continue
else:
raise FileExistsError('File is changed')
except Exception as e:
LOG.info('Ignoring dist stock conf %s because same name '
'found in user dir: %s',
os.path.basename(fn), e)
continue
add_stock(fn)

return stock

Expand Down

0 comments on commit 4caaf1a

Please sign in to comment.