Last active
July 8, 2022 01:26
-
-
Save 0x9900/070244e99bbc69e848c846b20f2619f5 to your computer and use it in GitHub Desktop.
DMR id generation for YSF2DMR gateway.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# (c) W6BSD Fred Cirera | |
# https://github.com/0x9900/ | |
# | |
# I use this program to generte the DMRId file for the YSF2DMR | |
# gateway. | |
# | |
import csv | |
import hashlib | |
import logging | |
import os | |
import shutil | |
import subprocess | |
import sys | |
from contextlib import closing | |
from tempfile import NamedTemporaryFile | |
import requests | |
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', | |
datefmt='%H:%M:%S', level=logging.INFO) | |
BLOCK_SIZE = 0x2000 | |
DMR_USERS = "https://www.radioid.net/static/user.csv" | |
DMRID_FILE = "/var/local/DMRIds.dat" | |
def genhash(filename): | |
hasher = hashlib.sha224() | |
with open(filename, 'r') as fd: | |
for block in fd.read(BLOCK_SIZE): | |
hasher.update(block) | |
logging.info("File %s\tdigest: %s", os.path.basename(filename), hasher.hexdigest()) | |
return hasher.digest() | |
def dmr_users(): | |
logging.info('Users source: %s', DMR_USERS) | |
with closing(requests.get(DMR_USERS, stream=True)) as fd: | |
reader = csv.reader(fd.iter_lines(), delimiter=',') | |
for row in reader: | |
yield row | |
def restart_ysf2dmr(): | |
logging.info('Restarting the ysf2dmr service') | |
proc = subprocess.Popen(["systemctl", "status", "ysf2dmr.service"], stdout=subprocess.PIPE) | |
if proc.wait() == 0: | |
for line in proc.stdout: | |
line = line.rstrip() | |
if not line: | |
continue | |
logging.info(line.rstrip()) | |
else: | |
logging.error("Error restarting ysf2dmr.service") | |
def gen_idfile(fdesc): | |
for user in dmr_users(): | |
if not user or not user[0].isdigit(): | |
continue | |
try: | |
call = user[1].split()[0] | |
except IndexError: | |
continue | |
try: | |
name = user[2].split()[0] | |
except IndexError: | |
name = 'Nobody' | |
idline = "{}\t{}\t{}\n".format(user[0], call, name) | |
fdesc.write(idline) | |
def main(): | |
if os.getuid() != 0: | |
logging.error("This program need to be run by root") | |
sys.exit(os.EX_NOPERM) | |
try: | |
with NamedTemporaryFile(mode="w", prefix='DMR_', delete=False) as tmpfile: | |
logging.info('Generating %s', tmpfile.name) | |
gen_idfile(tmpfile) | |
logging.info('Done') | |
except IOError as err: | |
logging.error(err) | |
sys.exit(os.EX_IOERR) | |
# Check the dmrid file for changes | |
if genhash(tmpfile.name) == genhash(DMRID_FILE): | |
logging.info('No changes') | |
os.unlink(tmpfile.name) | |
else: | |
logging.info('DMRId has changed updating the local database') | |
try: | |
os.chmod(tmpfile.name, 0644) | |
shutil.move(tmpfile.name, DMRID_FILE) | |
except IOError as err: | |
logging.error(err) | |
sys.exit(os.EX_IOERR) | |
else: | |
restart_ysf2dmr() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment