1

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

asked Dec 21, 2009 at 11:10
2
  • when did you create the '/new_sandbox/' dir? I think this is where your code fail. Commented Dec 21, 2009 at 11:18
  • for crying out loud, don't use MKS. switch to Subversion or something Commented Dec 21, 2009 at 11:27

5 Answers 5

3

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") 
answered Dec 21, 2009 at 11:25
Sign up to request clarification or add additional context in comments.

Comments

1

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")
answered Dec 21, 2009 at 11:24

Comments

1

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.

Paul Tarjan
51k59 gold badges176 silver badges214 bronze badges
answered Dec 21, 2009 at 11:21

1 Comment

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.
0

Not sure what the module mks_function does. But I see one issue with your code.

For example,

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

In the above chunk you check if the directory "/home/build/test/sandboxes/+'todaystr'" exists and a create a directory by name "value contained in todaystr" (say 2009年12月21日). This creates directory by name '2009-12-21' in the current working directory, rather than under : /home/build/test/sandboxes which is what you intended I guess. So change to the above directory before the call to mkdir. Also it is good to check the return status of mkdir to verify if the directory creation succeeded.

answered Dec 21, 2009 at 11:24

2 Comments

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.
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.
0

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")
answered Dec 21, 2009 at 12:00

1 Comment

ooh. the / operator used to join paths is rather experimental.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.