The goal of efficiency is more slack.

Sunday, October 13, 2019

Installing GStreamer Development for Visual Studio 2019

Install and create template.
  1. Browse to https://gstreamer.freedesktop.org/ > Downloads.
  2. Download and install both MinGW 64-bit runtime installer and development installer.
  3. In Visual Studio 2019, open \gstreamer\1.0\x86_64\share\vs\2010\gst-template\gst-template.vcxproj
  4. Project > Export Template.
 Run example.
  1. Download https://gitlab.freedesktop.org/gstreamer/gst-docs/
  2. Expand tutorials folder.
  3. Open tutorials.sln.
  4. Set solution platform to x64.
  5. Solution Explorer > Solution ''tutorials' > basic-tutorial-1 > Properties
    1. Debugging > Working Directory: $(GSTREAMER_1_0_ROOT_X86_64)\bin
      Note: To get the variable name, in cmd.exe, enter: set gstreamer
    2. Linker > Input > Ignore All Default Libraries > No (or delete setting)
    3. For Debug configuration, Ignore Specific Default Libraries: msvcrtd.lib
      Note: The gstreamer libraries were apparently built with /MD and not /MDd.

Friday, October 11, 2019

Fix route to VM after Cisco VPN removes them

Cisco AnyConnect VPN modifies/removes routes to the bridged network adapter on a VirtualBox VM when you log in. To connect to the VM again, you need to log off the VPN, as the Cisco VPN has a watchdog that reverts any route changes while the VPN is active. After logging off, run this reroute script:

#!/bin/bash

guestip="192.168.58.3"
guestmac="8:0:27:35:55:1d"

# The following 2 deletions are not always needed
# route -n delete ${guestip%.*}.0
# route -n delete $guestip/32

route -n add $guestip/32 -interface vboxnet0
arp -s $guestip $guestmac
netstat -rnf inet | grep 192

Thursday, October 10, 2019

VirtualBox Windows XP Guest File Sharing

How to share files hosted on a Windows XP VirtualBox guest on a Mac OS X host to a real Windows XP computer.
  1. Plug in Thunderbolt Ethernet adapter to host Macbook Air.
  2. Connect Ethernet cable between client computer and host Mac.
  3. Mac Host > Network Preferences > Thunderbolt Ethernet
    1. Configure IPv4: Manually
    2. IP Address: 10.11.0.1
    3. Subnet Mask: 255.255.0.0
  4. VM Settings > Network settings > Adapter [n]
    1. Attached to: Bridged Adapter
    2. Name: en3: Thunderbolt Ethernet
  5. Guest VM > Network Connections > Wired network > Properties >
    1. Internet Protocol (TCP/IP)
      1. Check Use the following IP Address or Alternate Configuration
      2. IP Address: 10.11.0.2
      3. Subnet Mask: 255.255.0.0
    2. Advanced > Windows Firewall > Settings > Exceptions > Check File and Print Sharing.
  6. Client computer > Network  Connections > Wired network > Properties > Internet Protocol (TCP/IP)
    1. Check Use the following IP Address or Alternate Configuration 
    2. IP Address: 10.11.0.3
    3.  Subnet Mask: 255.255.0.0
  7. (Optional test) Ping from each computer to the others.
  8. Guest VM > Share a folder
    1. Make folder C:\share
    2. Right click folder > Properties
    3. Check Share folder on network.
  9. Client computer > My Network Places > share on Guest VM
  10. Done

Tuesday, September 17, 2019

Sites for Django

How to use the Sites package for Django.

Follow instructions in documentation to install the Sites package. Then create a migration in one of your apps.

from __future__ import unicode_literals

from django.db import migrations

def set_site_name(apps, schema_editor):
    Site = apps.get_model('sites', 'site')
    try:
        site = Site.objects.get(id=1)
    except Site.DoesNotExist:
        site = Site()
    site.name = "Astrobiology"
    site.domain = "astrobiology.nasa.gov"
    site.save()

class Migration(migrations.Migration):

    dependencies = [
        ('sites', '0002_alter_domain_unique'),
    ]

    operations = [
        migrations.RunPython(set_site_name),
    ]


