<commit_before>import os
import re
from glob import glob
from urllib.request import urlopen, urlretrieve
from time import time
from zipfile import ZipFile
from itertools import combinations
from collections import defaultdict
from copy import deepcopy
import sys

def update_tsv_export(reporthook=None):
    """If export is missing or not current, download the current one.
       Returns True iff the export was updated."""

    # Is export file missing or older than 10 minutes?
    here = glob('WCA_export*_*.tsv.zip')
    if not here or time() - os.stat(max(here)).st_mtime > 10 * 60:

        # What's the current export on the WCA site?
        base = 'https://www.worldcubeassociation.org/results/misc/'
        try:
            with urlopen(base + 'export.html') as f:
                current = re.search(r'WCA_export\d+_\d+.tsv.zip',
                                    str(f.read())).group(0)
        except:
            print('failed looking for the newest export')
            return

        # Download if necessary, otherwise mark local as up-to-date
        if not os.path.isfile(current):
            if not reporthook:
                print('downloading export', current, '...')
            urlretrieve(base + current, current, reporthook)
            for h in here:
                if h != current:
                    os.remove(h)
            return True
        else:
            os.utime(max(here))

def prepair_data():
    global all_persons, event_names, all_averages, countries

    with ZipFile(max(glob('WCA_export*_*.tsv.zip'))) as zf:
        def load(wanted_table, wanted_columns):
            with zf.open('WCA_export_' + wanted_table + '.tsv') as tf:
                column_names, *rows = [line.split('\t') for line
                                       in tf.read().decode().splitlines()]
                columns = []
                for name in wanted_columns.split():
                    i = column_names.index(name)
                    column = [row[i] for row in rows]
                    try:
                        column = list(map(int, column))
                    except:
                        pass
                    columns.append(column)
                return list(zip(*columns))

        all_persons = list((id, name, countryId) for id, subid, name, countryId
                           in load('Persons', 'id subid name countryId')
                           if subid == 1)
        event_names = dict((id, name) for id, name
                           in load('Events', 'id name'))
        all_averages = load('RanksAverage', 'personId eventId best')
        countries = [country[0] for country in load('Countries', 'id')]

class TopTeams:
    def __init__(self, max_team_count):
        self.max_team_count = max_team_count
        self.teams = []

    def add(self, team, times, event_division):
        # check, if there is a team consisting of the same same people
        for i, (team2, times2, event_division2) in enumerate(self.teams):
            if set(team) == set(team2):
                if sorted(times, reverse=True) < sorted(times2, reverse=True):
                    self.teams[i] = (deepcopy(team), deepcopy(times),
                                     deepcopy(event_division))
                break
        else:
            self.teams.append((deepcopy(team), deepcopy(times),
                               deepcopy(event_division)))
        # sort teams by times and if necessary remove the last entry
        self.teams.sort(key=lambda t: sorted(t[1], reverse=True))
        if len(self.teams) > self.max_team_count:
            self.teams.pop()

    def get_worst_time(self):
        if len(self.teams) > 0:
            return self.teams[len(self.teams) - 1][1]
        else:
            return [float('inf')]

    def storage_full(self):
        return len(self.teams) == self.max_team_count

    def printTeams(self):
        global persons, event_names
        for team, times, event_division in self.teams:
            zipped_team = list(zip(team, times, event_division))
            zipped_team.sort(key=lambda t: t[1], reverse=True)
            for person, t, events in zipped_team:
                event_str = ', '.join([event_names[event] for event in events])
                time_str = ' (' + str(t/100) + ' seconds)'
                try:
                    print(persons[person] + ': ' + event_str + time_str)
                except:
                    print(person + ': ' + event_str + time_str)
            print('Total:', max(times)/100, '\n')

def search_for_team(country, team_size, events, number_of_top_teams,
                    show_output=True):
    global all_persons, all_averages
    global persons, averages, top_teams

    # organize the corresponding data
    persons = dict((id, name) for id, name, countryId in all_persons
                   if countryId == country or country == 'world')
    averages = defaultdict(dict)
    for personId, eventId, best in all_averages:
        if personId in persons and eventId in events:
            averages[personId][eventId] = best

    # remove people who have at least teamsize nemesis
    averages2 = dict()
    for person, person_events in averages.items():
        nemesis_count = -1  # a person is it's own nemesis
        for possible_nemesis, nemesis_events in averages.items():
            if set(person_events.keys()).issubset(set(nemesis_events.keys())):
                if all(person_events[event] >= nemesis_events[event]
                       for event in person_events):
                    nemesis_count += 1
                    if nemesis_count == team_size + number_of_top_teams - 1:
                        break
        else:
            averages2[person] = person_events
    averages = averages2

    top_teams = TopTeams(number_of_top_teams)
    for team in combinations(averages, team_size):
        divide_events(team, events, [0 for i in range(team_size)],
                      [[] for i in range(team_size)])

    if show_output:
        print('Top teams for %d-person teams for the'
              ' guildford_challenge in %s:' % (team_size, country))
        top_teams.printTeams()
    return top_teams

def divide_events(team, events_left, times, event_division):
    global top_teams, averages

    if len(events_left) == 0:
        top_teams.add(team, times, event_division)
    else:
        next_event = events_left[0]
        for i, person in enumerate(team):
            if next_event in averages[person]:
                times_copy = times[:]
                times_copy[i] += averages[person][next_event]
                if not top_teams.storage_full() or \
                   sorted(times_copy, reverse=True) < \
                   sorted(top_teams.get_worst_time(), reverse=True):
                    event_division2 = [events[:] for events in event_division]
                    event_division2[i].append(next_event)
                    divide_events(team, events_left[1:], times_copy,
                                  event_division2)

def country_ranking(team_size, events):
    global countries, all_persons, event_names
    persons_names = dict((id, name) for id, name, countryId in all_persons)
    country_rankings = []
    for country in countries:
        top_team = search_for_team(country, team_size, events, 1, False)
        if len(top_team.teams) > 0:
            country_rankings.append((country, deepcopy(top_team.teams[0])))

    country_rankings.sort(key=lambda i: sorted(i[1][1], reverse=True))

    for i, (country, team) in enumerate(country_rankings, start=1):
        time_str = str(max(team[1])/100) + ' seconds'
        print(str(i) + '. ' + country + ': ' + time_str)
        zipped_team = list(zip(team[0], team[1], team[2]))
        zipped_team.sort(key=lambda t: t[1], reverse=True)
        for person, t, events in zipped_team:
            event_str = ', '.join([event_names[event] for event in events])
            time_str = ' (' + str(t/100) + ' seconds)'
            try:
                print('   ' + persons_names[person] + ': ' +
                      event_str + time_str)
            except:
                print('   ' + person + ': ' +
                      event_str + time_str)

if __name__ == '__main__':
    update_tsv_export()
    prepair_data()

    global countries
    events = '777 666 555 minx 333ft 444 sq1 222 333 333oh clock pyram skewb'\
             .split()
    team_size = 3
    number_of_top_teams = 10
    country = None
    rank_by_country = False
    for command in sys.argv[1:]:
        m = re.findall(r'events=(.*)', command)
        if m:
            events = m[0].split()
            continue
        m = re.findall(r'team_size=(.*)', command)
        if m:
            try:
                team_size = int(m[0])
                continue
            except:
                pass
        m = re.findall(r'number_of_top_teams=(.*)', command)
        if m:
            try:
                number_of_top_teams = int(m[0])
                continue
            except:
                pass
        if command in countries:
            country = command
            continue
        if command == 'countries':
            rank_by_country = True
            continue
        if command == 'world':
            country = command
            continue
        # if nothing match, print usage
        print('Usage: ')
        print('  python guildford_challenge.py'
              ' country_name | countries | world')
        print('                    [events="event_names"]')
        print('                    [team_size=number]')
        print('                    [number_of_top_teams=number]')
        print()
        print('Examples: ')
        print('python guildford_challenge.py Finland')
        print('      list the top teams for Finland')
        print('python guildford_challenge.py "United Kingdom"'
              ' events="555 444 333 222 333oh sq1 pyram minx clock skewb"')
        print('      list the top teams for UK for the mini'
              ' guildford challenge')
        print('python guildford_challenge.py countries')
        print('       ranks the countries by their top team')
        print('python guildford_challenge.py world team_size=2')
        print('       top 2-person teams in the world')
        break
    else:
        if country:
            search_for_team(country, team_size, events, number_of_top_teams)
        elif rank_by_country:
            country_ranking(team_size, events)
<commit_msg>Parse argv with optparse<commit_after>import os
import re
from glob import glob
from urllib.request import urlopen, urlretrieve
from time import time
from zipfile import ZipFile
from itertools import combinations
from collections import defaultdict
from copy import deepcopy
import sys, optparse

def update_tsv_export(reporthook=None):
    """If export is missing or not current, download the current one.
       Returns True iff the export was updated."""

    # Is export file missing or older than 10 minutes?
    here = glob('WCA_export*_*.tsv.zip')
    if not here or time() - os.stat(max(here)).st_mtime > 10 * 60:

        # What's the current export on the WCA site?
        base = 'https://www.worldcubeassociation.org/results/misc/'
        try:
            with urlopen(base + 'export.html') as f:
                current = re.search(r'WCA_export\d+_\d+.tsv.zip',
                                    str(f.read())).group(0)
        except:
            print('failed looking for the newest export')
            return

        # Download if necessary, otherwise mark local as up-to-date
        if not os.path.isfile(current):
            if not reporthook:
                print('downloading export', current, '...')
            urlretrieve(base + current, current, reporthook)
            for h in here:
                if h != current:
                    os.remove(h)
            return True
        else:
            os.utime(max(here))

def prepair_data():
    global all_persons, event_names, all_averages, countries

    with ZipFile(max(glob('WCA_export*_*.tsv.zip'))) as zf:
        def load(wanted_table, wanted_columns):
            with zf.open('WCA_export_' + wanted_table + '.tsv') as tf:
                column_names, *rows = [line.split('\t') for line
                                       in tf.read().decode().splitlines()]
                columns = []
                for name in wanted_columns.split():
                    i = column_names.index(name)
                    column = [row[i] for row in rows]
                    try:
                        column = list(map(int, column))
                    except:
                        pass
                    columns.append(column)
                return list(zip(*columns))

        all_persons = list((id, name, countryId) for id, subid, name, countryId
                           in load('Persons', 'id subid name countryId')
                           if subid == 1)
        event_names = dict((id, name) for id, name
                           in load('Events', 'id name'))
        all_averages = load('RanksAverage', 'personId eventId best')
        countries = [country[0] for country in load('Countries', 'id')]

class TopTeams:
    def __init__(self, max_team_count):
        self.max_team_count = max_team_count
        self.teams = []

    def add(self, team, times, event_division):
        # check, if there is a team consisting of the same same people
        for i, (team2, times2, event_division2) in enumerate(self.teams):
            if set(team) == set(team2):
                if sorted(times, reverse=True) < sorted(times2, reverse=True):
                    self.teams[i] = (deepcopy(team), deepcopy(times),
                                     deepcopy(event_division))
                break
        else:
            self.teams.append((deepcopy(team), deepcopy(times),
                               deepcopy(event_division)))
        # sort teams by times and if necessary remove the last entry
        self.teams.sort(key=lambda t: sorted(t[1], reverse=True))
        if len(self.teams) > self.max_team_count:
            self.teams.pop()

    def get_worst_time(self):
        if len(self.teams) > 0:
            return self.teams[len(self.teams) - 1][1]
        else:
            return [float('inf')]

    def storage_full(self):
        return len(self.teams) == self.max_team_count

    def printTeams(self):
        global persons, event_names
        for team, times, event_division in self.teams:
            zipped_team = list(zip(team, times, event_division))
            zipped_team.sort(key=lambda t: t[1], reverse=True)
            for person, t, events in zipped_team:
                event_str = ', '.join([event_names[event] for event in events])
                time_str = ' (' + str(t/100) + ' seconds)'
                try:
                    print(persons[person] + ': ' + event_str + time_str)
                except:
                    print(person + ': ' + event_str + time_str)
            print('Total:', max(times)/100, '\n')

def search_for_team(country, team_size, events, number_of_top_teams,
                    show_output=True):
    global all_persons, all_averages
    global persons, averages, top_teams

    # organize the corresponding data
    persons = dict((id, name) for id, name, countryId in all_persons
                   if countryId == country or country == 'world')
    averages = defaultdict(dict)
    for personId, eventId, best in all_averages:
        if personId in persons and eventId in events:
            averages[personId][eventId] = best

    # remove people who have at least teamsize nemesis
    averages2 = dict()
    for person, person_events in averages.items():
        nemesis_count = -1  # a person is it's own nemesis
        for possible_nemesis, nemesis_events in averages.items():
            if set(person_events.keys()).issubset(set(nemesis_events.keys())):
                if all(person_events[event] >= nemesis_events[event]
                       for event in person_events):
                    nemesis_count += 1
                    if nemesis_count == team_size + number_of_top_teams - 1:
                        break
        else:
            averages2[person] = person_events
    averages = averages2

    top_teams = TopTeams(number_of_top_teams)
    for team in combinations(averages, team_size):
        divide_events(team, events, [0 for i in range(team_size)],
                      [[] for i in range(team_size)])

    if show_output:
        print('Top teams for %d-person teams for the'
              ' guildford_challenge in %s:' % (team_size, country))
        top_teams.printTeams()
    return top_teams

def divide_events(team, events_left, times, event_division):
    global top_teams, averages

    if len(events_left) == 0:
        top_teams.add(team, times, event_division)
    else:
        next_event = events_left[0]
        for i, person in enumerate(team):
            if next_event in averages[person]:
                times_copy = times[:]
                times_copy[i] += averages[person][next_event]
                if not top_teams.storage_full() or \
                   sorted(times_copy, reverse=True) < \
                   sorted(top_teams.get_worst_time(), reverse=True):
                    event_division2 = [events[:] for events in event_division]
                    event_division2[i].append(next_event)
                    divide_events(team, events_left[1:], times_copy,
                                  event_division2)

def country_ranking(team_size, events):
    global countries, all_persons, event_names
    persons_names = dict((id, name) for id, name, countryId in all_persons)
    country_rankings = []
    for country in countries:
        top_team = search_for_team(country, team_size, events, 1, False)
        if len(top_team.teams) > 0:
            country_rankings.append((country, deepcopy(top_team.teams[0])))

    country_rankings.sort(key=lambda i: sorted(i[1][1], reverse=True))

    for i, (country, team) in enumerate(country_rankings, start=1):
        time_str = str(max(team[1])/100) + ' seconds'
        print(str(i) + '. ' + country + ': ' + time_str)
        zipped_team = list(zip(team[0], team[1], team[2]))
        zipped_team.sort(key=lambda t: t[1], reverse=True)
        for person, t, events in zipped_team:
            event_str = ', '.join([event_names[event] for event in events])
            time_str = ' (' + str(t/100) + ' seconds)'
            try:
                print('   ' + persons_names[person] + ': ' +
                      event_str + time_str)
            except:
                print('   ' + person + ': ' +
                      event_str + time_str)

if __name__ == '__main__':
    parser = optparse.OptionParser("usage: %prog [options]")
    parser.add_option("-c", "--country", dest="country",
                      default="world", type="string",
                      help="country name, 'countries' or 'world'")
    parser.add_option("-e", "--events", dest="events",
                      default='777 666 555 minx 333ft 444 sq1 '
                              '222 333 333oh clock pyram skewb',
                      type="string", help="included events")
    parser.add_option("-s", "--size", dest="team_size",
                      default=3, type="int", help="team size")
    parser.add_option("-n", "--number", dest="number_of_top_teams",
                      default=10, type="int", help="show this many teams")

    (options, args) = parser.parse_args()

    update_tsv_export()
    prepair_data()

    if options.country == 'countries':
        country_ranking(options.team_size, options.events.split())
    else:
        search_for_team(options.country, options.team_size,
                        options.events.split(), options.number_of_top_teams)
<|endoftext|>


<commit_before>""" Wrapper for GwyfileObject from Libgwyfile C library """
import os.path

import numpy as np

from ._libgwyfile import ffi, lib

class GwyfileError(Exception):
    """ Class for Gwyfile C library errors"""
    pass

class Gwyfile():
    """Wrapper class for GwyfileObject from Libgwyfile C library

    Attributes:
        gwyfile (cdata  GwyfileObject*): gwyfile object from Libgwyfile C library

    """
    def __init__(self, c_gwyfile):
        if ffi.typeof(c_gwyfile) == ffi.typeof('GwyfileObject*'):
            self.c_gwyfile = c_gwyfile
        else:
            raise GwyfileError("Gwyfile should be initialized by <cdata GwyfileObject*>")

    def get_channels_ids(self):
        """Get list of channels ids

        Returns:
            [list (int)]: list of channels ids, e.g. [0, 1, 2]

        """
        nchannelsp = ffi.new("unsigned int*")
        ids = lib.gwyfile_object_container_enumerate_channels(self.c_gwyfile,
                                                              nchannelsp)
        if ids:
            return [ids[i] for i in range(nchannelsp[0])]
        else:
            return []

    def get_title(self, channel_id):
        """Get title of the channel

        Args:
            channel_id (int): id of the channel

        Returns:
            title (str): title of the channel

        """
        title_key = "/{:d}/data/title".format(channel_id)
        title_object = self._gwyfile_get_object(title_key)
        title = ffi.string(ffi.cast("char*", title_object)).decode('utf-8')
        return title

    def get_metadata(self, channel_id):
        """Get metadata from  the channel  data frame

        Args:
            channel_id (int): id of the channel

        Returns:
            metadata (dictionary): Python dictionary with the channel metadata
                                   Keys of the metadata dictionary:
                                       'xres' (int): Horizontal dimension in pixels
                                       'yres' (int): Vertical dimension in pixels
                                       'xreal' (float): Horizontal size in physical units
                                       'yreal' (float): Vertical size in physical units
                                       'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                                       'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        key = "/{:d}/data".format(channel_id)
        metadata = self._gwydf_get_metadata(key)
        return metadata

    def get_data(self, channel_id):
        """Get data from the channel

        Args:
            channel_id (int): id of the channel

        Returns:
            data (2D numpy array, float64): data frame from the channel

        """
        key = "/{:d}/data".format(channel_id)
        data = self._gwydf_get_data(key)
        return data

    def get_mask_metadata(self, channel_id):
        """Get metadata from the mask

        Args:
            channel_id (int): id of the channel

        Returns:
            metadata (dictionary): Python dictionary with the mask metadata
            keys of the metadata:
                'xres' (int): Horizontal dimension in pixels
                'yres' (int): Vertical dimension in pixels
                'xreal' (float): Horizontal size in physical units
                'yreal' (float): Vertical size in physical units
                'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        key = "/{:d}/mask".format(channel_id)
        metadata = self._gwydf_get_metadata(key)
        return metadata

    def get_mask_data(self, channel_id):
        """Get data from the mask

        Args:
            channel_id (int): id of the channel

        Returns:
            data (2D numpy array, float64): data frame from the mask

        """
        key = "/{:d}/mask".format(channel_id)
        data = self._gwydf_get_data(key)
        return data

    def get_presentation_metadata(self, channel_id):
        """Get metadata from the presentation

        Args:
            channel_id (int): id of the channel

        Returns:
            metadata (dictionary): Python dictionary with GWY
            keys of the metadata:
                'xres' (int): Horizontal dimension in pixels
                'yres' (int): Vertical dimension in pixels
                'xreal' (float): Horizontal size in physical units
                'yreal' (float): Vertical size in physical units
                'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        key = "/{:d}/show".format(channel_id)
        metadata = self._gwydf_get_metadata(key)
        return metadata

    def get_presentation_data(self, channel_id):
        """Get data from the presentation

        Args:
            channel_id (int): id of the channel

        Returns:
            data (2D numpy array, float64): data frame from the presentation

        """
        key = "/{:d}/show".format(channel_id)
        data = self._gwydf_get_data(key)
        return data

    def _gwyfile_get_object(self, key):
        """Get the object value with a name "key"

        Args:
            key (str): Name of the key, e.g. "/0/data"

        Returns:
            item_object (cdata GwyfileObject*): The value of the object

        """
        item = lib.gwyfile_object_get(self.c_gwyfile, key.encode('utf-8'))
        if not item:
            raise GwyfileError("Cannot find the item \"{}\"".format(key))
        item_object = lib.gwyfile_item_get_object(item)
        if not item_object:
            raise GwyfileError("Cannot find the object value of the item \"{}\"".format(key))
        return item_object

    def _gwydf_get_metadata(self, key):
        """Get metadata from  the data frame (channel, mask or presentation)

        Args:
            key (str): name of the data frame (e.g. "/0/data/")

        Returns:
            metadata (dictionary): Python dictionary with the data frame metadata

                                   Keys of the metadata dictionary:
                                       'xres' (int): Horizontal dimension in pixels
                                       'yres' (int): Vertical dimension in pixels
                                       'xreal' (float): Horizontal size in physical units
                                       'yreal' (float): Vertical size in physical units
                                       'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                                       'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        errorp = ffi.new("GwyfileError**")
        xresp = ffi.new("int32_t*")
        yresp = ffi.new("int32_t*")
        xrealp = ffi.new("double*")
        yrealp = ffi.new("double*")
        xyunitp = ffi.new("char**")
        zunitp = ffi.new("char**")

        metadata = {}

        df = self._gwyfile_get_object(key)

        if lib.gwyfile_object_datafield_get(df, errorp,
                                        ffi.new("char[]", b'xres'), xresp,
                                        ffi.new("char[]", b'yres'), yresp,
                                        ffi.new("char[]", b'xreal'), xrealp,
                                        ffi.new("char[]", b'yreal'), yrealp,
                                        ffi.new("char[]", b'si_unit_xy'), xyunitp,
                                        ffi.new("char[]", b'si_unit_z'), zunitp,
                                        ffi.NULL):
            metadata['xres'] = xresp[0]
            metadata['yres'] = yresp[0]
            metadata['xreal'] = xrealp[0]
            metadata['yreal'] = yrealp[0]
            metadata['xyunit'] = ffi.string(xyunitp[0]).decode('utf-8')
            metadata['zunit'] = ffi.string(zunitp[0]).decode('utf-8')
            return metadata
        else:
            error_msg = ffi.string(errorp[0].message).decode('utf-8')
            raise GwyfileError(error_msg)

    def _gwydf_get_data(self, key):
        """Get data array from the GWY dataframe (e.g. channel, mask, presentation)

        Args:
            key (str): name of the data frame (e.g. "/0/data")

        Returns:
            data (2D numpy array, float64): data from the data frame

        """
        xresp = ffi.new("int32_t*")
        yresp = ffi.new("int32_t*")
        errorp = ffi.new("GwyfileError**")

        df = self._gwyfile_get_object(key)

        if lib.gwyfile_object_datafield_get(df, errorp,
                                            ffi.new("char[]", b'xres'), xresp,
                                            ffi.new("char[]", b'yres'), yresp,
                                            ffi.NULL):
            xres = xresp[0]
            yres = yresp[0]
        else:
            error_msg = ffi.string(errorp[0].message).decode('utf-8')
            raise GwyfileError(error_msg)

        data = ffi.new("double[]", xres*yres)
        datap = ffi.new("double**", data)

        if lib.gwyfile_object_datafield_get(df, errorp,
                                            ffi.new("char[]", b'data'), datap,
                                            ffi.NULL):
            data_buf = ffi.buffer(datap[0], xres*yres*ffi.sizeof(data))
            data_array = np.frombuffer(data_buf, dtype=np.float64,
                                       count=xres*yres).reshape((xres, yres))
            return data_array
        else:
            error_msg = ffi.string(errorp[0].message).decode('utf-8')
            raise GwyfileError(error_msg)

    def _gwyobject_check(self, key):
        """Check the presence of the object

        Args:
            key(str): object key

        """
        item = lib.gwyfile_object_get(self.c_gwyfile, key.encode('utf-8'))
        if not item:
            return False
        else:
            return True

    def get_channel(self, channel_id):
        """Return channel data as GwyChannel object

        Args:
            channel_id (int): id of the channels
        """
        title = self.get_title(channel_id)

        data = self.get_data(channel_id)
        metadata = self.get_metadata(channel_id)
        channel_df = GwyDataframe(data, metadata)

        if self._gwyobject_check("/{:d}/mask".format(channel_id)):
            mask_metadata = self.get_mask_metadata(channel_id)
            mask_data = self.get_mask_data(channel_id)
            mask_df = GwyDataframe(mask_data, mask_metadata)
        else:
            mask_df = None

        if self._gwyobject_check("/{:d}/show".format(channel_id)):
            presentation_data = self.get_presentation_data(channel_id)
            presentation_metadata = self.get_presentation_metadata(channel_id)
            presentation_df = GwyDataframe(presentation_data, presentation_metadata)
        else:
            presentation_df = None

        channel = GwyChannel(title, channel_df, mask_df, presentation_df)
        return channel

    def get_container(self):
        """Return GwyContainer object

        """
        ids = self.get_channels_ids()
        channels = [self.get_channel(channel_id) for channel_id in ids]
        return GwyContainer(channels)

