pre-push 2.63 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/env python
import subprocess, urllib2, os

remote_dir  = "."
remote_url  = "http://community.elphel.com/files/production/"
private_key_path = "/home/user/.ssh/id_rsa"

production_dir = "production"
bkp_dir        = "uploaded"
rsync          = "rsync --ignore-existing -ashvv --exclude */ --exclude README . "+remote_dir
ssh            = ["-e","ssh -i "+private_key_path]
rsync = rsync.split()
rsync.extend(ssh)

#list of files - needs to be manually saved
source_files_extensions     = [".stp",".dxf",".igs",".dwb",".bmp",".png",".jpg",".pdf~"]

production_files_extensions = [".dwb.tar.gz",".dxf.tar.gz",".igs.tar.gz",".jpeg",".pdf",".stp.tar.gz"]
# file extension in design/xxxx-xx/xxx.dwb
design_files_extensions     = [".dwb"]

#change to production_dir/
os.chdir(os.getcwd()+"/"+production_dir)
#archive and convert file in production_dir
subprocess.check_output("../archive_stp_dxf_pdf_any_year.sh", stderr=subprocess.STDOUT);

#analyze production files
error_counter = 0

for file in os.listdir("."):
  if (file.endswith(".dwb.tar.gz")):
32 33 34
    #file_stripped=file.strip(".dwb.tar.gz")
    file_stripped=file[:-len(".dwb.tar.gz")]
    print "Processing "+file_stripped
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    #download the list of production files
    if (urllib2.urlopen(remote_url).read().find(file)!=-1): 
      print "ERROR("+file_stripped+"): Already in production"
      error_counter-=1
    else:
      for ext in production_files_extensions:
	if (not os.path.isfile(file_stripped+ext)):
	  print "ERROR("+file_stripped+"): Missing production file: "+file_stripped+ext
	  error_counter-=1
      if (error_counter==0):
	#move original source files to production_dir/bkp_dir
	for ext in source_files_extensions:
	  #move file to bkp_dir
	  if (os.path.isfile(file_stripped+ext)): os.rename(os.getcwd()+"/"+file_stripped+ext,os.getcwd()+"/"+bkp_dir+"/"+file_stripped+ext)
	#change .dwb to read-only but executable (as git supports only 0644 and 0755)
	for ext in design_files_extensions:
	  #print "Looking for "+file_stripped+ext
	  for root,dirs,files in os.walk(os.getcwd()+"/../design"):
	    for name in files:
	      if (name==file_stripped+ext):
		#git supports only 0644 and 0755
		os.chmod(os.path.join(root, name),0555)
		#commit change
		subprocess.check_output(["git","commit","-am","Production"], stderr=subprocess.STDOUT);
		break

if (error_counter==0):
  #run rsync
  subprocess.check_output(rsync, stderr=subprocess.STDOUT)
  #move all files from production_dir to production_dir/bkp_dir
  for file in os.listdir("."):
    if (os.path.isfile(file))and(file!="README"): os.rename(os.getcwd()+"/"+file,os.getcwd()+"/"+bkp_dir+"/"+file)

  print "PRE-PUSH HOOK: SUCCESS"

exit(error_counter)