<commit_before># -*- coding: UTF-8 -*-
#
# update.py - part of the FDroid server tools
# Copyright (C) 2010, Ciaran Gultnieks, ciaran@ciarang.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import shutil
import glob
import subprocess
import re
import zipfile
import md5
from xml.dom.minidom import Document
from optparse import OptionParser

#Read configuration...
execfile('config.py')

execfile('metadata.py')

# Parse command line...
parser = OptionParser()
parser.add_option("-c", "--createmeta", action="store_true", default=False,
                  help="Create skeleton metadata files that are missing")
parser.add_option("-v", "--verbose", action="store_true", default=False,
                  help="Spew out even more information than normal")
(options, args) = parser.parse_args()

icon_dir=os.path.join('repo','icons')

# Delete and re-create the icon directory...
if os.path.exists(icon_dir):
    shutil.rmtree(icon_dir)
os.mkdir(icon_dir)

# Gather information about all the apk files in the repo directory...
apks = []
for apkfile in glob.glob(os.path.join('repo','*.apk')):

    apkfilename = apkfile[5:]

    print "Processing " + apkfilename
    thisinfo = {}
    thisinfo['apkname'] = apkfilename
    thisinfo['size'] = os.path.getsize(apkfile)
    thisinfo['permissions'] = []
    thisinfo['features'] = []
    p = subprocess.Popen([aapt_path,'dump','badging',
       apkfile], stdout=subprocess.PIPE)
    output = p.communicate()[0]
    if options.verbose:
        print output
    if p.returncode != 0:
        print "ERROR: Failed to get apk information"
        sys.exit(1)
    for line in output.splitlines():
        if line.startswith("package:"):
            pat = re.compile(".*name='([a-zA-Z0-9.]*)'.*")
            thisinfo['id'] = re.match(pat, line).group(1)
            pat = re.compile(".*versionCode='([0-9]*)'.*")
            thisinfo['versioncode'] = re.match(pat, line).group(1)
            pat = re.compile(".*versionName='([^']*)'.*")
            thisinfo['version'] = re.match(pat, line).group(1)
        if line.startswith("application:"):
            pat = re.compile(".*label='([^']*)'.*")
            thisinfo['name'] = re.match(pat, line).group(1)
            pat = re.compile(".*icon='([^']*)'.*")
            thisinfo['iconsrc'] = re.match(pat, line).group(1)
        if line.startswith("sdkVersion:"):
            pat = re.compile(".*'([0-9]*)'.*")
            thisinfo['sdkversion'] = re.match(pat, line).group(1)
        if line.startswith("native-code:"):
            pat = re.compile(".*'([^']*)'.*")
            thisinfo['nativecode'] = re.match(pat, line).group(1)
        if line.startswith("uses-permission:"):
            pat = re.compile(".*'([^']*)'.*")
            perm = re.match(pat, line).group(1)
            if perm.startswith("android.permission."):
                perm = perm[19:]
            thisinfo['permissions'].append(perm)
        if line.startswith("uses-feature:"):
            pat = re.compile(".*'([^']*)'.*")
            perm = re.match(pat, line).group(1)
            if perm.startswith("android.feature."):
                perm = perm[16:]
            thisinfo['features'].append(perm)

    if not thisinfo.has_key('sdkversion'):
        print "  WARNING: no SDK version information found"
        thisinfo['sdkversion'] = 0

    # Calculate the md5...
    m = md5.new()
    f = open(apkfile, 'rb')
    while True:
        t = f.read(1024)
        if len(t) == 0:
            break
        m.update(t)
    thisinfo['md5'] = m.hexdigest()
    f.close()

    # Extract the icon file...
    apk = zipfile.ZipFile(apkfile, 'r')
    thisinfo['icon'] = (thisinfo['id'] + '.' +
        thisinfo['versioncode'] + '.png')
    iconfilename = os.path.join(icon_dir, thisinfo['icon'])
    iconfile = open(iconfilename, 'wb')
    iconfile.write(apk.read(thisinfo['iconsrc']))
    iconfile.close()
    apk.close()

    apks.append(thisinfo)

# Get all apps...
apps = read_metadata()

# Some information from the apks needs to be applied up to the application
# level. When doing this, we use the info from the most recent version's apk.
for app in apps:
    bestver = 0
    for apk in apks:
        if apk['id'] == app['id']:
            if apk['versioncode'] > bestver:
                bestver = apk['versioncode']
                bestapk = apk

    if bestver == 0:
        app['name'] = app['id']
        app['icon'] = ''
        print "WARNING: Application " + app['id'] + " has no packages"
    else:
        app['name'] = bestapk['name']
        app['icon'] = bestapk['icon']

# Generate warnings for apk's with no metadata (or create skeleton
# metadata files, if requested on the command line)
for apk in apks:
    found = False
    for app in apps:
        if app['id'] == apk['id']:
            found = True
            break
    if not found:
        if options.createmeta:
            f = open(os.path.join('metadata', apk['id'] + '.txt'), 'w')
            f.write("License:Unknown\n")
            f.write("Web Site:\n")
            f.write("Source Code:\n")
            f.write("Issue Tracker:\n")
            f.write("Summary:" + apk['name'] + "\n")
            f.write("Description:\n")
            f.write(apk['name'] + "\n")
            f.write(".\n")
            f.close()
            print "Generated skeleton metadata for " + apk['id']
        else:
            print "WARNING: " + apk['apkname'] + " (" + apk['id'] + ") has no metadata"
            print "       " + apk['name'] + " - " + apk['version']

# Create the index
doc = Document()

def addElement(name, value, doc, parent):
    el = doc.createElement(name)
    el.appendChild(doc.createTextNode(value))
    parent.appendChild(el)

root = doc.createElement("fdroid")
doc.appendChild(root)

apps_inrepo = 0
apps_disabled = 0

for app in apps:

    if app['disabled'] is None:
        apps_inrepo += 1
        apel = doc.createElement("application")
        apel.setAttribute("id", app['id'])
        root.appendChild(apel)

        addElement('id', app['id'], doc, apel)
        addElement('name', app['name'], doc, apel)
        addElement('summary', app['summary'], doc, apel)
        addElement('icon', app['icon'], doc, apel)
        addElement('description', app['description'], doc, apel)
        addElement('license', app['license'], doc, apel)
        addElement('web', app['web'], doc, apel)
        addElement('source', app['source'], doc, apel)
        addElement('tracker', app['tracker'], doc, apel)
        addElement('marketversion', app['marketversion'], doc, apel)
        addElement('marketvercode', app['marketvercode'], doc, apel)

        gotmarketver = False

        for apk in apks:
            if apk['id'] == app['id']:
                if apk['versioncode'] == app['marketvercode']:
                    gotmarketver = True
                apkel = doc.createElement("package")
                apel.appendChild(apkel)
                addElement('version', apk['version'], doc, apkel)
                addElement('versioncode', apk['versioncode'], doc, apkel)
                addElement('apkname', apk['apkname'], doc, apkel)
                addElement('hash', apk['md5'], doc, apkel)
                addElement('size', str(apk['size']), doc, apkel)
                addElement('sdkver', str(apk['sdkversion']), doc, apkel)
                perms = ""
                for p in apk['permissions']:
                    if len(perms) > 0:
                        perms += ","
                    perms += p
                if len(perms) > 0:
                    addElement('permissions', perms, doc, apkel)
                features = ""
                for f in apk['features']:
                    if len(features) > 0:
                        features += ","
                    features += f
                if len(features) > 0:
                    addElement('features', features, doc, apkel)

        if not gotmarketver and app['marketvercode'] != '0':
            print "WARNING: Don't have market version (" + app['marketversion'] + ") of " + app['name']
            print "         (" + app['id'] + ")"
            if options.verbose:
                # A bit of extra debug info, basically for diagnosing
                # app developer mistakes:
                print "         Market vercode:" + app['marketvercode']
                print "         Got:"
                for apk in apks:
                    if apk['id'] == app['id']:
                        print "           " + apk['versioncode'] + " - " + apk['version']

    else:
        apps_disabled += 1

of = open(os.path.join('repo','index.xml'), 'wb')
output = doc.toxml()
of.write(output)
of.close()

print "Finished."
print str(apps_inrepo) + " apps in repo"
print str(apps_disabled) + " disabled"

<commit_msg>Generate the index in alpha-sorted (by app name) order<commit_after># -*- coding: UTF-8 -*-
#
# update.py - part of the FDroid server tools
# Copyright (C) 2010, Ciaran Gultnieks, ciaran@ciarang.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import shutil
import glob
import subprocess
import re
import zipfile
import md5
from xml.dom.minidom import Document
from optparse import OptionParser

#Read configuration...
execfile('config.py')

execfile('metadata.py')

# Parse command line...
parser = OptionParser()
parser.add_option("-c", "--createmeta", action="store_true", default=False,
                  help="Create skeleton metadata files that are missing")
parser.add_option("-v", "--verbose", action="store_true", default=False,
                  help="Spew out even more information than normal")
(options, args) = parser.parse_args()

icon_dir=os.path.join('repo','icons')

# Delete and re-create the icon directory...
if os.path.exists(icon_dir):
    shutil.rmtree(icon_dir)
os.mkdir(icon_dir)

# Gather information about all the apk files in the repo directory...
apks = []
for apkfile in glob.glob(os.path.join('repo','*.apk')):

    apkfilename = apkfile[5:]

    print "Processing " + apkfilename
    thisinfo = {}
    thisinfo['apkname'] = apkfilename
    thisinfo['size'] = os.path.getsize(apkfile)
    thisinfo['permissions'] = []
    thisinfo['features'] = []
    p = subprocess.Popen([aapt_path,'dump','badging',
       apkfile], stdout=subprocess.PIPE)
    output = p.communicate()[0]
    if options.verbose:
        print output
    if p.returncode != 0:
        print "ERROR: Failed to get apk information"
        sys.exit(1)
    for line in output.splitlines():
        if line.startswith("package:"):
            pat = re.compile(".*name='([a-zA-Z0-9.]*)'.*")
            thisinfo['id'] = re.match(pat, line).group(1)
            pat = re.compile(".*versionCode='([0-9]*)'.*")
            thisinfo['versioncode'] = re.match(pat, line).group(1)
            pat = re.compile(".*versionName='([^']*)'.*")
            thisinfo['version'] = re.match(pat, line).group(1)
        if line.startswith("application:"):
            pat = re.compile(".*label='([^']*)'.*")
            thisinfo['name'] = re.match(pat, line).group(1)
            pat = re.compile(".*icon='([^']*)'.*")
            thisinfo['iconsrc'] = re.match(pat, line).group(1)
        if line.startswith("sdkVersion:"):
            pat = re.compile(".*'([0-9]*)'.*")
            thisinfo['sdkversion'] = re.match(pat, line).group(1)
        if line.startswith("native-code:"):
            pat = re.compile(".*'([^']*)'.*")
            thisinfo['nativecode'] = re.match(pat, line).group(1)
        if line.startswith("uses-permission:"):
            pat = re.compile(".*'([^']*)'.*")
            perm = re.match(pat, line).group(1)
            if perm.startswith("android.permission."):
                perm = perm[19:]
            thisinfo['permissions'].append(perm)
        if line.startswith("uses-feature:"):
            pat = re.compile(".*'([^']*)'.*")
            perm = re.match(pat, line).group(1)
            if perm.startswith("android.feature."):
                perm = perm[16:]
            thisinfo['features'].append(perm)

    if not thisinfo.has_key('sdkversion'):
        print "  WARNING: no SDK version information found"
        thisinfo['sdkversion'] = 0

    # Calculate the md5...
    m = md5.new()
    f = open(apkfile, 'rb')
    while True:
        t = f.read(1024)
        if len(t) == 0:
            break
        m.update(t)
    thisinfo['md5'] = m.hexdigest()
    f.close()

    # Extract the icon file...
    apk = zipfile.ZipFile(apkfile, 'r')
    thisinfo['icon'] = (thisinfo['id'] + '.' +
        thisinfo['versioncode'] + '.png')
    iconfilename = os.path.join(icon_dir, thisinfo['icon'])
    iconfile = open(iconfilename, 'wb')
    iconfile.write(apk.read(thisinfo['iconsrc']))
    iconfile.close()
    apk.close()

    apks.append(thisinfo)

# Get all apps...
apps = read_metadata()

# Some information from the apks needs to be applied up to the application
# level. When doing this, we use the info from the most recent version's apk.
for app in apps:
    bestver = 0
    for apk in apks:
        if apk['id'] == app['id']:
            if apk['versioncode'] > bestver:
                bestver = apk['versioncode']
                bestapk = apk

    if bestver == 0:
        app['name'] = app['id']
        app['icon'] = ''
        print "WARNING: Application " + app['id'] + " has no packages"
    else:
        app['name'] = bestapk['name']
        app['icon'] = bestapk['icon']

# Generate warnings for apk's with no metadata (or create skeleton
# metadata files, if requested on the command line)
for apk in apks:
    found = False
    for app in apps:
        if app['id'] == apk['id']:
            found = True
            break
    if not found:
        if options.createmeta:
            f = open(os.path.join('metadata', apk['id'] + '.txt'), 'w')
            f.write("License:Unknown\n")
            f.write("Web Site:\n")
            f.write("Source Code:\n")
            f.write("Issue Tracker:\n")
            f.write("Summary:" + apk['name'] + "\n")
            f.write("Description:\n")
            f.write(apk['name'] + "\n")
            f.write(".\n")
            f.close()
            print "Generated skeleton metadata for " + apk['id']
        else:
            print "WARNING: " + apk['apkname'] + " (" + apk['id'] + ") has no metadata"
            print "       " + apk['name'] + " - " + apk['version']

#Sort the app list by name, then the web site doesn't have to by default:
apps = sorted(apps, key=lambda app: app['name'])

# Create the index
doc = Document()

def addElement(name, value, doc, parent):
    el = doc.createElement(name)
    el.appendChild(doc.createTextNode(value))
    parent.appendChild(el)

root = doc.createElement("fdroid")
doc.appendChild(root)

apps_inrepo = 0
apps_disabled = 0

for app in apps:

    if app['disabled'] is None:
        apps_inrepo += 1
        apel = doc.createElement("application")
        apel.setAttribute("id", app['id'])
        root.appendChild(apel)

        addElement('id', app['id'], doc, apel)
        addElement('name', app['name'], doc, apel)
        addElement('summary', app['summary'], doc, apel)
        addElement('icon', app['icon'], doc, apel)
        addElement('description', app['description'], doc, apel)
        addElement('license', app['license'], doc, apel)
        addElement('web', app['web'], doc, apel)
        addElement('source', app['source'], doc, apel)
        addElement('tracker', app['tracker'], doc, apel)
        addElement('marketversion', app['marketversion'], doc, apel)
        addElement('marketvercode', app['marketvercode'], doc, apel)

        gotmarketver = False

        for apk in apks:
            if apk['id'] == app['id']:
                if apk['versioncode'] == app['marketvercode']:
                    gotmarketver = True
                apkel = doc.createElement("package")
                apel.appendChild(apkel)
                addElement('version', apk['version'], doc, apkel)
                addElement('versioncode', apk['versioncode'], doc, apkel)
                addElement('apkname', apk['apkname'], doc, apkel)
                addElement('hash', apk['md5'], doc, apkel)
                addElement('size', str(apk['size']), doc, apkel)
                addElement('sdkver', str(apk['sdkversion']), doc, apkel)
                perms = ""
                for p in apk['permissions']:
                    if len(perms) > 0:
                        perms += ","
                    perms += p
                if len(perms) > 0:
                    addElement('permissions', perms, doc, apkel)
                features = ""
                for f in apk['features']:
                    if len(features) > 0:
                        features += ","
                    features += f
                if len(features) > 0:
                    addElement('features', features, doc, apkel)

        if not gotmarketver and app['marketvercode'] != '0':
            print "WARNING: Don't have market version (" + app['marketversion'] + ") of " + app['name']
            print "         (" + app['id'] + ")"
            if options.verbose:
                # A bit of extra debug info, basically for diagnosing
                # app developer mistakes:
                print "         Market vercode:" + app['marketvercode']
                print "         Got:"
                for apk in apks:
                    if apk['id'] == app['id']:
                        print "           " + apk['versioncode'] + " - " + apk['version']

    else:
        apps_disabled += 1

of = open(os.path.join('repo','index.xml'), 'wb')
output = doc.toxml()
of.write(output)
of.close()

print "Finished."
print str(apps_inrepo) + " apps in repo"
print str(apps_disabled) + " disabled"

<|endoftext|>


<commit_before># Copyright 2014 Google Inc. All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Plugs that provide access to USB devices via ADB/Fastboot.

For details of what these interfaces look like, see adb_device.py and
fastboot_device.py.

To use these plugs:
  from openhtf import plugs
  from openhtf.plugs import usb

  @plugs.RequiresPlug(adb=usb.AdbPlug)
  def MyPhase(test, adb):
    adb.Shell('ls')
"""

import openhtf.plugs as plugs
from openhtf import conf
from openhtf.plugs.usb import adb_device
from openhtf.plugs.usb import fastboot_device
from openhtf.plugs.usb import local_usb

conf.Declare('usb_server', 'USB Server IP/Hostname')
conf.Declare('usb_server_port', 'USB Server Port',
             default_value=10000)

conf.Declare('libusb_rsa_key', 'A private key file for use by libusb auth.')

def _open_usb_handle(**kwargs):
  """Open a UsbHandle subclass, based on configuration.

  If configuration 'usb_server' is set, use it to connect to remote usb,
  otherwise attempt to connect locally.

  Args:
    **kwargs: Arguments to pass to respective handle's Open() method.

  Returns:
    Instance of UsbHandle.
  """
  if conf.Config().usb_server:
    return None
  else:
    return local_usb.LibUsbHandle.Open(**kwargs)

# pylint: disable=too-few-public-methods
class FastbootPlug(plugs.BasePlug):
  """Plug that provides fastboot."""

  def __new__(cls):
    device = fastboot_device.FastbootDevice.Connect(
        _open_usb_handle(
            interface_class=fastboot_device.CLASS,
            interface_subclass=fastboot_device.SUBCLASS,
            interface_protocol=fastboot_device.PROTOCOL))
    device.TearDown = device.Close  # pylint: disable=invalid-name
    return device

class AdbPlug(plugs.BasePlug):
  """Plug that provides ADB."""

  def __new__(cls):
    kwargs = {}
    if conf.Config().libusb_rsa_key:
      kwargs['rsa_keys'] = [adb_device.M2CryptoSigner(
          conf.Config().libusb_rsa_key)]

    device = adb_device.AdbDevice.Connect(
        _open_usb_handle(
            interface_class=adb_device.CLASS,
            interface_subclass=adb_device.SUBCLASS,
            interface_protocol=adb_device.PROTOCOL),
        **kwargs)
    device.TearDown = device.Close  # pylint: disable=invalid-name
    return device
<commit_msg> change for AdbPlug, adding serial number<commit_after># Copyright 2014 Google Inc. All Rights Reserved.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Plugs that provide access to USB devices via ADB/Fastboot.

For details of what these interfaces look like, see adb_device.py and
fastboot_device.py.

To use these plugs:
  from openhtf import plugs
  from openhtf.plugs import usb

  @plugs.RequiresPlug(adb=usb.AdbPlug)
  def MyPhase(test, adb):
    adb.Shell('ls')
"""
import commands
import logging

