Skip to content

Instantly share code, notes, and snippets.

@SimplyAhmazing
Last active December 28, 2016 21:42
Show Gist options
  • Save SimplyAhmazing/d26edd9071a492c4579441f894d5c01e to your computer and use it in GitHub Desktop.
Save SimplyAhmazing/d26edd9071a492c4579441f894d5c01e to your computer and use it in GitHub Desktop.

API Builders script source,

import json
import os
import random
import sys
import subprocess
import time


script_tag = "[OT API Builder] "
script_tab = "                 "


def build(tag):
    print(script_tag + "Starting API version build")
    print(script_tab + "tag: {}".format(tag))
    name = "opentrons" + tag.replace('.', '')
    print(script_tab + "Building API with name: {}".format(name))
    old_cwd = os.getcwd()
    os.chdir('./api')
    print(script_tab + "Changed CWD to: {}".format(os.getcwd()))
    print(script_tab + "Renaming api import from opentrons to: {}".format(name))

    cur_branch = get_current_tag()
    checkout_branch(tag)
    print(script_tag + 'CUR BRANCH IS: ', get_current_tag())

    print(script_tag + 'Renaming API')
    rename_api(name)

    print(script_tag + 'Running API tests to validate renaming')
    run_tests()

    print(script_tag + 'Installing API locally via pip')
    pip_install()

    print(script_tag + 'Cleaning up repo..')
    git_clean()

    print(script_tag + 'Successfully built API version: {}'.format(tag))


def run_tests():
    subprocess.call([
        "nosetests -s --logging-level=CRITICAL tests"
    ], shell=True)


def pip_install():
    subprocess.call([
        "pip install ."
    ], shell=True)


def rename_api(new_name):
    rename_imports_tmpl = "grep -E \"(from|import) ? opentrons\" . -rl | xargs sed -i \"\" 's/opentrons/{new_name}/'"
    rename_imports_cmd = rename_imports_tmpl.format(new_name=new_name)
    print('running cmd: ',rename_imports_cmd)
    subprocess.call([rename_imports_cmd], shell=True)

    rename_setup_tmpl = "ls setup.* | xargs -n 1 sed -i \"\" 's/opentrons/{new_name}/'"
    rename_setup_cmd = rename_setup_tmpl.format(new_name=new_name)
    subprocess.call([rename_setup_cmd], shell=True)

    manifest_tmpl = "echo MANIFEST.in | xargs -n 1 sed -i \"\" 's/opentrons/{new_name}/'"
    manifest_cmd = manifest_tmpl.format(new_name=new_name)
    subprocess.call([manifest_cmd], shell=True)

    subprocess.call([
        "mv opentrons {}".format(new_name)
    ], shell=True)


def get_current_tag():
    label = subprocess.check_output(["git", "describe", "--tag"])
    return label.decode().strip()


def checkout_branch(branch, create=False):
    call_args = ["git", "checkout", "-b", branch]
    if not create:
        del call_args[2]
    subprocess.call(call_args)


def delete_branch(branch):
    subprocess.call(["git", "branch", "-d", branch])


def git_clean():
    subprocess.call(["git", "checkout", "."])
    subprocess.call(["git", "clean", "-f", "-d"])


if __name__ == '__main__':
    build(sys.argv[1])

Invoking above API script builder against all 2.0* releases of the API results in,

$ git --git-dir ./api/.git tag -l | grep 2.0 | xargs -n 1 python builder.py
[OT API Builder] Starting API version build
                 tag: 2.0.0
                 Building API with name: opentrons200
                 Changed CWD to: /Users/ahmedabdalla/Code/opentrons/api-builder/api
                 Renaming api import from opentrons to: opentrons200
Note: checking out '2.0.0'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 86b3a7f... added gitignore
[OT API Builder] CUR BRANCH IS:  2.0.0
[OT API Builder] Renaming API
running cmd:  grep -E "(from|import) ? opentrons" . -rl | xargs sed -i "" 's/opentrons/opentrons200/'
[OT API Builder] Running API tests to validate renaming
..................................................................................................................................................
----------------------------------------------------------------------
Ran 146 tests in 2.895s

OK
[OT API Builder] Installing API locally via pip
Processing /Users/ahmedabdalla/Code/opentrons/api-builder/api
Requirement already satisfied (use --upgrade to upgrade): pyserial==3.2.1 in /Users/ahmedabdalla/.pyenv/versions/3.5.2/envs/api-builder/lib/python3.5/site-packages/pyserial-3.2.1-py3.5.egg (from opentrons200==2.0.0+0.g86b3a7f.dirty)
Installing collected packages: opentrons200
  Running setup.py install for opentrons200 ... done