Enter the latest migration from the "sites" app as a dependency to ensure there is a database table for sites.

On the development computer, run ./manage shell and change the domain of the site object to 127.0.0.1:8000.

Wednesday, September 11, 2019

List links in HTML files

This Python script walks through a directory of HTML files and extracts all the links. It outputs one line per link with each line also containing the pages in which the link appears.

import os
from os.path import join, getsize
from bs4 import BeautifulSoup
from urllib.parse import urlparse

# Links as keys, list of pages where the link appears as values.
links = {}

def get_links(filepath):
    with open(filepath, 'r') as fhandle:
        try:
            text = BeautifulSoup(fhandle, features="lxml")
        except UnicodeDecodeError:
            print()
            print("UnicodeDecodeError: " + filepath)
            return
        # Iterate through all the "a" tags
        for link in text.find_all("a"):
            url = link.get('href')
            # Ignore relative links and certain domain names.
            if url and urlparse(url).netloc \
                and urlparse(url).netloc not in ["example.com"]:
                # Append link to dictionary.
                if url not in links:
                    links[url] = []
                # Limit number of pages to associate with link.
                if len(links[url]) < 8:
                    links[url].append(filepath)

file_count = 0
fout = open("links.txt", "w")
os.chdir("../flattened")
# Iterate through all the files in the flattened site
for root, dirs, files in os.walk('.'):
    for f in files:
        if f not in [".DS_Store"]:
            get_links(join(root, f))
            print(".", end="", flush=True)
            file_count += 1
    # Stop after some files
    # if file_count > 100: break
print()

# Write file with links.
for link, pages in links.items():
    fout.write(link + ", " + str(pages) + "\n")
fout.close()

Thursday, June 20, 2019

Regular expression to match URLs

Here is a regular expression to match URLs, from http to the end.

(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?

Source: https://stackoverflow.com/a/9284473/199959

Note: Does not match some IP addresses.

Sunday, June 16, 2019

FeinCMS installation

Following directions at https://feincms-django-cms.readthedocs.io/en/stable/installation.html, I ran into some problems, probably due to a Django version mismatch. I'm using Django 1.11.x. This second tutorial helped fill in some blanks: https://www.digitalocean.com/community/tutorials/how-to-install-and-get-started-with-feincms . Below are my additions to the first documentation link.

Don't add context processors to the default set Django startproject created.

Create a folder for templates. Add it to settings.py:
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'my_project/templates'),
        ],

Create a template base.html with at least the following:
{% block content %} {% for content in feincms_page.content.main %} {{ content.render }} {% endfor %} {% endblock %}

Create a new Django project in VSCode

  • Go to user settings (cmd-,):
"python.venvPath": "~/.virtualenvs",
  • Quit VSCode.
  • Open a Python file in VSCode.
  • Select virtualenv in bottom bar.
  • Debug > Open Configurations to create launch.json. Otherwise, "Add Configurations" may not work.
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Server",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver",
                "--noreload",
            ],
            "debugOptions": [
                "RedirectOutput",
                "Django"
            ]
        },
        {
            "name": "Script",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runscript",
                "$(basename -s .py ${file})"
            ],
            "debugOptions": [
                "RedirectOutput",
                "Django"
            ]
        }
    ]
}
  • Click button on bottom right to Install Linting.

Install Django on a development Mac

Notes on installing a Django development environment on a Mac.

Install Python

  • Install Python 3 from www.python.org.
  • Run Install Cerltificates.command.
  • Install pip and virtualenv
pip3 install --upgrade pip
pip3 install virtualenvwrapper
Put virtualenvwrapper in .bash_profile:
# Virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper_lazy.sh

Create a Python virtual environment

mkvirtualenv -p /usr/bin/python2 my_proj
cd ~/Documents/my_proj
setvirtualenvproject
-p is optional

Install Visual Studio Code

  • Install shell command: Cmd-Shift-p > shell command
  • Install Python extension.

Install Postgres


Install Django

pip install Django

Create directory