import openhtf.plugs as plugs
from openhtf import conf
from openhtf.plugs.usb import adb_device
from openhtf.plugs.usb import fastboot_device
from openhtf.plugs.usb import local_usb

conf.Declare('usb_server', 'USB Server IP/Hostname')
conf.Declare('usb_server_port', 'USB Server Port',
             default_value=10000)
conf.Declare('libusb_rsa_key', 'A private key file for use by libusb auth.')
conf.Declare('unit_name', 'cambrionix unit name')

_LOG = logging.getLogger(__name__)

def _open_usb_handle(**kwargs):
  """Open a UsbHandle subclass, based on configuration.

  If configuration 'usb_server' is set, use it to connect to remote usb,
  otherwise attempt to connect locally.

  Args:
    **kwargs: Arguments to pass to respective handle's Open() method.

  Returns:
    Instance of UsbHandle.
  """
  if conf.Config().usb_server:
    return None
  else:
    return local_usb.LibUsbHandle.Open(**kwargs)

def _get_usb_serial(unit_name):
  """Get a usb serial based on the Cambrionix unit mac address in configuration."""
  cmd = '/usr/local/google/home/amyxchen/esuit64 -t "LIST, %s"' % unit_name
  serial = commands.getstatusoutput(cmd)[1]

  serial = 'LWP1A02A15110225'

  _LOG.error('get serial:%s on unit:%s' % (serial, unit_name))

  return serial

# pylint: disable=too-few-public-methods
class FastbootPlug(plugs.BasePlug):
  """Plug that provides fastboot."""

  def __new__(cls):
    serial = None
    unit_name = conf.Config().unit_name

    if unit_name:
      serial = _get_usb_serial(unit_name)

    device = fastboot_device.FastbootDevice.Connect(
        _open_usb_handle(
            interface_class=fastboot_device.CLASS,
            interface_subclass=fastboot_device.SUBCLASS,
            interface_protocol=fastboot_device.PROTOCOL,
            serial_number = serial
            ))
    device.TearDown = device.Close  # pylint: disable=invalid-name
    return device

class AdbPlug(plugs.BasePlug):
  """Plug that provides ADB."""

  def __new__(cls):
    kwargs = {}
    if conf.Config().libusb_rsa_key:
      kwargs['rsa_keys'] = [adb_device.M2CryptoSigner(
          conf.Config().libusb_rsa_key)]

    serial = None
    unit_name = conf.Config().unit_name

    if unit_name:
      serial = _get_usb_serial(unit_name)

    device = adb_device.AdbDevice.Connect(
        _open_usb_handle(
            interface_class=adb_device.CLASS,
            interface_subclass=adb_device.SUBCLASS,
            interface_protocol=adb_device.PROTOCOL,
            serial_number=serial
            ),
        **kwargs)
    device.TearDown = device.Close  # pylint: disable=invalid-name
    return device<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import cPickle as pickle
import sys, os, bz2
import parseWikt
import re

debug = True
if __name__ == '__main__' or debug:
    try:
        from IPython.Shell import IPShellEmbed
        ipshell = IPShellEmbed()
    except:
        from IPython import embed
        ipshell = embed

def buildDicts():
    dirname = os.path.dirname(sys.argv[0])

    filename = 'bgWiktBG.pickle.bz2'
    wiktfile = 'bgwiktionary-latest-pages-meta-current.xml.bz2'
    if (not os.path.isfile(os.path.join(dirname, filename))
        or (os.path.getctime(os.path.join(dirname, filename)) <
            os.path.getctime(os.path.join(dirname, wiktfile)))):
        parseWikt.parseBGwikt()
    f = bz2.BZ2File(os.path.join(dirname, filename), 'rb')
    (bg_bg, bg_type, bg_types) = pickle.load(f)
    f.close()

    filename = 'enWiktBG.pickle.bz2'
    wiktfile = 'enwiktionary-latest-pages-meta-current.xml.bz2'
    if (not os.path.isfile(os.path.join(dirname, filename))
        or (os.path.getctime(os.path.join(dirname, filename)) <
            os.path.getctime(os.path.join(dirname, wiktfile)))):
        parseWikt.parseENwikt()
    f = bz2.BZ2File(os.path.join(dirname, filename), 'rb')
    (bg_enWikt, en_bgWikt) = pickle.load(f)
    f.close()

    dirname = '/usr/share/bgoffice'
    filename = 'bg-en.dat'
    f = codecs.open(os.path.join(dirname, filename), 'r', 'cp1251')
    bg_enLines = f.read()
    f.close()

    bg_enEntries = bg_enLines.split('\x00')
    bg_en = {}
    vizhRE = re.compile(u'^вж.{0,1} ([\u0400-\u04FF\u0500-\u052F]+)$', re.UNICODE)
    for entry in bg_enEntries:
        lines = entry.split('\n')
        if not bg_enWikt.has_key(lines[0]):
            if len(lines) == 2 and vizhRE.search(lines[1]):
                bg_bg[lines[0]] = vizhRE.search(lines[1]).groups(1)[0]
            else:
                bg_en[lines[0]] = '
'.join(lines[1:])

    del bg_en['']

    filename = 'en-bg.dat'
    f = codecs.open(os.path.join(dirname, filename), 'r', 'cp1251')
    en_bgLines = f.read()
    f.close()

    en_bgEntries = en_bgLines.split('\x00')
    en_bg = {}
    for entry in en_bgEntries:
        lines = entry.split('\n')
        if not en_bgWikt.has_key(lines[0]):
            en_bg[lines[0]] = '
'.join(lines[1:])

    return (bg_en, en_bg, bg_bg, bg_enWikt, en_bgWikt, bg_type, bg_types)

if __name__ == '__main__':
    dicts = buildDicts()
    for mydict in dicts:
        print mydict.keys()[0:10]

    ipshell()
<commit_msg>Fail gracefully if mwlib isn't supported<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import cPickle as pickle
import sys, os, bz2
try:
    import parseWikt
except:
    pass
import re

debug = True
if __name__ == '__main__' or debug:
    try:
        from IPython.Shell import IPShellEmbed
        ipshell = IPShellEmbed()
    except:
        from IPython import embed
        ipshell = embed

def buildDicts():
    dirname = os.path.dirname(sys.argv[0])

    filename = 'bgWiktBG.pickle.bz2'
    wiktfile = 'bgwiktionary-latest-pages-meta-current.xml.bz2'
    if (not os.path.isfile(os.path.join(dirname, filename))
        or (os.path.getctime(os.path.join(dirname, filename)) <
            os.path.getctime(os.path.join(dirname, wiktfile)))):
        try:
            parseWikt.parseBGwikt()
        except:
            print "Please download " + filename + " from somewhere else."
            sys.exit( 1 )
    f = bz2.BZ2File(os.path.join(dirname, filename), 'rb')
    (bg_bg, bg_type, bg_types) = pickle.load(f)
    f.close()

    filename = 'enWiktBG.pickle.bz2'
    wiktfile = 'enwiktionary-latest-pages-meta-current.xml.bz2'
    if (not os.path.isfile(os.path.join(dirname, filename))
        or (os.path.getctime(os.path.join(dirname, filename)) <
            os.path.getctime(os.path.join(dirname, wiktfile)))):
        try:
            parseWikt.parseENwikt()
        except:
            print "Please download " + filename + " from somewhere else."
            sys.exit( 1 )
    f = bz2.BZ2File(os.path.join(dirname, filename), 'rb')
    (bg_enWikt, en_bgWikt) = pickle.load(f)
    f.close()

    dirname = '/usr/share/bgoffice'
    filename = 'bg-en.dat'
    f = codecs.open(os.path.join(dirname, filename), 'r', 'cp1251')
    bg_enLines = f.read()
    f.close()

    bg_enEntries = bg_enLines.split('\x00')
    bg_en = {}
    vizhRE = re.compile(u'^вж.{0,1} ([\u0400-\u04FF\u0500-\u052F]+)$', re.UNICODE)
    for entry in bg_enEntries:
        lines = entry.split('\n')
        if not bg_enWikt.has_key(lines[0]):
            if len(lines) == 2 and vizhRE.search(lines[1]):
                bg_bg[lines[0]] = vizhRE.search(lines[1]).groups(1)[0]
            else:
                bg_en[lines[0]] = '
