summaryrefslogtreecommitdiff
path: root/yarns/lib.py
diff options
context:
space:
mode:
Diffstat (limited to 'yarns/lib.py')
-rw-r--r--yarns/lib.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/yarns/lib.py b/yarns/lib.py
new file mode 100644
index 0000000..45bcb03
--- /dev/null
+++ b/yarns/lib.py
@@ -0,0 +1,69 @@
+import os
+import sys
+
+import cliapp
+
+import yarnutils
+
+datadir = os.environ['DATADIR']
+srcdir = os.environ['SRCDIR']
+
+vars = yarnutils.Variables(datadir)
+
+
+
+_next_match = 1
+
+def get_next_match():
+ global _next_match
+ name = 'MATCH_{}'.format(_next_match)
+ _next_match += 1
+ return os.environ[name]
+
+
+def cat_file(filename):
+ with open(filename) as f:
+ return f.read()
+
+
+def write_file(filename, content):
+ with open(filename, 'w') as f:
+ f.write(content)
+
+
+def runcmd(argv, **kwargs):
+ if 'cwd' not in kwargs:
+ kwargs['cwd'] = vars['cwd']
+ return cliapp.runcmd_unchecked(argv, **kwargs)
+
+
+def configure_git():
+ # Yarn sets $HOME to point at an empty temporary directory.
+ # Set up a simple git config so tests can use git.
+ runcmd(['git', 'config', '--global', 'user.email', 'tomjon@example.com'])
+ runcmd(['git', 'config', '--global', 'user.name', 'Tomjon Tester'])
+
+
+def run_distix(args, cwd=None):
+ if cwd is None:
+ cwd = vars['cwd']
+
+ distix = os.path.join(srcdir, 'distix')
+ log_file = os.path.join(datadir, 'distix.log')
+ exit_code, out, err = runcmd(
+ [distix, '--no-default-config', '--quiet', '--log', log_file] +
+ args,
+ cwd=cwd)
+ vars['exit'] = exit_code
+ vars['stdout'] = out
+ vars['stderr'] = err
+
+
+def mkmaildir(dirname):
+ os.makedirs(dirname)
+ for subdir in ['cur', 'tmp', 'new']:
+ os.mkdir(os.path.join(dirname, subdir))
+
+
+def unescape(s):
+ return '\n'.join(s.split('\\n'))