Tuesday, March 28, 2017

manipulating pdf files for fun and profit

Ok so you have a pdf file

get the following  tools

1)pdftk
2)mutool (part of mupdf-tools package)


then I need to rotate it 90 degrees

 pdftk in-file.pdf cat 1-endeast output out-file.pdf

this rotated it 90 degrees

Then i needed to split the pdf which had pages 2 on one to one on one:

mutool poster -x 2 input.pdf output.pdf

but sometimes the pages are out of order (even and odd)
so then i can do

pdftk infile.pdf shuffle even odd output outfile.pdf


also useful will be

djvu2pdf by christoph sighart


http://0x2a.at/s/projects/djvu2pdf

which looks like


#!/bin/sh
# (c) Christoph Sieghart 2007 - 2011

VERSION=0.9.2

# Flags for commandline options
S_FLAG=0
C_FLAG=0

trap 'if [ $S_FLAG -eq 1 ]; then clean_temp_dir; clean_cursor; fi' 0 1 2

#*****************************************************************************
# - Functions
#*****************************************************************************

check_print_error() {
    if [ $? -ne 0 ]
    then
        echo "Error: $0: $1"
        exit 1
    fi
}

print_version() {
    echo "djvu2pdf $VERSION - Christoph Sieghart "
}

print_help() {
    print_version
    echo -e "\n  Usage: ./djvu2pdf filename.djvu [filename.djvu] ...\n"
    echo "  -h       Prints this help"
    echo "  -v       Prints the version number"
    echo "  -s       Show status messages (A little bit slower - every page"
    echo "           gets dumped on its own and later recombined"
    echo -e "  -c       Don't use terminal escape sequences to move cursor \n"
    exit 0
}

print_quiet() {
    if [ $S_FLAG -eq 1 ]; then
        echo -en $1
    fi
}

move_cursor() {
    if [ $S_FLAG -eq 1 ]; then
        if [ $C_FLAG -eq 1 ]; then
            echo -ne "\n"
            return
        else
            # cursor magic
            echo -en "\033[35D"
        fi
    fi
}

clean_cursor() {
    # if the cursor 'magic' messes anything up
    if [ $S_FLAG -eq 1 ]; then
        if [ $C_FLAG -eq 0 ]; then
            tput sgr0
        fi
    fi
}

