# musicrepo.py -rw-r--r-- 5.0 KiB View raw
                                                                                
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import json
import re
import glob
import os
import unicodedata
import hashlib
import mmap
import argparse


def make_metapackage(repodir, pkgname, pkgver, depends):
    dep = " ".join(depends)
    result = f"""
# Generated by musicrepo
pkgname={pkgname}
pkgver={pkgver}
pkgrel=0
pkgdesc="musicrepo"
options="!check"
url="https://music"
arch="noarch"
license="propriatary"
depends="{dep}"
    """
    result += '\npackage() {\n\tmkdir -p "$pkgdir"\n}\n'
    apkfile = os.path.join(repodir, pkgname, 'APKBUILD')
    apkdir = os.path.dirname(apkfile)
    os.makedirs(apkdir, exist_ok=True)

    with open(apkfile, 'w') as handle:
        handle.write(result)


def make_apkbuild(repodir, artist, album, path):
    artist_slug = slug(artist)
    album_slug = slug(album)
    pkgname = f"musicdir-{artist_slug}-{album_slug}"

    pkgver = 1
    if os.path.isfile(os.path.join(path, 'pkgver')):
        with open(os.path.join(path, 'pkgver')) as handle:
            raw = handle.read()
        pkgver = int(raw.strip())

    apkfile = os.path.join(repodir, pkgname, 'APKBUILD')
    apkdir = os.path.dirname(apkfile)
    os.makedirs(apkdir, exist_ok=True)
    os.makedirs(os.path.join(apkdir, "tracks"), exist_ok=True)

    sources = ""
    sha512sums = []
    for file in glob.glob(os.path.join(path, '*')):
        sname = "tracks/" + os.path.basename(file).replace(' ', '-')
        sources += f"{sname}\n"
        if not os.path.islink(os.path.join(apkdir, sname)):
            os.symlink(file, os.path.join(apkdir, sname))

        h = get_sha512sum(file)
        hname = os.path.basename(sname)
        sha512sums.append(f"{h}  {hname}")
    sha512sums = "\n".join(sha512sums)

    result = f"""
# Generated by musicrepo
pkgname={pkgname}
_artist="{artist}"
_album="{album}"
pkgver={pkgver}
pkgrel=0
pkgdesc="{album} from {artist}"
url="https://music"
arch="noarch"
license="propriatary"
options="!check"
source="
{sources}
"
    """
    result += """
package()
{
    cd tracks
    for _i in ./*; do
        install -Dm644 "$_i" "$pkgdir"/usr/share/music/"$_artist"/"$_album"/"$_i"
    done
}
    """

    result += f'\nsha512sums="{sha512sums}"\n'

    with open(apkfile, 'w') as handle:
        handle.write(result)

    return pkgname, pkgver


def slug(value):
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub('[^\w\s-]', '', value).strip().lower()
    return re.sub('[-\s]+', '-', value)


def get_sha512sum(filename):
    h = hashlib.sha512()
    with open(filename, 'rb') as f:
        with mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ) as mm:
            h.update(mm)
    return h.hexdigest()


def make_repository(musicdir, repodir):
    music = {}
    statefile = os.path.join(repodir, 'musicrepo.state.json')
    state = {}
    bump = set()
    if os.path.isfile(statefile):
        with open(statefile) as handle:
            state = json.loads(handle.read())

    for path in glob.glob(os.path.join(musicdir, '*/*'), recursive=True):
        part = path.split('/')
        album = part[-1]
        artist = part[-2]

        match = re.match(r'(.+)\(\d\d\d\d\)', album)
        if match:
            album = match.groups(0)[0].strip()

        if artist not in music:
            music[artist] = {}

        music[artist][album] = path

    artist_pkgs = []
    for artist in music:
        albums_pkgs = []
        for album in music[artist]:
            path = music[artist][album]
            pkgname, pkgver = make_apkbuild(repodir, artist, album, path)
            albums_pkgs.append(pkgname)
            artist_slug = slug(artist)

            if pkgname not in state:
                print(f"New album: {album} from {artist}")
                bump.add(f'musicdir-{artist_slug}')
                state[pkgname] = pkgver

        if len(albums_pkgs):
            pkgname = f'musicdir-{artist_slug}'
            pkgver = 0
            if pkgname in state:
                pkgver = state[pkgname]
            else:
                print(f"Artist {artist} is new, creating metapackage")
                state[pkgname] = 0
                bump.add('musicdir-all')
            if pkgname in bump:
                state[pkgname] += 1
                pkgver += 1
                print(f"Artist {artist} has new album packages, bumping version to {pkgver}")
            make_metapackage(repodir, pkgname, pkgver, albums_pkgs)
            artist_pkgs.append(f'musicdir-{artist_slug}')

    if 'musicdir-all' not in state:
        state['musicdir-all'] = 0
    if 'musicdir-all' in bump:
        state['musicdir-all'] += 1
        print(f"Artist list changed, bumping musicdir-all to {state['musicdir-all']}")
    make_metapackage(repodir, 'musicdir-all', state['musicdir-all'], artist_pkgs)

    with open(statefile, 'w') as handle:
        handle.write(json.dumps(state))


def main():
    parser = argparse.ArgumentParser(description="music repo generator")
    parser.add_argument('musicdir')
    parser.add_argument('repodir')
    args = parser.parse_args()

    make_repository(os.path.expanduser(args.musicdir), os.path.expanduser(args.repodir))


if __name__ == '__main__':
    main()