'.join(lines[1:])

    del bg_en['']

    filename = 'en-bg.dat'
    f = codecs.open(os.path.join(dirname, filename), 'r', 'cp1251')
    en_bgLines = f.read()
    f.close()

    en_bgEntries = en_bgLines.split('\x00')
    en_bg = {}
    for entry in en_bgEntries:
        lines = entry.split('\n')
        if not en_bgWikt.has_key(lines[0]):
            en_bg[lines[0]] = '
'.join(lines[1:])

    return (bg_en, en_bg, bg_bg, bg_enWikt, en_bgWikt, bg_type, bg_types)

if __name__ == '__main__':
    dicts = buildDicts()
    for mydict in dicts:
        print mydict.keys()[0:10]

    ipshell()
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# Copyright (C) 2015, 2016 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import yaml
import time
import shutil
import netaddr
import datetime
from argparse import ArgumentParser, REMAINDER
from itertools import chain, islice
from requests.exceptions import ConnectionError
from pyroute2 import IPRoute
from nsenter import Namespace
from base import *
from exabgp import ExaBGP
from gobgp import GoBGP
from bird import BIRD
from quagga import Quagga
from tester import Tester
from monitor import Monitor
from settings import dckr
from Queue import Queue

def rm_line():
    print '\x1b[1A\x1b[2K\x1b[1D\x1b[1A'

def gc_thresh3():
    gc_thresh3 = '/proc/sys/net/ipv4/neigh/default/gc_thresh3'
    with open(gc_thresh3) as f:
        return int(f.read().strip())

def doctor(args):
    ver = dckr.version()['Version']
    ok = int(''.join(ver.split('.'))) >= 190
    print 'docker version ... {1} ({0})'.format(ver, 'ok' if ok else 'update to 1.9.0 at least')

    print 'bgperf image',
    if img_exists('bgperf/exabgp'):
        print '... ok'
    else:
        print '... not found. run `bgperf prepare`'

    for name in ['gobgp', 'bird', 'quagga']:
        print '{0} image'.format(name),
        if img_exists('bgperf/{0}'.format(name)):
            print '... ok'
        else:
            print '... not found. if you want to bench {0}, run `bgperf prepare`'.format(name)

    print '/proc/sys/net/ipv4/neigh/default/gc_thresh3 ... {0}'.format(gc_thresh3())

def prepare(args):
    ExaBGP.build_image(args.force)
    GoBGP.build_image(args.force)
    Quagga.build_image(args.force)
    BIRD.build_image(args.force)

def update(args):
    if args.image == 'all' or args.image == 'exabgp':
        ExaBGP.build_image(True, checkout=args.checkout)
    if args.image == 'all' or args.image == 'gobgp':
        GoBGP.build_image(True, checkout=args.checkout)
    if args.image == 'all' or args.image == 'quagga':
        Quagga.build_image(True, checkout=args.checkout)
    if args.image == 'all' or args.image == 'bird':
        BIRD.build_image(True, checkout=args.checkout)

def bench(args):
    config_dir = '{0}/{1}'.format(args.dir, args.bench_name)
    brname = args.bench_name + '-br'

    ip = IPRoute()
    ctn_intfs = flatten((l.get_attr('IFLA_IFNAME') for l in ip.get_links() if l.get_attr('IFLA_MASTER') == br) for br in ip.link_lookup(ifname=brname))

    if not args.repeat:
        # currently ctn name is same as ctn intf
        # TODO support proper mapping between ctn name and intf name
        for ctn in ctn_intfs:
            dckr.remove_container(ctn, force=True)

        if os.path.exists(config_dir):
            shutil.rmtree(config_dir)
    else:
        for ctn in ctn_intfs:
            if ctn != 'tester':
                dckr.remove_container(ctn, force=True)

    if args.file:
        with open(args.file) as f:
            conf = yaml.load(f)
    else:
        conf = gen_conf(args.neighbor_num, args.prefix_num)

    if len(conf['tester']) > gc_thresh3():
        print 'gc_thresh3({0}) is lower than the number of peer({1})'.format(gc_thresh3(), len(conf['tester']))
        print 'type next to increase the value'
        print '$ echo 16384 | sudo tee /proc/sys/net/ipv4/neigh/default/gc_thresh3'

    print 'run', args.target
    if args.target == 'gobgp':
        target = GoBGP
    elif args.target == 'bird':
        target = BIRD
    elif args.target == 'quagga':
        target = Quagga

    if args.image:
        target = target(args.target, '{0}/{1}'.format(config_dir, args.target), image=args.image)
    else:
        target = target(args.target, '{0}/{1}'.format(config_dir, args.target))
    target.run(conf, brname)

    print 'run monitor'
    m = Monitor('monitor', config_dir+'/monitor')
    m.run(conf, brname)

    print 'waiting bgp connection between {0} and monitor'.format(args.target)
    m.wait_established(conf['target']['local-address'].split('/')[0])

    if not args.repeat:
        print 'run tester'
        t = Tester('tester', config_dir+'/tester')
        t.run(conf, brname)

    start = datetime.datetime.now()

    q = Queue()

    target.stats(q)
    m.stats(q)

    def mem_human(v):
        if v > 1000 * 1000 * 1000:
            return '{0:.2f}GB'.format(float(v) / (1000 * 1000 * 1000))
        elif v > 1000 * 1000:
            return '{0:.2f}MB'.format(float(v) / (1000 * 1000))
        elif v > 1000:
            return '{0:.2f}KB'.format(float(v) / 1000)
        else:
            return '{0:.2f}B'.format(float(v))

    f = open(args.output, 'w') if args.output else None
    cpu = 0
    mem = 0
    while True:
        info = q.get()

        if info['who'] == target.name:
            cpu = info['cpu']
            mem = info['mem']

        if info['who'] == m.name:
            now = datetime.datetime.now()
            elapsed = now - start
            recved = info['info']['accepted'] if 'accepted' in info['info'] else 0
            if elapsed.seconds > 0:
                rm_line()
            print 'elapsed: {0}sec, cpu: {1:>4.2f}%, mem: {2}, recved: {3}'.format(elapsed.seconds, cpu, mem_human(mem), recved)
            f.write('{0}, {1}, {2}, {3}\n'.format(elapsed.seconds, cpu, mem, recved)) if f else None
            f.flush() if f else None

            if info['checked']:
                f.close() if f else None
                return

def gen_conf(neighbor, prefix):
    conf = {}
    conf['target'] = {
        'as': 1000,
        'router-id': '10.10.0.1',
        'local-address': '10.10.0.1/16',
    }

    conf['monitor'] = {
        'as': 1001,
        'router-id': '10.10.0.2',
        'local-address': '10.10.0.2/16',
        'check-points': [prefix * neighbor],
    }

    conf['tester'] = {}
    offset = 0

    it = netaddr.iter_iprange('100.0.0.0','160.0.0.0')
    for i in range(3, neighbor+3):
        router_id = '10.10.{0}.{1}'.format(i/255, i%255)
        conf['tester'][router_id] = {
            'as': 1000 + i,
            'router-id': router_id,
            'local-address': router_id + '/16',
            'paths': list('{0}/32'.format(ip) for ip in islice(it, prefix)),
        }
    return conf

def config(args):
    conf = gen_conf(args.neighbor_num, args.prefix_num)

    with open(args.output, 'w') as f:
        f.write(yaml.dump(conf))

if __name__ == '__main__':
    parser = ArgumentParser(description='BGP performance measuring tool')
    parser.add_argument('-b', '--bench-name', default='bgperf')
    parser.add_argument('-d', '--dir', default='/tmp')
    s = parser.add_subparsers()
    parser_doctor = s.add_parser('doctor', help='check env')
    parser_doctor.set_defaults(func=doctor)

    parser_prepare = s.add_parser('prepare', help='prepare env')
    parser_prepare.add_argument('-f', '--force', default=False, type=bool)
    parser_prepare.set_defaults(func=prepare)

    parser_update = s.add_parser('update', help='pull bgp docker images')
    parser_update.add_argument('image', choices=['exabgp', 'gobgp', 'bird', 'quagga', 'all'])
    parser_update.add_argument('-c', '--checkout', default='HEAD')
    parser_update.set_defaults(func=update)

    parser_bench = s.add_parser('bench', help='run benchmarks')
    parser_bench.add_argument('-t', '--target', choices=['gobgp', 'bird', 'quagga'], default='gobgp')
    parser_bench.add_argument('-i', '--image', help='specify custom docker image')
    parser_bench.add_argument('-r', '--repeat', action='store_true', help='use existing tester/monitor container')
    parser_bench.add_argument('-f', '--file', metavar='CONFIG_FILE')
    parser_bench.add_argument('-n', '--neighbor-num', default=100, type=int)
    parser_bench.add_argument('-p', '--prefix-num', default=100, type=int)
    parser_bench.add_argument('-o', '--output', metavar='STAT_FILE')
    parser_bench.set_defaults(func=bench)

    parser_config = s.add_parser('config', help='generate config')
    parser_config.add_argument('-o', '--output', default='bgperf.yml', type=str)
    parser_config.add_argument('-n', '--neighbor-num', default=100, type=int)
    parser_config.add_argument('-p', '--prefix-num', default=100, type=int)
    parser_config.set_defaults(func=config)

    args = parser.parse_args()
    args.func(args)