cd to the parent directory of the directory you wish to place the source files.
Create directory for source files with the Django startapp command.
django-admin startproject my_proj
cd my_proj
setvirtualenvproject

Create app

mkdir my_proj/my_form
./manage.py startapp my_form my_proj/my_form

Configure for VSCode

code . 
Create a new Django project in VSCode

Initialize git

git init
git add .
If .pyc files were uninentionally added:
git rm --cached '**.pyc'

Create .gitignore

touch .gitignore
code .gitignore
Input:
*.pyc

Static files

STATIC_URL: URL (relative to domain root) to trigger searching for static files.
STATICFILES_DIRS: List of directories in which to look for static files when collectstatic is run.
STATIC_ROOT: Django serves files from this directory when run under development mode. Also, collectstatic collects static files here. Upload this to server.

In development: project/static, sibling to project/manage.py
On production: /home/user/domain.com/public/static

settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'public/static/')   

Friday, March 22, 2019

Django in a subdirectory

How to serve a Django project as a subdirectory of another Django project's URL. For example, the first Django site is at www.example.com/ and the second site is at www.example.com/second-site/.

Software Used

  • Nginx
  • Uwsgi 2
  • Django

Procedure

yum install uwsgi-router-rewrite

Add route-uri to uwsgi configuration /etc/uwsgi.d/example.ini
 [uwsgi]

# Python
plugin = python36, router_rewrite

# Django
chdir = /srv/sites/example
module = example.wsgi
home = /srv/.virtualenvs/example
route-uri = ^/second-site(.*) rewrite:$1

# Process
master = true
processes = 5
socket = /run/uwsgi/example.sock
vacuum = true
procname-prefix-spaced = example
Nginx configuration
server {
...
    location /
second-site/static {
        access_log off;
        alias /
var/www/second-site/static;
    }

    location /second-site/ {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/example.sock;
    }
Configure Django to prefix the subdirectory name to redirects. In settings.py:
FORCE_SCRIPT_NAME = '/second-site'

STATIC_URL = FORCE_SCRIPT_NAME + '/static/'
STATIC_ROOT = '/var/www/
second-site/static'

MEDIA_URL = FORCE_SCRIPT_NAME + '/media/'
ADMIN_MEDIA_PREFIX = FORCE_SCRIPT_NAME + '/admin_media/'
SESSION_COOKIE_PATH = FORCE_SCRIPT_NAME
Reference: https://stackoverflow.com/questions/44987110/django-in-subdirectory-admin-site-is-not-working
(TODO)

Thursday, March 21, 2019

Tuesday, January 22, 2019

Backing up Windows 8.1 with GPartEd and Clonezilla

When I get a new Windows computer, I back up the OS so that I can revert to it should OS updates or other software make it fail in ways that are too complex for me to untangle. I generally don't trust bundled backup programs because they often don't back up system files in a predictable way or the restore process takes too long. After backing up, I run the latest OS updates and back up again.
  1. Disable hibernation:
    Running cmd as administrator, enter,
    powercfg /h off
  2. Disable swap:
    Control Panel > System > Advanced System Settings > Advanced tab > Performance > Settings > Advanced tab > Virtual Memory > Change... >
    • Uncheck "Automatically manage".
    • Select "No paging file" for partitions to be backed up.
    • Click "Set".
  3. Re-partition drive:
    • Note: For the initial backup, do not execute this step and instead back up the entire disk to capture the factory restore partition if it exists.
    • Run GPartEd live CD and re-partition the drive to create a partition for user documents and data if one doesn't exist already
  4. Move user docs:
    • In Windows Explorer, open C:\Users and Documents\[Your User].
    • Move user folders (Documents, Downloads, Favorites, Music, Pictures, Videos, etc.) to the newly created user partition using Properties > Location tab.
  5. Clone partitions:
    Run Clonezilla with image mode (as opposed to partition mode).
To restore:
  1. Clone partitions back to drive.
  2. Re-enable swap:
    Virtual Memory > Select "System manage size".
  3. Re-enable hibernation:
    powercfg /h on

Popular Posts

Recent Posts

Unordered List

Text Widget

Pages

Powered by Blogger.
Scroll To Top