File: util.py

package info (click to toggle)
midisnoop 0.1.2%2Bgit20141108.bc30f600187e-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 484 kB
  • sloc: cpp: 2,139; python: 243; makefile: 13
file content (99 lines) | stat: -rw-r--r-- 2,721 bytes parent folder | download
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
from gzip import open as _open
from os import (
    X_OK,
    access,
    chdir,
    environ,
    getcwd,
    makedirs,
    pardir,
    pathsep,
    sep
)
from os.path import abspath, dirname, isabs, isdir, isfile, join
from platform import mac_ver, win32_ver
from string import Template
from subprocess import PIPE, Popen

MAJOR_VERSION = 0
MINOR_VERSION = 1
REVISION = 3
VERSION = "%d.%d.%d" % (MAJOR_VERSION, MINOR_VERSION, REVISION)

PLATFORM_MACX = 1
PLATFORM_UNIX = 2
PLATFORM_WIN32 = 3

_PLATFORM = (mac_ver()[0] and PLATFORM_MACX) or \
    (win32_ver()[0] and PLATFORM_WIN32) or PLATFORM_UNIX

def createSourcePackage(path):
    gzipStream = _open(filename=path, mode="wb")
    try:
        gitArgs = ["git", "archive", "--format=tar",
                   "--prefix=midisnoop-%s%s" % (VERSION, sep), "HEAD"]
        oldDirectory = getcwd()
        chdir(getRootDirectory())
        try:
            process = Popen(gitArgs, stdout=PIPE, bufsize=-1)
            try:
                processOut = process.stdout
                while True:
                    data = processOut.read(8192)
                    if not data:
                        break
                    gzipStream.write(data)
            finally:
                result = process.wait()
            if result:
                raise Exception("git archive process failed")
        finally:
            chdir(oldDirectory)
    finally:
        gzipStream.close()

def findExecutable(command):
    if not isabs(command):
        path = environ.get("PATH")
        if path is not None:
            for path in path.split(pathsep):
                commandPath = abspath(join(path.strip('\"'), command))
                if isfile(commandPath) and access(commandPath, X_OK):
                    return commandPath
    commandPath = abspath(command)
    if isfile(commandPath) and access(commandPath, X_OK):
        return commandPath
    return None

def getPlatform():
    return _PLATFORM

def getResourcesDirectory():
    return join(getRootDirectory(), "resources")

def getRootDirectory():
    return abspath(join(dirname(__file__), pardir))

def getTemplatesDirectory():
    return join(getRootDirectory(), "templates")

def writeTemplate(destination, source, data):
    destination = abspath(destination)
    destinationDir = dirname(destination)
    if not isdir(destinationDir):
        makedirs(destinationDir)
    source = abspath(source)
    sourceDir = dirname(source)
    if not isdir(sourceDir):
        makedirs(sourceDir)
    fp = open(source)
    try:
        s = fp.read()
    finally:
        fp.close()
    s = Template(s).substitute(data)
    fp = open(destination, 'w')
    try:
        fp.write(s)
    finally:
        fp.close()