<commit_msg>bgperf.py: handle remote flag in target configuration<commit_after>#!/usr/bin/env python
#
# Copyright (C) 2015, 2016 Nippon Telegraph and Telephone Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import sys
import yaml
import time
import shutil
import netaddr
import datetime
from argparse import ArgumentParser, REMAINDER
from itertools import chain, islice
from requests.exceptions import ConnectionError
from pyroute2 import IPRoute
from nsenter import Namespace
from base import *
from exabgp import ExaBGP
from gobgp import GoBGP
from bird import BIRD
from quagga import Quagga
from tester import Tester
from monitor import Monitor
from settings import dckr
from Queue import Queue

def rm_line():
    print '\x1b[1A\x1b[2K\x1b[1D\x1b[1A'

def gc_thresh3():
    gc_thresh3 = '/proc/sys/net/ipv4/neigh/default/gc_thresh3'
    with open(gc_thresh3) as f:
        return int(f.read().strip())

def doctor(args):
    ver = dckr.version()['Version']
    ok = int(''.join(ver.split('.'))) >= 190
    print 'docker version ... {1} ({0})'.format(ver, 'ok' if ok else 'update to 1.9.0 at least')

    print 'bgperf image',
    if img_exists('bgperf/exabgp'):
        print '... ok'
    else:
        print '... not found. run `bgperf prepare`'

    for name in ['gobgp', 'bird', 'quagga']:
        print '{0} image'.format(name),
        if img_exists('bgperf/{0}'.format(name)):
            print '... ok'
        else:
            print '... not found. if you want to bench {0}, run `bgperf prepare`'.format(name)

    print '/proc/sys/net/ipv4/neigh/default/gc_thresh3 ... {0}'.format(gc_thresh3())

def prepare(args):
    ExaBGP.build_image(args.force)
    GoBGP.build_image(args.force)
    Quagga.build_image(args.force)
    BIRD.build_image(args.force)

def update(args):
    if args.image == 'all' or args.image == 'exabgp':
        ExaBGP.build_image(True, checkout=args.checkout)
    if args.image == 'all' or args.image == 'gobgp':
        GoBGP.build_image(True, checkout=args.checkout)
    if args.image == 'all' or args.image == 'quagga':
        Quagga.build_image(True, checkout=args.checkout)
    if args.image == 'all' or args.image == 'bird':
        BIRD.build_image(True, checkout=args.checkout)

def bench(args):
    config_dir = '{0}/{1}'.format(args.dir, args.bench_name)
    brname = args.bench_name + '-br'

    ip = IPRoute()
    ctn_intfs = flatten((l.get_attr('IFLA_IFNAME') for l in ip.get_links() if l.get_attr('IFLA_MASTER') == br) for br in ip.link_lookup(ifname=brname))

    if not args.repeat:
        # currently ctn name is same as ctn intf
        # TODO support proper mapping between ctn name and intf name
        for ctn in ctn_intfs:
            dckr.remove_container(ctn, force=True)

        if os.path.exists(config_dir):
            shutil.rmtree(config_dir)
    else:
        for ctn in ctn_intfs:
            if ctn != 'tester':
                dckr.remove_container(ctn, force=True)

    if args.file:
        with open(args.file) as f:
            conf = yaml.load(f)
    else:
        conf = gen_conf(args.neighbor_num, args.prefix_num)

    if len(conf['tester']) > gc_thresh3():
        print 'gc_thresh3({0}) is lower than the number of peer({1})'.format(gc_thresh3(), len(conf['tester']))
        print 'type next to increase the value'
        print '$ echo 16384 | sudo tee /proc/sys/net/ipv4/neigh/default/gc_thresh3'

    if args.target == 'gobgp':
        target = GoBGP
    elif args.target == 'bird':
        target = BIRD
    elif args.target == 'quagga':
        target = Quagga

    is_remote = True if 'remote' in conf['target'] and conf['target']['remote'] else False

    if not is_remote:
        print 'run', args.target
        if args.image:
            target = target(args.target, '{0}/{1}'.format(config_dir, args.target), image=args.image)
        else:
            target = target(args.target, '{0}/{1}'.format(config_dir, args.target))
        target.run(conf, brname)

    print 'run monitor'
    m = Monitor('monitor', config_dir+'/monitor')
    m.run(conf, brname)

    print 'waiting bgp connection between {0} and monitor'.format(args.target)
    m.wait_established(conf['target']['local-address'].split('/')[0])

    if not args.repeat:
        print 'run tester'
        t = Tester('tester', config_dir+'/tester')
        t.run(conf, brname)

    start = datetime.datetime.now()

    q = Queue()

    m.stats(q)
    if not is_remote:
        target.stats(q)

    def mem_human(v):
        if v > 1000 * 1000 * 1000:
            return '{0:.2f}GB'.format(float(v) / (1000 * 1000 * 1000))
        elif v > 1000 * 1000:
            return '{0:.2f}MB'.format(float(v) / (1000 * 1000))
        elif v > 1000:
            return '{0:.2f}KB'.format(float(v) / 1000)
        else:
            return '{0:.2f}B'.format(float(v))

    f = open(args.output, 'w') if args.output else None
    cpu = 0
    mem = 0
    while True:
        info = q.get()

        if info['who'] == target.name:
            cpu = info['cpu']
            mem = info['mem']

        if info['who'] == m.name:
            now = datetime.datetime.now()
            elapsed = now - start
            recved = info['info']['accepted'] if 'accepted' in info['info'] else 0
            if elapsed.seconds > 0:
                rm_line()
            print 'elapsed: {0}sec, cpu: {1:>4.2f}%, mem: {2}, recved: {3}'.format(elapsed.seconds, cpu, mem_human(mem), recved)
            f.write('{0}, {1}, {2}, {3}\n'.format(elapsed.seconds, cpu, mem, recved)) if f else None
            f.flush() if f else None

            if info['checked']:
                f.close() if f else None
                return

def gen_conf(neighbor, prefix):
    conf = {}
    conf['target'] = {
        'as': 1000,
        'router-id': '10.10.0.1',
        'local-address': '10.10.0.1/16',
    }

    conf['monitor'] = {
        'as': 1001,
        'router-id': '10.10.0.2',
        'local-address': '10.10.0.2/16',
        'check-points': [prefix * neighbor],
    }

    conf['tester'] = {}
    offset = 0

    it = netaddr.iter_iprange('100.0.0.0','160.0.0.0')
    for i in range(3, neighbor+3):
        router_id = '10.10.{0}.{1}'.format(i/255, i%255)
        conf['tester'][router_id] = {
            'as': 1000 + i,
            'router-id': router_id,
            'local-address': router_id + '/16',
            'paths': list('{0}/32'.format(ip) for ip in islice(it, prefix)),
        }
    return conf

def config(args):
    conf = gen_conf(args.neighbor_num, args.prefix_num)

    with open(args.output, 'w') as f:
        f.write(yaml.dump(conf))

if __name__ == '__main__':
    parser = ArgumentParser(description='BGP performance measuring tool')
    parser.add_argument('-b', '--bench-name', default='bgperf')
    parser.add_argument('-d', '--dir', default='/tmp')
    s = parser.add_subparsers()
    parser_doctor = s.add_parser('doctor', help='check env')
    parser_doctor.set_defaults(func=doctor)

    parser_prepare = s.add_parser('prepare', help='prepare env')
    parser_prepare.add_argument('-f', '--force', default=False, type=bool)
    parser_prepare.set_defaults(func=prepare)

    parser_update = s.add_parser('update', help='pull bgp docker images')
    parser_update.add_argument('image', choices=['exabgp', 'gobgp', 'bird', 'quagga', 'all'])
    parser_update.add_argument('-c', '--checkout', default='HEAD')
    parser_update.set_defaults(func=update)

    parser_bench = s.add_parser('bench', help='run benchmarks')
    parser_bench.add_argument('-t', '--target', choices=['gobgp', 'bird', 'quagga'], default='gobgp')
    parser_bench.add_argument('-i', '--image', help='specify custom docker image')
    parser_bench.add_argument('-r', '--repeat', action='store_true', help='use existing tester/monitor container')
    parser_bench.add_argument('-f', '--file', metavar='CONFIG_FILE')
    parser_bench.add_argument('-n', '--neighbor-num', default=100, type=int)
    parser_bench.add_argument('-p', '--prefix-num', default=100, type=int)
    parser_bench.add_argument('-o', '--output', metavar='STAT_FILE')
    parser_bench.set_defaults(func=bench)

    parser_config = s.add_parser('config', help='generate config')
    parser_config.add_argument('-o', '--output', default='bgperf.yml', type=str)
    parser_config.add_argument('-n', '--neighbor-num', default=100, type=int)
    parser_config.add_argument('-p', '--prefix-num', default=100, type=int)
    parser_config.set_defaults(func=config)

    args = parser.parse_args()
    args.func(args)
<|endoftext|>


<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Just render EM something.

Usage:
    ./EM.py [options] (--show|-s) <i>
    ./EM.py [options] <i> <outname>

