In [68]:
import os
import sys
import subprocess

In [38]:
os.getcwd()


Out[38]:
'C:\\Users\\Matt\\Programming\\ML-Notebooks\\python'

In [26]:
subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE).stdout.read()


Out[26]:
b'Python Basics.ipynb\nPython-FileSystem.ipynb\n'

In [29]:
p = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout
for line in p.readlines():
    print(line)


b'Python Basics.ipynb\n'
b'Python-FileSystem.ipynb\n'

In [37]:
!git rev-parse --short HEAD


f982b59

In [61]:
weight_dir = "../data_out3/"
file_name = "weights_fake"
commit = !git rev-parse --short HEAD

In [62]:
newfile = os.path.join(weight_dir, file_name + "_" + commit[0])

In [63]:
newfile


Out[63]:
'../data_out3/weights_fake_f982b59'

In [67]:
"keras" in !git status --porcelain


  File "<ipython-input-67-842afb38aa8d>", line 1
    "keras" in !git status --porcelain
               ^
SyntaxError: invalid syntax

In [72]:
this_script = os.path.split(sys.argv[0])[1]

In [73]:
!git diff --quiet this_script


fatal: ambiguous argument 'this_script': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

In [75]:
p = subprocess.Popen('git status', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout
for line in p.readlines():
    print(line)


b'On branch master\n'
b"Your branch is up-to-date with 'origin/master'.\n"
b'Changes not staged for commit:\n'
b'  (use "git add/rm <file>..." to update what will be committed)\n'
b'  (use "git checkout -- <file>..." to discard changes in working directory)\n'
b'\n'
b'\tmodified:   ../keras/keras100-Overview.ipynb\n'
b'\tmodified:   ../keras/keras200-CNN.ipynb\n'
b'\tdeleted:    ../keras/keras202-VGG16.ipynb\n'
b'\n'
b'Untracked files:\n'
b'  (use "git add <file>..." to include in what will be committed)\n'
b'\n'
b'\t../keras/images/100dutycycle.png\n'
b'\t../keras/images/dutycycle.png\n'
b'\t../keras/keras110-TrainingBasics.ipynb\n'
b'\t../keras/keras202-VGG16FineTuning.ipynb\n'
b'\tPython-FileSystem.ipynb\n'
b'\n'
b'no changes added to commit (use "git add" and/or "git commit -a")\n'

In [ ]:
import os, sys, subprocess

this_script = os.path.split(sys.argv[0])[1]
cmd = ['git diff --quiet', this_script]
changed = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
#changed = subprocess.Popen('git diff --quiet cdiscount_model_49_classes.py', shell=True, stdout=subprocess.PIPE)
wait = changed.communicate()[0]

if changed.returncode:
    print("Updates to", this_script, "need to be git committed before running. Exiting...")
    sys.exit()
else:
    weight_dir = "../data_out3/Xception_weights"
    file_name = "Xception49_Epoch{epoch:02d}-ValAcc{val_acc:.3f}"
    commit = subprocess.Popen('git rev-parse --short HEAD', shell=True, stdout=subprocess.PIPE).stdout.read().decode('utf-8')[:-1]

    newfile = os.path.join(weight_dir, file_name + '_' + commit + '.hdf5')
    print("Weights will be saved to:\n", "   ", newfile)