<commit_before>import subutai
import subuco
import hashlib
import datetime
import os
import stat
import tarfile
from shutil import copyfile
from subprocess import call
from time import sleep
from subprocess import Popen, PIPE

# This file provides functions and classes specific to
# Darwin platform

def GetVirtualMachineName():
    m = hashlib.md5()
    m.update(datetime.datetime.now().isoformat().encode('utf-8'))
    return "subutai-dd-" + m.hexdigest()[:5]

def CleanSSHKeys(host, port):
    call(['ssh-keygen', '-R', '['+host+']:'+port])

def CheckCocoasudo(install):
    if not os.path.exists(install+"bin/cocoasudo"):
        return False
    return True

def InstallCocoasudo(tmp, install, progress):
    subutai.AddStatus("Downloading cocoasudo application")
    subutai.download("cocoasudo")
    while subutai.isDownloadComplete() != 1:
        sleep(0.05)

    try:
        copyfile(tmp+"cocoasudo", install+"bin/cocoasudo")
        st = os.stat(install+"bin/cocoasudo")
        os.chmod(install+"bin/cocoasudo", st.st_mode | stat.S_IEXEC)
    except:
        subutai.RaiseError("Failed to install cocoasudo. Aborting")
        return -87

def CheckVirtualBox():
    if not os.path.exists("/Applications/VirtualBox.app"):
        return False
    return True

