slackbuilds.py 2.94 KB
Newer Older
David Spencer's avatar
David Spencer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
"""
SBo Dashboard
slackbuilds.py
Functions and dict for accessing a local SlackBuilds.org clone

David Spencer 2018-08-28
See LICENCE for copyright information
"""

import os
import re
import glob
import subprocess
import logging

import globals
import stats

#-----------------------------------------------------------------------

def update_slackbuilds():
    """
    Update the local slackbuilds git repo from remote
    """
    logging.info("started")
    #### needs error handling

    if os.path.isfile(os.path.join(globals.sbdir,"ChangeLog.txt")):
        subprocess.run(
            "git checkout master ; git pull --ff-only origin",
            shell=True,
            cwd=globals.sbdir,
            check=True,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
            )
    else:
        subprocess.run(
            "git clone git://git.slackbuilds.org/slackbuilds.git {:s}".format(globals.sbdir),
            shell=True,
            cwd=globals.dashdir,
            check=True,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL
            )

    logging.info("finished")

#-----------------------------------------------------------------------

# Create a dict from the SlackBuilds info files, for easy access
SBinfo={}

def read_slackbuilds_info():
    """
    Read data from SlackBuilds info files into the SBinfo dict
    """
    logging.debug("reading .info files")

    # These regexes are used >7000 times, so we might as well compile them.
    re_maint=re.compile(r"MAINTAINER=\".*\"")
    re_email=re.compile(r"EMAIL=\".*\"")

    infocount=0

    for infofile in glob.glob(globals.sbdir+"/*/*/*.info"):
        infocount = infocount+1

        prgnam=infofile.split("/")[-2]
        catnam=infofile.split("/")[-3]

        try:
            with open(infofile, "r", encoding="utf-8") as openinfo:
                info = openinfo.read()
        except:
            with open(infofile, "r", encoding="latin-1") as openinfo:
                info = openinfo.read()

        maintainerarray=re_maint.findall(info)
        emailarray=re_email.findall(info)
        if len(maintainerarray) == 1 and len(emailarray) == 1:
            maintainer=maintainerarray[0][12:-1]
            email=emailarray[0][7:-1]
        else:
            # We're not here to validate the .info files.
            # If there's not exactly one MAINTAINER and EMAIL, bollocks to it.
            logging.warn("{:s} has {:d} maintainers and {:d} emails".format(infofile,len(maintainerarray),len(emailarray)))
            continue

        SBinfo.update({ prgnam: { "catnam": catnam, "maintainer": maintainer, "email": email} })

    if infocount != len(SBinfo):
        logging.warn("{:d} duplicate prgnams in slackbuilds repo".format(infocount-len(SBinfo)))

    stats.setStats("slackbuilds.count",infocount)
    logging.debug("finished reading {:d} .info files".format(infocount))

#-----------------------------------------------------------------------