clean_temp_files() {
    rm -rf "$TEMP"/* >/dev/null 2>&1
}

clean_temp_dir() {
    rm -rf "$TEMP" >/dev/null 2>&1
}

#*****************************************************************************
# - Programm checks
#*****************************************************************************

# MacOS and BSD compatibility
which seq >/dev/null 2>&1
if [ $? -eq 0 ]; then
    SEQ=seq
  else
    SEQ=jot
fi

for i in "ddjvu djvulibre" "gs ghostscript"; do
    BINARY=$(echo $i | awk '{print $1'})
    PACKAGE=$(echo $i | awk '{print $2'})
    which $BINARY >/dev/null 2>&1
    check_print_error "$BINARY not found. Install $PACKAGE."
done


#*****************************************************************************
# - Commandline options
#*****************************************************************************

while getopts hvcs opt
do
    case "$opt" in
        v) print_version
        exit 0;;
        h) print_help;;
        c) C_FLAG=1;;
        s) S_FLAG=1;;
        \?) exit 1;;
    esac
done

shift `expr $OPTIND - 1`

if [ $C_FLAG -eq 1 ]; then
    if [ $S_FLAG -eq 0 ]; then
        echo -e "Error: $0: The option -c only makes sense with -s"
        exit 1
    fi
fi

if [ -z "$1" ]; then
    echo -e "Error: $0: No file specified for conversion"
    exit 1
fi

#*****************************************************************************
# - Main loop
#*****************************************************************************

while [ $# -gt 0 ]; do

    if [ ! -f "$1" ]; then
        echo -e "Error: $0: File '$1' not found"
        exit 1
    fi

    FILENAME=$1
    FILEBASE=`basename "$FILENAME" .djvu`
    OUTPUTFILE="${FILEBASE}.pdf"

    if [ $S_FLAG -eq 0 ]; then
        ddjvu -format=pdf "${FILENAME}" "${OUTPUTFILE}" 2> /dev/null
    else

        #*****************************************************************************
        # - Temporary Files
        #*****************************************************************************

        TEMP="${TMPDIR:=/tmp}/djvu2pdf.$$"
        mkdir $TEMP
        check_print_error "Creating temporary directory failed!"

        # Child processes should use our temporary directory
        TMPDIR=$TEMP

        PAGES=`djvudump "$FILENAME" | grep pages | awk '{print $8;}'`
        ZEROCOUNT=$(expr `echo $PAGES | wc -m` - 1)
        COUNT=1
        TENS=1

        print_quiet "Converting $FILENAME to $OUTPUTFILE\n"

        #
        # We dump every page and print the status message


        for COUNT in `$SEQ 1 $PAGES`;
        do
            if [ `expr $COUNT / $TENS` -gt 0 ]
            then
                TENS=`expr $TENS \* 10`
                ZEROCOUNT=`expr $ZEROCOUNT - 1`
                ZEROS=$(for i in `$SEQ 1 $ZEROCOUNT`; do echo -n 0; done)
            fi

            ddjvu -format=pdf -page $COUNT "$FILENAME" "$TEMP/$FILEBASE.${ZEROS}$COUNT.pdf" 2> /dev/null

            print_quiet "Page $COUNT/$PAGES dumped"
            move_cursor
        done

        clean_cursor

        #
        # The pages get combined into one big happy .pdf
        #

        print_quiet "\nDumping finished - writing $OUTPUTFILE\n"

        gs -dSAFER -dQUIET -dBATCH -sDEVICE=pdfwrite -sOutputFile="$OUTPUTFILE" -DNOPAUSE "$TEMP/$FILEBASE".*.pdf
        check_print_error "Error in creating pdf file"

        clean_temp_files
    fi

    shift
done

exit 0

Thursday, January 28, 2016

how to connect an apache cgi-script to postgreql database





How do I connect to a local postgres database without password from a script within Apache
up vote 0 down vote favorite
   

I have a Python script that should load some data into postgres when a POST request is sent to Apache Webserver. In the script a system user (dbuser) is used to connect to the database (which works fine with psql). The script however cannot connect when it is executed within Apache, returning the following error:

Peer authentication failed for user dbuser

Is there a way to allow the script to connect without providing it the user password?
python apache postgresql cgi
shareimprove this question
   
asked Jun 4 '15 at 9:48
a1an
8791329
   
add a comment
1 Answer
active oldest votes
up vote 1 down vote
   

The solution I've found uses ident authentication with user maps.

The first thing to notice is that, although an username is provided in the script, when connecting via Apache, that user is used for peer authentication (which fails, requiring a password). However, the system user requesting access to postgresql is the one running Apache (namely www-data), thus enabling us to configure an user map, allowing is to authenticate to the server as another system user (thus leveraging ident authentication). Here follows the configuration files content:

In pg_ident.conf we configure the user map:

# MAPNAME      SYSTEM-USERNAME         PG-USERNAME
web            www-data                dbuser
web            dbuser                  dbuser

In pg_hba.conf we add the map as an option to the local peer authentication:

# "local" is for Unix domain socket connections only
# TYPE  DATABASE     USER    ADDRESS     METHOD
local   all          all                 peer map=web

After reloading the server, the script can access the database as if it was executed directly the the user "dbuser", without the need for a password.
shareimprove this answer

Saturday, January 2, 2016

too many files in directory need to split into subdirectories with 100 files


#!/usr/bin/perl -w
use strict;
use File::Copy;
my $fn;
my @total= (1 .. 100);
my $dir = shift;
opendir (my $dh , $dir);

my @files = grep { $_ ne '.' && $_ ne '..' } readdir $dh;

my @letters = ("a".."z") ;

MYLOOPS:
foreach my $let1 ( @letters ) {
    foreach my $let2 (@letters)   {
       mkdir "$dir$let1$let2";

for my $count (@total) {
$fn =  pop  @files;
if ( $fn ) {
  move ("$dir/$fn", "$dir$let1$let2/$fn");
} else  {
    last MYLOOPS;
}
   
} }}

Thursday, March 20, 2014


;; this should delete the last line 00:00

(let ((beg (point)))
(forward-line 0)
(delete-region beg (point)))

;;then do this

(goto-char (point-min))

this solves that first problem
then

then use this in autohotkey

make-emacs-top.ahk

SetTitleMatchMode 1
WinActivate emacs
WinSet, AlwaysOnTop, On, emacs
WinSet, Top, ,emacs
Return

Sunday, March 16, 2014

defun get-stuff ()
(interactive)
(shell-command '"c:/user/get.ahk")
(goto-char (point-min))
(delete-region
(re-search-forward "00\:00")
(point-max))
(goto-char 1)
(message " hi there meshulum"))

(defun clean-stuff ()
(interactive)
(goto-char (point-min))
(delete-region
(re-search-forward "00\:00")
(point-max))
(goto-char 1)
(message " hi there user"))

(defun both-stuff ()
(interactive)
(progn
(get-stuff)
(clean-stuff)
))





(defun put-stuff ()
(interactive)
(kill-ring-save (point-min) (point-max))
(shell-command '"c:/ure/put.ahk"))


put.ahk
SetTtileMatchMode 1
text=%Clipboard%
WinActivate Nuance
Send ^a
Send {Del}
Sleep 100
Clipboard = %text%
WinActivate Nuance
Send ^v
Return


get.ahk
SetTitleMatchMode 1
WinWait Nuance
WinGetText, text;
Clipboard = %text%
WinActivate eamcs
Send ^y
Return


Thursday, March 13, 2014

write interactive emacs functin

global-set-key (kbd "C-c C-g")
(lambda ()
(interactive)
(shell-command "/home/user/trial.pl")))


Sunday, March 9, 2014

ideas to capture to emacs and avoid nuance


SetTitleMatchMode 1
WinWait Nuance
WinGetText, text ;
MsgBox, the text is: `n%text%
WinActivate Nuance
Send ^a
Send {Del}
Sleep 100
Clipboard = %text%
WinActivate Nuance

Send ^v
Return

Wednesday, February 26, 2014

stumpwm problem with mouse on gtk3 apps such as evince and other apps


I have been using stumpwm for several years and am very happy and can't contempl
ate otherwise.

I just noticed a change in debian sid.

The new version of evince 3.10 (which is a pdf viewer)
mousewheel no longer scrolls up and down the page while running under stumpwm.

I reported to evince upstream and they denied it as a problem on their end.

I found they are right when i opened up lxde icewm gnome etc evince worked norma
lly.

Note evince 3.4 under stumpwm (debian wheezy) was fine.

It was only when i upgraded 3.10 or even using jhbuild latest 3.11 same behavior
.

Do you have any idea what the change in mouse handling that would affect this ap
plication.
I have not noticed other applications.

is it a change in the xorg?

thus qpdfview works with normal scroll behavior and okular.

I mentioned on #stumpwm irc

where it was confirmed by alezost



other applications
evince
nautilus
eog


-----------

It can be solved by

> GDK_CORE_DEVICE_EVENTS=1 evince file.pdf

will work normally with the mouse

can this be set in the .profile or .bashrc ???? don't know how

Sunday, November 10, 2013

Reformat the output of hebcal to be more useful


want to format the output from hebcal on combined lines like so




#!/usr/bin/perl -w
use strict;
use v5.10;
my @output=`hebcal -dsC 'new york'`;
my %hdata;
chomp @output;
foreach (@output) {
my ($date, $data)= split (/\s/,$_,2);
my ($month,$day,$year)=split(/\//,$date);
my $date=sprintf('%02s/%02s/%d',$month,$day,$year);

if (defined $hdata{$date}){
push( @{$hdata{$date}}, $data);
}
else {
$hdata{$date}=[$data];
}
}

foreach (sort (keys %hdata)) {

say "$_ @{$hdata{$_}}";
}

Thursday, January 24, 2013

CUPS Printer not printing after a cups power cycle: Unable to write print data: Broken pipe


http://www.novell.com/support/kb/doc.php?id=7006889


Situation
Printer errors out upon printing a job. The error results from either an incompatible printing format or some other software related issue. To clear the error and resume printer operation it is necessary to power cycle the printing device.
Once the printer is back online and display a green status light it does not print any job. If the workstation sending the print jobs is rebooted the situation does not change and the printer remains in ready mode. Yet printing from other workstations to the same printer is possible.

Resolution
On the workstation where print jobs never complete edit the file /etc/cups/printers.conf with a text editor like vi. Run the following command:

sudo vi /etc/cups/printers.conf

When prompted enter the administrative password for the system.
Vi will open the file for viewing and editing. At the top of the file you will note the text highlighted in red below.
You will need to edit the first line and change the string to State Idle.
You will need to delete the second line entirely

Below is what the printers.conf file looks like when the printer is not accepting jobs

# Printer configuration file for CUPS v1.3.9
# Written by cupsd on 2010-06-15 00:28

Info Brother HL-1470N BR-Script2
Location My office
DeviceURI socket://192.168.1.55:9100
State Stopped
StateMessage Unable to write print data: Broken pipe
StateTime 1276586906
Accepting Yes
Shared Yes



Below is what the printers.conf file should look like once the editing is complete.

# Printer configuration file for CUPS v1.3.9
# Written by cupsd on 2010-09-03 12:26

Info Brother HL-1470N BR-Script2
Location My office
DeviceURI socket://192.168.1.55:9100
State Idle
StateTime 1283541947
Accepting Yes
Shared Yes


Save your changes and exit vi.
You will need to restart CUPS on the workstation to reload the configuration information from printers.conf. Perform the following command to accomplish this:

sudo /etc/init.d/cups restart

Normal printing operations will resume once the CUPS daemon is restarted.

Thursday, June 7, 2012

How to make a wperl.exe executable in windows from scratch

GUI scripts
version 2.1

The standard perl.exe is a console based application, this is the right
thing usually, but sometimes it might be tedious.

Imagine you have a Tk or Win32::GUI based script, you start it by a
doubleclick on an icon and an ugly empty useless console appears and
stays on screen until you close the script. Not a nice thing huh?

There are several solutions.

Hiding the console
You may hide the console as soon as the script starts.

use Win32::GUI; BEGIN
{Win32::GUI::Hide(scalar(Win32::GUI::GetPerlWindow()))};

There are three problems with this solution.

The first is that the console flashes on screen when you start the
program. Even if you use BEGIN{} to hide it as soon as possible.

Second, if you run such a script from a console, it will hide the
console instead of detaching itself and leaving the console visible and
ready to take other commands.

Now let's think about what to do when the script ends. If it keeps the
console hidden and it was started by a doubleclick on an icon everything
is cool, but if it was started from a command prompt, the prompt is
still running, but it's invisible. This way, you'd run out of memory.

If you on the other hand show the console, it may flash on screen.

Solutions
For some time I advertised a way to prepare a perl.exe that doesn't
create a console. This is no longer needed if you use ActivePerl. The
distribution contains wperl.exe which is just that. If you compiled Perl
yourself you may need to create such an executable yourself. It's easier
than it may seem:

All you have to do is to copy your perl.exe to wperl.exe and run this :

c:\> EDITBIN.EXE /subsystem:windows wperl.exe

I'm using extension .gpl for the scripts that are to be run by wperl,
but this is up to you.

Since the editbin.exe is part of Microsoft's development tools and AFAWK
is not free, Jan wrote the following script that is able to do the same.

Usage:
exetype c:\perl\bin\guiperl.exe WINDOWS

I've tested this both under WinNT4.0 (sp4) and Win95sp1.

An updated version of the script is now installed with ActivePerl as
c:\perl\bin\exetype.bat.

You may use it to "de-consolize" any EXEcutable of course ;-)

Script
This script was written by Jan Dubois . Thanks,
Jan :-)

============================exetype.pl=============================
#!perl
use strict;
unless (@ARGV == 2) {
print "Usage: $0 exefile [CONSOLE|WINDOWS]\n";
exit;
}
unless ($ARGV[1] =~ /^(console|windows)$/i) {
print "Invalid subsystem $ARGV[1], please use CONSOLE or WINDOWS\n";
exit;
}
my ($record,$magic,$offset,$size);
open EXE, "+< $ARGV[0]" or die "Cannot open $ARGV[0]: $!"; binmode EXE; read EXE, $record, 32*4; ($magic,$offset) = unpack "Sx58L", $record; die "Not an MSDOS executable file" unless $magic == 0x5a4d; seek EXE, $offset, 0; read EXE, $record, 24; ($magic,$size) = unpack "Lx16S", $record; die "PE header not found" unless $magic == 0x4550; die "Optional header not in NT32 format" unless $size == 224; seek EXE, $offset+24+68, 0; print EXE pack "S", uc($ARGV[1]) eq 'CONSOLE' ? 3 : 2; close EXE; Automatic setup I've written a script that creates the wperl.exe (if you do not have it already) and sets the file extension mapping for extensions you specify. You may find it at http://Jenda.Krynicky.cz/perl/makeGUIperl.pl.txt You need Win32::Registry2 patch from my pages! http://Jenda.Krynicky.cz Subprocesses If your application doesn't have a console, your subprocesses should not have one either. It would look dirty if a console flicked on the screen every time you use system() or backticks. Since build 632 of ActivePerl you may use this BEGIN { Win32::SetChildShowWindow(0) if defined &Win32::SetChildShowWindow }; to instruct Perl to hide the consoles of such processes. Author The text was written by Jan Krynicky and is based on
discussion on Perl-Win32-Users mailing list, the code is by Jan Dubois
.

Tuesday, April 3, 2012

kprinter equivalents

xpp
gtklp
both work nicely

emacs settings to try

(custom-set-variables
'(lpr-command "gtklp")
'(ps-lpr-command "gtklp")
)

Saturday, January 28, 2012

annoying beep in console

create a file
.inputrc
set bell-style visible

and can use xset b 0
as well
------------------
.emacs file

and how to shut off in emacs?
this will make it visible

(setq visible-bell 1)
;; Now some people find the flashing annoying. To turn the alarm totally off, you can use this:
(setq ring-bell-function 'ignore)

Thursday, November 24, 2011

trouble with texlive-base in sid upgrade

apt-get upgrade or dist-upgrade

I had persistent problems

errors were encountered while processing:
texlive-base
texlive-latex-base
feynmf
texlive-latex-recommended
texlive-fonts-recommended
pdfjam
texlive
texlive-generic-recommended
texlive-luatex
texlive-metapost
texlive-pictures
texmacs

ie the following


Reading database ... 642501 files and directories currently installed.)
Preparing to replace texlive-latex-base 2009-14 (using .../texlive-latex-base_2009-15_all.deb) ...
Unpacking replacement texlive-latex-base ...
Processing triggers for man-db ...
Setting up texlive-base (2009-15) ...
mktexlsr: Updating /var/lib/texmf/ls-R-TEXLIVE...
mktexlsr: Updating /var/lib/texmf/ls-R-TEXMFMAIN...
mktexlsr: Updating /var/lib/texmf/ls-R...
mktexlsr: Done.
Running mktexlsr. This may take some time... done.
Building format(s) --all --cnffile /etc/texmf/fmt.d/10texlive-base.cnf.
This may take some time...
fmtutil-sys failed. Output has been stored in
/tmp/fmtutil.QRFBRZLF
Please include this file if you report a bug.

dpkg: error processing texlive-base (--configure):
subprocess installed post-installation script returned error exit status 1
configured to not write apport reports
dpkg: dependency problems prevent configuration of texlive-latex-base:
texlive-latex-base depends on texlive-base (>= 2009-1); however:
Package texlive-base is not configured yet.
dpkg: error processing texlive-latex-base (--configure):
dependency problems - leaving unconfigured
configured to not write apport reports
dpkg: dependency problems prevent confi

---------------
so what i did was read the bug report


http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=648278
i tried this so let us see what happens with the next attempt to upgrade
Rashi:/home/mlaks# mktexlsr /usr/share/texmf
mktexlsr: Updating /var/lib/texmf/ls-R-TEXMFMAIN...
mktexlsr: Done.
Rashi:/home/mlaks#

This did not help:

then see the remark there about installing all the texlive-base dependencies
at the same time

-----------------
Holger Levsen wrote:

> during a test with piuparts I noticed your package failed to install. As per
> definition of the release team this makes the package too buggy for a
> release, thus the severity.

Thank you, I found the reason - both bug reports seem to point to the
same problem. It does not show up when all dependencies of texlive-base
are installed in the same aptitude or dpkg run, only when they are
configured first and texlive-base is unpacked afterwards.

And of course it is in my new libpaper'ing code...

A fix is in svn, I'll upload soon.

Regards, Frank

-----------
that fixes it


so I did this

apt-get remove --purge texlive-base texlive-common texlive-binaries texlive-doc-base tex-common luatex

(copied down all the stuff being purged and then did)


apt-get install texlive-base texlive-common texlive-binaries texlive-doc-base tex-common luatex

and then we were ok

next step is to reinstall whole batch of files that were removed
:)

Sunday, September 25, 2011

How to get rid of all the rc entries in dpkg -l?

i just did the following and it seems to work

dpkg -l|grep -e ^rc|perl -pe 's/rc\s+(\S+)\s+\d.*/$1/'|xargs apt-get -y remove --purge

use at your own risk!

Thursday, September 8, 2011

oraysa and toratemet interesting project to review at google code

Wednesday, August 17, 2011

how to persist a connection?

ssh
or
vpnc?

ssh
then open a screen session
screen
create new window
^-a c
then split window
^-a S ## capitol S
move to other window
^-a tab
grab the window to the frame
^-a "shift+'" and select a window to place there
then make a busy application
wait -n 10 date
will create a very busy application with connection

How to do screen within screen???
^-a-a
ie
^-a then a regular a
done!!!

Sunday, August 7, 2011

windws focus follows mouse registry hacks

inportant information i keep losing.

http://sinewalker.wordpress.com/2010/03/10/ms-windows-focus-follows-mouse-registry-hacks/

1. Run regedit (Win+R, regedit, OK)
2. Open up the key HKEY_CURRENT_USER\Control Panel\Mouse
3. Change the value of the REG_DWORD ActiveWindowTracking to 0×0000001 (1)

4. Open up the key HKEY_CURRENT_USER\Control Panel\Desktop
5. Add 1 to the MSB (most significant byte) of the REG_BINARY UserPreferencesMask. That is, if the current value is 00 3e 03 80 12 00 00 00, then change it to 01 3e 03 80 12 00 00 00. If you used the GUI as above, the MSB will be 41 (which brings the focused window to the front as well), so change it back to 01.


6. To make the focus a little slower, so that pop-up windows are useable, you also want to change the focus timing. Change the REG_DWORD ActiveWindTrkTimeout (also in HKEY_CURRENT_USER\Control Panel\Desktop) to the number of milliseconds to wait before focus shifts to the window under the mouse. I like a value of 0×00000080 (128ms). You might like 200ms (c8), or some faster or slower value.

So, in summary, the Registry keys are as follows for X11-like window focus:

HKEY_CURRENT_USERS\Control Panel\Mouse
REG_DWORD ActiveWindowTracking = 0x00000001 (1)

HKEY_CURRENT_USERS\Control Panel\Desktop
REG_DWORD ActiveWindTrkTimeout = 0x00000080 (128)
REG_BINARY UserPreferencesMask = 01 .. .. .. .. .. .. .. ..

Sunday, May 22, 2011

letting a remote machine know about urxvt-unicode terminal

REMOTE=remotesystem.domain

infocmp rxvt-unicode | ssh $REMOTE "mkdir -p .terminfo && cat >/tmp/ti && tic /tmp/ti"

works nicely if you cant install urxvt there.

see man for urxvt

Monday, April 18, 2011

checking a downloaded and created cd

ok do a md5sum on the download to check it is ok
then do
ls - filename
this tells you the $size

then do
let $value= $size/2048

dd if=/dev/cdrom bs=2048 count=$value |md5sum
and check this against the file to check you cut the disk right


https://help.ubuntu.com/community/HowToMD5SUM