def CheckAndKillVirtualBox():
    p = Popen(['ps', 'aux'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
    stdout, stderr = p.communicate('')
    count = 0
    index = 1
    substr = 'VBoxHeadless'
    while index >= 0:
        index = stdout.find(substr, index+len(substr), len(stdout))
        print(index)
        count = count + 1
        if count > 10:
            break

    if (count > 1):
        p = Popen(['killall', '-9', substr], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        stdout, stderr = p.communicate('')

    return 0

def InstallVirtualBox(tmp, install, progress):
    subutai.AddStatus("Downloading VirtualBox")
    subutai.download("VirtualBox.pkg")
    while subutai.isDownloadComplete() != 1:
        sleep(0.05)
        progress.setVboxProgress(subutai.GetBytesDownload())
        progress.updateProgress()

    subutai.AddStatus("Installing VirtualBox")
    try:
        CheckAndKillVirtualBox()
        if not CheckOsascript():
            returnCode = call([install+"bin/cocoasudo",
                               '--prompt="Install VirtualBox"',
                               'installer',
                               '-pkg',
                               tmp+'VirtualBox.pkg',
                               '-target',
                               '/'])
            if returnCode != 0:
                return 21
        else:
            ins = 'do shell script "installer -pkg '+tmp+'VirtualBox.pkg -target /" with administrator privileges'
            p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
            stdout, stderr = p.communicate(ins)
            if p.returncode != 0:
                return 23

    except:
        subutai.RaiseError("Failed to install VirtualBox. Aborting")
        sleep(10)
        return 45

    progress.setVboxProgress(progress.getVboxSize())
    progress.updateProgress()
    if not os.path.exists("/Applications/VirtualBox.app"):
        subutai.AddStatus("Failed to install VirtualBox. Aborting")
        return 24

    return 0

def CheckOsascript():
    p = Popen(['which', 'osascript'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
    stdout, stderr = p.communicate('')
    if p.returncode != 0:
        return False
    return True

def InstallPeerPrerequisites(tmp, install, progress):
    rc = 0
    if not CheckOsascript() and not CheckCocoasudo(install):
        rc = InstallCocoasudo(tmp, install, progress)
        if rc != 0:
            return rc

    if not CheckVirtualBox():
        rc = InstallVirtualBox(tmp, install, progress)

    return rc

class P2P:
    def __init__(self, tmp, install):
        self.P2PFile = 'p2p.exe'
        self.RemoteP2PFile = 'p2p.exe'
        self.NssmFile = 'nssm.exe'
        self.TapFile = 'tap-windows-9.21.2.exe'
        self.tmp = tmp
        self.install = install
        self.progress = subuco.Progress()
        self.Daemon = 'io.subutai.p2p.daemon.plist'
        self.LogConf = 'p2p.conf'

    def PreInstall(self):
        self.progress.setP2P(self.P2PRemoteFile)
        self.progress.setNssm(self.NssmFile)
        self.progress.setTuntap(self.TapFile)
        self.progress.setCocoasudo(self.CocoasudoFile)
        self.calculateTotal()

    def Download(self):
        rc = 0

        self.__installTuntap(self.progress)

        subutai.download(self.RemoteP2PFile)
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setP2PProgress(subutai.GetBytesDownload())

        self.progress.setP2PProgress(progress.getP2PSize())
        self.progress.updateProgress()
        try:
            copyfile(self.tmp+self.RemoteP2PFile, self.install+"bin/"+self.P2PFile)
        except:
            subutai.RaiseError("Failed to move p2p binary to " +
                            self.install + "bin/"+self.P2PFile)
            return 19

        try:
            st = os.stat(self.install+"/bin/"+self.P2PFile)
            os.chmod(self.install+"/bin/"+self.P2PFile, st.st_mode | stat.S_IEXEC)
        except:
            subutai.RaiseError("Failed to make p2p binary executable")
            return 31

        if not os.path.exists(self.install+self.NssmFile):
            subutai.download(self.NssmFile)
            while subutai.isDownloadComplete() != 1:
                sleep(0.05)
                self.progress.setNssmProgress(subutai.GetBytesDownload())
                self.updateProgress()

            self.progress.setNssmProgress(self.progress.getNssmSize())
            self.progress.updateProgress()

            try:
                copyfile(self.tmp+self.NssmFile, self.install+self.NssmFile)
            except:
                subutai.RaiseError("Failed to move NSSM")

        return rc

    def Install(self):
        pass

    def PostInstall(self):
        self.__writeConfiguration()
        postinst = subuco.PostInstall(self.tmp)
        postinst.append('cp '+self.tmp+self.Daemon+' /Library/LaunchDaemons/'+self.daemon)
        postinst.append('cp '+self.tmp+self.LogConf+' /etc/newsyslog.d/'+self.LogConf)
        postinst.append('launchctl load /Library/LaunchDaemons/'+self.daemon)
        postinst.append('installer -pkg '+self.tmp+self.TapFile+' -target /')
        postinst.append('ln -s '+self.install+'bin/p2p /usr/local/bin/p2p')
        ins = 'do shell script "/bin/sh '+postinst.get()+'" with administrator privileges'
        p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        stdout, stderr = p.communicate(ins)
        if p.returncode != 0:
            return 91
        return 0

    def __checkTuntap(self):
        if not os.path.exists('/dev/tap0'):
            return False
        return True

    def __installTuntap(self):
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setTuntapProgress(subutai.GetBytesDownload())

        self.progress.setTuntapProgress(progress.getTuntapSize())
        self.progress.updateProgress()
        return 0

    def __writeConfiguration(self):
        service = '''
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        <dict>
        <key>Label</key>
        <string>io.subutai.p2p.daemon</string>

        <key>ProgramArguments</key>
        <array>
        <string>/usr/local/bin/p2p</string>
        <string>daemon</string>
        </array>

        <key>KeepAlive</key>
        <true/>

        <key>StandardOutPath</key>
        <string>/var/log/p2p.log</string>

        <key>StandardErrorPath</key>
        <string>/var/log/p2p.log</string>

        <key>Debug</key>
        <true/>
        </dict>
        </plist>
        '''.strip()

        daemonFile = 'io.subutai.p2p.daemon.plist'

        f = open(self.tmp+daemonFile, 'w')
        f.write(service)
        f.close()

        syslog = '''
        # logfilename          [owner:group]    mode count size when  flags [/pid_file] [sig_num]
        /var/log/p2p.log                       644  7     *    $D0   J
        '''.strip()
        sf = open(self.tmp+'p2p.conf', 'w')
        sf.write(syslog)
        sf.close()
        return 0

class Tray:
    def __init__(self, tmp, install):
        self.TrayFile = 'SubutaiTray_libs_osx.tar.gz'
        self.CocoasudoFile = 'cocoasudo'
        self.LibsshFile = 'libssh2-1.6.0-0_osx.pkg'
        self.tmp = tmp
        self.install = install
        self.progress = subuco.Progress()

    def GetTrayFile(self):
        return self.TrayFile

    def GetCocoasudoFile(self):
        return self.CocoasudoFile

    def GetLibsshFile(self):
        return self.LibsshFile

    def Download(self):
        rc = 0

        if not CheckOsascript() and not CheckCocoasudo(self.install):
        rc = InstallCocoasudo(self.tmp, self.install, self.progress)
        if rc != 0:
            return rc

        if not self.__checkLibssh():
            subutai.AddStatus("Downloading libssh2")
            subutai.download(self.LibsshFile)
            while subutai.isDownloadComplete() != 1:
                sleep(0.05)
                self.progress.setLibsshProgress(subutai.GetBytesDownload())
                self.progress.updateProgress()

            self.progress.setLibsshProgress(self.progress.getLibsshSize())
            self.progress.updateProgress()

        subutai.download(self.TrayFile)
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setTrayProgress(subutai.GetBytesDownload())
            self.progress.updateProgress()

        self.progress.setTrayProgress(self.progress.getTraySize())
        self.progress.updateProgress()
        return rc

    def PreInstall(self):
        self.progress.setTray(self.TrayFile)
        self.progress.setCocoasudo(self.CocoasudoFile)
        self.progress.setLibssh(self.LibsshFile)
        self.progress.calculateTotal()

    def PostInstall(self):
        try:
            tar = tarfile.open(self.tmp+self.TrayFile, "r:gz")
            tar.extractall("/Applications/Subutai")
            tar.close()
        except:
            subutai.RaiseError("Failed to unpack Tray archive. Aborting")
            return 86
        postinst = subuco.PostInstall(self.tmp)
        postinst.append('installer -pkg '+self.tmp+self.LibsshFile+' -target /')
        ins = 'do shell script "/bin/sh '+postinst.get()+'" with administrator privileges'
        p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
        stdout, stderr = p.communicate(ins)
        if p.returncode != 0:
            return 91

        try:
            launch = 'tell application "SubutaiTray.app" to activate'
            p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
            stdout, stderr = p.communicate(launch)
        except:
            subutai.RaiseError("Failed to launch Subutai Tray application")

        return 0

    def __checkLibssh():
        if not os.path.exists('/usr/local/lib/libssh2.dylib'):
            return False
        return True

class E2E:
    def __init__(self, tmp, install):
        self.GoogleChromeFile = 'GoogleChrome_osx.tar.gz'
        self.CocoasudoFile = 'cocoasudo'
        self.tmp = tmp
        self.install = install
        self.progress = subuco.Progress()

    def PreInstall(self):
        self.progress.SetChrome(self.GoogleChromeFile)
        self.progress.setCocoasudo(self.cocoasudoFile)
        self.progress.calculateTotal()

    def Download(self):
        rc = 0
        if not CheckOsascript() and not CheckCocoasudo(self.install):
        rc = InstallCocoasudo(self.tmp, self.install, self.progress)
        if rc != 0:
            return rc

        if not _checkGoogleChrome():
            subutai.AddStatus("Downloading Google Chrome")
            subutai.download(self.GoogleChromeFile)
            while subutai.isDownloadComplete() != 1:
                sleep(0.05)
                self.progress.setChromeProgress(subutai.GetBytesDownload())
                self.progress.updateProgress()

            self.progress.setChromeProgress(self.progress.getChromeSize())
            self.updateProgress()

        return rc

    def Install(self):
        try:
            script = 'do shell script "/usr/bin/tar -xf '+
                     self.tmp+
                     self.GoogleChromeFile+
                     ' -C /Applications" with administrator privileges'
            p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
            stdout, stderr = p.communicate(script)
        except:
            subutai.RaiseError("Failed to install Google Chrome")
            return 64
        return 0

    def PostInstall(self):
        location = os.environ['HOME'] + '/Library/Application Support/Google/Chrome/External Extensions'
        if not os.path.exists(location):
            os.makedirs(location)

        ete = '{\n\t"external_update_url": "https://clients2.google.com/service/update2/crx"\n}'

        try:
            f = open(location+"/kpmiofpmlciacjblommkcinncmneeoaa.json", 'w')
            f.write(ete)
            f.close()
        except:
            subutai.RaiseError("Can't write plugin to Extensions directory")
            return 68

        return 0

    def __checkGoogleChrome(self):
        if not os.path.exists('/Applications/Google Chrome.app'):
            return False
        return True<commit_msg>Windows support<commit_after>import subutai
import subuco
import hashlib
import datetime
import os
import stat
import zipfile
from shutil import copyfile
from subprocess import call
from time import sleep
from subprocess import Popen, PIPE

# This file provides functions and classes specific to
# Darwin platform

def GetVirtualMachineName():
    m = hashlib.md5()
    m.update(datetime.datetime.now().isoformat().encode('utf-8'))
    return "subutai-dw-" + m.hexdigest()[:5]

def CleanSSHKeys(host, port):
    try:
        call(['ssh-keygen', '-R', '['+host+']:'+port])
    except:
        subutai.RaiseError("Failed to clean SSH keys")
        return 78
    return 0

def CheckVirtualBox():
    if subutai.IsVBoxInstalled() != 0:
        return False
    return True

def InstallVirtualBox(tmp, install, progress):
    subutai.AddStatus("Downloading VirtualBox")
    vboxfile = "VirtualBox.exe"
    subutai.download(vboxfile)
    while subutai.isDownloadComplete() != 1:
        progress.setVboxProgress(subutai.GetBytesDownload())
        progress.updateProgress()

    subutai.AddStatus("Installing VirtualBox")
    try:
        call([tmp+vboxfile, '-silent'])
    except:
        subutai.RaiseError("Failed to install VirtualBox. Aborting")
        return 45

    return 0

def InstallPeerPrerequisites(tmp, install, progress):
    rc = 0
    if not CheckVirtualBox():
        rc = InstallVirtualBox(tmp, install, progress)
    return rc

class P2P:
    def __init__(self, tmp, install):
        self.P2PFile = 'p2p.exe'
        self.RemoteP2PFile = 'p2p.exe'
        self.NssmFile = 'nssm.exe'
        self.TapFile = 'tap-windows-9.21.2.exe'
        self.tmp = tmp
        self.install = install
        self.progress = subuco.Progress()

    def PreInstall(self):
        self.progress.setP2P(self.RemoteP2PFile)
        self.progress.setTuntap(self.TapFile)
        self.progress.setNssm(self.NssmFile)
        self.progress.calculateTotal()
        return 0

    def Download(self):
        rc = 0
        subutai.AddStatus("Installing")
        if not self.__checkNssm():
            subutai.download(self.NssmFile)
            while subutai.isDownloadComplete() != 1:
                sleep(0.05)
                self.progress.setNssmProgress(subutai.GetBytesDownload())
                self.progress.updateProgress()

            self.progress.setNssmProgress(self.progress.getNssmSize())
            self.progress.updateProgress()
            try:
                copyfile(self.tmp+self.NssmFile, self.install+"bin/"+self.NssmFile)
            except:
                subutai.RaiseError("Failed to move NSSM file to it's destination")
                return 14
        else:
            subutai.UnregisterService("Subutai P2P")
            subutai.ProcessKill("nssm.exe")
            subutai.ProcessKill("p2p.exe")

        self.__installTuntap()

        subutai.download(self.RemoteP2PFile)
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setP2PProgress(subutai.GetBytesDownload())
            self.progress.updateProgress()

        self.progress.setP2PProgress(self.progress.getP2PSize())
        self.progress.updateProgress()
        try:
            copyfile(self.tmp+self.RemoteP2PFile, self.install+"bin/"+self.P2PFile)
        except:
            subutai.RaiseError("Failed to move p2p binary to " +
                            self.install + "bin/"+self.P2PFile)
            return 19

        try:
            st = os.stat(self.install+"/bin/"+self.P2PFile)
            os.chmod(self.install+"/bin/"+self.P2PFile, st.st_mode | stat.S_IEXEC)
        except:
            subutai.RaiseError("Failed to make p2p binary executable")
            return 31

        return rc

    def PostInstall(self):
        subutai.RegisterService("Subutai P2P", self.install+"bin/p2p.exe|daemon")
        return 0

    def __checkNssm(self):
        if os.path.exists(self.install+self.NssmFile):
            return True
        return False

    def __installTuntap(self):
        subutai.AddStatus("Downloading TUN/TAP driver")
        subutai.download(self.TapFile)
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setTuntapProgress(subutai.GetBytesDownload())
            self.progress.updateProgress()

        self.progress.setTuntapProgress(self.progress.getTuntapSize())
        self.progress.updateProgress()
        subutai.AddStatus("Installing TUN/TAP driver")
        call([self.tmp+self.TapFile, '/S'])
        return 0

class Tray:
    def __init__(self, tmp, install):
        self.TrayFile = 'SubutaiTray_libs.zip'
        self.LibsshFile = 'ssh.zip'
        self.tmp = tmp
        self.install = install
        self.progress = subuco.Progress()

    def GetTrayFile(self):
        return self.TrayFile

    def GetLibsshFile(self):
        return self.LibsshFile

    def Download(self):
        rc = 0
        subutai.AddStatus("Downloading libssh2")
        subutai.download(self.LibsshFile)
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setLibsshProgress(subutai.GetBytesDownload())
            self.progress.updateProgress()

        self.progress.setLibsshProgress(self.progress.getLibsshSize())
        self.progress.updateProgress()

        subutai.ProcessKill("SubutaiTray.exe")
        subutai.ProcessKill("ssh.exe")
        subutai.ProcessKill("ssh-keygen.exe")

        subutai.download(self.TrayFile)
        while subutai.isDownloadComplete() != 1:
            sleep(0.05)
            self.progress.setTrayProgress(subutai.GetBytesDownload())
            self.progress.updateProgress()

        self.progress.setTrayProgress(self.progress.getTraySize())
        self.progress.updateProgress()

        try:
            zf = zipfile.ZipFile(self.tmp+self.TrayFile, 'r')
            zf.extractall(self.install)
            zf.close()
            zfl = zipfile.ZipFile(self.tmp+self.LibsshFile, 'r')
            zfl.extractall(self.install+"/bin")
            zfl.close()
        except:
            subutai.RaiseError("Failed to replace tray")

        return rc

    def PreInstall(self):
        self.progress.setTray(self.TrayFile)
        self.progress.setLibssh(self.LibsshFile)
        self.progress.calculateTotal()
        return 0

    def PostInstall(self):
        subutai.AddStatus("Writing configuration")
        unPath = self.install.replace('\\', '/')
        unVBoxPath = subutai.GetVBoxPath().replace('\\', '/')
        f = open(unPath+"/tray/subutai_tray.ini", "w")
        f.write("P2P_Path="+unPath+"/bin/p2p.exe\n")
        if unVBoxPath != "":
            f.write("VBoxManage_Path="+unVBoxPath+"\n")
        f.write("Ssh_Path="+unPath+"/bin/ssh.exe\n")
        f.write("Ssh_Keygen_Cmd="+unPath+"/bin/ssh-keygen.exe\n")
        f.close()

        return 0

class E2E:
    def __init__(self, tmp, install):
        self.GoogleChromeFile = 'GoogleChromeStandaloneEnterprise64.msi'
        self.tmp = tmp
        self.install = install
        self.progress = subuco.Progress()

    def PreInstall(self):
        self.progress.setChrome(self.GoogleChromeFile)
        self.progress.calculateTotal()
        return 0

    def Download(self):
        rc = 0
        if not self.__checkGoogleChrome():
            subutai.AddStatus("Downloading Google Chrome")
            subutai.download(self.GoogleChromeFile)
            while subutai.isDownloadComplete() != 1:
                sleep(0.05)
                self.progress.setChromeProgress(subutai.GetBytesDownload())
                self.progress.updateProgress()

            self.progress.setChromeProgress(self.progress.getChromeSize())
            self.progress.updateProgress()

        return rc

    def Install(self):
        subutai.AddStatus("Installing Chrome")
        try:
            call(['msiexec', '/qn', '/i', self.tmp+self.GoogleChromeFile])
        except:
            subutai.RaiseError("Failed to install Google Chrome")
            return 28
        return 0

    def PostInstall(self):
        subutai.AddStatus("Installing Browser Plugin")
        subutai.RegisterPlugin()
        return 0

    def __checkGoogleChrome(self):
        if subutai.IsChromeInstalled() != 0:
            return False
        return True<|endoftext|>


<commit_before>import argparse
import os
import pathlib
import sys
from subprocess import STDOUT, check_call

if sys.platform != "darwin":
    raise NotImplementedError

parser = argparse.ArgumentParser()
parser.add_argument(
    "--application-id",
    required=True,
    help="Certificate ID (should be added to the keychain).",
)
args = parser.parse_args()

path = pathlib.Path(__file__).parent.absolute()
dvc = path / "dist" / "dvc"
for root, _, fnames in os.walk(dvc):
    for fname in fnames:
        fpath = os.path.join(root, fname)
        print(f"signing {fpath}")
        check_call(
            [
                "codesign",
                "--force",
                "--verbose",
                "-s",
                args.application_id,
                "-o",
                "runtime",
                "--entitlements",
                "entitlements.plist",
                fpath,
            ],
            stderr=STDOUT,
            timeout=90,
        )
<commit_msg>sign: increase timeout to 180 sec<commit_after>import argparse
import os
import pathlib
import sys
from subprocess import STDOUT, check_call

if sys.platform != "darwin":
    raise NotImplementedError

parser = argparse.ArgumentParser()
parser.add_argument(
    "--application-id",
    required=True,
    help="Certificate ID (should be added to the keychain).",
)
args = parser.parse_args()

path = pathlib.Path(__file__).parent.absolute()
dvc = path / "dist" / "dvc"
for root, _, fnames in os.walk(dvc):
    for fname in fnames:
        fpath = os.path.join(root, fname)
        print(f"signing {fpath}")
        check_call(
            [
                "codesign",
                "--force",
                "--verbose",
                "-s",
                args.application_id,
                "-o",
                "runtime",
                "--entitlements",
                "entitlements.plist",
                fpath,
            ],
            stderr=STDOUT,
            timeout=180,
        )
<|endoftext|>


<commit_before>#  This file is part of the myhdl library, a Python package for using
#  Python as a Hardware Description Language.
#
#  Copyright (C) 2003 Jan Decaluwe
#
#  The myhdl library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation; either version 2.1 of the
#  License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.

#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

""" Module with the always_comb function. """

__author__ = "Jan Decaluwe <jan@jandecaluwe.com>"
__revision__ = "$Revision$"
__date__ = "$Date$"

from __future__ import generators

import inspect
from types import FunctionType
import compiler

from myhdl import Signal
from myhdl._util import _isgeneratorfunction

class Error(Exception):
    """always_comb Error"""
    def __init__(self, arg=""):
        self.arg = arg
    def __str__(self):
        msg = self.__doc__
        if self.arg:
            msg = msg + ": " + str(self.arg)
        return msg

class ArgumentError(Error):
    """ always_comb argument should be a normal (non-generator) function"""

class NrOfArgsError(Error):
    """ always_comb argument should be a function without arguments"""

class ScopeError(Error):
    """always_comb argument should be a local function"""

class SignalAsInoutError(Error):
    """signal used as inout in always_comb function argument"""

class EmbeddedFunctionError(Error):
    """embedded functions in always_comb function argument not supported"""

def always_comb(func):
    f = inspect.getouterframes(inspect.currentframe())[1][0]
    if type(func) is not FunctionType:
        raise ArgumentError
    if _isgeneratorfunction(func):
        raise ArgumentError
    if func.func_code.co_argcount:
        raise NrOfArgsError
    if func.func_name not in f.f_locals:
        raise ScopeError
    varnames = func.func_code.co_varnames
    sigdict = {}
    for dict in (f.f_locals, f.f_globals):
        for n, v in dict.items():
            if isinstance(v, Signal) and n not in varnames:
                sigdict[n] = v
    c = _AlwaysComb(func, sigdict)
    return c.genfunc()

INPUT, OUTPUT, INOUT = range(3)

class _SigNameVisitor(object):
    def __init__(self, sigdict):
        self.inputs = []
        self.outputs = []
        self.toplevel = 1
        self.sigdict = sigdict

    def visitModule(self, node):
        inputs = self.inputs
        outputs = self.outputs
        self.visit(node.node)
        for n in inputs:
            if n in outputs:
                raise SignalAsInoutError(n)

    def visitFunction(self, node):
        if self.toplevel:
            self.toplevel = 0 # skip embedded functions
            self.visit(node.code)
        else:
            raise EmbeddedFunctionError

    def visitName(self, node, access=INPUT):
        if node.name not in self.sigdict:
            return
        if access == INPUT:
            self.inputs.append(node.name)
        elif access == OUTPUT:
            self.outputs.append(node.name)
        elif access == INOUT:
            raise SignalAsInoutError(node.name)
        else:
            raise Error

    def visitAssign(self, node, access=OUTPUT):
        for n in node.nodes:
            self.visit(n, OUTPUT)
        self.visit(node.expr, INPUT)

    def visitAssAttr(self, node, access=OUTPUT):
        self.visit(node.expr, OUTPUT)

    def visitSubscript(self, node, access=INPUT):
        self.visit(node.expr, access)
        for n in node.subs:
            self.visit(n, INPUT)

    def visitSlice(self, node, access=INPUT):
        self.visit(node.expr, access)
        if node.lower:
            self.visit(node.lower, INPUT)
        if node.upper:
            self.visit(node.upper, INPUT)

    def visitAugAssign(self, node, access=INPUT):
        self.visit(node.node, INOUT)
        self.visit(node.expr, INPUT)

    def visitClass(self, node):
        pass # skip

    def visitExec(self, node):
        pass # skip

class _AlwaysComb(object):

    def __init__(self, func, sigdict):
        self.func = func
        self.sigdict = sigdict
        s = inspect.getsource(func)
        s = s.lstrip()
        tree = compiler.parse(s)
        v = _SigNameVisitor(sigdict)
        compiler.walk(tree, v)
        v.inputs.sort()
        v.outputs.sort()
        self.inputs = v.inputs
        self.outputs = v.outputs

    def genfunc(self):
        inputsigs = tuple([self.sigdict[n] for n in self.inputs])
        func = self.func
        while 1:
            func()
            yield inputsigs

<commit_msg>doc string<commit_after>#  This file is part of the myhdl library, a Python package for using
#  Python as a Hardware Description Language.
#
#  Copyright (C) 2003 Jan Decaluwe
#
#  The myhdl library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation; either version 2.1 of the
#  License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.

#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

""" Module with the always_comb function. """

__author__ = "Jan Decaluwe <jan@jandecaluwe.com>"
__revision__ = "$Revision$"
__date__ = "$Date$"

from __future__ import generators

import inspect
from types import FunctionType
import compiler

from myhdl import Signal
from myhdl._util import _isgeneratorfunction

class Error(Exception):
    """always_comb Error"""
    def __init__(self, arg=""):
        self.arg = arg
    def __str__(self):
        msg = self.__doc__
        if self.arg:
            msg = msg + ": " + str(self.arg)
        return msg

class ArgumentError(Error):
    """ always_comb argument should be a classic function"""

class NrOfArgsError(Error):
    """ always_comb argument should be a function without arguments"""

class ScopeError(Error):
    """always_comb argument should be a local function"""

class SignalAsInoutError(Error):
    """signal used as inout in always_comb function argument"""

class EmbeddedFunctionError(Error):
    """embedded functions in always_comb function argument not supported"""

def always_comb(func):
    f = inspect.getouterframes(inspect.currentframe())[1][0]
    if type(func) is not FunctionType:
        raise ArgumentError
    if _isgeneratorfunction(func):
        raise ArgumentError
    if func.func_code.co_argcount:
        raise NrOfArgsError
    if func.func_name not in f.f_locals:
        raise ScopeError
    varnames = func.func_code.co_varnames
    sigdict = {}
    for dict in (f.f_locals, f.f_globals):
        for n, v in dict.items():
            if isinstance(v, Signal) and n not in varnames:
                sigdict[n] = v
    c = _AlwaysComb(func, sigdict)
    return c.genfunc()

INPUT, OUTPUT, INOUT = range(3)

class _SigNameVisitor(object):
    def __init__(self, sigdict):
        self.inputs = []
        self.outputs = []
        self.toplevel = 1
        self.sigdict = sigdict

    def visitModule(self, node):
        inputs = self.inputs
        outputs = self.outputs
        self.visit(node.node)
        for n in inputs:
            if n in outputs:
                raise SignalAsInoutError(n)

    def visitFunction(self, node):
        if self.toplevel:
            self.toplevel = 0 # skip embedded functions
            self.visit(node.code)
        else:
            raise EmbeddedFunctionError

    def visitName(self, node, access=INPUT):
        if node.name not in self.sigdict:
            return
        if access == INPUT:
            self.inputs.append(node.name)
        elif access == OUTPUT:
            self.outputs.append(node.name)
        elif access == INOUT:
            raise SignalAsInoutError(node.name)
        else:
            raise Error

    def visitAssign(self, node, access=OUTPUT):
        for n in node.nodes:
            self.visit(n, OUTPUT)
        self.visit(node.expr, INPUT)

    def visitAssAttr(self, node, access=OUTPUT):
        self.visit(node.expr, OUTPUT)

    def visitSubscript(self, node, access=INPUT):
        self.visit(node.expr, access)
        for n in node.subs:
            self.visit(n, INPUT)

    def visitSlice(self, node, access=INPUT):
        self.visit(node.expr, access)
        if node.lower:
            self.visit(node.lower, INPUT)
        if node.upper:
            self.visit(node.upper, INPUT)

    def visitAugAssign(self, node, access=INPUT):
        self.visit(node.node, INOUT)
        self.visit(node.expr, INPUT)

    def visitClass(self, node):
        pass # skip

    def visitExec(self, node):
        pass # skip

class _AlwaysComb(object):

    def __init__(self, func, sigdict):
        self.func = func
        self.sigdict = sigdict
        s = inspect.getsource(func)
        s = s.lstrip()
        tree = compiler.parse(s)
        v = _SigNameVisitor(sigdict)
        compiler.walk(tree, v)
        v.inputs.sort()
        v.outputs.sort()
        self.inputs = v.inputs
        self.outputs = v.outputs

    def genfunc(self):
        inputsigs = tuple([self.sigdict[n] for n in self.inputs])
        func = self.func
        while 1:
            func()
            yield inputsigs

<|endoftext|>


<commit_before>import datetime

from django.utils import timezone
from django.test import TestCase

from .models import Question

class QuestionMethodTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() should return False for questions whose
        pub_date is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertEqual(future_question.was_published_recently(), False)
<commit_msg>Added 2 more tests for the Question model<commit_after>import datetime

from django.utils import timezone
from django.test import TestCase

from .models import Question

class QuestionMethodTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() should return False for questions whose
        pub_date is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertEqual(future_question.was_published_recently(), False)

    def test_was_published_recently_with_old_question(self):
        """
        was_published_recently() should return False for questions whose
        pub_date is older than 1 day.
        """
        time = timezone.now() - datetime.timedelta(days=30)
        old_question = Question(pub_date=time)
        self.assertEqual(old_question.was_published_recently(), False)

    def test_was_published_recently_with_recent_question(self):
        """
        was_published_recently() should return True for questions whose
        pub_date is within the last day.
        """
        time = timezone.now() - datetime.timedelta(hours=1)
        recent_question = Question(pub_date=time)
        self.assertEqual(recent_question.was_published_recently(), True)
<|endoftext|>


<commit_before>#!/usr/bin/env python

import pickle
from time import sleep
import argparse

from ytd.downloader.StockDownloader import StockDownloader
from ytd.downloader.ETFDownloader import ETFDownloader
from ytd.downloader.FutureDownloader import FutureDownloader
from ytd.downloader.IndexDownloader import IndexDownloader
from ytd.downloader.MutualFundDownloader import MutualFundDownloader
from ytd.downloader.CurrencyDownloader import CurrencyDownloader
from ytd.downloader.WarrantDownloader import WarrantDownloader
from ytd.downloader.BondDownloader import BondDownloader
from ytd.compat import unicode

import tablib
import bs4

options = {
    "stocks": StockDownloader(),
    "etf": ETFDownloader(),
    "future": FutureDownloader(),
    "index": IndexDownloader(),
    "mutualfund": MutualFundDownloader(),
    "currency": CurrencyDownloader(),
    "warrant": WarrantDownloader(),
    "bond": BondDownloader(),
}

def loadDownloader(tickerType):
    with open(tickerType + ".pickle", "rb") as f:
        return pickle.load(f)

def saveDownloader(downloader, tickerType):
    with open(tickerType + ".pickle", "wb") as f:
        pickle.dump(downloader, file=f, protocol=pickle.HIGHEST_PROTOCOL)

def downloadEverything(downloader, tickerType, insecure):

    loop = 0
    while not downloader.isDone():

        symbols = downloader.nextRequest(insecure)
        print("Got " + str(len(symbols)) + " downloaded " + downloader.type + " symbols:")
        if(len(symbols)>2):
            print (" " + unicode(symbols[0]))
            print (" " + unicode(symbols[1]))
            print ("  ect...")
        downloader.printProgress()

        # Save download state occasionally.
        # We do this in case this long running is suddenly interrupted.
        loop = loop + 1
        if loop % 200 == 0:
            print ("Saving downloader to disk...")
            saveDownloader(downloader, tickerType)
            print ("Downloader successfully saved.")
            print ("")

        if not downloader.isDone():
            sleep(5)  # So we don't overload the server.

def main():
    downloader = None

    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--insecure", help="use HTTP instead of HTTPS", action="store_true")
    parser.add_argument('type', help='The type to download, this can be: '+" ".join(list(options.keys())))
    args = parser.parse_args()

    if args.insecure:
        print("Using insecure connection")

    tickerType = args.type = args.type.lower()

    print("Checking if we can resume a old download session")
    try:
        downloader = loadDownloader(tickerType)
        print("Downloader found on disk, resuming")
    except:
        print("No old downloader found on disk")
        print("Starting a new session")
        if tickerType not in options:
            print("Error: " + tickerType + " is not a valid type option. See --help")
            exit(1)
        else:
            downloader = options[tickerType]

    try:
        if not downloader.isDone():
            print("Downloading " + downloader.type)
            print("")
            downloadEverything(downloader, tickerType, args.insecure)
            print ("Saving downloader to disk...")
            saveDownloader(downloader, tickerType)
            print ("Downloader successfully saved.")
            print ("")
        else:
            print("The downloader has already finished downloading everything")
            print("")

    except Exception as ex:
        print("A exception occurred while downloading. Suspending downloader to disk")
        saveDownloader(downloader, tickerType)
        print("Successfully saved download state")
        print("Remove downloader.pickle if this error persists")
        print("Issues can be reported on https://github.com/Benny-/Yahoo-ticker-symbol-downloader/issues")
        print("")
        raise
    except KeyboardInterrupt as ex:
        print("Suspending downloader to disk")
        saveDownloader(downloader, tickerType)

    if downloader.isDone():
        print("Exporting "+downloader.type+" symbols")

        data = tablib.Dataset()
        data.headers = downloader.getRowHeader()

        for symbol in downloader.getCollectedSymbols():
            data.append(symbol.getRow())

        with open(downloader.type + '.csv', 'w') as f:
            f.write(data.csv)

        with open(downloader.type + '.json', 'w') as f:
            f.write(data.json)

        with open(downloader.type + '.yaml', 'w') as f:
            f.write(data.yaml)

        with open(downloader.type + '.xls', 'wb') as f:
            f.write(data.xls)

if __name__ == "__main__":
    main()
<commit_msg>Removed unused import<commit_after>#!/usr/bin/env python

import pickle
from time import sleep
import argparse

from ytd.downloader.StockDownloader import StockDownloader
from ytd.downloader.ETFDownloader import ETFDownloader
from ytd.downloader.FutureDownloader import FutureDownloader
from ytd.downloader.IndexDownloader import IndexDownloader
from ytd.downloader.MutualFundDownloader import MutualFundDownloader
from ytd.downloader.CurrencyDownloader import CurrencyDownloader
from ytd.downloader.WarrantDownloader import WarrantDownloader
from ytd.downloader.BondDownloader import BondDownloader
from ytd.compat import unicode

import tablib

options = {
    "stocks": StockDownloader(),
    "etf": ETFDownloader(),
    "future": FutureDownloader(),
    "index": IndexDownloader(),
    "mutualfund": MutualFundDownloader(),
    "currency": CurrencyDownloader(),
    "warrant": WarrantDownloader(),
    "bond": BondDownloader(),
}

def loadDownloader(tickerType):
    with open(tickerType + ".pickle", "rb") as f:
        return pickle.load(f)

def saveDownloader(downloader, tickerType):
    with open(tickerType + ".pickle", "wb") as f:
        pickle.dump(downloader, file=f, protocol=pickle.HIGHEST_PROTOCOL)

def downloadEverything(downloader, tickerType, insecure):

    loop = 0
    while not downloader.isDone():

        symbols = downloader.nextRequest(insecure)
        print("Got " + str(len(symbols)) + " downloaded " + downloader.type + " symbols:")
        if(len(symbols)>2):
            print (" " + unicode(symbols[0]))
            print (" " + unicode(symbols[1]))
            print ("  ect...")
        downloader.printProgress()

        # Save download state occasionally.
        # We do this in case this long running is suddenly interrupted.
        loop = loop + 1
        if loop % 200 == 0:
            print ("Saving downloader to disk...")
            saveDownloader(downloader, tickerType)
            print ("Downloader successfully saved.")
            print ("")

        if not downloader.isDone():
            sleep(5)  # So we don't overload the server.

def main():
    downloader = None

    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--insecure", help="use HTTP instead of HTTPS", action="store_true")
    parser.add_argument('type', help='The type to download, this can be: '+" ".join(list(options.keys())))
    args = parser.parse_args()

    if args.insecure:
        print("Using insecure connection")

    tickerType = args.type = args.type.lower()

    print("Checking if we can resume a old download session")
    try:
        downloader = loadDownloader(tickerType)
        print("Downloader found on disk, resuming")
    except:
        print("No old downloader found on disk")
        print("Starting a new session")
        if tickerType not in options:
            print("Error: " + tickerType + " is not a valid type option. See --help")
            exit(1)
        else:
            downloader = options[tickerType]

    try:
        if not downloader.isDone():
            print("Downloading " + downloader.type)
            print("")
            downloadEverything(downloader, tickerType, args.insecure)
            print ("Saving downloader to disk...")
            saveDownloader(downloader, tickerType)
            print ("Downloader successfully saved.")
            print ("")
        else:
            print("The downloader has already finished downloading everything")
            print("")

    except Exception as ex:
        print("A exception occurred while downloading. Suspending downloader to disk")
        saveDownloader(downloader, tickerType)
        print("Successfully saved download state")
        print("Remove downloader.pickle if this error persists")
        print("Issues can be reported on https://github.com/Benny-/Yahoo-ticker-symbol-downloader/issues")
        print("")
        raise
    except KeyboardInterrupt as ex:
        print("Suspending downloader to disk")
        saveDownloader(downloader, tickerType)

    if downloader.isDone():
        print("Exporting "+downloader.type+" symbols")

        data = tablib.Dataset()
        data.headers = downloader.getRowHeader()

        for symbol in downloader.getCollectedSymbols():
            data.append(symbol.getRow())

        with open(downloader.type + '.csv', 'w') as f:
            f.write(data.csv)

        with open(downloader.type + '.json', 'w') as f:
            f.write(data.json)

        with open(downloader.type + '.yaml', 'w') as f:
            f.write(data.yaml)

        with open(downloader.type + '.xls', 'wb') as f:
            f.write(data.xls)

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>from django.conf.urls.defaults import patterns, include, url

from django.views.generic import DetailView, ListView

from core import models
from core.views import PlaceListView

person_patterns = patterns('core.views',
    url(r'^all/',
        ListView.as_view(model=models.Person),
        name='person_list'),
    url(r'^politicians/',
        ListView.as_view(
            queryset=models.Position.objects.all()
                .filter(title__display_order__isnull=False)
                .currently_active()
                .order_by('-title__display_order', 'place__slug')
            ),
        name='politician_list'),

    # featured person ajax load
    url(r'^featured/(?P<direction>(before|after))/(?P<current_slug>[-\w]+)',
        'featured_person',
        name='featured_person'),

    url(r'^(?P<slug>[-\w]+)/',       'person',       name='person'),
  )

place_patterns = patterns('core.views',
    url(r'^all/',
        PlaceListView.as_view(queryset=models.Place.objects.all()),
        name='place_list'),
    url(r'^constituencies/',
        PlaceListView.as_view(queryset=models.Place.objects.all().constituencies(), context_object_name='constituencies'),
        name='constituency_list'),
    url(r'^counties/',
        PlaceListView.as_view(queryset=models.Place.objects.all().counties(), context_object_name='counties'),
        name='county_list'),

    url(r'^is/(?P<slug>[-\w]+)/', 'place_kind', name='place_kind'),
    url(r'^(?P<slug>[-\w]+)/$',   'place',      name='place'),

    # Tab content
    url(
        r'^(?P<slug>[-\w]+)/related_person_tab',
        DetailView.as_view(
            model=models.Place,
            template_name_suffix='_related_person_tab',
        ),
        name="place_related_person_tab",
    ),
    url(
        r'^(?P<slug>[-\w]+)/related_organisation_tab',
        DetailView.as_view(
            model=models.Place,
            template_name_suffix='_related_organisation_tab',
        ),
        name="place_related_organisation_tab",
    ),
                          )

organisation_patterns = patterns('core.views',
    url(r'^all/', 'organisation_list', name='organisation_list'),

    # Tab content
    url(
        r'^(?P<slug>[-\w]+)/related_person_tab',
        DetailView.as_view(
            model=models.Organisation,
            template_name_suffix='_related_person_tab',
        ),
        name="organisation_related_person_tab",
    ),

    url(r'^is/(?P<slug>[-\w]+)/', 'organisation_kind', name='organisation_kind'),
    url(r'^(?P<slug>[-\w]+)/$',   'organisation',      name='organisation'),

                                 )

urlpatterns = patterns('core.views',
    # Homepage
    url(r'^$', 'home', name='home'),

    (r'^person/', include(person_patterns)),
    (r'^place/', include(place_patterns)),
    (r'^organisation/', include(organisation_patterns)),

    url(r'^position/(?P<slug>[-\w]+)/',     'position',     name='position'),

    # specials
    url(r'^parties', 'parties', name='parties'),

    # Ajax select
    url(r'^ajax_select/', include('ajax_select.urls')),
)
<commit_msg>Sadly, this can't work because we need to know the organisation that the position relates to.<commit_after>from django.conf.urls.defaults import patterns, include, url

from django.views.generic import DetailView, ListView

from core import models
from core.views import PlaceListView

person_patterns = patterns('core.views',
    url(r'^all/',
        ListView.as_view(model=models.Person),
        name='person_list'),
    url(r'^politicians/',
        ListView.as_view(queryset=models.Position.objects.all().current_mp_positions().order_by('place__slug')),
        name='politician_list'),

    # featured person ajax load
    url(r'^featured/(?P<direction>(before|after))/(?P<current_slug>[-\w]+)',
        'featured_person',
        name='featured_person'),

    url(r'^(?P<slug>[-\w]+)/',       'person',       name='person'),
  )

place_patterns = patterns('core.views',
    url(r'^all/',
        PlaceListView.as_view(queryset=models.Place.objects.all()),
        name='place_list'),
    url(r'^constituencies/',
        PlaceListView.as_view(queryset=models.Place.objects.all().constituencies(), context_object_name='constituencies'),
        name='constituency_list'),
    url(r'^counties/',
        PlaceListView.as_view(queryset=models.Place.objects.all().counties(), context_object_name='counties'),
        name='county_list'),

    url(r'^is/(?P<slug>[-\w]+)/', 'place_kind', name='place_kind'),
    url(r'^(?P<slug>[-\w]+)/$',   'place',      name='place'),

    # Tab content
    url(
        r'^(?P<slug>[-\w]+)/related_person_tab',
        DetailView.as_view(
            model=models.Place,
            template_name_suffix='_related_person_tab',
        ),
        name="place_related_person_tab",
    ),
    url(
        r'^(?P<slug>[-\w]+)/related_organisation_tab',
        DetailView.as_view(
            model=models.Place,
            template_name_suffix='_related_organisation_tab',
        ),
        name="place_related_organisation_tab",
    ),
                          )

organisation_patterns = patterns('core.views',
    url(r'^all/', 'organisation_list', name='organisation_list'),

    # Tab content
    url(
        r'^(?P<slug>[-\w]+)/related_person_tab',
        DetailView.as_view(
            model=models.Organisation,
            template_name_suffix='_related_person_tab',
        ),
        name="organisation_related_person_tab",
    ),

    url(r'^is/(?P<slug>[-\w]+)/', 'organisation_kind', name='organisation_kind'),
    url(r'^(?P<slug>[-\w]+)/$',   'organisation',      name='organisation'),

                                 )

urlpatterns = patterns('core.views',
    # Homepage
    url(r'^$', 'home', name='home'),

    (r'^person/', include(person_patterns)),
    (r'^place/', include(place_patterns)),
    (r'^organisation/', include(organisation_patterns)),

    url(r'^position/(?P<slug>[-\w]+)/',     'position',     name='position'),

    # specials
    url(r'^parties', 'parties', name='parties'),

    # Ajax select
    url(r'^ajax_select/', include('ajax_select.urls')),
)
<|endoftext|>


<commit_before>from . import VT100Test

class CSITest(VT100Test):
    def test_cup(self):
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[10;10H")
        assert self.vt.cursor_pos() == (9, 9)

        self.process("\033[H")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[8H")
        assert self.vt.cursor_pos() == (7, 0)

        self.process("\033[0;0H")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[1;1H")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[500;500H")
        assert self.vt.cursor_pos() == (23, 79)

    def test_directional_movement(self):
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[C")
        assert self.vt.cursor_pos() == (0, 1)

        self.process("\033[C")
        assert self.vt.cursor_pos() == (0, 2)

        self.process("\033[20C")
        assert self.vt.cursor_pos() == (0, 22)

        self.process("\033[D")
        assert self.vt.cursor_pos() == (0, 21)

        self.process("\033[D")
        assert self.vt.cursor_pos() == (0, 20)

        self.process("\033[9D")
        assert self.vt.cursor_pos() == (0, 11)

        self.process("\033[500C")
        assert self.vt.cursor_pos() == (0, 79)

        self.process("\033[500D")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[B")
        assert self.vt.cursor_pos() == (1, 0)

        self.process("\033[B")
        assert self.vt.cursor_pos() == (2, 0)

        self.process("\033[20B")
        assert self.vt.cursor_pos() == (22, 0)

        self.process("\033[A")
        assert self.vt.cursor_pos() == (21, 0)

        self.process("\033[A")
        assert self.vt.cursor_pos() == (20, 0)

        self.process("\033[9A")
        assert self.vt.cursor_pos() == (11, 0)

        self.process("\033[500B")
        assert self.vt.cursor_pos() == (23, 0)

        self.process("\033[500A")
        assert self.vt.cursor_pos() == (0, 0)
<commit_msg>tests for screen clears<commit_after>from . import VT100Test

class CSITest(VT100Test):
    def test_cup(self):
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[10;10H")
        assert self.vt.cursor_pos() == (9, 9)

        self.process("\033[H")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[8H")
        assert self.vt.cursor_pos() == (7, 0)

        self.process("\033[0;0H")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[1;1H")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[500;500H")
        assert self.vt.cursor_pos() == (23, 79)

    def test_directional_movement(self):
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[C")
        assert self.vt.cursor_pos() == (0, 1)

        self.process("\033[C")
        assert self.vt.cursor_pos() == (0, 2)

        self.process("\033[20C")
        assert self.vt.cursor_pos() == (0, 22)

        self.process("\033[D")
        assert self.vt.cursor_pos() == (0, 21)

        self.process("\033[D")
        assert self.vt.cursor_pos() == (0, 20)

        self.process("\033[9D")
        assert self.vt.cursor_pos() == (0, 11)

        self.process("\033[500C")
        assert self.vt.cursor_pos() == (0, 79)

        self.process("\033[500D")
        assert self.vt.cursor_pos() == (0, 0)

        self.process("\033[B")
        assert self.vt.cursor_pos() == (1, 0)

        self.process("\033[B")
        assert self.vt.cursor_pos() == (2, 0)

        self.process("\033[20B")
        assert self.vt.cursor_pos() == (22, 0)

        self.process("\033[A")
        assert self.vt.cursor_pos() == (21, 0)

        self.process("\033[A")
        assert self.vt.cursor_pos() == (20, 0)

        self.process("\033[9A")
        assert self.vt.cursor_pos() == (11, 0)

        self.process("\033[500B")
        assert self.vt.cursor_pos() == (23, 0)

        self.process("\033[500A")
        assert self.vt.cursor_pos() == (0, 0)

    def test_ed(self):
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == ("\n" * 24)

        self.process("foo\033[5;5Hbar\033[10;10Hbaz\033[20;20Hquux")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == 'foo' + ("\n" * 4) + '    bar' + ("\n" * 5) + '         baz' + ("\n" * 10) + '                   quux' + ("\n" * 5)
        self.process("\033[10;12H\033[0J")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == 'foo' + ("\n" * 4) + '    bar' + ("\n" * 5) + '         ba' + ("\n" * 15)
        self.process("\033[5;7H\033[1J")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == ("\n" * 4) + '      r' + ("\n" * 5) + '         ba' + ("\n" * 15)
        self.process("\033[7;7H\033[2J")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == ("\n" * 24)

        self.process("\033[H")
        self.process("foo\033[5;5Hbar\033[10;10Hbaz\033[20;20Hquux")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == 'foo' + ("\n" * 4) + '    bar' + ("\n" * 5) + '         baz' + ("\n" * 10) + '                   quux' + ("\n" * 5)
        self.process("\033[10;12H\033[?0J")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == 'foo' + ("\n" * 4) + '    bar' + ("\n" * 5) + '         ba' + ("\n" * 15)
        self.process("\033[5;7H\033[?1J")
        print(self.vt.get_string_plaintext(0, 0, 500, 500).replace('\n', '\\n'))
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == ("\n" * 4) + '      r' + ("\n" * 5) + '         ba' + ("\n" * 15)
        self.process("\033[7;7H\033[?2J")
        assert self.vt.get_string_plaintext(0, 0, 500, 500) == ("\n" * 24)
<|endoftext|>


<commit_before># No shebang line, this module is meant to be imported
#
# This file is part of PyFarm.
# Copyright (C) 2008-2012 Oliver Palmer
#
# PyFarm is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyFarm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public Lic

'''
base functions and initial setup of argument parsing for the project.
'''

import argparse

def tobool(value):
    '''converts the incoming value to a boolean'''
    if isinstance(value, bool): return value
    elif isinstance(value, (int, long, float)): return bool(value)
    elif value.lower() in ("false", "no", "0"): return False
    elif value.lower() in ("true", "yes", "1"): return True
    else:
        raise TypeError("failed to convert %s to a boolean value" % value)
# end tobool

def tolist(value):
    '''converts the incoming value to a list'''
    if isinstance(value, (str, unicode)):
        if "," in value:
            return [ v.strip() for v in value.split(",") ]
        else:
            return [ value ]
    else:
        raise TypeError("failed to convert %s to a list" % value)
# end tolist

def evalnone(value):
    '''
    If the value provided can be converted to None then return None instead
    of returning a string of None
    '''
    if isinstance(value, (str, unicode)) and value in ('none', 'None', 'NONE', 'null'):
        return None
    return value
# end evalnone

def printOptions(options, log):
    '''prints out the keys and values being applied to the options'''
    def _log(msg):
        log.msg(msg, system="argparse")

    for key, value in vars(options).iteritems():
        _log("%s: %s" % (key, value))
#        _log(msg)
# end printOptions

# common argument handling setup
parser = argparse.ArgumentParser()
parser.add_argument(
    '--force-kill', default=False, type=tobool,
    help='kill any currently running process before starting'
)
parser.add_argument(
    '--wait', default=False, type=tobool,
    help='waits for running processes to terminate first'
)
parser.add_argument(
    '--log', default=None,
    help='location to send the logfile to'
)
parser.add_argument(
    '--remove-lock', action='store_true', default=False,
    help='Removes the lock file on disk before starting if one exists.  This' +
         ' is mainly used if you already know the process does not exist and' +
         ' you do not wish to remove the lock file manually.'
)
parser.add_argument(
    '--port', type=int,
    help='sets the port the service should run on (default: %(default)s)'
)
parser.add_argument(
    '--db', default=None, type=tolist,
    help='Overrides the base database configuration name(s).  Entries should' +
         ' either be in csv form or a single entry string.'
)
<commit_msg>fixed boolean type arguments<commit_after># No shebang line, this module is meant to be imported
#
# This file is part of PyFarm.
# Copyright (C) 2008-2012 Oliver Palmer
#
# PyFarm is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyFarm is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public Lic

'''
base functions and initial setup of argument parsing for the project.
'''

import argparse

def tobool(value):
    '''converts the incoming value to a boolean'''
    if isinstance(value, bool): return value
    elif isinstance(value, (int, long, float)): return bool(value)
    elif value.lower() in ("false", "no", "0"): return False
    elif value.lower() in ("true", "yes", "1"): return True
    else:
        raise TypeError("failed to convert %s to a boolean value" % value)
# end tobool

def tolist(value):
    '''converts the incoming value to a list'''
    if isinstance(value, (str, unicode)):
        if "," in value:
            return [ v.strip() for v in value.split(",") ]
        else:
            return [ value ]
    else:
        raise TypeError("failed to convert %s to a list" % value)
# end tolist

def evalnone(value):
    '''
    If the value provided can be converted to None then return None instead
    of returning a string of None
    '''
    if isinstance(value, (str, unicode)) and value in ('none', 'None', 'NONE', 'null'):
        return None
    return value
# end evalnone

def printOptions(options, log):
    '''prints out the keys and values being applied to the options'''
    def _log(msg):
        log.msg(msg, system="argparse")

    for key, value in vars(options).iteritems():
        _log("%s: %s" % (key, value))
#        _log(msg)
# end printOptions

# common argument handling setup
parser = argparse.ArgumentParser()
parser.add_argument(
    '--force-kill', action='store_true',
    help='kill any currently running process before starting'
)
parser.add_argument(
    '--wait', action='store_true',
    help='waits for running processes to terminate first'
)
parser.add_argument(
    '--log', default=None,
    help='location to send the logfile to'
)
parser.add_argument(
    '--remove-lock', action='store_true',
    help='Removes the lock file on disk before starting if one exists.  This' +
         ' is mainly used if you already know the process does not exist and' +
         ' you do not wish to remove the lock file manually.'
)
parser.add_argument(
    '--port', type=int,
    help='sets the port the service should run on (default: %(default)s)'
)
parser.add_argument(
    '--db', default=None, type=tolist,
    help='Overrides the base database configuration name(s).  Entries should' +
         ' either be in csv form or a single entry string.'
)
<|endoftext|>


<commit_before>__author__ = "Ivan Infante and Felipe Zapata"

# ================> Python Standard  and third-party <==========
import numpy as np
import os
import pyparsing as pa
from scipy.optimize import curve_fit

# ==================> Internal modules <==========
from nac.common import (hbar, r2meV, fs_to_cm)
# ==========================<>=================================

def autocorrelate(f):
    """
    Compute and returns the un-normalized and normalized autocorrelation
    of given function
    """
    d_f = f - f.mean()
    # Compute the autocorrelation function
    uacf = np.correlate(d_f, d_f, "full")[-d_f.size:] / d_f.size
    # Compute the normalized autocorrelation function
    nacf = uacf / uacf[0]
    return uacf, nacf

def gauss_function(x, sigma):
    """
    Gaussian function used to fit the dephasing time
    """
    return np.exp(-0.5 * (-x / sigma) ** 2)

def func_conv(x_real, x_grid, delta):
    """
    A Gaussian function computed on a grid used to convolute spectra
    """
    return np.exp(-2 * (x_grid - x_real) ** 2 / delta ** 2)

def convolute(x, y, x_points, sigma):
    """
    A function used to convolute spectra on a grid of x_points. You need as input x, y and the grid where to convolute.
    """
    # Compute gaussian prefactor
    prefactor = np.sqrt(2.0) / (sigma * np.sqrt(np.pi))
    # Convolute spectrum over grid
    y_points = prefactor * np.stack(np.sum( y * func_conv(x, x_point, sigma) ) for x_point in x_points)
    return y_points

def dephasing(f):
    """
    Computes the dephasing time of a given function using optical response
    formalisms:
    S. Mukamel, Principles of Nonlinear Optical Spectroscopy, 1995
    About the implementation we use the 2nd order cumulant expansion.
    See also eq. (2) in : Kilina et al. Phys. Rev. Lett., 110, 180404, (2013)
    To calculate the dephasing time tau we fit the dephasing function to a
    gaussian of the type : exp(-0.5 * (-x / tau) ** 2)
    """
    ts = np.arange(f.shape[0])
    cumu_ii = np.stack(np.sum(f[0:i]) for i in range(ts.size)) / hbar
    cumu_i = np.stack(np.sum(cumu_ii[0:i]) for i in range(ts.size)) / hbar
    deph = np.exp(-cumu_i)
    np.seterr(over='ignore')
    popt = curve_fit(gauss_function, ts, deph)[0]
    xs = np.exp(-0.5 * (-ts / popt[0]) ** 2)
    deph = np.column_stack((deph, xs))
    rate = popt[0]
    return deph, rate

def spectral_density(f, dt):
    """
    Fourier Transform of a given function f using a dense grid with 100000 points.
    In the case of a FFT of a normalized autocorrelation function,
    this corresponds to a spectral density
    """
    f_fft = abs(1 / np.sqrt(2 * np.pi) * np.fft.fft(f, 100000)) ** 2
    # Fourier Transform of the time axis
    freq = np.fft.fftfreq(len(f_fft), dt)
    # Conversion of the x axis (given in cycles/fs) to cm-1
    freq = freq * fs_to_cm
    return f_fft, freq

def read_couplings(path_hams, ts):
    """
    This function reads the non adiabatic coupling vectors from the files
    generated for the NAMD simulations
    """
    files_im = [os.path.join(path_hams, 'Ham_{}_im'.format(i)) for i in range(ts)]
    xs = np.stack(np.loadtxt(fn) for fn in files_im)
    return xs * r2meV  # return energies in meV

def read_energies(path_hams, ts):
    """
    This function reads the molecular orbital energies of each state from
    the files generated for the NAMD simulations
    """
    files_re = [os.path.join(path_hams, 'Ham_{}_re'.format(i)) for i in range(ts)]
    xs = np.stack(np.diag(np.loadtxt(fn)) for fn in files_re)
    return xs * r2meV / 1000  # return energies in eV

def read_energies_pyxaid(path, fn, nstates, nconds):
    """
    This function reads the molecular orbital energies of each state from
    the output files generated by PYXAID
    """
    inpfile = os.path.join(path, fn)
    cols = tuple(range(5, nstates * 2 + 5, 2))
    xs = np.stack(np.loadtxt('{}{}'.format(inpfile, j), usecols=cols)
                  for j in range(nconds)).transpose()
    # Rows = timeframes ; Columns = states ; tensor = initial conditions
    xs = xs.swapaxes(0, 1)
    return xs

def read_pops_pyxaid(path, fn, nstates, nconds):
    """
    This function reads the population of each state from
    the output files generated by PYXAID
    """
    inpfile = os.path.join(path, fn)
    cols = tuple(range(3, nstates * 2 + 3, 2))
    xs = np.stack(np.loadtxt('{}{}'.format(inpfile, j), usecols=cols)
                  for j in range(nconds)).transpose()
    # Rows = timeframes ; Columns = states ; tensor = initial conditions
    xs = xs.swapaxes(0, 1)
    return xs

def parse_list_of_lists(xs):
    """
    Parse a list of list of integers using pyparsing
    """
    enclosed = pa.Forward()  # Parser to be defined later
    natural = pa.Word(pa.nums)  # Natural Number
    # Nested Grammar
    nestedBrackets = pa.nestedExpr(pa.Suppress('['), pa.Suppress(']'), content=enclosed)
    enclosed << (natural | pa.Suppress(',') | nestedBrackets)
    try:
        rs = enclosed.parseString(xs).asList()[0]
        return list(map(lambda x: list(map(int, x)), rs))
    except pa.ParseException:
        raise RuntimeError("Invalid Macro states Specification")

<commit_msg>Fixed small bug in tools.py<commit_after>__author__ = "Ivan Infante and Felipe Zapata"

# ================> Python Standard  and third-party <==========
import numpy as np
import os
import pyparsing as pa
from scipy.optimize import curve_fit

# ==================> Internal modules <==========
from nac.common import (hbar, r2meV, fs_to_cm)
# ==========================<>=================================

def autocorrelate(f):
    """
    Compute and returns the un-normalized and normalized autocorrelation
    of given function
    """
    d_f = f - f.mean()
    # Compute the autocorrelation function
    uacf = np.correlate(d_f, d_f, "full")[-d_f.size:] / d_f.size
    # Compute the normalized autocorrelation function
    nacf = uacf / uacf[0]
    return uacf, nacf

def gauss_function(x, sigma):
    """
    Gaussian function used to fit the dephasing time
    """
    return np.exp(-0.5 * (-x / sigma) ** 2)

def func_conv(x_real, x_grid, delta):
    """
    A Gaussian function computed on a grid used to convolute spectra
    """
    return np.exp(-2 * (x_grid - x_real) ** 2 / delta ** 2)

def convolute(x, y, x_points, sigma):
    """
    A function used to convolute spectra on a grid of x_points. You need as input x, y and the grid where to convolute.
    """
    # Compute gaussian prefactor
    prefactor = np.sqrt(2.0) / (sigma * np.sqrt(np.pi))
    # Convolute spectrum over grid
    y_points = prefactor * np.stack(np.sum( y * func_conv(x, x_point, sigma) ) for x_point in x_points)
    return y_points

def dephasing(f, dt):
    """
    Computes the dephasing time of a given function using optical response
    formalisms:
    S. Mukamel, Principles of Nonlinear Optical Spectroscopy, 1995
    About the implementation we use the 2nd order cumulant expansion.
    See also eq. (2) in : Kilina et al. Phys. Rev. Lett., 110, 180404, (2013)
    To calculate the dephasing time tau we fit the dephasing function to a
    gaussian of the type : exp(-0.5 * (-x / tau) ** 2)
    """
    ts = np.arange(f.shape[0]) * dt
    cumu_ii = np.stack(np.sum(f[0:i]) for i in range(ts.size)) / hbar
    cumu_i = np.stack(np.sum(cumu_ii[0:i]) for i in range(ts.size)) / hbar
    deph = np.exp(-cumu_i)
    np.seterr(over='ignore')
    popt = curve_fit(gauss_function, ts, deph)[0]
    xs = np.exp(-0.5 * (-ts / popt[0]) ** 2)
    deph = np.column_stack((deph, xs))
    rate = popt[0]
    return deph, rate

def spectral_density(f, dt):
    """
    Fourier Transform of a given function f using a dense grid with 100000 points.
    In the case of a FFT of a normalized autocorrelation function,
    this corresponds to a spectral density
    """
    f_fft = abs(1 / np.sqrt(2 * np.pi) * np.fft.fft(f, 100000)) ** 2
    # Fourier Transform of the time axis
    freq = np.fft.fftfreq(len(f_fft), dt)
    # Conversion of the x axis (given in cycles/fs) to cm-1
    freq = freq * fs_to_cm
    return f_fft, freq

def read_couplings(path_hams, ts):
    """
    This function reads the non adiabatic coupling vectors from the files
    generated for the NAMD simulations
    """
    files_im = [os.path.join(path_hams, 'Ham_{}_im'.format(i)) for i in range(ts)]
    xs = np.stack(np.loadtxt(fn) for fn in files_im)
    return xs * r2meV  # return energies in meV

def read_energies(path_hams, ts):
    """
    This function reads the molecular orbital energies of each state from
    the files generated for the NAMD simulations
    """
    files_re = [os.path.join(path_hams, 'Ham_{}_re'.format(i)) for i in range(ts)]
    xs = np.stack(np.diag(np.loadtxt(fn)) for fn in files_re)
    return xs * r2meV / 1000  # return energies in eV

def read_energies_pyxaid(path, fn, nstates, nconds):
    """
    This function reads the molecular orbital energies of each state from
    the output files generated by PYXAID
    """
    inpfile = os.path.join(path, fn)
    cols = tuple(range(5, nstates * 2 + 5, 2))
    xs = np.stack(np.loadtxt('{}{}'.format(inpfile, j), usecols=cols)
                  for j in range(nconds)).transpose()
    # Rows = timeframes ; Columns = states ; tensor = initial conditions
    xs = xs.swapaxes(0, 1)
    return xs

def read_pops_pyxaid(path, fn, nstates, nconds):
    """
    This function reads the population of each state from
    the output files generated by PYXAID
    """
    inpfile = os.path.join(path, fn)
    cols = tuple(range(3, nstates * 2 + 3, 2))
    xs = np.stack(np.loadtxt('{}{}'.format(inpfile, j), usecols=cols)
                  for j in range(nconds)).transpose()
    # Rows = timeframes ; Columns = states ; tensor = initial conditions
    xs = xs.swapaxes(0, 1)
    return xs

def parse_list_of_lists(xs):
    """
    Parse a list of list of integers using pyparsing
    """
    enclosed = pa.Forward()  # Parser to be defined later
    natural = pa.Word(pa.nums)  # Natural Number
    # Nested Grammar
    nestedBrackets = pa.nestedExpr(pa.Suppress('['), pa.Suppress(']'), content=enclosed)
    enclosed << (natural | pa.Suppress(',') | nestedBrackets)
    try:
        rs = enclosed.parseString(xs).asList()[0]
        return list(map(lambda x: list(map(int, x)), rs))
    except pa.ParseException:
        raise RuntimeError("Invalid Macro states Specification")

<|endoftext|>


<commit_before># Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class Elmerfem(CMakePackage):
    """Elmer is an open source multiphysical simulation software. It
    includes physical models of fluid dynamics, structural mechanics,
    electromagnetics, heat transfer and acoustics."""

    homepage = "https://www.csc.fi/web/elmer"
    url      = "https://github.com/ElmerCSC/elmerfem/archive/release-8.4.tar.gz"

    version('8.4', sha256='cc3ce807d76798361592cc14952cdc3db1ad8f9bac038017514033ce9badc5b3')

    variant('openmp', default=True, description='Enable OpenMP support.')
    variant('mumps', default=False, description='Enable MUMPS support.')
    variant('hypre', default=False, description='Enable Hypre support.')
    variant('trilinos', default=False, description='Enable Trilinos support.')
    variant('zoltan', default=False, description='Enable Zoltan support.')
    variant('lua', default=False, description='Enable Lua support.')

    depends_on('mpi')
    depends_on('netcdf-fortran')
    depends_on('blas')
    depends_on('lapack')
    depends_on('mumps', when='+mumps')
    depends_on('hypre', when='+hypre')
    depends_on('trilinos~hypre~zoltan~zoltan2', when='+trilinos')
    depends_on('zoltan+fortran', when='+zoltan')
    depends_on('lua@5.1.5', when='+lua')

    def cmake_args(self):

        spec = self.spec

        args = ['-DWITH_ElmerIce=ON', '-DWITH_CONTRIB=ON', '-DWITH_MPI=ON']

        if '+openmp' in spec:
            args.append('-DWITH_OpenMP=ON')
        else:
            args.append('-DWITH_OpenMP=OFF')

        if '+mumps' in spec:
            args.append('-DWITH_Mumps=ON')
        else:
            args.append('-DWITH_Mumps=OFF')

        if '+hypre' in spec:
            args.append('-DWITH_Hypre=ON')
        else:
            args.append('-DWITH_Hypre=OFF')

        if '+trilinos' in spec:
            args.extend([
                '-DWITH_Trilinos=ON',
                '-DCMAKE_CXX_STANDARD=11',
            ])
        else:
            args.append('-DWITH_Trilinos=OFF')

        if '+lua' in spec:
            args.extend([
                '-DWITH_LUA=ON',
                '-DUSE_SYSTEM_LUA=ON'
            ])
            if '%gcc' in spec:
                args.append('-DCMAKE_Fortran_FLAGS=-ffree-line-length-none')

        else:
            args.append('-DWITH_LUA=OFF')

        if '+zoltan' in spec:
            args.extend([
                '-DWITH_Zoltan=ON',
                '-DUSE_SYSTEM_ZOLTAN=ON'
            ])
        else:
            args.append('-DWITH_Zoltan=OFF')

        return args

    def setup_run_environment(self, env):
        env.set('ELMER_HOME', self.prefix)
<commit_msg>elmer-fem: add devel branch (#13630)<commit_after># Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *

class Elmerfem(CMakePackage):
    """Elmer is an open source multiphysical simulation software. It
    includes physical models of fluid dynamics, structural mechanics,
    electromagnetics, heat transfer and acoustics."""

    homepage = "https://www.csc.fi/web/elmer"
    url      = "https://github.com/ElmerCSC/elmerfem/archive/release-8.4.tar.gz"

    version('8.4', sha256='cc3ce807d76798361592cc14952cdc3db1ad8f9bac038017514033ce9badc5b3')
    version('devel', git='https://github.com/ElmerCSC/elmerfem.git', branch='devel')

    variant('openmp', default=True, description='Enable OpenMP support.')
    variant('mumps', default=False, description='Enable MUMPS support.')
    variant('hypre', default=False, description='Enable Hypre support.')
    variant('trilinos', default=False, description='Enable Trilinos support.')
    variant('zoltan', default=False, description='Enable Zoltan support.')
    variant('lua', default=False, description='Enable Lua support.')

    depends_on('mpi')
    depends_on('netcdf-fortran')
    depends_on('blas')
    depends_on('lapack')
    depends_on('mumps', when='+mumps')
    depends_on('hypre', when='+hypre')
    depends_on('trilinos~hypre~zoltan~zoltan2', when='+trilinos')
    depends_on('zoltan+fortran', when='+zoltan')
    depends_on('lua@5.1.5', when='+lua')

    def cmake_args(self):

        spec = self.spec

        args = ['-DWITH_ElmerIce=ON', '-DWITH_CONTRIB=ON', '-DWITH_MPI=ON']

        if '+openmp' in spec:
            args.append('-DWITH_OpenMP=ON')
        else:
            args.append('-DWITH_OpenMP=OFF')

        if '+mumps' in spec:
            args.append('-DWITH_Mumps=ON')
        else:
            args.append('-DWITH_Mumps=OFF')

        if '+hypre' in spec:
            args.append('-DWITH_Hypre=ON')
        else:
            args.append('-DWITH_Hypre=OFF')

        if '+trilinos' in spec:
            args.extend([
                '-DWITH_Trilinos=ON',
                '-DCMAKE_CXX_STANDARD=11',
            ])
        else:
            args.append('-DWITH_Trilinos=OFF')

        if '+lua' in spec:
            args.extend([
                '-DWITH_LUA=ON',
                '-DUSE_SYSTEM_LUA=ON'
            ])
            if '%gcc' in spec:
                args.append('-DCMAKE_Fortran_FLAGS=-ffree-line-length-none')

        else:
            args.append('-DWITH_LUA=OFF')

        if '+zoltan' in spec:
            args.extend([
                '-DWITH_Zoltan=ON',
                '-DUSE_SYSTEM_ZOLTAN=ON'
            ])
        else:
            args.append('-DWITH_Zoltan=OFF')

        return args

    def setup_run_environment(self, env):
        env.set('ELMER_HOME', self.prefix)
<|endoftext|>


<commit_before>import csv
import re
import matplotlib.pyplot as plt
import math

class StatsWriter:
    """ Is able to be populated by stats and then be exported into various formats.
            file is the csvfile saved into
            data is the stats object
    """

    def __init__(self, csvfile, graphfile, init_counter=15):
        self.csvfile = csvfile
        self.graphfile = graphfile
        self.data = []
        self.init_counter = init_counter
        """ internal constant being defined """
        self.ylim_max_nl_total = 0.5
        """ upper ylim for plot y-axis for nl vs total mentions """

    def addrow(self, dictstats, mode):
        """
        adds one dataset stats object as dictionary into the data-array
        """
        dictstats['mode'] = mode
        self.data.append(dictstats)

    def writecsv(self):
        """
        write the stats into a csv file
        """
        with open(self.csvfile, "w", encoding='utf-8') as f:
            fieldnames = ['mode',
                          'nl_mention_nr',
                          'tot_mention_nr',
                          'nl_token_nr',
                          'tot_token_nr',
                          'abstract_nl_mention_nr',
                          'abstract_nl_token_nr',
                          'abstract_tot_token_nr',
                          'full_nl_mention_nr',
                          'full_nl_token_nr',
                          'full_tot_token_nr',
                          'nl_mention_array',
                          'abstract_nl_mention_array',
                          'full_nl_mention_array']
            w = csv.DictWriter(f, fieldnames=fieldnames)
            w.writeheader()
            for row in self.data:
                w.writerow(row)

    def makegraph(self):
        """
        make the graph
        """
        # TODO (3) matplotlib create graph
        # xposition as int and label as string
        x_pos = []
        label = []
        bar_color = []

        # param arrays to be shown in graph
        simple_array = []  # nl absolute nr
        nl_total_ratio_array = []  # nl vs total ratio

        abstract_token_ratio_array = []  # abstract_nl_token / abstract_tot_token
        full_token_ratio_array = []  # full_nl_token / full_tot_token
        abstract_full_ratio_array = []  # abstract_token_ratio / full_token_ratio

        # helping vars
        re_compiled_param = re.compile(r'_(\d+)$')
        simple_counter = self.init_counter
        total_counter = 0

        for row in self.data:
            total_counter += 1
            # TODO add standard error
            nl_total_ratio = row['nl_mention_nr'] / float(row['tot_mention_nr'])
            # abstract = abstract_tokens/tokens in abstract
            # full = full_tokens/tokens in full
            # abstract full ratio = abstract/full

            is_not_ok = row['abstract_tot_token_nr'] == 0 or row['full_tot_token_nr'] == 0 or \
                        row['abstract_nl_token_nr'] == 0 or row['full_nl_token_nr'] == 0
            if is_not_ok:
                abstract_full_ratio = 0
                abstract_token_ratio = 0
                full_token_ratio = 0
            else:
                abstract_token_ratio = row['abstract_nl_token_nr'] / float(row['abstract_tot_token_nr'])
                full_token_ratio = row['full_nl_token_nr'] / float(row['full_tot_token_nr'])
                abstract_full_ratio = abstract_token_ratio / full_token_ratio

            match = re.search(re_compiled_param, row['mode'])
            if match:
                x_pos.append(int(match.group(1)))
                label.append(row['mode'])
                # match.group(1) = min_length param as int
            else:
                x_pos.append(simple_counter)
                label.append(row['mode'])
                simple_counter += 1

            # TODO make interesting bars as param not hard coded
            if row['mode'] == "Carsten" or row['mode'] == "Inclusive_18":
                bar_color.append('orange')
            else:
                bar_color.append('blue')

            # print(nl_total_ratio)
            # nl total ratio
            if nl_total_ratio > 0:
                nl_total_ratio_array.append(nl_total_ratio)
            else:
                nl_total_ratio_array.append(0)

            # print(abstract_full_ratio)
            # abstract vs full ratio
            if abstract_full_ratio > 0:
                abstract_full_ratio_array.append(math.log(abstract_full_ratio))
            else:
                abstract_full_ratio_array.append(0)

            # abstract_token_ratio
            abstract_token_ratio_array.append(abstract_token_ratio)

            # full_token_ratio
            full_token_ratio_array.append(full_token_ratio)

            # abstract_nl_nr / abstract_tot_token_nr
            if row['abstract_tot_token_nr'] > 0:
                simple_abstract_ratio = row['abstract_nl_mention_nr'] / float(row['abstract_tot_token_nr'])
            else:
                simple_abstract_ratio = 0

            # nl mention nr
            simple_array.append(row['nl_mention_nr'])

        # subplot for nl total ratio array
        fig1 = plt.figure()
        fig1.add_axes([0.1, 0.22, 0.88, 0.74])
        # plt.subplot(121)
        plt.bar(x_pos, nl_total_ratio_array, color=bar_color)
        plt.xticks([x + 0.3 for x in x_pos], label, rotation=90)
        # print([x + 0.3 for x in x_pos])
        # fig.ylabel("NL vs Total mention ratio")
        plt.xlim(min(x_pos), max(x_pos) * 1.05)
        plt.ylim(0, self.ylim_max_nl_total)
        plt.show()

        # subplot for abstract vs full ratio
        # only if the array contains non zeros
        if set(abstract_full_ratio_array) != {0}:
            # plt.subplot(122)
            fig2 = plt.figure()
            fig2.add_axes([0.1, 0.22, 0.88, 0.74])
            plt.bar(x_pos, abstract_full_ratio_array, color=bar_color)
            plt.xticks(x_pos, label, rotation=90)  # TODO shift position to get correct labeling on bars
            plt.ylabel("Abstract vs Full document ratio")
            plt.xlim(min(x_pos) * 0.95, max(x_pos) * 1.05)
            # plt.ylim(min([x for x in abstract_full_ratio_array if x > 0]) * 0.95, max(abstract_full_ratio_array) * 1.05)
            plt.ylim(1, 3)
            # OPTIONAL define better border for ylim

        # subplot for abstract token ratio
        # plt.subplot(223)
        # plt.bar(x_pos, abstract_token_ratio_array)
        # plt.xticks(x_pos, label, rotation=90)
        # plt.ylabel("Abstract: NL Tokens / Tot Tokens")

        # subplot for full token ratio
        # plt.subplot(224)
        # plt.bar(x_pos, full_token_ratio_array)
        # plt.xticks(x_pos, label, rotation=90)
        # plt.ylabel("Full: Nl Tokens / Tot Tokens")

        # subplot minimum one abstract/full
        # OPTIONAL plot minimum one abstract/full

        # plt.plot(annotate_array, nl_total_ratio_array, 'rs', annotate_array, abstract_full_ratio_array, 'bs')
        # plt.axis([self.init_counter, self.init_counter + total_counter - 1, 0, 3])

        plt.show()
<commit_msg>[#50] Fix Xticks 2nd Plot<commit_after>import csv
import re
import matplotlib.pyplot as plt
import math

class StatsWriter:
    """ Is able to be populated by stats and then be exported into various formats.
            file is the csvfile saved into
            data is the stats object
    """

    def __init__(self, csvfile, graphfile, init_counter=15):
        self.csvfile = csvfile
        self.graphfile = graphfile
        self.data = []
        self.init_counter = init_counter
        """ internal constant being defined """
        self.ylim_max_nl_total = 0.5
        """ upper ylim for plot y-axis for nl vs total mentions """

    def addrow(self, dictstats, mode):
        """
        adds one dataset stats object as dictionary into the data-array
        """
        dictstats['mode'] = mode
        self.data.append(dictstats)

    def writecsv(self):
        """
        write the stats into a csv file
        """
        with open(self.csvfile, "w", encoding='utf-8') as f:
            fieldnames = ['mode',
                          'nl_mention_nr',
                          'tot_mention_nr',
                          'nl_token_nr',
                          'tot_token_nr',
                          'abstract_nl_mention_nr',
                          'abstract_nl_token_nr',
                          'abstract_tot_token_nr',
                          'full_nl_mention_nr',
                          'full_nl_token_nr',
                          'full_tot_token_nr',
                          'nl_mention_array',
                          'abstract_nl_mention_array',
                          'full_nl_mention_array']
            w = csv.DictWriter(f, fieldnames=fieldnames)
            w.writeheader()
            for row in self.data:
                w.writerow(row)

    def makegraph(self):
        """
        make the graph
        """
        # TODO (3) matplotlib create graph
        # xposition as int and label as string
        x_pos = []
        label = []
        bar_color = []

        # param arrays to be shown in graph
        simple_array = []  # nl absolute nr
        nl_total_ratio_array = []  # nl vs total ratio

        abstract_token_ratio_array = []  # abstract_nl_token / abstract_tot_token
        full_token_ratio_array = []  # full_nl_token / full_tot_token
        abstract_full_ratio_array = []  # abstract_token_ratio / full_token_ratio

        # helping vars
        re_compiled_param = re.compile(r'_(\d+)$')
        simple_counter = self.init_counter
        total_counter = 0

        for row in self.data:
            total_counter += 1
            # TODO add standard error
            nl_total_ratio = row['nl_mention_nr'] / float(row['tot_mention_nr'])
            # abstract = abstract_tokens/tokens in abstract
            # full = full_tokens/tokens in full
            # abstract full ratio = abstract/full

            is_not_ok = row['abstract_tot_token_nr'] == 0 or row['full_tot_token_nr'] == 0 or \
                        row['abstract_nl_token_nr'] == 0 or row['full_nl_token_nr'] == 0
            if is_not_ok:
                abstract_full_ratio = 0
                abstract_token_ratio = 0
                full_token_ratio = 0
            else:
                abstract_token_ratio = row['abstract_nl_token_nr'] / float(row['abstract_tot_token_nr'])
                full_token_ratio = row['full_nl_token_nr'] / float(row['full_tot_token_nr'])
                abstract_full_ratio = abstract_token_ratio / full_token_ratio

            match = re.search(re_compiled_param, row['mode'])
            if match:
                x_pos.append(int(match.group(1)))
                label.append(row['mode'])
                # match.group(1) = min_length param as int
            else:
                x_pos.append(simple_counter)
                label.append(row['mode'])
                simple_counter += 1

            # TODO make interesting bars as param not hard coded
            if row['mode'] == "Carsten" or row['mode'] == "Inclusive_18":
                bar_color.append('orange')
            else:
                bar_color.append('blue')

            # print(nl_total_ratio)
            # nl total ratio
            if nl_total_ratio > 0:
                nl_total_ratio_array.append(nl_total_ratio)
            else:
                nl_total_ratio_array.append(0)

            # print(abstract_full_ratio)
            # abstract vs full ratio
            if abstract_full_ratio > 0:
                abstract_full_ratio_array.append(math.log(abstract_full_ratio))
            else:
                abstract_full_ratio_array.append(0)

            # abstract_token_ratio
            abstract_token_ratio_array.append(abstract_token_ratio)

            # full_token_ratio
            full_token_ratio_array.append(full_token_ratio)

            # abstract_nl_nr / abstract_tot_token_nr
            if row['abstract_tot_token_nr'] > 0:
                simple_abstract_ratio = row['abstract_nl_mention_nr'] / float(row['abstract_tot_token_nr'])
            else:
                simple_abstract_ratio = 0

            # nl mention nr
            simple_array.append(row['nl_mention_nr'])

        # subplot for nl total ratio array
        fig1 = plt.figure()
        fig1.add_axes([0.1, 0.22, 0.88, 0.74])
        # plt.subplot(121)
        plt.bar(x_pos, nl_total_ratio_array, color=bar_color)
        plt.xticks([x + 0.3 for x in x_pos], label, rotation=90)
        # print([x + 0.3 for x in x_pos])
        # fig.ylabel("NL vs Total mention ratio")
        plt.xlim(min(x_pos), max(x_pos) * 1.05)
        plt.ylim(0, self.ylim_max_nl_total)
        plt.show()

        # subplot for abstract vs full ratio
        # only if the array contains non zeros
        if set(abstract_full_ratio_array) != {0}:
            # plt.subplot(122)
            fig2 = plt.figure()
            fig2.add_axes([0.1, 0.22, 0.88, 0.74])
            plt.bar(x_pos, abstract_full_ratio_array, color=bar_color)
            xticks_pos = list(map(lambda x: x + 0.4, x_pos))
            plt.xticks(xticks_pos, label, rotation=90)  # TODO shift position to get correct labeling on bars
            plt.ylabel("Abstract vs Full document ratio")
            plt.xlim(min(x_pos) * 0.95, max(x_pos) * 1.05)
            # plt.ylim(min([x for x in abstract_full_ratio_array if x > 0]) * 0.95, max(abstract_full_ratio_array) * 1.05)
            plt.ylim(1, 3)
            # OPTIONAL define better border for ylim

        # subplot for abstract token ratio
        # plt.subplot(223)
        # plt.bar(x_pos, abstract_token_ratio_array)
        # plt.xticks(x_pos, label, rotation=90)
        # plt.ylabel("Abstract: NL Tokens / Tot Tokens")

        # subplot for full token ratio
        # plt.subplot(224)
        # plt.bar(x_pos, full_token_ratio_array)
        # plt.xticks(x_pos, label, rotation=90)
        # plt.ylabel("Full: Nl Tokens / Tot Tokens")

        # subplot minimum one abstract/full
        # OPTIONAL plot minimum one abstract/full

        # plt.plot(annotate_array, nl_total_ratio_array, 'rs', annotate_array, abstract_full_ratio_array, 'bs')
        # plt.axis([self.init_counter, self.init_counter + total_counter - 1, 0, 3])

        plt.show()
<|endoftext|>