Options:
    --help -h
    --show -s          Show
    --verbose -v       Make some noise.
    --nozip -U         flds are NOT gzipped.
    --zip   -Z         flds are gzipped. If neither of these two are set,
                       guess based on name.
    --log10 -l         Log it.
    --lims=LIM         Set lims [default: (1e2,6e8)]
    --highlight=H      Set highlight for field quantity.
    --quantity=Q -Q Q  Render this quantity. Quantities are E_norm, E_energy,
                       B_norm, B_energy, EM_energy, and S. [default: E_energy]
    --dir=D -D D       Read from this dir [default: .]
    --restrict=R       Restrict it.
    --x-restrict=R     Restrict by positions as a 4 tuple.
    --target=D -t D    Plot contours of this density. If D is a list,
                       plot multiple contours.
    --targetc=C        Set these colors for the contours. If C is a list,
                       plot these colors. [default: darkred]
    --targeta=A        Set the target alphas. If A is a list
                       plot multiple alphas. [default: 0.15]
    --targetq=Q        Set the target quantity. If Q is a list
                       plot multiple quantities. [default: RhoN10]
    --linthresh=L      Set the linear threshold for SymLogPlot [default: 1e7]
    --linscale=L       Set the linear threshold for SymLogPlot [default: 1e7]
    --cmap=CMAP        Set the colormap. [default: viridis]
    --equal -E         Make spatial dimensions equal.
    --blur=R           Blur with this radius.
    --t-offset=T       Set time offset in fs. [default: 0].
    --traj=F           Plot trajectories from this file. If not used,
                       will not plot trajectories.
    --traj-offset=O    Set the offset and factor to get the relevant
                       trajectory timestep such that i_t = factor*i+offset, where
                       i is the passed index in <i>. The factor and offset are passed
                       to this option in the form of a tuple (factor, offset). If not
                       used, script will search for the closest time in the trajectory
                       file.
    --traj-tail=T      Set the "length" in time of the tail. Accepts "inf" which means for
                       all times, otherwise subtract from the time determined by other
                       options and move back that many steps in fs. [default: inf]
    --traj-n=N         Plot only first N trajectories. Pass a 3-tuple in order to slice.
    --traj-energy      Color the trajectory lines by their energy.
    --traj-mass        Set the rest mass of the pmovie particles. [default: 0.511e6]
    --traj-E-log       Logarithmic color for trajectories.
    --traj-maxE=E      Set the maximum E in eV explicitly. If set, anything above
                       will be cut off.
    --traj-minE=E      Set the minimum E in eV. [default: 1]
    --traj-newfmt      Use the new trajectory format.
    --traj-qlinear     Use linear easing for trajectories instead of log-ish.
'''
from docopt import docopt;
import numpy as np;
import numpy.linalg as lin;
from pys import parse_ftuple, parse_ituple, parse_ctuple, parse_stuple;
from pys import fltrx_s, srx_s, rgbrx_s, quote_subs
from lspreader.flds import read_indexed, restrict
from lspplot.sclr import S, E_energy,B_energy,EM_energy, vector_norm, smooth2Dp;
from lspplot.pc import pc, highlight, trajectories;
from lspplot.consts import c,mu0,e0;
import re;

opts = docopt(__doc__,help=True);
if opts['--nozip']:
    gzip = False;
elif opts['--zip']:
    gzip = True;
else:
    gzip = 'guess';

def parseit(s,rx=fltrx_s,parsef=parse_ftuple,quote=False):
    ''' helper for parsing tuples '''
    #this needs to be match! not search to not match parens.
    if re.match(rx, s):
        if quote:
            s = quote_subs(s);
        return [eval(s)]
    else:
        return parsef(s, length=None);

quantity = opts['--quantity'];
quantities = dict(
    E_norm=dict(
        fvar=['E'],
        read= lambda d: vector_norm(d,'E')*1e5,
        title="Electric Field Norm",
        units="V/m"),
    E_energy={
        'fvar':['E'], 'read': E_energy,
        'title': "Electric Field Energy",
        'units': "J / cc"},
    B_norm=dict(
        fvar=['B'],
        read= lambda d: vector_norm(d,'B'),
        title="Magnetic Field Norm",
        units="gauss"),
    B_energy={
        'fvar':['B'], 'read': B_energy,
        'title': "Magnetic Field Energy",
        'units': "J / cc"},
    EM_energy={
        'fvar':['E','B'], 'read': EM_energy,
        'title': "Electromagnetic Field Energy",
        'units': "J / cc"},
    S={
        'fvar':['E','B'], 'read': S,
        'title': "Poynting Vector Norm",
        'units': "W / cm$^2$"},
)
def mkread(s,N):
    '''lambda's aren't great.'''
    def _f(d):
        return d[s]*N;
    return _f;
quantities.update({ '{}{}'.format(field,comp):dict(
    fvar = [field],
    title='{} field {} component'.format(field,comp),
    units=unit,
    read = mkread('{}{}'.format(field,comp),scale))
  for field,unit,scale in [('E','V/m',1e5),('B','gauss',1.0)]
  for comp in ['x','y','z'] });

if quantity not in quantities:
    print("quantity is not one of {}".format(quantities.keys()));
    quit();
fvar=quantities[quantity]['fvar']
read=quantities[quantity]['read']
titlestr=quantities[quantity]['title']
units=quantities[quantity]['units']

if opts['--target']:
    svar = parseit(opts['--targetq'], rx=srx_s,parsef=parse_stuple,quote=True)
    #unique
    svar = list(set(svar));
else:
    svar = None;
#####################################
#reading data
d = read_indexed(int(opts['<i>']),
    flds=fvar,sclr=svar,
    gzip=gzip,dir=opts['--dir'],
              gettime=True,vector_norms=False);
t  = d['t'];
#choosing positions
ylabel =  'z' if np.isclose(d['y'].max(),d['y'].min()) else 'y';
if opts['--x-restrict']:
    res = parse_ftuple(opts['--x-restrict'], length=4);
    res[:2] = [ np.abs(d['x'][:,0]*1e4 - ires).argmin() for ires in res[:2] ];
    res[2:] = [ np.abs(d[ylabel][0,:]*1e4 - ires).argmin() for ires in res[2:] ];
    #including the edges
    res[1]+=1;
    res[3]+=1;
    restrict(d,res);
elif opts['--restrict']:
    res = parse_ituple(opts['--restrict'],length=None);
    restrict(d,res);

#trajectories
if opts['--traj']:
    factor, offset = None, None;
    if opts['--traj-offset']:
        factor, offset = parse_ituple(opts['--traj-offset'],length=2);
    with np.load(opts['--traj'], mmap_mode='r') as f:
        if factor:
            tri = i*factor+offset;
            trt = f['time'][tri];
            if not np.isclose(trt,t):
                import sys
                sys.stderr.write(
                    "warning: time from trajectory is {} while time from sclr is {}\n".format(
                        trt,t));
        else:
            tri = np.sum((f['time'] <= t).astype(int));
            trt = f['time'][tri];
        tri_start=None;
        if opts['--traj-tail'] != 'inf':
            tail = float(opts['--traj-tail'])*1e-15*1e9;
            tri_start = np.sum((f['time'] <= t-tail).astype(int));
        pn_end=None;
        pn_start=0;
        pn_step=1;
        if opts['--traj-n']:
            if re.match('^[0-9]+$',opts['--traj-n']):
                pn_end=int(opts['--traj-n']);
            else:
                pn_start,pn_end,pn_step=parse_ituple(opts['--traj-n'],length=3);
        if not opts['--traj-newfmt']:
            tr = f['data'][tri_start:tri+1, pn_start:pn_end:pn_step];
        else:
            keys = list(f.keys());
            ps = len(keys) - 1;
            #magic, think about it
            sz = len(keys[0] if keys[0] != 'time' else keys[1])
            fmt ='{{:0{}}}'.format(sz);
            if pn_end is None:
                pn_end = ps;
            else:
                pn_end = min(ps,pn_end);
            tr = np.array([
                f[fmt.format(i)][tri_start:tri+1] for i in range(pn_start,pn_end,pn_step) ]).T
    if opts['--verbose']:
        print("size of trajectories: {}".format(tr.shape));
        print("final time is {}".format(trt));
        print("with sclr time as {}".format(t));
    pass;

####################################
#massaging data
x,y = d['x']*1e4,d[ylabel]*1e4
q = read(d);

if opts['--blur']:
    rad = float(opts['--blur']);
    w=rad*6;
    q,x,y = smooth2Dp(
        q, (x,y), [0.1,0.1], [0.6,0.6]);

#####################################
#plotting

#getting options from user
mn,mx = parse_ftuple(opts['--lims'],length=2);

#plot the density
toff=float(opts['--t-offset']);
title="{}\nTime: {:.2f} fs".format(titlestr,t*1e6+toff);
r=pc(
    q,(x,y), lims=(mn,mx),log=opts['--log10'],
    clabel=units, title=title,
    agg=not opts['--show'],
    linthresh=float(opts['--linthresh']),
    linscale=float(opts['--linscale']),
    cmap=opts['--cmap'],);

if opts['--highlight']:
    highlight(
        r, float(opts['--highlight']),
        color="lightyellow", alpha=0.5);

if opts['--target']:
    if opts['--target'] == 'True':
        H = [1.7e21];
    else:
        H = parseit(opts['--target']);
    rgbrx_s = r"\( *(?:{numrx} *, *){{{rep1}}}{numrx} *,{{0,1}} *\)".format(
        rep1=2,
        numrx=fltrx_s);
    crx_s = "(?:{srx}|{rgbrx})".format(srx=srx_s,rgbrx=rgbrx_s);

    C = parseit(opts['--targetc'], rx=crx_s, parsef=parse_ctuple, quote=True);
    Q = parseit(opts['--targetq'], rx=srx_s, parsef=parse_stuple, quote=True);
    A = parseit(opts['--targeta']);
    def lenl(I):
        if len(I) == 1:
            I = I*len(H);
        return I;
    C = lenl(C);
    Q = lenl(Q);
    A = lenl(A);
    for h,c,l,a in zip(H,C,Q,A):
        cq=d[l];
        if opts['--blur']:
            offx=(d[l].shape[0]-q.shape[0])/2
            offy=(d[l].shape[1]-q.shape[1])/2
            cq = cq[offx:-offx,offy:-offy];
        highlight(r, h, q=cq, color=c, alpha=a);
gm = lambda itr: np.sqrt(itr['ux']**2+itr['uy']**2+itr['uz']**2+1);
massE = float(opts['--traj-mass']);
if opts['--traj']:
    tr[ylabel]*=1e4;
    tr['x']*=1e4;
    if opts['--traj-energy']:
        en = lambda itr:np.nan_to_num(massE*(gm(itr)-1));
        if opts['--traj-E-log']:
            minE = float(opts['--traj-minE']);
            def _energy(itr):
                E = en(itr);
                return np.log10(
                    np.where(E < minE, minE, E));
            energy=_energy;
        else:
            energy=en;
        #find max energy
        if opts['--traj-maxE']:
            maxE=float(opts['--traj-maxE']);
            def _cf(itr):
                E = energy(itr);
                return np.where(E>maxE, 1.0, E/maxE)
            cf = _cf;
        else:
            maxE=np.max(energy(tr));
            cf = lambda itr: energy(itr)/maxE;
    else:
        cf = None;
    #massaging alpha
    maxq=np.max(np.abs(tr['q'])[0,:]);
    if opts['--traj-qinvpow']:
        p = 1.0/float(opts['--traj-qinvpow']);
        alphaf = lambda itr: (np.abs(itr['q'])[0]/maxq)**p
    else:
        alphaf = lambda itr: np.abs(itr['q'])[0]/maxq
    trajectories(
        r, tr,
        alpha=alphaf,
        lw=0,
        coords = [ylabel,'x'],
        cmap   = 'copper',
        #simple = True,
        color_quantity=cf);

import matplotlib.pyplot as plt;
if opts['--equal']:
    plt.axis('equal');
    r['axes'].autoscale(tight=True);
if opts['--show']:
    plt.show();
else:
    plt.savefig(opts['<outname>']);
<commit_msg>trying a power, correctly<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
'''
Just render EM something.

