| 1 | from gzip import open as _open |
| 2 | from os import ( |
| 3 | X_OK, |
| 4 | access, |
| 5 | chdir, |
| 6 | environ, |
| 7 | getcwd, |
| 8 | makedirs, |
| 9 | pardir, |
| 10 | pathsep, |
| 11 | sep |
| 12 | ) |
| 13 | from os.path import abspath, dirname, isabs, isdir, isfile, join |
| 14 | from platform import mac_ver, win32_ver |
| 15 | from string import Template |
| 16 | from subprocess import PIPE, Popen |
| 17 | |
| 18 | MAJOR_VERSION = 0 |
| 19 | MINOR_VERSION = 1 |
| 20 | REVISION = 3 |
| 21 | VERSION = "%d.%d.%d" % (MAJOR_VERSION, MINOR_VERSION, REVISION) |
| 22 | |
| 23 | PLATFORM_MACX = 1 |
| 24 | PLATFORM_UNIX = 2 |
| 25 | PLATFORM_WIN32 = 3 |
| 26 | |
| 27 | _PLATFORM = (mac_ver()[0] and PLATFORM_MACX) or \ |
| 28 | (win32_ver()[0] and PLATFORM_WIN32) or PLATFORM_UNIX |
| 29 | |
| 30 | def createSourcePackage(path): |
| 31 | gzipStream = _open(filename=path, mode="wb") |
| 32 | try: |
| 33 | gitArgs = ["git", "archive", "--format=tar", |
| 34 | "--prefix=midisnoop-%s%s" % (VERSION, sep), "HEAD"] |
| 35 | oldDirectory = getcwd() |
| 36 | chdir(getRootDirectory()) |
| 37 | try: |
| 38 | process = Popen(gitArgs, stdout=PIPE, bufsize=-1) |
| 39 | try: |
| 40 | processOut = process.stdout |
| 41 | while True: |
| 42 | data = processOut.read(8192) |
| 43 | if not data: |
| 44 | break |
| 45 | gzipStream.write(data) |
| 46 | finally: |
| 47 | result = process.wait() |
| 48 | if result: |
| 49 | raise Exception("git archive process failed") |
| 50 | finally: |
| 51 | chdir(oldDirectory) |
| 52 | finally: |
| 53 | gzipStream.close() |
| 54 | |
| 55 | def findExecutable(command): |
| 56 | if not isabs(command): |
| 57 | path = environ.get("PATH") |
| 58 | if path is not None: |
| 59 | for path in path.split(pathsep): |
| 60 | commandPath = abspath(join(path.strip('\"'), command)) |
| 61 | if isfile(commandPath) and access(commandPath, X_OK): |
| 62 | return commandPath |
| 63 | commandPath = abspath(command) |
| 64 | if isfile(commandPath) and access(commandPath, X_OK): |
| 65 | return commandPath |
| 66 | return None |
| 67 | |
| 68 | def getPlatform(): |
| 69 | return _PLATFORM |
| 70 | |
| 71 | def getResourcesDirectory(): |
| 72 | return join(getRootDirectory(), "resources") |
| 73 | |
| 74 | def getRootDirectory(): |
| 75 | return abspath(join(dirname(__file__), pardir)) |
| 76 | |
| 77 | def getTemplatesDirectory(): |
| 78 | return join(getRootDirectory(), "templates") |
| 79 | |
| 80 | def writeTemplate(destination, source, data): |
| 81 | destination = abspath(destination) |
| 82 | destinationDir = dirname(destination) |
| 83 | if not isdir(destinationDir): |
| 84 | makedirs(destinationDir) |
| 85 | source = abspath(source) |
| 86 | sourceDir = dirname(source) |
| 87 | if not isdir(sourceDir): |
| 88 | makedirs(sourceDir) |
| 89 | fp = open(source) |
| 90 | try: |
| 91 | s = fp.read() |
| 92 | finally: |
| 93 | fp.close() |
| 94 | s = Template(s).substitute(data) |
| 95 | fp = open(destination, 'w') |
| 96 | try: |
| 97 | fp.write(s) |
| 98 | finally: |
| 99 | fp.close() |