autorun doesn't comply with python3

Post in this forum for problems at boot time, if there was no problem for burning the disc. For example can't find the CD, or strange errors when you press enter at the very first prompt.
nvieville
Posts: 17
Joined: 16 May 2008, 14:58
Location: France

autorun doesn't comply with python3

Postby nvieville » 05 May 2017, 13:20

Hello,

While trying to automate some tasks on SystemRescueCD via autoruns, I found that a bash script copied at the root of ISO as autorun0, for example, isn't launched by /usr/sbin/autorun. There seems that /usr/sbin/autorun python script isn't totally compliant with python3.

Here is below a proposed patch that seems to work for me:

Code: Select all

--- /usr/sbin/autorun.orig   2017-05-05 09:55:52.582000000 +0000
+++ /usr/sbin/autorun   2017-05-05 10:11:36.282000000 +0000
@@ -34,7 +34,7 @@
 autoruntmp=basedir+'/tmp'
 defaultsrc=['/livemnt/boot','/var/autorun/cdrom','/root','/usr/share/sys.autorun']
 ar_autoruns=['']+list('0123456789ABCDEF')
-cmdline=file('/proc/cmdline').read()
+cmdline=open('/proc/cmdline').read()
 autorunfiles=[]
 ar_nowait='n'
 ar_nodel='n'
@@ -43,8 +43,8 @@
 
 # ----------------------- functions definitions -------------------------------
 def processdostextfiles(curfile): # remove all '\r' in that file
-   txt=file(curfile).read().replace('\r','')
-   txtfile=file(curfile, 'wb')
+   txt=open(curfile).read().replace('\r','')
+   txtfile=open(curfile, 'w')
    txtfile.write(txt)
    txtfile.close()
 
@@ -57,8 +57,8 @@
 
 def copyfilefct_basic(src, dest):
    if os.path.isfile(src):
-      dstfile=file(dest, 'wb')
-      dstfile.write(file(src).read())
+      dstfile=open(dest, 'w')
+      dstfile.write(open(src).read())
       dstfile.close()
       os.chmod(dest, 755)
       return 0
@@ -126,7 +126,7 @@
       if status!=0:
          print('fatal error: cannot mount (%s)'%mnt1)
          sys.exit(1)
-          search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
+         search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
       subprocess.getstatusoutput(mnt2)
    elif re.match('^nfs://', ar_source): # mount an nfs share
       source=ar_source.replace('nfs://','')
@@ -136,7 +136,7 @@
       if status!=0:
          print('fatal error: cannot mount (%s)'%mnt1)
          sys.exit(1)
-          search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
+         search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
       subprocess.getstatusoutput(mnt2)
    elif re.match('^smb://', ar_source): # mount a samba share
       source=ar_source.replace('smb://','')
@@ -146,7 +146,7 @@
       if status!=0:
          print('fatal error: cannot mount (%s)'%mnt1)
          sys.exit(1)
-          search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
+         search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
       subprocess.getstatusoutput(mnt2)
    else: # search in all the default directories
       if ar_source!='':
@@ -168,7 +168,7 @@
       cmd='sh %s 2>&1 | tee %s'%(curfile, redir)
       print('\n'); printtitle('executing %s'%filebase, '=')
       status=os.system(cmd)
-      fileres=open(redir+'.return','wb')
+      fileres=open(redir+'.return','w')
       fileres.write(str(status)+'\n')
       fileres.close()
       print('\n'+'='*80+'\n')
@@ -209,7 +209,7 @@
 if os.path.isfile(pidfile): sys.exit(0)
 
 # create lockfile
-lockfile=open(pidfile, 'wb')
+lockfile=open(pidfile, 'w')
 lockfile.write(str(os.getpid()))
 
 try:


Hope this will help.

Cordially,


--
NVieville

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Re: autorun doesn't comply with python3

Postby admin » 17 May 2017, 17:21

Thanks for your bug fix. I have added your fix to the latest version. Can you please confirm it works with systemrescuecd-x86-5.0.1-beta007.iso or more recent versions ?

Beta versions can be downloaded there:
http://beta.system-rescue-cd.org/

gernot
Posts: 1127
Joined: 07 Apr 2010, 16:19