Usage:
    ./EM.py [options] (--show|-s) <i>
    ./EM.py [options] <i> <outname>

Options:
    --help -h
    --show -s          Show
    --verbose -v       Make some noise.
    --nozip -U         flds are NOT gzipped.
    --zip   -Z         flds are gzipped. If neither of these two are set,
                       guess based on name.
    --log10 -l         Log it.
    --lims=LIM         Set lims [default: (1e2,6e8)]
    --highlight=H      Set highlight for field quantity.
    --quantity=Q -Q Q  Render this quantity. Quantities are E_norm, E_energy,
                       B_norm, B_energy, EM_energy, and S. [default: E_energy]
    --dir=D -D D       Read from this dir [default: .]
    --restrict=R       Restrict it.
    --x-restrict=R     Restrict by positions as a 4 tuple.
    --target=D -t D    Plot contours of this density. If D is a list,
                       plot multiple contours.
    --targetc=C        Set these colors for the contours. If C is a list,
                       plot these colors. [default: darkred]
    --targeta=A        Set the target alphas. If A is a list
                       plot multiple alphas. [default: 0.15]
    --targetq=Q        Set the target quantity. If Q is a list
                       plot multiple quantities. [default: RhoN10]
    --linthresh=L      Set the linear threshold for SymLogPlot [default: 1e7]
    --linscale=L       Set the linear threshold for SymLogPlot [default: 1e7]
    --cmap=CMAP        Set the colormap. [default: viridis]
    --equal -E         Make spatial dimensions equal.
    --blur=R           Blur with this radius.
    --t-offset=T       Set time offset in fs. [default: 0].
    --traj=F           Plot trajectories from this file. If not used,
                       will not plot trajectories.
    --traj-offset=O    Set the offset and factor to get the relevant
                       trajectory timestep such that i_t = factor*i+offset, where
                       i is the passed index in <i>. The factor and offset are passed
                       to this option in the form of a tuple (factor, offset). If not
                       used, script will search for the closest time in the trajectory
                       file.
    --traj-tail=T      Set the "length" in time of the tail. Accepts "inf" which means for
                       all times, otherwise subtract from the time determined by other
                       options and move back that many steps in fs. [default: inf]
    --traj-n=N         Plot only first N trajectories. Pass a 3-tuple in order to slice.
    --traj-energy      Color the trajectory lines by their energy.
    --traj-mass        Set the rest mass of the pmovie particles. [default: 0.511e6]
    --traj-E-log       Logarithmic color for trajectories.
    --traj-maxE=E      Set the maximum E in eV explicitly. If set, anything above
                       will be cut off.
    --traj-minE=E      Set the minimum E in eV. [default: 1]
    --traj-newfmt      Use the new trajectory format.
    --traj-qinvpow     Use an inverse power for the charge to alpha instead of
                       linear easing. [default: 2.0]
