tags:

views:

310

answers:

5

Hello,

I am new to python and I'm trying to create a program that creates a directory with todays date, create a sandbox into that directory and run the make file in the sandbox. I am having trouble getting the variables to be picked up in the os.path lines. The code is posted below:

#!/usr/bin/python  
import mks_function  
from mks_function import mks_create_sandbox  
import sys, os, time, datetime  
import os.path  

today = datetime.date.today()  # get today's date as a datetime type  

todaystr = today.isoformat()   # get string representation: YYYY-MM-DD  
                           # from a datetime type.  

if not os.path.exists('/home/build/test/sandboxes/'+todaystr):  
 os.mkdir(todaystr)  
else:  
 pass  

if not os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/project.pj'):  
 mks_create_sandbox()  
else:  
 pass  

if os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/Makefile'):  
 os.system("make >make_results.txt 2>&1")

Any help would be appreciated, Thanks

+1  A: 

I think you want to change a few things:

def makeSandbox():
  sbdir = os.path.join('/home/build/test/sandboxes/',todaystr)
  if not os.path.exists(sbdir):  
    os.mkdir(sbdir)  # <- fully qualified path
  else:  
    pass

And I don't really see what variables need to be picked up, seems fine to me.

extraneon
I believe the problem lies in this line: if not os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/project.pj'): he create dir '/home/build/test/sandboxes/'+todaystr, but when he tried to create file inside it, he forgot to create /new_sandbox/ dir.
silent
A: 
sateesh
check return status? It's Python, it will raise an exception if creation fails. Also, it's better to specify the directory with full path than to change directory -- that often leads to more fragile programs.
kaizer.se
Coming from perl my understanding was that it was a good practice to test retvalues of system calls.Thanks for your comment, now I understand that the right thing to do in Python is to take care of(handle) exceptions.
sateesh
A: 

Please try adding chdir code before you call make

if os.path.exists('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/Makefile'):
 os.chdir('/home/build/test/sandboxes/'+todaystr+'/new_sandbox/')
 os.system("make >make_results.txt 2>&1")
S.Mark
+2  A: 

a couple of notes:

#!/usr/bin/env python  
# import mks_function .. you won't need this ...

from mks_function import mks_create_sandbox  
import os, datetime  

# import time, sys .. these aren't used in this snippet 
# import os.path .. just refer to os.path, since os is already imported

# get today's date as a datetime type  
todaystr = datetime.date.today().isoformat()  

# .. use os.path.join()
if not os.path.exists(os.path.join('/home/build/test/sandboxes/', todaystr)):  
    os.mkdir(os.path.join('/home/build/test/sandboxes/', todaystr))  
# .. 'else: pass' is unnecessary

if not os.path.exists(os.path.join(
    '/home/build/test/sandboxes/', todaystr, '/new_sandbox/project.pj')):  

    # i'm not seen, that the sandbox is created in the right directory here
    # maybe you should change the working directory via ..
    # os.chdir(os.path.join('/home/build/test/sandboxes/', todaystr))
    mks_create_sandbox()  

if os.path.exists(os.path.join(
    '/home/build/test/sandboxes/', todaystr, '/new_sandbox/Makefile')):  

    # .. change to the right directory
    os.chdir(os.path.join(
        '/home/build/test/sandboxes/', todaystr, '/new_sandbox/'))

    os.system("make > make_results.txt 2>&1")
The MYYN
A: 

path module might help in this case:

#!/usr/bin/env python  
from mks_function import mks_create_sandbox  
import os, datetime  

from path import path

sandboxes = path('/home/build/test/sandboxes/')
today = sandboxes / datetime.date.today().isoformat()
today.mkdir() # create directory if it doesn't exist

project = today / "new_sandbox/project.pj"
project.parent.mkdir() # create sandbox directory if it doesn't exist
if not project.isfile():  
   mks_create_sandbox()  

makefile = project.parent / "Makefile"
if makefile.isfile():
   os.chdir(makefile.parent)  
   os.system("make >make_results.txt 2>&1")
J.F. Sebastian
ooh. the `/` operator used to join paths is rather experimental.
kaizer.se