class GwyDataframe():
    """Class for Gwy Dataframe representation

    Attributes:
        data (np.float64 array): 2D numpy array with the Dataframe data
        xres (int): Horizontal dimension of the dataframe in pixels
        yres (int): Vertical dimension of the dataframe in pixels
        xreal (float): Horizontal size of the dataframe in physical units
        yreal (float): Vertical size of the dataframe in physical units
        xyunit (str): Physical unit of lateral dimensions, base SI unit, e.g. 'm'
        zunit (str): Physical unit of vertical dimension, base SI unit, e.g. 'm'

    """
    def __init__(self, data, metadata):
        """
        Args:
            data (np.float64 array): 2D numpy array with GWY dataframe data
            metadata (dictionary): Python dictionary with GWY dataframe metadata

        """
        self.data = data
        for key in metadata:
            setattr(self, key, metadata[key])

class GwyChannel():
    """Class for Gwy channel representation.
    Contains at least one dataframe.
    Could also contain Mask or Presentation data frames.

    Attributes:
        title (str): Title of the GWY channel
        dataframe (GwyDataframe): Dataframe of the channel
        mask (GwyDataframe): Mask of the channel
        presentation (GwyDataframe): Presentation of the channel

    """
    def __init__(self, title, dataframe, mask=None, presentation=None):
        self.title = title
        self.dataframe = dataframe
        self.mask = mask
        self.presentation = presentation

class GwyContainer():
    """Class for Gwy container representation.

    Attributes:
        channels (list): list of GwyChannel objects

    """
    def __init__(self, channels):
        self.channels = channels

def read_gwy(filename):
    """Read gwy file

    Args:
        filename (str): Name of the gwyddion file

    Returns:
        Object of the Gwyfile class

    """
    errorp = ffi.new("GwyfileError**")

    if not os.path.isfile(filename):
        raise OSError("Cannot read file {}".format(filename))

    c_gwyfile = lib.gwyfile_read_file(filename.encode('utf-8'), errorp)

    if not c_gwyfile:
        error_msg = ffi.string(errorp[0].message).decode('utf-8')
        raise GwyfileError(error_msg)
    elif not ffi.string(lib.gwyfile_object_name(c_gwyfile)) == b'GwyContainer':
        error_msg = "The top-level object in the file {} is not a GwyContainer".format(filename)
        raise GwyfileError(error_msg)

    return Gwyfile(c_gwyfile)

<commit_msg>Rename dataframe to datafield (correponding to libgwyfile)<commit_after>""" Wrapper for GwyfileObject from Libgwyfile C library """
import os.path

import numpy as np

from ._libgwyfile import ffi, lib

class GwyfileError(Exception):
    """ Class for Gwyfile C library errors"""
    pass

class Gwyfile():
    """Wrapper class for GwyfileObject from Libgwyfile C library

    Attributes:
        gwyfile (cdata  GwyfileObject*): gwyfile object from Libgwyfile C library

    """
    def __init__(self, c_gwyfile):
        if ffi.typeof(c_gwyfile) == ffi.typeof('GwyfileObject*'):
            self.c_gwyfile = c_gwyfile
        else:
            raise GwyfileError("Gwyfile should be initialized by <cdata GwyfileObject*>")

    def get_channels_ids(self):
        """Get list of channels ids

        Returns:
            [list (int)]: list of channels ids, e.g. [0, 1, 2]

        """
        nchannelsp = ffi.new("unsigned int*")
        ids = lib.gwyfile_object_container_enumerate_channels(self.c_gwyfile,
                                                              nchannelsp)
        if ids:
            return [ids[i] for i in range(nchannelsp[0])]
        else:
            return []

    def get_title(self, channel_id):
        """Get title of the channel

        Args:
            channel_id (int): id of the channel

        Returns:
            title (str): title of the channel

        """
        title_key = "/{:d}/data/title".format(channel_id)
        title_object = self._gwyfile_get_object(title_key)
        title = ffi.string(ffi.cast("char*", title_object)).decode('utf-8')
        return title

    def get_metadata(self, channel_id):
        """Get metadata from  the channel data field

        Args:
            channel_id (int): id of the channel

        Returns:
            metadata (dictionary): Python dictionary with the channel data field metadata
                                   Keys of the metadata dictionary:
                                       'xres' (int): Horizontal dimension in pixels
                                       'yres' (int): Vertical dimension in pixels
                                       'xreal' (float): Horizontal size in physical units
                                       'yreal' (float): Vertical size in physical units
                                       'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                                       'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        key = "/{:d}/data".format(channel_id)
        metadata = self._gwydf_get_metadata(key)
        return metadata

    def get_data(self, channel_id):
        """Get data from the channel data field

        Args:
            channel_id (int): id of the channel

        Returns:
            data (2D numpy array, float64): data field from the channel

        """
        key = "/{:d}/data".format(channel_id)
        data = self._gwydf_get_data(key)
        return data

    def get_mask_metadata(self, channel_id):
        """Get metadata from the mask

        Args:
            channel_id (int): id of the channel

        Returns:
            metadata (dictionary): Python dictionary with the mask metadata
            keys of the metadata:
                'xres' (int): Horizontal dimension in pixels
                'yres' (int): Vertical dimension in pixels
                'xreal' (float): Horizontal size in physical units
                'yreal' (float): Vertical size in physical units
                'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        key = "/{:d}/mask".format(channel_id)
        metadata = self._gwydf_get_metadata(key)
        return metadata

    def get_mask_data(self, channel_id):
        """Get data from the mask

        Args:
            channel_id (int): id of the channel

        Returns:
            data (2D numpy array, float64): data field from the mask

        """
        key = "/{:d}/mask".format(channel_id)
        data = self._gwydf_get_data(key)
        return data

    def get_presentation_metadata(self, channel_id):
        """Get metadata from the presentation

        Args:
            channel_id (int): id of the channel

        Returns:
            metadata (dictionary): Python dictionary with GWY
            keys of the metadata:
                'xres' (int): Horizontal dimension in pixels
                'yres' (int): Vertical dimension in pixels
                'xreal' (float): Horizontal size in physical units
                'yreal' (float): Vertical size in physical units
                'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        key = "/{:d}/show".format(channel_id)
        metadata = self._gwydf_get_metadata(key)
        return metadata

    def get_presentation_data(self, channel_id):
        """Get data from the presentation

        Args:
            channel_id (int): id of the channel

        Returns:
            data (2D numpy array, float64): data field from the presentation

        """
        key = "/{:d}/show".format(channel_id)
        data = self._gwydf_get_data(key)
        return data

    def _gwyfile_get_object(self, key):
        """Get the object value with a name "key"

        Args:
            key (str): Name of the key, e.g. "/0/data"

        Returns:
            item_object (cdata GwyfileObject*): The value of the object

        """
        item = lib.gwyfile_object_get(self.c_gwyfile, key.encode('utf-8'))
        if not item:
            raise GwyfileError("Cannot find the item \"{}\"".format(key))
        item_object = lib.gwyfile_item_get_object(item)
        if not item_object:
            raise GwyfileError("Cannot find the object value of the item \"{}\"".format(key))
        return item_object

    def _gwydf_get_metadata(self, key):
        """Get metadata from  the data field (channel, mask or presentation)

        Args:
            key (str): name of the data field (e.g. "/0/data/")

        Returns:
            metadata (dictionary): Python dictionary with the data field metadata

                                   Keys of the metadata dictionary:
                                       'xres' (int): Horizontal dimension in pixels
                                       'yres' (int): Vertical dimension in pixels
                                       'xreal' (float): Horizontal size in physical units
                                       'yreal' (float): Vertical size in physical units
                                       'xyunit' (str): Physical units of lateral dimensions, base SI units, e.g. "m"
                                       'zunit' (str): Physical unit of vertical dimension, base SI unit, e.g. "m"

        """
        errorp = ffi.new("GwyfileError**")
        xresp = ffi.new("int32_t*")
        yresp = ffi.new("int32_t*")
        xrealp = ffi.new("double*")
        yrealp = ffi.new("double*")
        xyunitp = ffi.new("char**")
        zunitp = ffi.new("char**")

        metadata = {}

        df = self._gwyfile_get_object(key)

        if lib.gwyfile_object_datafield_get(df, errorp,
                                        ffi.new("char[]", b'xres'), xresp,
                                        ffi.new("char[]", b'yres'), yresp,
                                        ffi.new("char[]", b'xreal'), xrealp,
                                        ffi.new("char[]", b'yreal'), yrealp,
                                        ffi.new("char[]", b'si_unit_xy'), xyunitp,
                                        ffi.new("char[]", b'si_unit_z'), zunitp,
                                        ffi.NULL):
            metadata['xres'] = xresp[0]
            metadata['yres'] = yresp[0]
            metadata['xreal'] = xrealp[0]
            metadata['yreal'] = yrealp[0]
            metadata['xyunit'] = ffi.string(xyunitp[0]).decode('utf-8')
            metadata['zunit'] = ffi.string(zunitp[0]).decode('utf-8')
            return metadata
        else:
            error_msg = ffi.string(errorp[0].message).decode('utf-8')
            raise GwyfileError(error_msg)

    def _gwydf_get_data(self, key):
        """Get data array from the GWY data field (e.g. channel, mask, presentation)

        Args:
            key (str): name of the data field (e.g. "/0/data")

        Returns:
            data (2D numpy array, float64): data from the data field

        """
        xresp = ffi.new("int32_t*")
        yresp = ffi.new("int32_t*")
        errorp = ffi.new("GwyfileError**")

        df = self._gwyfile_get_object(key)

        if lib.gwyfile_object_datafield_get(df, errorp,
                                            ffi.new("char[]", b'xres'), xresp,
                                            ffi.new("char[]", b'yres'), yresp,
                                            ffi.NULL):
            xres = xresp[0]
            yres = yresp[0]
        else:
            error_msg = ffi.string(errorp[0].message).decode('utf-8')
            raise GwyfileError(error_msg)

        data = ffi.new("double[]", xres*yres)
        datap = ffi.new("double**", data)

        if lib.gwyfile_object_datafield_get(df, errorp,
                                            ffi.new("char[]", b'data'), datap,
                                            ffi.NULL):
            data_buf = ffi.buffer(datap[0], xres*yres*ffi.sizeof(data))
            data_array = np.frombuffer(data_buf, dtype=np.float64,
                                       count=xres*yres).reshape((xres, yres))
            return data_array
        else:
            error_msg = ffi.string(errorp[0].message).decode('utf-8')
            raise GwyfileError(error_msg)

    def _gwyobject_check(self, key):
        """Check the presence of the object

        Args:
            key(str): object key

        """
        item = lib.gwyfile_object_get(self.c_gwyfile, key.encode('utf-8'))
        if not item:
            return False
        else:
            return True

    def get_channel(self, channel_id):
        """Return channel data as GwyChannel object

        Args:
            channel_id (int): id of the channels
        """
        title = self.get_title(channel_id)

        data = self.get_data(channel_id)
        metadata = self.get_metadata(channel_id)
        channel_df = GwyDatafield(data, metadata)

        if self._gwyobject_check("/{:d}/mask".format(channel_id)):
            mask_metadata = self.get_mask_metadata(channel_id)
            mask_data = self.get_mask_data(channel_id)
            mask_df = GwyDatafield(mask_data, mask_metadata)
        else:
            mask_df = None

        if self._gwyobject_check("/{:d}/show".format(channel_id)):
            presentation_data = self.get_presentation_data(channel_id)
            presentation_metadata = self.get_presentation_metadata(channel_id)
            presentation_df = GwyDatafield(presentation_data, presentation_metadata)
        else:
            presentation_df = None

        channel = GwyChannel(title, channel_df, mask_df, presentation_df)
        return channel

    def get_container(self):
        """Return GwyContainer object

        """
        ids = self.get_channels_ids()
        channels = [self.get_channel(channel_id) for channel_id in ids]
        return GwyContainer(channels)

class GwyDatafield():
    """Class for Gwy Datafield representation

    Attributes:
        data (np.float64 array): 2D numpy array with the datafield
        xres (int): Horizontal dimension of the data field in pixels
        yres (int): Vertical dimension of the data field in pixels
        xreal (float): Horizontal size of the data field in physical units
        yreal (float): Vertical size of the data field in physical units
        xyunit (str): Physical unit of lateral dimensions, base SI unit, e.g. 'm'
        zunit (str): Physical unit of vertical dimension, base SI unit, e.g. 'm'

    """
    def __init__(self, data, metadata):
        """
        Args:
            data (np.float64 array): 2D numpy array with GWY data field
            metadata (dictionary): Python dictionary with GWY datafield metadata

        """
        self.data = data
        for key in metadata:
            setattr(self, key, metadata[key])

class GwyChannel():
    """Class for Gwy channel representation.
    Contains at least one datafield.
    Could also contain Mask or Presentation datafields.

    Attributes:
        title (str): Title of the GWY channel
        datafield (GwyDatafield): Datafield of the channel
        mask (GwyDatafield): Mask of the channel
        presentation (GwyDatafield): Presentation of the channel

    """
    def __init__(self, title, datafield, mask=None, presentation=None):
        self.title = title
        self.datafield = datafield
        self.mask = mask
        self.presentation = presentation

class GwyContainer():
    """Class for Gwy container representation.

    Attributes:
        channels (list): list of GwyChannel objects

    """
    def __init__(self, channels):
        self.channels = channels

def read_gwy(filename):
    """Read gwy file

    Args:
        filename (str): Name of the gwyddion file

    Returns:
        Object of the Gwyfile class

    """
    errorp = ffi.new("GwyfileError**")

    if not os.path.isfile(filename):
        raise OSError("Cannot read file {}".format(filename))

    c_gwyfile = lib.gwyfile_read_file(filename.encode('utf-8'), errorp)

    if not c_gwyfile:
        error_msg = ffi.string(errorp[0].message).decode('utf-8')
        raise GwyfileError(error_msg)
    elif not ffi.string(lib.gwyfile_object_name(c_gwyfile)) == b'GwyContainer':
        error_msg = "The top-level object in the file {} is not a GwyContainer".format(filename)
        raise GwyfileError(error_msg)

    return Gwyfile(c_gwyfile)

<|endoftext|>


<commit_before>import time
import random
import pygame
import pygame.midi

pygame.init()

pygame.midi.init()

print pygame.midi.get_default_output_id()
print pygame.midi.get_device_info(1)

midi_Output = pygame.midi.Output(1)
midi_Output.set_instrument(0)

# Class that represents a midi instrument.
class MidiInstrument:
    def __init__(self, no_of_positions=120):
        self.no_of_positions = no_of_positions
        self.playing_notes = set()
        self.stopping_notes = set()

    # Call this to dispatch midi messages
    def update(self):
        messages_dict = {}
        for note in self.stopping_notes:
            messages_dict[str(note.position)] = [note.position, 0, 0]
        for note in self.playing_notes:
            if str(note.position) in messages_dict:
                messages_dict[str(note.position)][1] += note.volume
            else:
                messages_dict[str(note.position)] = [note.position, note.volume, 0]
            if messages_dict[str(note.position)][2] > 112:
                messages_dict[str(note.position)][2] = 112
        for message in messages_dict.values():
            midi_Output.note_on(message[0], velocity=message[1], channel=message[2])
        self.stopping_notes = set()

    # Adds a chord or note to start playing. Will play once update called and until stop called.
    def play(self, obj):
        if isinstance(obj, Chord):
            for note in obj.notes:
                self.play(note)
        else:
            self.playing_notes.add(obj)

    # Stop playing a note or chord. Will take effect once update called.
    def stop(self, obj):
        if isinstance(obj, Chord):
            for note in obj.notes:
                self.stop(note)
        elif obj in self.playing_notes:
            self.playing_notes.remove(obj)
            self.stopping_notes.add(obj)

    # Stop playing everything
    def stop_all(self):
        self.stopping_notes.update(self.playing_notes)
        self.playing_notes = set()

# noinspection PyClassHasNoInit
class Key:
    C = 0
    C_Sharp = 1
    D = 2
    D_Sharp = 3
    E = 4
    F = 5
    F_Sharp = 6
    G = 7
    G_Sharp = 8
    A = 9
    A_Sharp = 10
    B = 11

# Represents a note
class Note:
    def __init__(self, position, volume=112):
        self.position = position
        self.volume = volume

# Represents a chord
class Chord:
    # These lists are positions within a scale. [0, 2, 4] is first, third and fifth which is a regular triad
    triad = [0, 2, 4]
    triad_octave = [0, 2, 4, 7]
    suspended_second = [0, 2, 3]
    suspended_fourth = [0, 2, 5]
    seventh = [0, 2, 4, 7]
    seventh_octave = [0, 2, 4, 6, 7]
    sixth = [0, 2, 4, 5, 7]

    all = [triad, triad_octave, suspended_second, suspended_fourth, seventh, seventh_octave, sixth]

    def __init__(self, notes):
        self.notes = notes

    def add_stress(self, note):
        self.notes.append(note)

# Represents a scale
class Scale:
    major = [0, 2, 4, 5, 7, 9, 11]
    minor = [0, 2, 3, 5, 7, 8, 10]
    minor_pentatonic = [0, 3, 5, 7, 10]
    minor_blues = [0, 3, 5, 6, 7, 10]

    all = [major, minor, minor_pentatonic, minor_blues]

    # Make a new scale with a scale passed to it (e.g. scale = Scale(minor_blues))
    def __init__(self, scale, key=Key.C, base_octave=3):
        self.scale = scale
        self.length = len(scale)
        self.base_octave = base_octave
        self.key = key

    def interval_to_position(self, interval):
        return self.key + self.scale[interval % self.length] + 12 * (interval / self.length + self.base_octave)

    # Get a note from this scale starting with a given interval from the root (0, 1, 2, 3 etc.)
    def note(self, interval):
        position = self.interval_to_position(interval)
        return Note(position)

    # Get a chord from this scale starting with a given interval from the root (0, 1, 2, 3 etc.) Set the chord type
    # using intervals (e.g. chord = scale.chord(0, intervals=Chord.triad) gives the root triad. Chords always in key!)
    def chord(self, interval, intervals=Chord.triad):
        positions = map(lambda i: self.interval_to_position(interval + i), intervals)
        return Chord(map(Note, positions))

    # Go up by number of octaves
    def change_octave(self, by):
        self.base_octave = self.base_octave + by
<commit_msg>using note_off, but still with odd double note issue<commit_after>import time
import random
import pygame
import pygame.midi

pygame.init()

pygame.midi.init()

print pygame.midi.get_default_output_id()
print pygame.midi.get_device_info(1)

midi_Output = pygame.midi.Output(1)
midi_Output.set_instrument(0)

# Class that represents a midi instrument.
class MidiInstrument:
    def __init__(self, no_of_positions=120):
        self.no_of_positions = no_of_positions
        self.playing_notes = set()
        self.stopping_notes = set()

    # Call this to dispatch midi messages
    def update(self):
        messages_dict = {}
        for note in self.stopping_notes:
            messages_dict[str(note.position)] = [note.position, 0, 0]
        for note in self.playing_notes:
            if str(note.position) in messages_dict:
                messages_dict[str(note.position)][1] += note.volume
            else:
                messages_dict[str(note.position)] = [note.position, note.volume, 0]
            if messages_dict[str(note.position)][2] > 112:
                messages_dict[str(note.position)][2] = 112
        for message in messages_dict.values():
            if message[1] > 0:
                print "note on {}".format(message)
                midi_Output.note_on(message[0], velocity=message[1], channel=message[2])
            else:
                print "note off {}".format(message)
                midi_Output.note_off(message[0], velocity=0, channel=message[2])
        self.stopping_notes = set()

    # Adds a chord or note to start playing. Will play once update called and until stop called.
    def play(self, obj):
        if isinstance(obj, Chord):
            for note in obj.notes:
                self.play(note)
        else:
            self.playing_notes.add(obj)

    # Stop playing a note or chord. Will take effect once update called.
    def stop(self, obj):
        if isinstance(obj, Chord):
            for note in obj.notes:
                self.stop(note)
        elif obj in self.playing_notes:
            self.playing_notes.remove(obj)
            self.stopping_notes.add(obj)

    # Stop playing everything
    def stop_all(self):
        self.stopping_notes.update(self.playing_notes)
        self.playing_notes = set()

# noinspection PyClassHasNoInit
class Key:
    C = 0
    C_Sharp = 1
    D = 2
    D_Sharp = 3
    E = 4
    F = 5
    F_Sharp = 6
    G = 7
    G_Sharp = 8
    A = 9
    A_Sharp = 10
    B = 11

# Represents a note
class Note:
    def __init__(self, position, volume=112):
        self.position = position
        self.volume = volume

# Represents a chord
class Chord:
    # These lists are positions within a scale. [0, 2, 4] is first, third and fifth which is a regular triad
    triad = [0, 2, 4]
    triad_octave = [0, 2, 4, 7]
    suspended_second = [0, 2, 3]
    suspended_fourth = [0, 2, 5]
    seventh = [0, 2, 4, 7]
    seventh_octave = [0, 2, 4, 6, 7]
    sixth = [0, 2, 4, 5, 7]

    all = [triad, triad_octave, suspended_second, suspended_fourth, seventh, seventh_octave, sixth]

    def __init__(self, notes):
        self.notes = notes

    def add_stress(self, note):
        self.notes.append(note)

# Represents a scale
class Scale:
    major = [0, 2, 4, 5, 7, 9, 11]
    minor = [0, 2, 3, 5, 7, 8, 10]
    minor_pentatonic = [0, 3, 5, 7, 10]
    minor_blues = [0, 3, 5, 6, 7, 10]

    all = [major, minor, minor_pentatonic, minor_blues]

    # Make a new scale with a scale passed to it (e.g. scale = Scale(minor_blues))
    def __init__(self, scale, key=Key.C, base_octave=3):
        self.scale = scale
        self.length = len(scale)
        self.base_octave = base_octave
        self.key = key

    def interval_to_position(self, interval):
        return self.key + self.scale[interval % self.length] + 12 * (interval / self.length + self.base_octave)

    # Get a note from this scale starting with a given interval from the root (0, 1, 2, 3 etc.)
    def note(self, interval):
        position = self.interval_to_position(interval)
        return Note(position)

    # Get a chord from this scale starting with a given interval from the root (0, 1, 2, 3 etc.) Set the chord type
    # using intervals (e.g. chord = scale.chord(0, intervals=Chord.triad) gives the root triad. Chords always in key!)
    def chord(self, interval, intervals=Chord.triad):
        positions = map(lambda i: self.interval_to_position(interval + i), intervals)
        return Chord(map(Note, positions))

    # Go up by number of octaves
    def change_octave(self, by):
        self.base_octave = self.base_octave + by
<|endoftext|>


<commit_before>#!/usr/bin/env python
from __future__ import print_function

import os
import shutil
import subprocess
import platform
from distutils.command.build import build

from setuptools import Extension, find_packages, setup

try:
    import numpy as np
except ImportError:
    print('Please install numpy before installing neuroglancer')
    raise

static_files = ['main.bundle.js', 'chunk_worker.bundle.js', 'styles.css', 'index.html']

class bundle_client(build):

    user_options = [
        ('client-bundle-type=', None,
         'The nodejs bundle type. "min" (default) creates condensed static files for production, "dev" creates human-readable files.'
         )
    ]

    def initialize_options(self):

        self.client_bundle_type = 'min'

    def finalize_options(self):

        if self.client_bundle_type not in ['min', 'dev']:
            raise RuntimeError('client-bundle-type has to be one of "min" or "dev"')

    def run(self):

        this_dir = os.path.abspath(os.path.dirname(__file__))
        project_dir = os.path.join(this_dir, '..')

        build_dir = os.path.join(project_dir, 'dist/python-' + self.client_bundle_type)
        static_dir = os.path.join(this_dir, 'neuroglancer/static')

        print("Project dir " + project_dir)
        print("Build dir " + build_dir)
        print("Static dir " + static_dir)

        prev_dir = os.path.abspath('.')
        os.chdir(project_dir)

        target = {"min": "build-python-min", "dev": "build-python"}

        try:
            t = target[self.client_bundle_type]
            subprocess.call(['npm', 'i'])
            res = subprocess.call(['npm', 'run', t])
        except:
            raise RuntimeError(
                'Could not run \'npm run %s\'. Make sure node.js >= v5.9.0 is installed and in your path.'
                % t)

        if res != 0:
            raise RuntimeError('failed to bundle neuroglancer node.js project')

        try:
            os.mkdir(static_dir)
        except OSError:
            pass

        for f in static_files:
            shutil.copy(os.path.join(build_dir, f), os.path.join(static_dir, f))

        os.chdir(prev_dir)

setup_dir = os.path.dirname(__file__)
src_dir = os.path.join(setup_dir, 'ext/src')
openmesh_dir = os.path.join(setup_dir, 'ext/third_party/openmesh/OpenMesh/src')
local_sources = [
    '_neuroglancer.cc',
    'openmesh_dependencies.cc',
    'on_demand_object_mesh_generator.cc',
    'voxel_mesh_generator.cc',
    'mesh_objects.cc',
]

USE_OMP = False
if USE_OMP:
    openmp_flags = ['-fopenmp']
else:
    openmp_flags = []

extra_compile_args = ['-std=c++11', '-fvisibility=hidden', '-O3'] + openmp_flags
if platform.system() == 'Darwin':
    extra_compile_args.insert(0, '-stdlib=libc++')

setup(
    name='neuroglancer',
    version='1.0.6',
    description='Python data backend for neuroglancer, a WebGL-based viewer for volumetric data',
    author='Jeremy Maitin-Shepard, Jan Funke',
    author_email='jbms@google.com, jfunke@iri.upc.edu',
    url='https://github.com/google/neuroglancer',
    license='Apache License 2.0',
    packages=find_packages(),
    package_data={
        'neuroglancer.static': static_files,
    },
    install_requires=[
        "Pillow>=3.2.0",
        "numpy>=1.11.0",
        'requests',
        'tornado',
        'sockjs-tornado',
        'six',
        'google-apitools',
    ],
    extras_require={
        ":python_version<'3.2'": ['futures'],
    },
    ext_modules=[
        Extension(
            'neuroglancer._neuroglancer',
            sources=[os.path.join(src_dir, name) for name in local_sources],
            language='c++',
            include_dirs=[np.get_include(), openmesh_dir],
            extra_compile_args=extra_compile_args,
            extra_link_args=openmp_flags),
    ],
    cmdclass={'bundle_client': bundle_client},
)
<commit_msg>chore(python): update package version to 1.0.7<commit_after>#!/usr/bin/env python
from __future__ import print_function

import os
import shutil
import subprocess
import platform
from distutils.command.build import build

from setuptools import Extension, find_packages, setup

try:
    import numpy as np
except ImportError:
    print('Please install numpy before installing neuroglancer')
    raise

static_files = ['main.bundle.js', 'chunk_worker.bundle.js', 'styles.css', 'index.html']

class bundle_client(build):

    user_options = [
        ('client-bundle-type=', None,
         'The nodejs bundle type. "min" (default) creates condensed static files for production, "dev" creates human-readable files.'
         )
    ]

    def initialize_options(self):

        self.client_bundle_type = 'min'

    def finalize_options(self):

        if self.client_bundle_type not in ['min', 'dev']:
            raise RuntimeError('client-bundle-type has to be one of "min" or "dev"')

    def run(self):

        this_dir = os.path.abspath(os.path.dirname(__file__))
        project_dir = os.path.join(this_dir, '..')

        build_dir = os.path.join(project_dir, 'dist/python-' + self.client_bundle_type)
        static_dir = os.path.join(this_dir, 'neuroglancer/static')

        print("Project dir " + project_dir)
        print("Build dir " + build_dir)
        print("Static dir " + static_dir)

        prev_dir = os.path.abspath('.')
        os.chdir(project_dir)

        target = {"min": "build-python-min", "dev": "build-python"}

        try:
            t = target[self.client_bundle_type]
            subprocess.call(['npm', 'i'])
            res = subprocess.call(['npm', 'run', t])
        except:
            raise RuntimeError(
                'Could not run \'npm run %s\'. Make sure node.js >= v5.9.0 is installed and in your path.'
                % t)

        if res != 0:
            raise RuntimeError('failed to bundle neuroglancer node.js project')

        try:
            os.mkdir(static_dir)
        except OSError:
            pass

        for f in static_files:
            shutil.copy(os.path.join(build_dir, f), os.path.join(static_dir, f))

        os.chdir(prev_dir)

setup_dir = os.path.dirname(__file__)
src_dir = os.path.join(setup_dir, 'ext/src')
openmesh_dir = os.path.join(setup_dir, 'ext/third_party/openmesh/OpenMesh/src')
local_sources = [
    '_neuroglancer.cc',
    'openmesh_dependencies.cc',
    'on_demand_object_mesh_generator.cc',
    'voxel_mesh_generator.cc',
    'mesh_objects.cc',
]

USE_OMP = False
if USE_OMP:
    openmp_flags = ['-fopenmp']
else:
    openmp_flags = []

extra_compile_args = ['-std=c++11', '-fvisibility=hidden', '-O3'] + openmp_flags
if platform.system() == 'Darwin':
    extra_compile_args.insert(0, '-stdlib=libc++')

setup(
    name='neuroglancer',
    version='1.0.7',
    description='Python data backend for neuroglancer, a WebGL-based viewer for volumetric data',
    author='Jeremy Maitin-Shepard, Jan Funke',
    author_email='jbms@google.com, jfunke@iri.upc.edu',
    url='https://github.com/google/neuroglancer',
    license='Apache License 2.0',
    packages=find_packages(),
    package_data={
        'neuroglancer.static': static_files,
    },
    install_requires=[
        "Pillow>=3.2.0",
        "numpy>=1.11.0",
        'requests',
        'tornado',
        'sockjs-tornado',
        'six',
        'google-apitools',
    ],
    extras_require={
        ":python_version<'3.2'": ['futures'],
    },
    ext_modules=[
        Extension(
            'neuroglancer._neuroglancer',
            sources=[os.path.join(src_dir, name) for name in local_sources],
            language='c++',
            include_dirs=[np.get_include(), openmesh_dir],
            extra_compile_args=extra_compile_args,
            extra_link_args=openmp_flags),
    ],
    cmdclass={'bundle_client': bundle_client},
)
<|endoftext|>


<commit_before>#!/usr/bin/env python
import sys
import os.path
from distutils.core import setup, Extension
import distutils.sysconfig
from Cython.Build import cythonize
import numpy

numpyIncludeDir = numpy.get_include()

compile_args = ['-fopenmp']
link_args = ['-fopenmp']
if os.path.exists('debug'):
    compile_args += ['-O0', '-U NDEBUG']

libSrcDir = '../libd3q15/'

cyDeltaExt = Extension('d3q15.CyDelta', ['d3q15/CyDelta.pyx'],
                       # define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
                       include_dirs=[numpyIncludeDir],
                       extra_compile_args=compile_args,
                       extra_link_args=link_args)

cyDeltaExt = cythonize([cyDeltaExt])[0]

ext_modules = [Extension('d3q15._d3q15', ['d3q15/d3q15.i',
                                          libSrcDir+'d3q15.c',
                                          libSrcDir+'prng.c',
                                          libSrcDir+'noise.c',
                                          libSrcDir+'checks.c',
                                          libSrcDir+'bc_pbc.c',
                                          libSrcDir+'bc_noslip.c',
                                          libSrcDir+'bc_freeslip.c',
                                          libSrcDir+'bc_wall.c',
                                          libSrcDir+'force_none.c',
                                          libSrcDir+'force_const.c'],
                         include_dirs=[libSrcDir, numpyIncludeDir],
                         extra_compile_args=['-std=c99'] + compile_args,
                         extra_link_args=link_args),

               Extension('d3q15._CDelta', ['d3q15/CDelta.i',
                                           'd3q15/CDelta.c'],
                         include_dirs=[numpyIncludeDir,],
                         extra_compile_args=compile_args,
                         extra_link_args=link_args),

               cyDeltaExt,

               Extension('d3q15._utils', ['d3q15/utils.i',
                                          'd3q15/utils.c',
                                          'd3q15/CDelta.c'],
                         include_dirs=[libSrcDir, numpyIncludeDir],
                         extra_compile_args=compile_args,
                         extra_link_args=link_args),

               Extension('d3q15.fallers.__helpers',
                         ['d3q15/fallers/helpers.i',
                          'd3q15/fallers/_helpers.c',
                          #libSrcDir+'noise.c',
                          'd3q15/XArray.c',
                          'd3q15/CDelta.c',
                          'd3q15/utils.c'],
                         include_dirs=[libSrcDir, numpyIncludeDir],
                         extra_compile_args=compile_args,
                         extra_link_args=link_args),
               ]

# run the script to generate eigenvectors.h
cmd = '%s %s C > %s' % (sys.executable,
                        os.path.join(os.path.dirname(__file__),
                                     libSrcDir, 'eigenvectors.py'),
                        os.path.join(os.path.dirname(__file__),
                                     libSrcDir, 'eigenvectors.h'))
if os.system(cmd):
    raise EnvironmentError("Problem generating eigenvectors.h with command: %s" % cmd)

# run the script to generate eigenvectors.py
cmd = '%s %s py > %s' % (sys.executable,
                      os.path.join(os.path.dirname(__file__), libSrcDir, 'eigenvectors.py'),
                      os.path.join(os.path.dirname(__file__), 'd3q15', 'eigenvectors.py'))
if os.system(cmd):
    raise EnvironmentError("Problem generating eigenvectors.py with command: %s" % cmd)

setup(name='d3q15',
      version='1.0',
      description='3 dimensional, 15 velocity Lattice Boltzmann code.',
      author='Rupert Nash',
      author_email='rupert.nash@ed.ac.uk',
      packages=['d3q15', 'd3q15.fallers'],
      ext_modules=ext_modules
      )

setup(name='dqTools',
      version='1.0',
      description='Run time and analysis tools for use with d3q15',
      author='Rupert Nash',
      author_email='rupert.nash@ed.ac.uk',
      packages=['dqTools']
      )
<commit_msg>Add linux build tweaks<commit_after>#!/usr/bin/env python
import sys
import os.path
from distutils.core import setup, Extension
import distutils.sysconfig
from Cython.Build import cythonize
import numpy

numpyIncludeDir = numpy.get_include()

compile_args = ['-fopenmp']
link_args = ['-fopenmp']
if os.path.exists('debug'):
    compile_args += ['-O0', '-U NDEBUG']

libSrcDir = '../libd3q15/'

cyDeltaExt = Extension('d3q15.CyDelta', ['d3q15/CyDelta.pyx'],
                       # define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')],
                       include_dirs=[numpyIncludeDir],
                       extra_compile_args=compile_args,
                       extra_link_args=link_args)

cyDeltaExt = cythonize([cyDeltaExt])[0]

ext_modules = [Extension('d3q15._d3q15', ['d3q15/d3q15.i',
                                          libSrcDir+'d3q15.c',
                                          libSrcDir+'prng.c',
                                          libSrcDir+'noise.c',
                                          libSrcDir+'checks.c',
                                          libSrcDir+'bc_pbc.c',
                                          libSrcDir+'bc_noslip.c',
                                          libSrcDir+'bc_freeslip.c',
                                          libSrcDir+'bc_wall.c',
                                          libSrcDir+'force_none.c',
                                          libSrcDir+'force_const.c'],
                         include_dirs=[libSrcDir, numpyIncludeDir],
                         extra_compile_args=['-std=c99'] + compile_args,
                         extra_link_args=link_args),

               Extension('d3q15._CDelta', ['d3q15/CDelta.i',
                                           'd3q15/CDelta.c'],
                         include_dirs=[numpyIncludeDir,],
                         extra_compile_args=compile_args,
                         extra_link_args=link_args),

               cyDeltaExt,

               Extension('d3q15._utils', ['d3q15/utils.i',
                                          'd3q15/utils.c',
                                          'd3q15/CDelta.c'],
                         include_dirs=[libSrcDir, numpyIncludeDir],
                         extra_compile_args=compile_args,
                         extra_link_args=link_args),

               Extension('d3q15.fallers.__helpers',
                         ['d3q15/fallers/helpers.i',
                          'd3q15/fallers/_helpers.c',
                          libSrcDir+'prng.c',
                          libSrcDir+'noise.c',
                          'd3q15/XArray.c',
                          'd3q15/CDelta.c',
                          'd3q15/utils.c'],
                         include_dirs=[libSrcDir, numpyIncludeDir],
                         extra_compile_args=['-std=c99']+compile_args,
                         extra_link_args=link_args),
               ]

# run the script to generate eigenvectors.h
cmd = '%s %s C > %s' % (sys.executable,
                        os.path.join(os.path.dirname(__file__),
                                     libSrcDir, 'eigenvectors.py'),
                        os.path.join(os.path.dirname(__file__),
                                     libSrcDir, 'eigenvectors.h'))
if os.system(cmd):
    raise EnvironmentError("Problem generating eigenvectors.h with command: %s" % cmd)

# run the script to generate eigenvectors.py
cmd = '%s %s py > %s' % (sys.executable,
                      os.path.join(os.path.dirname(__file__), libSrcDir, 'eigenvectors.py'),
                      os.path.join(os.path.dirname(__file__), 'd3q15', 'eigenvectors.py'))
if os.system(cmd):
    raise EnvironmentError("Problem generating eigenvectors.py with command: %s" % cmd)

setup(name='d3q15',
      version='1.0',
      description='3 dimensional, 15 velocity Lattice Boltzmann code.',
      author='Rupert Nash',
      author_email='rupert.nash@ed.ac.uk',
      packages=['d3q15', 'd3q15.fallers'],
      ext_modules=ext_modules
      )

setup(name='dqTools',
      version='1.0',
      description='Run time and analysis tools for use with d3q15',
      author='Rupert Nash',
      author_email='rupert.nash@ed.ac.uk',
      packages=['dqTools']
      )
<|endoftext|>


<commit_before>#!/usr/bin/env python
from __future__ import print_function

import os
import shutil
import subprocess
import platform
from distutils.command.build import build
from setuptools.command.build_ext import build_ext

from setuptools import Extension, find_packages, setup

class build_ext_subclass(build_ext):
    def finalize_options(self):
        build_ext.finalize_options(self)
        # Prevent numpy from thinking it is still in its setup process:
        __builtins__.__NUMPY_SETUP__ = False
        import numpy
        self.include_dirs.append(numpy.get_include())

static_files = ['main.bundle.js', 'chunk_worker.bundle.js', 'tfjs-library.bundle.js', 'index.html']

class bundle_client(build):

    user_options = [
        ('client-bundle-type=', None,
         'The nodejs bundle type. "min" (default) creates condensed static files for production, "dev" creates human-readable files.'
         )
    ]

    def initialize_options(self):

        self.client_bundle_type = 'min'

    def finalize_options(self):

        if self.client_bundle_type not in ['min', 'dev']:
            raise RuntimeError('client-bundle-type has to be one of "min" or "dev"')

    def run(self):

        this_dir = os.path.abspath(os.path.dirname(__file__))
        project_dir = os.path.join(this_dir, '..')

        build_dir = os.path.join(project_dir, 'dist/python-' + self.client_bundle_type)
        static_dir = os.path.join(this_dir, 'neuroglancer/static')

        print("Project dir " + project_dir)
        print("Build dir " + build_dir)
        print("Static dir " + static_dir)

        prev_dir = os.path.abspath('.')
        os.chdir(project_dir)

        target = {"min": "build-python-min", "dev": "build-python"}

        try:
            t = target[self.client_bundle_type]
            subprocess.call(['npm', 'i'])
            res = subprocess.call(['npm', 'run', t])
        except:
            raise RuntimeError(
                'Could not run \'npm run %s\'. Make sure node.js >= v5.9.0 is installed and in your path.'
                % t)

        if res != 0:
            raise RuntimeError('failed to bundle neuroglancer node.js project')

        try:
            os.mkdir(static_dir)
        except OSError:
            pass

        for f in static_files:
            shutil.copy(os.path.join(build_dir, f), os.path.join(static_dir, f))

        os.chdir(prev_dir)

setup_dir = os.path.dirname(__file__)
src_dir = os.path.join(setup_dir, 'ext/src')
openmesh_dir = os.path.join(setup_dir, 'ext/third_party/openmesh/OpenMesh/src')
local_sources = [
    '_neuroglancer.cc',
    'openmesh_dependencies.cc',
    'on_demand_object_mesh_generator.cc',
    'voxel_mesh_generator.cc',
    'mesh_objects.cc',
]

USE_OMP = False
if USE_OMP:
    openmp_flags = ['-fopenmp']
else:
    openmp_flags = []

extra_compile_args = ['-std=c++11', '-fvisibility=hidden', '-O3'] + openmp_flags
if platform.system() == 'Darwin':
    extra_compile_args.insert(0, '-stdlib=libc++')

setup(
    name='neuroglancer',
    version='1.0.13',
    description='Python data backend for neuroglancer, a WebGL-based viewer for volumetric data',
    author='Jeremy Maitin-Shepard, Jan Funke',
    author_email='jbms@google.com, jfunke@iri.upc.edu',
    url='https://github.com/google/neuroglancer',
    license='Apache License 2.0',
    packages=find_packages(),
    package_data={
        'neuroglancer.static': static_files,
    },
    setup_requires=[
        "numpy>=1.11.0",
    ],
    install_requires=[
        "Pillow>=3.2.0",
        "numpy>=1.11.0",
        'requests',
        'tornado<6',
        'sockjs-tornado',
        'six',
        'google-apitools',
    ],
    test_requires=[
        'pytest',
    ],
    extras_require={
        ":python_version<'3.2'": ['futures'],
    },
    ext_modules=[
        Extension(
            'neuroglancer._neuroglancer',
            sources=[os.path.join(src_dir, name) for name in local_sources],
            language='c++',
            include_dirs=[openmesh_dir],
            define_macros=[
                ('_USE_MATH_DEFINES', None),  # Needed by OpenMesh when used with MSVC
            ],
            extra_compile_args=extra_compile_args,
            extra_link_args=openmp_flags),
    ],
    cmdclass={
        'bundle_client': bundle_client,
        'build_ext': build_ext_subclass,
    },
)
<commit_msg>chore(python): bump package version to 1.0.14<commit_after>#!/usr/bin/env python
from __future__ import print_function

import os
import shutil
import subprocess
import platform
from distutils.command.build import build
from setuptools.command.build_ext import build_ext

from setuptools import Extension, find_packages, setup

class build_ext_subclass(build_ext):
    def finalize_options(self):
        build_ext.finalize_options(self)
        # Prevent numpy from thinking it is still in its setup process:
        __builtins__.__NUMPY_SETUP__ = False
        import numpy
        self.include_dirs.append(numpy.get_include())

static_files = ['main.bundle.js', 'chunk_worker.bundle.js', 'tfjs-library.bundle.js', 'index.html']

class bundle_client(build):

    user_options = [
        ('client-bundle-type=', None,
         'The nodejs bundle type. "min" (default) creates condensed static files for production, "dev" creates human-readable files.'
         )
    ]

    def initialize_options(self):

        self.client_bundle_type = 'min'

    def finalize_options(self):

        if self.client_bundle_type not in ['min', 'dev']:
            raise RuntimeError('client-bundle-type has to be one of "min" or "dev"')

    def run(self):

        this_dir = os.path.abspath(os.path.dirname(__file__))
        project_dir = os.path.join(this_dir, '..')

        build_dir = os.path.join(project_dir, 'dist/python-' + self.client_bundle_type)
        static_dir = os.path.join(this_dir, 'neuroglancer/static')

        print("Project dir " + project_dir)
        print("Build dir " + build_dir)
        print("Static dir " + static_dir)

        prev_dir = os.path.abspath('.')
        os.chdir(project_dir)

        target = {"min": "build-python-min", "dev": "build-python"}

        try:
            t = target[self.client_bundle_type]
            subprocess.call(['npm', 'i'])
            res = subprocess.call(['npm', 'run', t])
        except:
            raise RuntimeError(
                'Could not run \'npm run %s\'. Make sure node.js >= v5.9.0 is installed and in your path.'
                % t)

        if res != 0:
            raise RuntimeError('failed to bundle neuroglancer node.js project')

        try:
            os.mkdir(static_dir)
        except OSError:
            pass

        for f in static_files:
            shutil.copy(os.path.join(build_dir, f), os.path.join(static_dir, f))

        os.chdir(prev_dir)

setup_dir = os.path.dirname(__file__)
src_dir = os.path.join(setup_dir, 'ext/src')
openmesh_dir = os.path.join(setup_dir, 'ext/third_party/openmesh/OpenMesh/src')
local_sources = [
    '_neuroglancer.cc',
    'openmesh_dependencies.cc',
    'on_demand_object_mesh_generator.cc',
    'voxel_mesh_generator.cc',
    'mesh_objects.cc',
]

USE_OMP = False
if USE_OMP:
    openmp_flags = ['-fopenmp']
else:
    openmp_flags = []

extra_compile_args = ['-std=c++11', '-fvisibility=hidden', '-O3'] + openmp_flags
if platform.system() == 'Darwin':
    extra_compile_args.insert(0, '-stdlib=libc++')

setup(
    name='neuroglancer',
    version='1.0.14',
    description='Python data backend for neuroglancer, a WebGL-based viewer for volumetric data',
    author='Jeremy Maitin-Shepard, Jan Funke',
    author_email='jbms@google.com, jfunke@iri.upc.edu',
    url='https://github.com/google/neuroglancer',
    license='Apache License 2.0',
    packages=find_packages(),
    package_data={
        'neuroglancer.static': static_files,
    },
    setup_requires=[
        "numpy>=1.11.0",
    ],
    install_requires=[
        "Pillow>=3.2.0",
        "numpy>=1.11.0",
        'requests',
        'tornado<6',
        'sockjs-tornado',
        'six',
        'google-apitools',
    ],
    test_requires=[
        'pytest',
    ],
    extras_require={
        ":python_version<'3.2'": ['futures'],
    },
    ext_modules=[
        Extension(
            'neuroglancer._neuroglancer',
            sources=[os.path.join(src_dir, name) for name in local_sources],
            language='c++',
            include_dirs=[openmesh_dir],
            define_macros=[
                ('_USE_MATH_DEFINES', None),  # Needed by OpenMesh when used with MSVC
            ],
            extra_compile_args=extra_compile_args,
            extra_link_args=openmp_flags),
    ],
    cmdclass={
        'bundle_client': bundle_client,
        'build_ext': build_ext_subclass,
    },
)
<|endoftext|>


<commit_before># Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=invalid-name, exec-used
"""Setup TVM package."""
from __future__ import absolute_import
import os
import shutil
import sys
import sysconfig
import platform

from setuptools import find_packages
from setuptools.dist import Distribution

# need to use distutils.core for correct placement of cython dll
if "--inplace" in sys.argv:
    from distutils.core import setup
    from distutils.extension import Extension
else:
    from setuptools import setup
    from setuptools.extension import Extension

CURRENT_DIR = os.path.dirname(__file__)

def get_lib_path():
    """Get library path, name and version"""
    # We can not import `libinfo.py` in setup.py directly since __init__.py
    # Will be invoked which introduces dependences
    libinfo_py = os.path.join(CURRENT_DIR, './tvm/_ffi/libinfo.py')
    libinfo = {'__file__': libinfo_py}
    exec(compile(open(libinfo_py, "rb").read(), libinfo_py, 'exec'), libinfo, libinfo)
    version = libinfo['__version__']
    if not os.getenv('CONDA_BUILD'):
        lib_path = libinfo['find_lib_path']()
        libs = [lib_path[0]]
        if libs[0].find("runtime") == -1:
            for name in lib_path[1:]:
                if name.find("runtime") != -1:
                    libs.append(name)
                    break
    else:
        libs = None
    return libs, version

LIB_LIST, __version__ = get_lib_path()

def config_cython():
    """Try to configure cython and return cython configuration"""
    if os.name == 'nt':
        print("WARNING: Cython is not supported on Windows, will compile without cython module")
        return []
    sys_cflags = sysconfig.get_config_var("CFLAGS")

    if "i386" in sys_cflags and "x86_64" in sys_cflags:
        print("WARNING: Cython library may not be compiled correctly with both i386 and x64")
        return []
    try:
        from Cython.Build import cythonize
        # from setuptools.extension import Extension
        if sys.version_info >= (3, 0):
            subdir = "_cy3"
        else:
            subdir = "_cy2"
        ret = []
        path = "tvm/_ffi/_cython"
        if os.name == 'nt':
            library_dirs = ['tvm', '../build/Release', '../build']
            libraries = ['libtvm']
        else:
            library_dirs = None
            libraries = None
        for fn in os.listdir(path):
            if not fn.endswith(".pyx"):
                continue
            ret.append(Extension(
                "tvm._ffi.%s.%s" % (subdir, fn[:-4]),
                ["tvm/_ffi/_cython/%s" % fn],
                include_dirs=["../include/",
                              "../3rdparty/dmlc-core/include",
                              "../3rdparty/dlpack/include",
                ],
                library_dirs=library_dirs,
                libraries=libraries,
                language="c++"))
        return cythonize(ret, compiler_directives={"language_level": 3})
    except ImportError:
        print("WARNING: Cython is not installed, will compile without cython module")
        return []

class BinaryDistribution(Distribution):
    def has_ext_modules(self):
        return True

    def is_pure(self):
        return False

include_libs = False
wheel_include_libs = False
if not os.getenv('CONDA_BUILD'):
    if "bdist_wheel" in sys.argv:
        wheel_include_libs = True
    else:
        include_libs = True

setup_kwargs = {}

# For bdist_wheel only
if wheel_include_libs:
    with open("MANIFEST.in", "w") as fo:
        for path in LIB_LIST:
            shutil.copy(path, os.path.join(CURRENT_DIR, 'tvm'))
            _, libname = os.path.split(path)
            fo.write("include tvm/%s\n" % libname)
    setup_kwargs = {
        "include_package_data": True
    }

if include_libs:
    curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    for i, path in enumerate(LIB_LIST):
        LIB_LIST[i] = os.path.relpath(path, curr_path)
    setup_kwargs = {
        "include_package_data": True,
        "data_files": [('tvm', LIB_LIST)]
    }

setup(name='tvm',
      version=__version__,
      description="TVM: An End to End Tensor IR/DSL Stack for Deep Learning Systems",
      zip_safe=False,
      install_requires=[
        'numpy',
        'decorator',
        'attrs',
        'psutil',
        ],
      packages=find_packages(),
      distclass=BinaryDistribution,
      url='https://github.com/dmlc/tvm',
      ext_modules=config_cython(),
      **setup_kwargs)

if wheel_include_libs:
    # Wheel cleanup
    os.remove("MANIFEST.in")
    for path in LIB_LIST:
        _, libname = os.path.split(path)
        os.remove("tvm/%s" % libname)
<commit_msg>[Relay] Install Relay Prelude program in package install (#4227)<commit_after># Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=invalid-name, exec-used
"""Setup TVM package."""
from __future__ import absolute_import
import os
import shutil
import sys
import sysconfig
import platform

from setuptools import find_packages
from setuptools.dist import Distribution

# need to use distutils.core for correct placement of cython dll
if "--inplace" in sys.argv:
    from distutils.core import setup
    from distutils.extension import Extension
else:
    from setuptools import setup
    from setuptools.extension import Extension

CURRENT_DIR = os.path.dirname(__file__)

def get_lib_path():
    """Get library path, name and version"""
    # We can not import `libinfo.py` in setup.py directly since __init__.py
    # Will be invoked which introduces dependences
    libinfo_py = os.path.join(CURRENT_DIR, './tvm/_ffi/libinfo.py')
    libinfo = {'__file__': libinfo_py}
    exec(compile(open(libinfo_py, "rb").read(), libinfo_py, 'exec'), libinfo, libinfo)
    version = libinfo['__version__']
    if not os.getenv('CONDA_BUILD'):
        lib_path = libinfo['find_lib_path']()
        libs = [lib_path[0]]
        if libs[0].find("runtime") == -1:
            for name in lib_path[1:]:
                if name.find("runtime") != -1:
                    libs.append(name)
                    break
    else:
        libs = None
    return libs, version

LIB_LIST, __version__ = get_lib_path()

def config_cython():
    """Try to configure cython and return cython configuration"""
    if os.name == 'nt':
        print("WARNING: Cython is not supported on Windows, will compile without cython module")
        return []
    sys_cflags = sysconfig.get_config_var("CFLAGS")

    if "i386" in sys_cflags and "x86_64" in sys_cflags:
        print("WARNING: Cython library may not be compiled correctly with both i386 and x64")
        return []
    try:
        from Cython.Build import cythonize
        # from setuptools.extension import Extension
        if sys.version_info >= (3, 0):
            subdir = "_cy3"
        else:
            subdir = "_cy2"
        ret = []
        path = "tvm/_ffi/_cython"
        if os.name == 'nt':
            library_dirs = ['tvm', '../build/Release', '../build']
            libraries = ['libtvm']
        else:
            library_dirs = None
            libraries = None
        for fn in os.listdir(path):
            if not fn.endswith(".pyx"):
                continue
            ret.append(Extension(
                "tvm._ffi.%s.%s" % (subdir, fn[:-4]),
                ["tvm/_ffi/_cython/%s" % fn],
                include_dirs=["../include/",
                              "../3rdparty/dmlc-core/include",
                              "../3rdparty/dlpack/include",
                ],
                library_dirs=library_dirs,
                libraries=libraries,
                language="c++"))
        return cythonize(ret, compiler_directives={"language_level": 3})
    except ImportError:
        print("WARNING: Cython is not installed, will compile without cython module")
        return []

class BinaryDistribution(Distribution):
    def has_ext_modules(self):
        return True

    def is_pure(self):
        return False

include_libs = False
wheel_include_libs = False
if not os.getenv('CONDA_BUILD'):
    if "bdist_wheel" in sys.argv:
        wheel_include_libs = True
    else:
        include_libs = True

setup_kwargs = {}

# For bdist_wheel only
if wheel_include_libs:
    with open("MANIFEST.in", "w") as fo:
        for path in LIB_LIST:
            shutil.copy(path, os.path.join(CURRENT_DIR, 'tvm'))
            _, libname = os.path.split(path)
            fo.write("include tvm/%s\n" % libname)
    setup_kwargs = {
        "include_package_data": True
    }

if include_libs:
    curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    for i, path in enumerate(LIB_LIST):
        LIB_LIST[i] = os.path.relpath(path, curr_path)
    setup_kwargs = {
        "include_package_data": True,
        "data_files": [('tvm', LIB_LIST)]
    }

def get_package_data_files():
    # Relay standard libraries
    return ['relay/std/prelude.rly']

setup(name='tvm',
      version=__version__,
      description="TVM: An End to End Tensor IR/DSL Stack for Deep Learning Systems",
      zip_safe=False,
      install_requires=[
        'numpy',
        'decorator',
        'attrs',
        'psutil',
        ],
      packages=find_packages(),
      package_dir={'tvm': 'tvm'},
      package_data={'tvm': get_package_data_files()},
      distclass=BinaryDistribution,
      url='https://github.com/dmlc/tvm',
      ext_modules=config_cython(),
      **setup_kwargs)

if wheel_include_libs:
    # Wheel cleanup
    os.remove("MANIFEST.in")
    for path in LIB_LIST:
        _, libname = os.path.split(path)
        os.remove("tvm/%s" % libname)
<|endoftext|>


<commit_before>
import sys, os, os.path, string
import json

swig_check = os.system("which swig")
if swig_check != 0:
    print "Error: you must install SWIG first."
    sys.exit(1)

extra_link_args = []
extra_compile_args = ["-std=c++11", "-Wall", "-Wextra", "-pthread"]
extra_libraries = []

if sys.platform == 'darwin':
    extra_compile_args += ["-m64", "-D_REENTRANT"]
    extra_libraries += []
elif sys.platform.startswith("linux"):
    extra_compile_args += []
    extra_link_args += []
    extra_libraries += []

if len(set(('develop', 'release', 'bdist_egg', 'bdist_rpm',
            'bdist_wininst', 'install_egg_info', 'build_sphinx',
            'egg_info', 'easy_install', 'upload',
            )).intersection(sys.argv)) > 0:
    import setuptools
    extra_setuptools_args = dict(
        zip_safe=False,  # the package can run out of an .egg file
        )
else:
    extra_setuptools_args = dict()

from numpy.distutils.core import setup, Extension

version = "0.1.1rc1"

init_py = open("paratext/__init__.py", "w")

print >>init_py, ("""#!/usr/bin/python
__all__ = ['paratext']

