annotate utils.py @ 8:32587f12102c draft

Uploaded
author greg
date Fri, 22 Jun 2018 08:40:34 -0400
parents 3384b6a842b0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7ba9469800b9 Uploaded
greg
parents:
diff changeset
1 import os
7ba9469800b9 Uploaded
greg
parents:
diff changeset
2 import shutil
7ba9469800b9 Uploaded
greg
parents:
diff changeset
3 import subprocess
7ba9469800b9 Uploaded
greg
parents:
diff changeset
4 import sys
7ba9469800b9 Uploaded
greg
parents:
diff changeset
5
7ba9469800b9 Uploaded
greg
parents:
diff changeset
6 FSTDERR = 'stderr.txt'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
7 FSTDOUT = 'stdout.txt'
7ba9469800b9 Uploaded
greg
parents:
diff changeset
8
7ba9469800b9 Uploaded
greg
parents:
diff changeset
9
7ba9469800b9 Uploaded
greg
parents:
diff changeset
10 def check_execution_errors(rc, fstderr, fstdout):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
11 if rc != 0:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
12 fh = open(fstdout, 'rb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
13 out_msg = fh.read()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
14 fh.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
15 fh = open(fstderr, 'rb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
16 err_msg = fh.read()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
17 fh.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
18 msg = '%s\n%s\n' % (str(out_msg), str(err_msg))
7ba9469800b9 Uploaded
greg
parents:
diff changeset
19 stop_err(msg)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
20
7ba9469800b9 Uploaded
greg
parents:
diff changeset
21
7ba9469800b9 Uploaded
greg
parents:
diff changeset
22 def get_response_buffers():
7ba9469800b9 Uploaded
greg
parents:
diff changeset
23 fstderr = os.path.join(os.getcwd(), FSTDERR)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
24 fherr = open(fstderr, 'wb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
25 fstdout = os.path.join(os.getcwd(), FSTDOUT)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
26 fhout = open(fstdout, 'wb')
7ba9469800b9 Uploaded
greg
parents:
diff changeset
27 return fstderr, fherr, fstdout, fhout
7ba9469800b9 Uploaded
greg
parents:
diff changeset
28
7ba9469800b9 Uploaded
greg
parents:
diff changeset
29
6
3384b6a842b0 Uploaded
greg
parents: 0
diff changeset
30 def move_directory_files(source_dir, destination_dir, copy=False, remove_source_dir=False):
0
7ba9469800b9 Uploaded
greg
parents:
diff changeset
31 source_directory = os.path.abspath(source_dir)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
32 destination_directory = os.path.abspath(destination_dir)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
33 if not os.path.isdir(destination_directory):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
34 os.makedirs(destination_directory)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
35 for dir_entry in os.listdir(source_directory):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
36 source_entry = os.path.join(source_directory, dir_entry)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
37 if copy:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
38 shutil.copy(source_entry, destination_directory)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
39 else:
7ba9469800b9 Uploaded
greg
parents:
diff changeset
40 shutil.move(source_entry, destination_directory)
6
3384b6a842b0 Uploaded
greg
parents: 0
diff changeset
41 if remove_source_dir:
3384b6a842b0 Uploaded
greg
parents: 0
diff changeset
42 os.rmdir(source_directory)
0
7ba9469800b9 Uploaded
greg
parents:
diff changeset
43
7ba9469800b9 Uploaded
greg
parents:
diff changeset
44
7ba9469800b9 Uploaded
greg
parents:
diff changeset
45 def run_command(cmd):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
46 fstderr, fherr, fstdout, fhout = get_response_buffers()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
47 proc = subprocess.Popen(args=cmd, stderr=fherr, stdout=fhout, shell=True)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
48 rc = proc.wait()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
49 # Check results.
7ba9469800b9 Uploaded
greg
parents:
diff changeset
50 fherr.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
51 fhout.close()
7ba9469800b9 Uploaded
greg
parents:
diff changeset
52 check_execution_errors(rc, fstderr, fstdout)
7ba9469800b9 Uploaded
greg
parents:
diff changeset
53
7ba9469800b9 Uploaded
greg
parents:
diff changeset
54
7ba9469800b9 Uploaded
greg
parents:
diff changeset
55 def stop_err(msg):
7ba9469800b9 Uploaded
greg
parents:
diff changeset
56 sys.exit(msg)