вторник, 5 июня 2018 г.

To my surprise, pep8 style guide check tool was renamed to pycodestyle in 2016. The package is deprecated now.
Thought autopep8 not.

pep8 has been renamed to pycodestyle (GitHub issue #466)
Use of the pep8 tool will be removed in a future release.
Please install and use `pycodestyle` instead.

пятница, 1 июня 2018 г.

Debug django rest API from shell

It is possible to debug step by step Django rest framework API in pdb/ipdb debugger.
  • Import API on line (1)
  • Replace root@root with valid email of user with correct permissions on line (2)
  • Paste code in shell and run
  • Execution will start on Django internal code. To go to your API use breakpoints.

import ipdb #or pdb
_user = User.objects.get(

email__contains='root@root')#(2)valid user email
from rest_framework.test import APIRequestFactory
from rest_framework.test import force_authenticate
_factory = APIRequestFactory()

_request=_factory.post('', {})
force_authenticate(_request, _user)
from project.apps.users.api import ApiMySettingsView as api # (1)
_view=api.as_view()

responce=ipdb.runcall(_view,_request) #debug
#responce=_view() #just run
vars(responce)


http://www.django-rest-framework.org/api-guide/testing/
https://docs.python.org/3/library/pdb.html

среда, 15 ноября 2017 г.

Automate check for a new Nvidia or Intel drivers

Sometimes you need to know when a new version of graphics driver is available and do not want to install constantly running in background tools from a manufacturer. Like Nvidia experience or Intel driver assist tool. Or you need notifications for the remote machine.

Luckily there is exists packages for Nvidia and Intel in the chocolatey project repository. And they updated pretty quickly. So using API it is possible to poll them for a new version.

https://github.com/kotofos/check-chocolatey-package-version

code example:

import xml.etree.ElementTree as ET
import zipfile
from io import BytesIO
from urllib.request import urlretrieve, urlopen

driver_url_map = {
    'nvidia': 'https://chocolatey.org/api/v2/package/nvidia-display-driver',
    'intel': 'https://chocolatey.org/api/v2/package/intel-graphics-driver',
}


def download(url):
    f = BytesIO()
    response = urlopen(url)
    f.write(response.read())
    return f


def get_current_version(name):
    url = driver_url_map[name]
    archive = download(url)
    zip = zipfile.ZipFile(archive)

    string = zip.read(url.rpartition('/')[-1] + '.nuspec').decode()
    string = "\n".join(string.split("\r\n"))
    root = ET.fromstring(string)

    tag_name = ('{http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd}' 
                'version')
    for elem in root.iter():
        if elem.tag == tag_name:
            return elem.text


if __name__ == '__main__':
    for name in ('nvidia', 'intel'):
        res = get_current_version(name)
        print('version for', name, res)

среда, 4 октября 2017 г.

Install software from chocolatey and cygwin with ansible

It is possible to use chocolatey to manage Windows computers from ansible.

choco install vlc -y works fine in Cygwin. And also over Cygwin's ssh server from ansible.

- name: Check command exist
  shell: command -v procdump

  register: procdump_res

- name: Install procdump
  command: choco install procdump -y

  when: procdump_res.rc == 1

вторник, 18 июля 2017 г.

isatty under pycharm

If you want to print something from python script only on real TTY (terminal), not in a file or redirected stream there is built in check for that: sys.stdout.isatty().
But it will return False for the pycharm console. To detect that script is executed under pycharm add or 'PYCHARM_HOSTED' in os.environ

is_direct_output = sys.stdout.isatty() or 'PYCHARM_HOSTED' in os.environ

понедельник, 3 октября 2016 г.

Shutdown server and wait till the connection is down with ansible

When executing something like "ansible all -m command -a "shutdown now" --ask-pass -b" on ubuntu server 16 ansible will power off the machine, but return the error 

192.168.0.2 | FAILED | rc=0 >>
MODULE FAILURE  
                        


The solution is to add the delay before shutdown command using shell module and sleep.

shutdown.yml:
 
---
- hosts: all
  tasks:
  - name: shutdown
    shell: nohup bash -c "sleep 2s && shutdown now" &
    async: 0
    poll: 0
    ignore_errors: true
    become: true

  - name: wait for server power off
    local_action: wait_for host="{{ inventory_hostname }}"
      state=stopped port=22 delay=10 timeout=300
    become: false


Wait for server fower off task will check that at least ssh on the host is down. But it will not notify if shutdown itself hangs.