summaryrefslogtreecommitdiff
path: root/scripts/perf/logsdir.py
blob: 5ab45a28b61400d10c8510d48e650f6afcc0ef25 (plain)
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
#!/usr/bin/env python3
"""\
@file   logsdir.py
@author Nat Goodspeed
@date   2024-09-12
@brief  Locate the Second Life logs directory for the current user on the
        current platform.

$LicenseInfo:firstyear=2024&license=viewerlgpl$
Copyright (c) 2024, Linden Research, Inc.
$/LicenseInfo$
"""

import os
from pathlib import Path
import platform

class Error(Exception):
    pass

# logic used by SLVersionChecker
def logsdir():
    app = 'SecondLife'
    system = platform.system()
    if (system == 'Darwin'):
        base_dir = os.path.join(os.path.expanduser('~'),
                                'Library','Application Support',app)
    elif (system == 'Linux'):
        base_dir = os.path.join(os.path.expanduser('~'),
                                '.' + app.lower())
    elif (system == 'Windows'):
        appdata = os.getenv('APPDATA')
        base_dir = os.path.join(appdata, app)
    else:
        raise ValueError("Unsupported platform '%s'" % system)

    return os.path.join(base_dir, 'logs')

def latest_file(dirpath, pattern):
    files = Path(dirpath).glob(pattern)
    sort = [(p.stat().st_mtime, p) for p in files if p.is_file()]
    sort.sort(reverse=True)
    try:
        return sort[0][1]
    except IndexError:
        raise Error(f'No {pattern} files in {dirpath}')