Re: autorun doesn't comply with python3

Postby gernot » 18 May 2017, 14:43

I get a error message with x86-5.0.1-beta007 :

"/etc/init.d/autorun restart" show this:

File "/usr/sbin/autorun", line 129
search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
^
TabError: inconsistent use of tabs and spaces in indentation
* ERROR: autorun failed to start


Gernot

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Re: autorun doesn't comply with python3

Postby admin » 18 May 2017, 20:09

Thanks for testing. It should be fixed in systemrescuecd-x86-5.0.1-beta010.iso

gernot
Posts: 1127
Joined: 07 Apr 2010, 16:19

Re: autorun doesn't comply with python3

Postby gernot » 19 May 2017, 16:28

Now i get an other error:

Traceback (most recent call last):
File "/usr/sbin/autorun", line 213, in <module>
lockfile.write(str(os.getpid()))
TypeError: 'str' does not support the buffer interface

Just remove /run/autorun.pid and start autorun again to see the message. The first time the screen overwrites fast.

Gernot

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Re: autorun doesn't comply with python3

Postby admin » 20 May 2017, 19:16

Ok I have reverted to the initial autorun based on python2 for now (in beta012). It should fix all problems.

nvieville
Posts: 17
Joined: 16 May 2008, 14:58
Location: France

Re: autorun doesn't comply with python3

Postby nvieville » 23 May 2017, 12:51

Hello,

Sorry for the delay.
Thanks for your responses.

@gernot
The proposed patch was build against the version 5.0.0 of systemrescuecd and I didn't notice this response "TypeError: 'str' does not support the buffer interface" in my tests. Actually, trying to reproduce this bug on my Fedora25 with python 3.5 fails and both lines shown below works correctly.

Maybe the line:

Code: Select all

lockfile.write(str(os.getpid()))


should be modified as:

Code: Select all

lockfile.write("{!s}".format(os.getpid()))