'''
from docopt import docopt;
import numpy as np;
import numpy.linalg as lin;
from pys import parse_ftuple, parse_ituple, parse_ctuple, parse_stuple;
from pys import fltrx_s, srx_s, rgbrx_s, quote_subs
from lspreader.flds import read_indexed, restrict
from lspplot.sclr import S, E_energy,B_energy,EM_energy, vector_norm, smooth2Dp;
from lspplot.pc import pc, highlight, trajectories;
from lspplot.consts import c,mu0,e0;
import re;

opts = docopt(__doc__,help=True);
if opts['--nozip']:
    gzip = False;
elif opts['--zip']:
    gzip = True;
else:
    gzip = 'guess';

def parseit(s,rx=fltrx_s,parsef=parse_ftuple,quote=False):
    ''' helper for parsing tuples '''
    #this needs to be match! not search to not match parens.
    if re.match(rx, s):
        if quote:
            s = quote_subs(s);
        return [eval(s)]
    else:
        return parsef(s, length=None);

quantity = opts['--quantity'];
quantities = dict(
    E_norm=dict(
        fvar=['E'],
        read= lambda d: vector_norm(d,'E')*1e5,
        title="Electric Field Norm",
        units="V/m"),
    E_energy={
        'fvar':['E'], 'read': E_energy,
        'title': "Electric Field Energy",
        'units': "J / cc"},
    B_norm=dict(
        fvar=['B'],
        read= lambda d: vector_norm(d,'B'),
        title="Magnetic Field Norm",
        units="gauss"),
    B_energy={
        'fvar':['B'], 'read': B_energy,
        'title': "Magnetic Field Energy",
        'units': "J / cc"},
    EM_energy={
        'fvar':['E','B'], 'read': EM_energy,
        'title': "Electromagnetic Field Energy",
        'units': "J / cc"},
    S={
        'fvar':['E','B'], 'read': S,
        'title': "Poynting Vector Norm",
        'units': "W / cm$^2$"},
)
def mkread(s,N):
    '''lambda's aren't great.'''
    def _f(d):
        return d[s]*N;
    return _f;
quantities.update({ '{}{}'.format(field,comp):dict(
    fvar = [field],
    title='{} field {} component'.format(field,comp),
    units=unit,
    read = mkread('{}{}'.format(field,comp),scale))
  for field,unit,scale in [('E','V/m',1e5),('B','gauss',1.0)]
  for comp in ['x','y','z'] });

if quantity not in quantities:
    print("quantity is not one of {}".format(quantities.keys()));
    quit();
fvar=quantities[quantity]['fvar']
read=quantities[quantity]['read']
titlestr=quantities[quantity]['title']
units=quantities[quantity]['units']

if opts['--target']:
    svar = parseit(opts['--targetq'], rx=srx_s,parsef=parse_stuple,quote=True)
    #unique
    svar = list(set(svar));
else:
    svar = None;
#####################################
#reading data
d = read_indexed(int(opts['<i>']),
    flds=fvar,sclr=svar,
    gzip=gzip,dir=opts['--dir'],
              gettime=True,vector_norms=False);
t  = d['t'];
#choosing positions
ylabel =  'z' if np.isclose(d['y'].max(),d['y'].min()) else 'y';
if opts['--x-restrict']:
    res = parse_ftuple(opts['--x-restrict'], length=4);
    res[:2] = [ np.abs(d['x'][:,0]*1e4 - ires).argmin() for ires in res[:2] ];
    res[2:] = [ np.abs(d[ylabel][0,:]*1e4 - ires).argmin() for ires in res[2:] ];
    #including the edges
    res[1]+=1;
    res[3]+=1;
    restrict(d,res);
elif opts['--restrict']:
    res = parse_ituple(opts['--restrict'],length=None);
    restrict(d,res);

#trajectories
if opts['--traj']:
    factor, offset = None, None;
    if opts['--traj-offset']:
        factor, offset = parse_ituple(opts['--traj-offset'],length=2);
    with np.load(opts['--traj'], mmap_mode='r') as f:
        if factor:
            tri = i*factor+offset;
            trt = f['time'][tri];
            if not np.isclose(trt,t):
                import sys
                sys.stderr.write(
                    "warning: time from trajectory is {} while time from sclr is {}\n".format(
                        trt,t));
        else:
            tri = np.sum((f['time'] <= t).astype(int));
            trt = f['time'][tri];
        tri_start=None;
        if opts['--traj-tail'] != 'inf':
            tail = float(opts['--traj-tail'])*1e-15*1e9;
            tri_start = np.sum((f['time'] <= t-tail).astype(int));
        pn_end=None;
        pn_start=0;
        pn_step=1;
        if opts['--traj-n']:
            if re.match('^[0-9]+$',opts['--traj-n']):
                pn_end=int(opts['--traj-n']);
            else:
                pn_start,pn_end,pn_step=parse_ituple(opts['--traj-n'],length=3);
        if not opts['--traj-newfmt']:
            tr = f['data'][tri_start:tri+1, pn_start:pn_end:pn_step];
        else:
            keys = list(f.keys());
            ps = len(keys) - 1;
            #magic, think about it
            sz = len(keys[0] if keys[0] != 'time' else keys[1])
            fmt ='{{:0{}}}'.format(sz);
            if pn_end is None:
                pn_end = ps;
            else:
                pn_end = min(ps,pn_end);
            tr = np.array([
                f[fmt.format(i)][tri_start:tri+1] for i in range(pn_start,pn_end,pn_step) ]).T
    if opts['--verbose']:
        print("size of trajectories: {}".format(tr.shape));
        print("final time is {}".format(trt));
        print("with sclr time as {}".format(t));
    pass;

####################################
#massaging data
x,y = d['x']*1e4,d[ylabel]*1e4
q = read(d);

if opts['--blur']:
    rad = float(opts['--blur']);
    w=rad*6;
    q,x,y = smooth2Dp(
        q, (x,y), [0.1,0.1], [0.6,0.6]);

#####################################
#plotting

#getting options from user
mn,mx = parse_ftuple(opts['--lims'],length=2);

#plot the density
toff=float(opts['--t-offset']);
title="{}\nTime: {:.2f} fs".format(titlestr,t*1e6+toff);
r=pc(
    q,(x,y), lims=(mn,mx),log=opts['--log10'],
    clabel=units, title=title,
    agg=not opts['--show'],
    linthresh=float(opts['--linthresh']),
    linscale=float(opts['--linscale']),
    cmap=opts['--cmap'],);

if opts['--highlight']:
    highlight(
        r, float(opts['--highlight']),
        color="lightyellow", alpha=0.5);

if opts['--target']:
    if opts['--target'] == 'True':
        H = [1.7e21];
    else:
        H = parseit(opts['--target']);
    rgbrx_s = r"\( *(?:{numrx} *, *){{{rep1}}}{numrx} *,{{0,1}} *\)".format(
        rep1=2,
        numrx=fltrx_s);
    crx_s = "(?:{srx}|{rgbrx})".format(srx=srx_s,rgbrx=rgbrx_s);

    C = parseit(opts['--targetc'], rx=crx_s, parsef=parse_ctuple, quote=True);
    Q = parseit(opts['--targetq'], rx=srx_s, parsef=parse_stuple, quote=True);
    A = parseit(opts['--targeta']);
    def lenl(I):
        if len(I) == 1:
            I = I*len(H);
        return I;
    C = lenl(C);
    Q = lenl(Q);
    A = lenl(A);
    for h,c,l,a in zip(H,C,Q,A):
        cq=d[l];
        if opts['--blur']:
            offx=(d[l].shape[0]-q.shape[0])/2
            offy=(d[l].shape[1]-q.shape[1])/2
            cq = cq[offx:-offx,offy:-offy];
        highlight(r, h, q=cq, color=c, alpha=a);
gm = lambda itr: np.sqrt(itr['ux']**2+itr['uy']**2+itr['uz']**2+1);
massE = float(opts['--traj-mass']);
if opts['--traj']:
    tr[ylabel]*=1e4;
    tr['x']*=1e4;
    if opts['--traj-energy']:
        en = lambda itr:np.nan_to_num(massE*(gm(itr)-1));
        if opts['--traj-E-log']:
            minE = float(opts['--traj-minE']);
            def _energy(itr):
                E = en(itr);
                return np.log10(
                    np.where(E < minE, minE, E));
            energy=_energy;
        else:
            energy=en;
        #find max energy
        if opts['--traj-maxE']:
            maxE=float(opts['--traj-maxE']);
            def _cf(itr):
                E = energy(itr);
                return np.where(E>maxE, 1.0, E/maxE)
            cf = _cf;
        else:
            maxE=np.max(energy(tr));
            cf = lambda itr: energy(itr)/maxE;
    else:
        cf = None;
    #massaging alpha
    maxq=np.max(np.abs(tr['q'])[0,:]);
    if opts['--traj-qinvpow']:
        p = 1.0/float(opts['--traj-qinvpow']);
        alphaf = lambda itr: (np.abs(itr['q'])[0]/maxq)**p
    else:
        alphaf = lambda itr: np.abs(itr['q'])[0]/maxq
    trajectories(
        r, tr,
        alpha=alphaf,
        lw=0,
        coords = [ylabel,'x'],
        cmap   = 'copper',
        #simple = True,
        color_quantity=cf);

import matplotlib.pyplot as plt;
if opts['--equal']:
    plt.axis('equal');
    r['axes'].autoscale(tight=True);
if opts['--show']:
    plt.show();
else:
    plt.savefig(opts['<outname>']);
<|endoftext|>


<commit_before>"""
Uploading a text file to scribd.com.
"""

import time
import logging
import tempfile
import scribd

last_bytes_sent = 0
def progress(bytes_sent, bytes_total):
    print("%s of %s (%s%%)" % (bytes_sent, bytes_total, bytes_sent*100/bytes_total))

def upload(filepath, API_KEY, API_SECRET):
    # Configure the Scribd API.
    scribd.config(API_KEY, API_SECRET)
    doc_id = None
    try:
        # Upload the document from a file.
        doc = scribd.api_user.upload(
            open(filepath,'rb'),
            progress_callback=progress,
            req_buffer = tempfile.TemporaryFile()
            )
        # Poll API until conversion is complete.
        while doc.get_conversion_status() != 'DONE':
            # Sleep to prevent a runaway loop that will block the script.
            time.sleep(2)
        doc_id = doc.id
        return doc_id
    except scribd.ResponseError, err:
        print 'Scribd failed: code=%d, error=%s' % (err.errno, err.strerror)
        return err.strerror

if __name__ == '__main__':
    main()
<commit_msg>Moved these functions into prr<commit_after><|endoftext|>


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

"""Change the current working directory.
"""

from __future__ import print_function

import argparse
import os
import sys

def main(args):
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("dir", action="store", nargs="?",
                   default=os.environ["HOME2"], type=str,
                   help="the new working directory")
    ns = p.parse_args(args)

    status = 0

    try:
        if os.path.exists(ns.dir):
            if os.path.isdir:
                # chdir does not raise exception until listdir is called, so check for access here
                if os.access(ns.dir, os.R_OK):
                    os.chdir(ns.dir)
                else:
                    print('cd: {} access denied'.format(ns.dir))
            else:
                print('cd: %s: Not a directory' % ns.dir)
        else:
            print ('cd: %s: No such file or directory' % ns.dir)
    except Exception as err:
        print("cd: {}: {!s}".format(type(err).__name__, err), file=sys.stderr)
        status = 1

    sys.exit(status)

if __name__ == "__main__":
    main(sys.argv[1:])
<commit_msg>Fixed a function call bug in cd.py<commit_after>#!/usr/bin/env python
########################################################################.......

"""Change the current working directory.
"""

from __future__ import print_function

import argparse
import os
import sys

def main(args):
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("dir", action="store", nargs="?",
                   default=os.environ["HOME2"], type=str,
                   help="the new working directory")
    ns = p.parse_args(args)

    status = 0

    try:
        if os.path.exists(ns.dir):
            if os.path.isdir(ns.dir):
                # chdir does not raise exception until listdir is called, so check for access here
                if os.access(ns.dir, os.R_OK):
                    os.chdir(ns.dir)
                else:
                    print('cd: {} access denied'.format(ns.dir))
            else:
                print('cd: %s: Not a directory' % ns.dir)
        else:
            print ('cd: %s: No such file or directory' % ns.dir)
    except Exception as err:
        print("cd: {}: {!s}".format(type(err).__name__, err), file=sys.stderr)
        status = 1

    sys.exit(status)

if __name__ == "__main__":
    main(sys.argv[1:])
<|endoftext|>