-
Notifications
You must be signed in to change notification settings - Fork 56
/
utilities.py
92 lines (79 loc) · 2.76 KB
/
utilities.py
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
32
33
34
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import subprocess
from contextlib import contextmanager
# Get decoded output of a command, even if it fails!
def get_output(commands):
try:
# We'll want error output, too
output = subprocess.check_output(commands, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print(e)
output = e.output
return output.decode('utf-8')
# Immediate subdirectories
def get_packages(directory):
for _, packages, _ in os.walk(directory):
return packages # exit early
return []
# Immediate files
def get_files(directory):
print(directory)
for _, _, files in os.walk(directory):
return files # exit early
return []
# Use a directory for a task
@contextmanager
def directory(directoryName):
oldwd = os.getcwd()
os.chdir(directoryName)
try:
yield
finally:
os.chdir(oldwd)
# 'Parse' git branch output
def get_branchname():
branches = []
output = get_output(['git', 'branch'])
branches = output.splitlines()
for branch in branches:
if '*' in branch: # current branch is starred
return branch.replace('*', '').replace(' ', '') # Get rid of * and whitespace
# Get all LOCAL branches
def get_branches():
branches = []
output = get_output(['git', 'branch'])
# Remove whitespace and *
branches = output.replace('*', '').replace(' ', '').splitlines()
return branches
# Finds the author of a commit
def get_author(SHA=None):
if SHA is None:
output = subprocess.check_output(['git', 'log', 'HEAD', '-1'])
else:
output = subprocess.check_output(['git', 'show', SHA])
authorline = output.decode('utf-8').splitlines()[1]
_, author, email = authorline.split()
return author, email
# Used by git hooks, (called from top level pyGSTi directory)
def run_pylint(commands):
with directory('test/'):
result = subprocess.call(commands)
return result
# return a dict of filenames that correspond to full paths
def get_file_names():
fileNames = {}
for subdir, _, files in os.walk(os.getcwd()):
for filename in files:
if filename.endswith('.py') and filename.startswith('test'):
fileNames[filename] = subdir + os.sep + filename
return fileNames
# Wrapper for git diff
def get_changed_files():
output = get_output(['git', 'diff', '--name-only'])
return output.splitlines()
# Changed files in /pygsti (not tests/repotools)
def get_changed_core_files(core='pygsti'):
return (filename.split(core, 1)[1] for filename in get_changed_files() if core in filename)
# Immediate packages under pygsti that have changed (i.e. tools, drivers..)
def get_changed_packages():
return (corefile.split('/')[1] for corefile in get_changed_core_files())