to be compliant with Python 3.x in beta version of systemrescuecd. But according to the Python 3.6 documentation (https://docs.python.org/3.6/library/str ... ing-syntax) original form should work.

@admin
Is there a public git repository (read-only) of systemrescuecd beta?
I noticed that in the sourceforge repository the /usr/sbin/autorun file is the 5.0.0 one, so none of the actual modifications of the beta version can be viewed.

Thank you for your great work with this tool I use regularly for a few years.

Cordially,


--
NVieville

gernot
Posts: 1127
Joined: 07 Apr 2010, 16:19

Re: autorun doesn't comply with python3

Postby gernot » 23 May 2017, 17:37

This in not the only problem.
I tried something and found that also opening the pidfile as textfile "wt" help.

The main problem is that "commands" is removed in python3
I tried to substitute it with "subprocess" but i have problems with it.

Gernot

nvieville
Posts: 17
Joined: 16 May 2008, 14:58
Location: France

Re: autorun doesn't comply with python3

Postby nvieville » 25 May 2017, 15:45

Hello,

Maybe you should try to replace something like this:

Code: Select all

def copyfilefct_http(src, dest):
        cmd="wget %s -O %s"%(src, dest)
       (status, output)=commands.getstatusoutput(cmd)
        if status==0:
                os.chmod(dest, 755)
                return 0
        else:
                os.unlink(dest)
                return -1


by something like that:

Code: Select all

def copyfilefct_http(src, dest):
        cmd = ['wget', src, '-O', dest]
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (output, status) = p.communicate()
        if p.returncode == 0:
                os.chmod(dest, 755)
                return 0
        else:
                os.unlink(dest)
                return -1


My skill in Python is not so high (beginner), but feel free to make any comment about this proposition.

Cordially,


--
NVieville

gernot
Posts: 1127
Joined: 07 Apr 2010, 16:19

Re: autorun doesn't comply with python3

Postby gernot » 27 May 2017, 08:52

It work now, but need a expert view...

Code: Select all

#!/usr/bin/python3
# Distributed under the terms of the GNU General Public License v2
# The original bash version of autorun was developed by Pierre Dorgueil in 2003
# The current python implementation has been developed by Francois Dupoux in 2008
# ----------------------- changeslog: -----------------------------------------
# 2003-10-01: Pierre Dorgueil --> original bash version of autorun for sysresccd
# 2008-01-26: Francois Dupoux --> rewrote autorun in python to support http
# 2008-01-27: Francois Dupoux --> added 'ar_ignorefail', 'ar_nodel', 'ar_disable'
#
# ----------------------- autorun exec rules: ---------------------------------
# - pass 'ar_source=/dev/fd#' to request floppy device test
# - CD is tested if no floppy requested or no autorun found on floppy
# - if a file named 'autorun' is found on any media, it is always run, except if
#   option 'ar_disable' is used
# - if a file named 'autorun[0-9]' is found on any media, it is run if either
#      - 'autoruns=...' arg did specify its number (ex. autoruns=1,3,5), or
#      - no 'autoruns=...' arg was passed
# - pass autoruns=no to prevent running any 'autorun[0-9A-F]' file
# - defaults to allow all 'autorun[0-9A-F]' files
# - if many autorun files are to be run,
#      - always in alphab order: autorun, then autorun0, then autorun1 etc...
#      - first non-zero exit code stops all (except if ar_ignorefail is used)
# - if option 'ar_nodel' is used, the temp copy of the script will not be deleted
# - if option 'ar_ignorefail' is used, do not stop autorun if a script failed
# - if option 'ar_disable' is used, absolutely no autorun script will be run

import sys, os, re, subprocess

# ----------------------- autorun default parameters --------------------------
pidfile='/run/autorun.pid'
basedir='/var/autorun'
autorunlog=basedir+'/log'
autorunmnt=basedir+'/mnt'
autoruntmp=basedir+'/tmp'
defaultsrc=['/livemnt/boot','/var/autorun/cdrom','/root','/usr/share/sys.autorun']
ar_autoruns=['']+list('0123456789ABCDEF')
cmdline=open('/proc/cmdline').read()
autorunfiles=[]
ar_nowait='n'
ar_nodel='n'
ar_ignorefail='n'
ar_source=''

# ----------------------- functions definitions -------------------------------
def processdostextfiles(curfile): # remove all '\r' in that file
        txt=open(curfile).read().replace('\r','')
        txtfile=open(curfile, 'wt')
        txtfile.write(txt)
        txtfile.close()

def printtitle(title, padding):
        totallen=80
        startpos=int(totallen/2)-int(len(title)/2)
        remain=totallen-startpos-len(title)
        text=(padding*startpos)+title+(padding*remain)
        print (text)

def copyfilefct_basic(src, dest):
        if os.path.isfile(src):
                dstfile=open(dest, 'wt')
                dstfile.write(open(src).read())
                dstfile.close()
                os.chmod(dest, 755)
                return 0
        else:
                return -1

def copyfilefct_http(src, dest):
        cmd="wget %s -O %s"%(src, dest)
        p = subprocess.Popen(cmd.split(" "), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (output, status) = p.communicate()
        if p.returncode == 0:
                os.chmod(dest, 755)
                return 0
        else:
                os.unlink(dest)
                return -1

def search_autoruns(dirname, ar_autoruns, copyfilefct):
        found=0
        for ext in ar_autoruns:
                curpath=os.path.join(dirname, 'autorun%s'%ext)
                newpath=os.path.join(autoruntmp, 'autorun%s'%ext)
                if copyfilefct(curpath, newpath)==0:
                        autorunfiles.append(newpath)
                        found+=1
        return found

def main():
        global ar_nowait, ar_nodel, ar_ignorefail, ar_source, ar_autoruns
        status=0 # in case no autorun executed
        print ('Initializing autorun...\n')

        # ---- brutal kernel args parsing ----
        for opt in cmdline.split():
                if re.match('^subdir=', opt):
                        param=opt.replace('subdir=','')
                        defaultsrc.insert(0, '/livemnt/boot/' + param)
                if re.match('^ar_nowait$', opt):
                        ar_nowait='y'
                if re.match('^ar_nodel$', opt):
                        ar_nodel='y'
                if re.match('^ar_ignorefail$', opt):
                        ar_ignorefail='y'
                if re.match('^ar_disable$', opt):
                        ar_autoruns=[]
                if re.match('^ar_source=', opt):
                        param=opt.replace('ar_source=','')
                        ar_source=param
                if re.match('^autoruns=', opt):
                        param=opt.replace('autoruns=','')
                        if param=='no':
                                ar_autoruns=['']
                        else:
                                ar_autoruns=['']+param.split(',')

        if len(ar_autoruns)==0: # nothing to do
                sys.exit(0)

        # ---- parse the autorun sources ----
        if re.match('^http://', ar_source):
                search_autoruns(ar_source, ar_autoruns, copyfilefct_http)
        elif re.match('^/dev/', ar_source): # mount a partition/device
                mnt1='mount %s %s'%(ar_source, autorunmnt)
                mnt2='umount %s'%autorunmnt
                p = subprocess.Popen((mnt1.split(" ")), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (output, status) = p.communicate()
                if p.returncode != 0:
                        print ('fatal error: cannot mount (%s)'%mnt1)
                        sys.exit(1)
                search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
                subprocess.Popen(mnt2.split(" "))
        elif re.match('^nfs://', ar_source): # mount an nfs share
                source=ar_source.replace('nfs://','')
                mnt1='mount -t nfs -o nolock %s %s'%(source, autorunmnt)
                mnt2='umount %s'%autorunmnt
                p = subprocess.Popen((mnt1.split(" ")), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (output, status) = p.communicate()
                if p.returncode != 0:
                        print ('fatal error: cannot mount (%s)'%mnt1)
                        sys.exit(1)
                search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
                subprocess.Popen(mnt2.split(" "))
        elif re.match('^smb://', ar_source): # mount a samba share
                source=ar_source.replace('smb://','')
                mnt1='mount -t cifs //%s %s'%(source, autorunmnt)
                mnt2='umount %s'%autorunmnt
                p = subprocess.Popen((mnt1.split(" ")), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                (output, status) = p.communicate()
                if p.returncode != 0:
                        print ('fatal error: cannot mount (%s)'%mnt1)
                        sys.exit(1)
                search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
                subprocess.Popen(mnt2.split(" "))
        else: # search in all the default directories
                if ar_source!='':
                        print ('cannot find a valid ar_source, searching scripts in the default directories')
                found=0
                for curdir in defaultsrc:
                        if found==0:
                                found+=search_autoruns(curdir, ar_autoruns, copyfilefct_basic)

        # ---- remove user setable ar_nowait flag if set ----
        if os.path.isfile('/etc/ar_nowait'):
                os.unlink('/etc/ar_nowait')

        # ---- execute the autorun scripts found ----
        for curfile in autorunfiles:
                processdostextfiles(curfile)
                filebase=os.path.basename(curfile)
                redir=os.path.join(autorunlog, filebase)
                cmd='sh %s 2>&1 | tee %s'%(curfile, redir)
                print ('\n'); printtitle('executing %s'%filebase, '=')
                status=os.system(cmd)
                fileres=open(redir+'.return','wt')
                fileres.write(str(status)+'\n')
                fileres.close()
                print ('\n'+'='*80+'\n')

                # first non zero exit stops all
                if status!=0 and ar_ignorefail=='n':
                        print ('execution of %s returned %d: aborting autorun'%(filebase,status))
                        ar_nowait='y'
                        break;

        # ---- delete the copies of the scripts ----
        if ar_nodel=='n':
                for curfile in autorunfiles:
                        print ('removing %s'%curfile)
                        os.unlink(curfile)

        # ---- wait a keypress feature -----
        if os.path.isfile('/etc/ar_nowait'):
                ar_nowait='y'
        if ar_nowait=='n' and len(autorunfiles)>0:
                print('end of autorun scripts, press <Enter> to continue')
                sys.stdin.read(1)

        return status

# ----------------------- autorun main ----------------------------------------
# run autorun only one time (tty1)
#(status, output) = commands.getstatusoutput('tty')
#if status!=0: sys.exit(1)
#if output not in ('/dev/tty1', '/dev/ttyS0', '/dev/ttyS1') : sys.exit(1)

# mkdir /var/autorun, logdir
for curdir in (basedir, autorunlog, autorunmnt, autoruntmp):
        if not os.path.isdir(curdir):
                os.mkdir(curdir)

# exit if already done
if os.path.isfile(pidfile): sys.exit(0)

# create lockfile
lockfile=open(pidfile, 'wt')
lockfile.write(str(os.getpid()))


try:
        res=main()
        sys.exit(res)
finally:
        os.unlink(pidfile)

nvieville
Posts: 17
Joined: 16 May 2008, 14:58
Location: France

Re: autorun doesn't comply with python3

Postby nvieville » 27 May 2017, 16:25

Hi gernot,

Thanks for your work on re-writing this python 2 script to python 3.

I'm not an expert, but maybe the split function you used to separate each element of cmd may have some unwanted effects with paths or file names containing spaces (not the case in this script, but user's sometimes do it and this script is supposed to launch other scripts dynamically).

Try the code below in python3 script or a python3 shell:

Code: Select all

src="/src/src2 with space/src_file"
dest="/dest/dest2/dest_file"
cmd="wget %s -O %s"%(src, dest)
print("With cmd splitted:")
print(cmd.split(" "))
cmd = ['wget', src, '-O', dest]
print("With cmd as list:")
print(cmd)


I haven't tried to push the test to its ultimate step: launch a command with both of the possibilities and see what happens.
Tell me if you want me to try it, but in this case, I'll ask you a delay (next week), as I'm going to be "off" till the end of the weekend.

Feel free to make any comment about this message.

Cordially,


--
NVieville

gernot
Posts: 1127
Joined: 07 Apr 2010, 16:19

Re: autorun doesn't comply with python3

Postby gernot » 27 May 2017, 19:05

Yes i know about the space problem, but wanted to keep the script changes as small as possible.

A question to native python users:
Popen stores the output streams. Should we close p after reading stream and returncode?

Gernot

nvieville
Posts: 17
Joined: 16 May 2008, 14:58
Location: France

Re: autorun doesn't comply with python3

Postby nvieville » 28 May 2017, 22:02

Hi,

After reading the autorun python file, I noticed that every time that "commands" was called, only the status was checked and used, so, as an example, I think it's possible to modify and to simplify original function like this:

Code: Select all

def copyfilefct_http(src, dest):
        cmd="wget %s -O %s"%(src, dest)
       (status, output)=commands.getstatusoutput(cmd)
        if status==0:
                os.chmod(dest, 755)
                return 0
        else:
                os.unlink(dest)
                return -1


to something like that:

Code: Select all

def copyfilefct_http(src, dest):
        cmd = ['wget', src, '-O', dest]
        p = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if p.returncode == 0:
                os.chmod(dest, 755)
                return 0
        else:
                os.unlink(dest)
                return -1


In this example, if the line:

Code: Select all

p = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

is replaced by:

Code: Select all

p = subprocess.run(cmd)

the command STDOUT and STDERR are shown in the console.

About your question, what I've understood of the documentation is that the p.communicate() call will deal with the opened pipes and in case of timeout of the command called will raise an exception. See here: https://docs.python.org/3/library/subpr ... ommunicate.
In the new proposition, there is no need to deal with the pipes (again, if I understood it correctly).

Hope this will help.

Cordially,


--
NVieville

gernot
Posts: 1127
Joined: 07 Apr 2010, 16:19

Re: autorun doesn't comply with python3

Postby gernot » 29 May 2017, 17:04

subprocess.run did not work, but i found a other solution.
Also the split is removed.

Gernot

Code: Select all

#!/usr/bin/python3
# Distributed under the terms of the GNU General Public License v2
# The original bash version of autorun was developed by Pierre Dorgueil in 2003
# The current python implementation has been developed by Francois Dupoux in 2008
# ----------------------- changeslog: -----------------------------------------
# 2003-10-01: Pierre Dorgueil --> original bash version of autorun for sysresccd
# 2008-01-26: Francois Dupoux --> rewrote autorun in python to support http
# 2008-01-27: Francois Dupoux --> added 'ar_ignorefail', 'ar_nodel', 'ar_disable'
#
# ----------------------- autorun exec rules: ---------------------------------
# - pass 'ar_source=/dev/fd#' to request floppy device test
# - CD is tested if no floppy requested or no autorun found on floppy
# - if a file named 'autorun' is found on any media, it is always run, except if
#   option 'ar_disable' is used
# - if a file named 'autorun[0-9]' is found on any media, it is run if either
#      - 'autoruns=...' arg did specify its number (ex. autoruns=1,3,5), or
#      - no 'autoruns=...' arg was passed
# - pass autoruns=no to prevent running any 'autorun[0-9A-F]' file
# - defaults to allow all 'autorun[0-9A-F]' files
# - if many autorun files are to be run,
#      - always in alphab order: autorun, then autorun0, then autorun1 etc...
#      - first non-zero exit code stops all (except if ar_ignorefail is used)
# - if option 'ar_nodel' is used, the temp copy of the script will not be deleted
# - if option 'ar_ignorefail' is used, do not stop autorun if a script failed
# - if option 'ar_disable' is used, absolutely no autorun script will be run

import sys, os, re, subprocess

# ----------------------- autorun default parameters --------------------------
pidfile='/run/autorun.pid'
basedir='/var/autorun'
autorunlog=basedir+'/log'
autorunmnt=basedir+'/mnt'
autoruntmp=basedir+'/tmp'
defaultsrc=['/livemnt/boot','/var/autorun/cdrom','/root','/usr/share/sys.autorun']
ar_autoruns=['']+list('0123456789ABCDEF')
cmdline=open('/proc/cmdline').read()
autorunfiles=[]
ar_nowait='n'
ar_nodel='n'
ar_ignorefail='n'
ar_source=''

# ----------------------- functions definitions -------------------------------
def processdostextfiles(curfile): # remove all '\r' in that file
        txt=open(curfile).read().replace('\r','')
        txtfile=open(curfile, 'wt')
        txtfile.write(txt)
        txtfile.close()

def printtitle(title, padding):
        totallen=80
        startpos=int(totallen/2)-int(len(title)/2)
        remain=totallen-startpos-len(title)
        text=(padding*startpos)+title+(padding*remain)
        print (text)

def copyfilefct_basic(src, dest):
        if os.path.isfile(src):
                dstfile=open(dest, 'wt')
                dstfile.write(open(src).read())
                dstfile.close()
                os.chmod(dest, 755)
                return 0
        else:
                return -1

def copyfilefct_http(src, dest):

        cmd=('wget','-q',src,'-O',dest)
        p = subprocess.Popen(cmd)
        p.wait()
        if p.returncode == 0:
                os.chmod(dest, 755)
                return 0
        else:
                os.unlink(dest)
                return -1

def search_autoruns(dirname, ar_autoruns, copyfilefct):
        found=0
        for ext in ar_autoruns:
                curpath=os.path.join(dirname, 'autorun%s'%ext)
                newpath=os.path.join(autoruntmp, 'autorun%s'%ext)
                if copyfilefct(curpath, newpath)==0:
                        autorunfiles.append(newpath)
                        found+=1
        return found

def main():
        global ar_nowait, ar_nodel, ar_ignorefail, ar_source, ar_autoruns
        status=0 # in case no autorun executed
        print ('Initializing autorun...\n')

        # ---- brutal kernel args parsing ----
        for opt in cmdline.split():
                if re.match('^subdir=', opt):
                        param=opt.replace('subdir=','')
                        defaultsrc.insert(0, '/livemnt/boot/' + param)
                if re.match('^ar_nowait$', opt):
                        ar_nowait='y'
                if re.match('^ar_nodel$', opt):
                        ar_nodel='y'
                if re.match('^ar_ignorefail$', opt):
                        ar_ignorefail='y'
                if re.match('^ar_disable$', opt):
                        ar_autoruns=[]
                if re.match('^ar_source=', opt):
                        param=opt.replace('ar_source=','')
                        ar_source=param
                if re.match('^autoruns=', opt):
                        param=opt.replace('autoruns=','')
                        if param=='no':
                                ar_autoruns=['']
                        else:
                                ar_autoruns=['']+param.split(',')

        if len(ar_autoruns)==0: # nothing to do
                sys.exit(0)

        # ---- parse the autorun sources ----
        if re.match('^http://', ar_source):
                search_autoruns(ar_source, ar_autoruns, copyfilefct_http)
        elif re.match('^/dev/', ar_source): # mount a partition/device
                mnt1=('mount',ar_source,autorunmnt)
                mnt2=('umount',autorunmnt)
                p = subprocess.Popen(mnt1)
                p.wait()
                if p.returncode != 0:
                        print ('fatal error: cannot mount', mnt1)
                        sys.exit(1)
                search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
                subprocess.Popen(mnt2)
        elif re.match('^nfs://', ar_source): # mount an nfs share
                source=ar_source.replace('nfs://','')
                mnt1=('mount','-t','nfs','-o','nolock',source,autorunmnt)
                mnt2=('umount',autorunmnt)
                p = subprocess.Popen(mnt1)
                p.wait()
                if p.returncode != 0:
                        print ('fatal error: cannot mount', mnt1)
                        sys.exit(1)
                search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
                subprocess.Popen(mnt2)
        elif re.match('^smb://', ar_source): # mount a samba share
                source=ar_source.replace('smb://','')
                mnt1=('mount','-t','cifs','//%s'%source,autorunmnt)
                mnt2=('umount',autorunmnt)
                p = subprocess.Popen(mnt1)
                p.wait()
                if p.returncode != 0:
                        print ('fatal error: cannot mount',mnt1)
                        sys.exit(1)
                search_autoruns(autorunmnt, ar_autoruns, copyfilefct_basic)
                subprocess.Popen(mnt2)
        else: # search in all the default directories
                if ar_source!='':
                        print ('cannot find a valid ar_source, searching scripts in the default directories')
                found=0
                for curdir in defaultsrc:
                        if found==0:
                                found+=search_autoruns(curdir, ar_autoruns, copyfilefct_basic)

        # ---- remove user setable ar_nowait flag if set ----
        if os.path.isfile('/etc/ar_nowait'):
                os.unlink('/etc/ar_nowait')

        # ---- execute the autorun scripts found ----
        for curfile in autorunfiles:
                processdostextfiles(curfile)
                filebase=os.path.basename(curfile)
                redir=os.path.join(autorunlog, filebase)
                cmd='sh %s 2>&1 | tee %s'%(curfile, redir)
                print ('\n'); printtitle('executing %s'%filebase, '=')
                status=os.system(cmd)
                fileres=open(redir+'.return','wt')
                fileres.write(str(status)+'\n')
                fileres.close()
                print ('\n'+'='*80+'\n')

                # first non zero exit stops all
                if status!=0 and ar_ignorefail=='n':
                        print ('execution of %s returned %d: aborting autorun'%(filebase,status))
                        ar_nowait='y'
                        break;

        # ---- delete the copies of the scripts ----
        if ar_nodel=='n':
                for curfile in autorunfiles:
                        print ('removing %s'%curfile)
                        os.unlink(curfile)

        # ---- wait a keypress feature -----
        if os.path.isfile('/etc/ar_nowait'):
                ar_nowait='y'
        if ar_nowait=='n' and len(autorunfiles)>0:
                print('end of autorun scripts, press <Enter> to continue')
                sys.stdin.read(1)

        return status

# ----------------------- autorun main ----------------------------------------
# run autorun only one time (tty1)
#(status, output) = commands.getstatusoutput('tty')
#if status!=0: sys.exit(1)
#if output not in ('/dev/tty1', '/dev/ttyS0', '/dev/ttyS1') : sys.exit(1)

# mkdir /var/autorun, logdir
for curdir in (basedir, autorunlog, autorunmnt, autoruntmp):
        if not os.path.isdir(curdir):
                os.mkdir(curdir)

# exit if already done
if os.path.isfile(pidfile): sys.exit(0)

# create lockfile
lockfile=open(pidfile, 'wt')
lockfile.write(str(os.getpid()))


try:
        res=main()
        sys.exit(res)
finally:
        os.unlink(pidfile)

admin
Site Admin
Posts: 2715
Joined: 17 Jul 2003, 09:44

Re: autorun doesn't comply with python3

Postby admin » 30 May 2017, 06:37

The latest version of the Python3 script prepared by Gernot has been included in systemrescuecd-x86-5.0.2-beta001.iso


Return to “Boot problems”

Who is online

Users browsing this forum: No registered users and 6 guests