import core, helpers
from core import *

import paratext_internal
import warnings

__version__ = "%s"
""" % version)

init_py.close()

print version

swig_cmd = 'swig -c++ -python -I../src/ -outdir ./ ../src/paratext_internal.i'

print "running swig: ", swig_cmd
os.system(swig_cmd)

setup(name='paratext',
      version=version,
      description='Reads text files in parallel. The first release includes a paralell CSV reader.',
      long_description="""
See README
""",
      keywords=['csv', 'reading'],
      ext_modules=[Extension('_paratext_internal',
                             ['../src/paratext_internal_wrap.cxx', '../src/paratext_internal.cpp'],
                             extra_link_args = extra_link_args,
                             extra_compile_args = extra_compile_args,
                             include_dirs=['../src/'],
                             libraries=["stdc++"] + extra_libraries),
                   ],
      py_modules=["paratext_internal"],
      author="Damian Eads",
      author_email="damian@wise.io",
      license="GNU General Public License",
      packages = ['paratext'],
      url = 'http://wise.io',
      include_package_data = True,
      **extra_setuptools_args
      )
<commit_msg>Use subprocess instead of os.system<commit_after>
import sys, os, os.path, string, subprocess
import json

p = subprocess.Popen(["which", "swig"])
p.communicate("")
if p.returncode != 0:
    print("Error: you must install SWIG first.")
    sys.exit(1)

extra_link_args = []
extra_compile_args = ["-std=c++11", "-Wall", "-Wextra", "-pthread"]
extra_libraries = []

if sys.platform == 'darwin':
    extra_compile_args += ["-m64", "-D_REENTRANT"]
    extra_libraries += []
elif sys.platform.startswith("linux"):
    extra_compile_args += []
    extra_link_args += []
    extra_libraries += []

if len(set(('develop', 'release', 'bdist_egg', 'bdist_rpm',
            'bdist_wininst', 'install_egg_info', 'build_sphinx',
            'egg_info', 'easy_install', 'upload',
            )).intersection(sys.argv)) > 0:
    import setuptools
    extra_setuptools_args = dict(
        zip_safe=False,  # the package can run out of an .egg file
        )
else:
    extra_setuptools_args = dict()

from numpy.distutils.core import setup, Extension

version = "0.1.1rc1"

init_py = open("paratext/__init__.py", "w")

print >>init_py, ("""#!/usr/bin/python
__all__ = ['paratext']

import core, helpers
from core import *

import paratext_internal
import warnings

__version__ = "%s"
""" % version)

init_py.close()

print(version)

swig_cmd = ["swig", "-c++", "-python", "-I../src/", "-outdir", "./", "../src/paratext_internal.i"]

print("running swig: ", swig_cmd)
p = subprocess.Popen(swig_cmd)
p.communicate("")
if p.returncode != 0:
    print("Error: building")
    sys.exit(1)

setup(name='paratext',
      version=version,
      description='Reads text files in parallel. The first release includes a paralell CSV reader.',
      long_description="""
See README
""",
      keywords=['csv', 'reading'],
      ext_modules=[Extension('_paratext_internal',
                             ['../src/paratext_internal_wrap.cxx', '../src/paratext_internal.cpp'],
                             extra_link_args = extra_link_args,
                             extra_compile_args = extra_compile_args,
                             include_dirs=['../src/'],
                             libraries=["stdc++"] + extra_libraries),
                   ],
      py_modules=["paratext_internal"],
      author="Damian Eads",
      author_email="damian@wise.io",
      license="GNU General Public License",
      packages = ['paratext'],
      url = 'http://wise.io',
      include_package_data = True,
      **extra_setuptools_args
      )
<|endoftext|>