Successfully installed opentrons200-2.0.0+0.g86b3a7f.dirty
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[OT API Builder] Cleaning up repo..
Removing containers/
Removing opentrons200/
Removing tests/opentrons/containers/opentrons200-data/
[OT API Builder] Successfully built API version: 2.0.0
[OT API Builder] Starting API version build
                 tag: 2.0.1
                 Building API with name: opentrons201
                 Changed CWD to: /Users/ahmedabdalla/Code/opentrons/api-builder/api
                 Renaming api import from opentrons to: opentrons201
Previous HEAD position was 86b3a7f... added gitignore
HEAD is now at c91187b... removed typo and fixed mix description to match code
[OT API Builder] CUR BRANCH IS:  2.0.1
[OT API Builder] Renaming API
running cmd:  grep -E "(from|import) ? opentrons" . -rl | xargs sed -i "" 's/opentrons/opentrons201/'
[OT API Builder] Running API tests to validate renaming
........................................................................................................................................................
----------------------------------------------------------------------
Ran 152 tests in 3.290s

OK
[OT API Builder] Installing API locally via pip
Processing /Users/ahmedabdalla/Code/opentrons/api-builder/api
Requirement already satisfied (use --upgrade to upgrade): pyserial==3.2.1 in /Users/ahmedabdalla/.pyenv/versions/3.5.2/envs/api-builder/lib/python3.5/site-packages/pyserial-3.2.1-py3.5.egg (from opentrons201==2.0.1+0.gc91187b.dirty)
Installing collected packages: opentrons201
  Running setup.py install for opentrons201 ... done
Successfully installed opentrons201-2.0.1+0.gc91187b.dirty
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[OT API Builder] Cleaning up repo..
Removing containers/
Removing opentrons201/
Removing smoothie/
Removing tests/opentrons/containers/opentrons201-data/
[OT API Builder] Successfully built API version: 2.0.1
[OT API Builder] Starting API version build
                 tag: 2.0.2
                 Building API with name: opentrons202
                 Changed CWD to: /Users/ahmedabdalla/Code/opentrons/api-builder/api
                 Renaming api import from opentrons to: opentrons202
Previous HEAD position was c91187b... removed typo and fixed mix description to match code
HEAD is now at aebfcf2... Bumped pinned version of requests in setup.py to be in sync with conda's repo
[OT API Builder] CUR BRANCH IS:  2.0.2
[OT API Builder] Renaming API
running cmd:  grep -E "(from|import) ? opentrons" . -rl | xargs sed -i "" 's/opentrons/opentrons202/'
[OT API Builder] Running API tests to validate renaming
................................................................................................................................................................
----------------------------------------------------------------------
Ran 160 tests in 4.272s

OK
[OT API Builder] Installing API locally via pip
Processing /Users/ahmedabdalla/Code/opentrons/api-builder/api
Requirement already satisfied (use --upgrade to upgrade): dill==0.2.5 in /Users/ahmedabdalla/.pyenv/versions/3.5.2/envs/api-builder/lib/python3.5/site-packages (from opentrons202==2.0.2+0.gaebfcf2.dirty)
Requirement already satisfied (use --upgrade to upgrade): requests==2.12.4 in /Users/ahmedabdalla/.pyenv/versions/3.5.2/envs/api-builder/lib/python3.5/site-packages (from opentrons202==2.0.2+0.gaebfcf2.dirty)
Requirement already satisfied (use --upgrade to upgrade): pyserial==3.2.1 in /Users/ahmedabdalla/.pyenv/versions/3.5.2/envs/api-builder/lib/python3.5/site-packages/pyserial-3.2.1-py3.5.egg (from opentrons202==2.0.2+0.gaebfcf2.dirty)
Installing collected packages: opentrons202
  Running setup.py install for opentrons202 ... done
Successfully installed opentrons202-2.0.2+0.gaebfcf2.dirty
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
[OT API Builder] Cleaning up repo..
Removing containers/
Removing opentrons202/
Removing tests/opentrons/containers/opentrons202-data/
[OT API Builder] Successfully built API version: 2.0.2

Running pip list shows,

pip list                                  ‹6.9.2› ‹api-builder› ‹system› [16:18:37]
dill (0.2.5)
nose (1.3.7)
opentrons200 (2.0.0+0.g86b3a7f.dirty)
opentrons201 (2.0.1+0.gc91187b.dirty)
opentrons202 (2.0.2+0.gaebfcf2.dirty)
pip (8.1.1)
pyserial (3.2.1)
requests (2.12.4)
setuptools (20.10.1)
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

Then in a python REPL do,

python                                    ‹6.9.2› ‹api-builder› ‹system› [16:39:41]
Python 3.5.2 (default, Dec 26 2016, 22:42:27)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from opentrons200 import Robot as Robot200
>>> from opentrons201 import Robot as Robot201
>>> from opentrons202 import Robot as Robot202
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment